Merge pull request #4103 from CalebKL/caleb/task/move-free-space-to-IO-thread

Move free space to io thread
This commit is contained in:
Kelson 2024-12-10 17:16:04 +01:00 committed by GitHub
commit 9fc6e66ecc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 38 additions and 44 deletions

View File

@ -208,7 +208,7 @@ class CopyMoveFileHandler @Inject constructor(
} }
} }
fun handleDetectingFileSystemState() { suspend fun handleDetectingFileSystemState() {
if (isBookLessThan4GB()) { if (isBookLessThan4GB()) {
performCopyMoveOperationIfSufficientSpaceAvailable() performCopyMoveOperationIfSufficientSpaceAvailable()
} else { } else {
@ -217,7 +217,7 @@ class CopyMoveFileHandler @Inject constructor(
} }
} }
fun handleCannotWrite4GbFileState() { suspend fun handleCannotWrite4GbFileState() {
if (isBookLessThan4GB()) { if (isBookLessThan4GB()) {
performCopyMoveOperationIfSufficientSpaceAvailable() performCopyMoveOperationIfSufficientSpaceAvailable()
} else { } else {
@ -240,14 +240,12 @@ class CopyMoveFileHandler @Inject constructor(
} }
} }
fun performCopyMoveOperationIfSufficientSpaceAvailable() { suspend fun performCopyMoveOperationIfSufficientSpaceAvailable() {
lifecycleScope?.launch { val availableSpace = storageCalculator.availableBytes(File(sharedPreferenceUtil.prefStorage))
val availableSpace = storageCalculator.availableBytes(File(sharedPreferenceUtil.prefStorage)) if (hasNotSufficientStorageSpace(availableSpace)) {
if (hasNotSufficientStorageSpace(availableSpace)) { fileCopyMoveCallback?.insufficientSpaceInStorage(availableSpace)
fileCopyMoveCallback?.insufficientSpaceInStorage(availableSpace) } else {
} else { performCopyMoveOperation()
performCopyMoveOperation()
}
} }
} }

View File

@ -680,14 +680,16 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal
private fun storeDeviceInPreferences( private fun storeDeviceInPreferences(
storageDevice: StorageDevice storageDevice: StorageDevice
) { ) {
sharedPreferenceUtil.putPrefStorage( lifecycleScope.launch {
sharedPreferenceUtil.getPublicDirectoryPath(storageDevice.name) sharedPreferenceUtil.putPrefStorage(
) sharedPreferenceUtil.getPublicDirectoryPath(storageDevice.name)
sharedPreferenceUtil.putStoragePosition( )
if (storageDevice.isInternal) INTERNAL_SELECT_POSITION sharedPreferenceUtil.putStoragePosition(
else EXTERNAL_SELECT_POSITION if (storageDevice.isInternal) INTERNAL_SELECT_POSITION
) else EXTERNAL_SELECT_POSITION
// after selecting the storage try to copy/move the zim file. )
copyMoveFileHandler?.copyMoveZIMFileInSelectedStorage(storageDevice) // after selecting the storage try to copy/move the zim file.
copyMoveFileHandler?.copyMoveZIMFileInSelectedStorage(storageDevice)
}
} }
} }

View File

@ -128,16 +128,15 @@ class CopyMoveFileHandlerTest {
} }
@Test @Test
fun validateZimFileCanCopyOrMoveShouldReturnFalseWhenDetectingFileSystem() = fun validateZimFileCanCopyOrMoveShouldReturnFalseWhenDetectingFileSystem() = runTest {
runTest { every { fileHandler.isBookLessThan4GB() } returns true
every { fileHandler.isBookLessThan4GB() } returns true prepareFileSystemAndFileForMockk(fileSystemState = DetectingFileSystem)
prepareFileSystemAndFileForMockk(fileSystemState = DetectingFileSystem)
val result = fileHandler.validateZimFileCanCopyOrMove(storageFile) val result = fileHandler.validateZimFileCanCopyOrMove(storageFile)
assertFalse(result) assertFalse(result)
verify { fileHandler.handleDetectingFileSystemState() } coVerify { fileHandler.handleDetectingFileSystemState() }
} }
@Test @Test
fun validateZimFileCanCopyOrMoveShouldReturnFalseWhenCannotWrite4GbFile() = runBlocking { fun validateZimFileCanCopyOrMoveShouldReturnFalseWhenCannotWrite4GbFile() = runBlocking {
@ -151,23 +150,23 @@ class CopyMoveFileHandlerTest {
val result = fileHandler.validateZimFileCanCopyOrMove(storageFile) val result = fileHandler.validateZimFileCanCopyOrMove(storageFile)
assertFalse(result) assertFalse(result)
verify { fileHandler.handleCannotWrite4GbFileState() } coVerify { fileHandler.handleCannotWrite4GbFileState() }
} }
@Test @Test
fun handleDetectingFileSystemStateShouldPerformCopyMoveOperationIfBookLessThan4GB() { fun handleDetectingFileSystemStateShouldPerformCopyMoveOperationIfBookLessThan4GB() = runTest {
fileHandler = spyk(fileHandler) fileHandler = spyk(fileHandler)
prepareFileSystemAndFileForMockk() prepareFileSystemAndFileForMockk()
every { fileHandler.isBookLessThan4GB() } returns true every { fileHandler.isBookLessThan4GB() } returns true
every { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() } just Runs coEvery { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() } just Runs
fileHandler.handleDetectingFileSystemState() fileHandler.handleDetectingFileSystemState()
verify { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() } coVerify { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() }
} }
@Test @Test
fun handleDetectingFileSystemStateShouldObserveFileSystemStateIfBookGreaterThan4GB() { fun handleDetectingFileSystemStateShouldObserveFileSystemStateIfBookGreaterThan4GB() = runTest {
fileHandler = spyk(fileHandler) fileHandler = spyk(fileHandler)
prepareFileSystemAndFileForMockk(fileSystemState = DetectingFileSystem) prepareFileSystemAndFileForMockk(fileSystemState = DetectingFileSystem)
every { fileHandler.isBookLessThan4GB() } returns false every { fileHandler.isBookLessThan4GB() } returns false
@ -178,19 +177,19 @@ class CopyMoveFileHandlerTest {
} }
@Test @Test
fun handleCannotWrite4GbFileStateShouldPerformCopyMoveOperationIfBookLessThan4GB() { fun handleCannotWrite4GbFileStateShouldPerformCopyMoveOperationIfBookLessThan4GB() = runTest {
fileHandler = spyk(fileHandler) fileHandler = spyk(fileHandler)
prepareFileSystemAndFileForMockk() prepareFileSystemAndFileForMockk()
every { fileHandler.isBookLessThan4GB() } returns true every { fileHandler.isBookLessThan4GB() } returns true
every { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() } just Runs coEvery { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() } just Runs
fileHandler.handleCannotWrite4GbFileState() fileHandler.handleCannotWrite4GbFileState()
verify { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() } coVerify { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() }
} }
@Test @Test
fun handleCannotWrite4GbFileStateShouldCallCallbackIfBookGreaterThan4GB() { fun handleCannotWrite4GbFileStateShouldCallCallbackIfBookGreaterThan4GB() = runTest {
fileHandler = spyk(fileHandler) fileHandler = spyk(fileHandler)
prepareFileSystemAndFileForMockk() prepareFileSystemAndFileForMockk()
every { fileHandler.isBookLessThan4GB() } returns false every { fileHandler.isBookLessThan4GB() } returns false

View File

@ -42,7 +42,7 @@ object Versions {
const val io_objectbox: String = "3.5.0" const val io_objectbox: String = "3.5.0"
const val io_mockk: String = "1.13.7" const val io_mockk: String = "1.13.13"
const val android_arch_lifecycle_extensions: String = "1.1.1" const val android_arch_lifecycle_extensions: String = "1.1.1"

View File

@ -19,17 +19,12 @@
package org.kiwix.kiwixmobile.core.extensions package org.kiwix.kiwixmobile.core.extensions
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.io.File import java.io.File
suspend fun File.isFileExist(): Boolean = withContext(Dispatchers.IO) { exists() } suspend fun File.isFileExist(): Boolean = withContext(Dispatchers.IO) { exists() }
fun File.freeSpace(): Long = runBlocking { suspend fun File.freeSpace(): Long = withContext(Dispatchers.IO) { freeSpace }
withContext(Dispatchers.IO) {
freeSpace
}
}
suspend fun File.totalSpace(): Long = withContext(Dispatchers.IO) { totalSpace } suspend fun File.totalSpace(): Long = withContext(Dispatchers.IO) { totalSpace }

View File

@ -28,7 +28,7 @@ import javax.inject.Inject
class SetPreferredStorageWithMostSpace @Inject constructor( class SetPreferredStorageWithMostSpace @Inject constructor(
private val storageCalculator: StorageCalculator, private val storageCalculator: StorageCalculator,
private val sharedPreferenceUtil: SharedPreferenceUtil private val sharedPreferenceUtil: SharedPreferenceUtil,
) : SideEffect<Unit> { ) : SideEffect<Unit> {
override fun invokeWith(activity: AppCompatActivity) { override fun invokeWith(activity: AppCompatActivity) {
activity.lifecycleScope.launch { activity.lifecycleScope.launch {