Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move free space to io thread #4103

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class CopyMoveFileHandler @Inject constructor(
}
}

fun handleDetectingFileSystemState() {
suspend fun handleDetectingFileSystemState() {
if (isBookLessThan4GB()) {
performCopyMoveOperationIfSufficientSpaceAvailable()
} else {
Expand All @@ -217,7 +217,7 @@ class CopyMoveFileHandler @Inject constructor(
}
}

fun handleCannotWrite4GbFileState() {
suspend fun handleCannotWrite4GbFileState() {
if (isBookLessThan4GB()) {
performCopyMoveOperationIfSufficientSpaceAvailable()
} else {
Expand All @@ -240,14 +240,12 @@ class CopyMoveFileHandler @Inject constructor(
}
}

fun performCopyMoveOperationIfSufficientSpaceAvailable() {
lifecycleScope?.launch {
val availableSpace = storageCalculator.availableBytes(File(sharedPreferenceUtil.prefStorage))
if (hasNotSufficientStorageSpace(availableSpace)) {
fileCopyMoveCallback?.insufficientSpaceInStorage(availableSpace)
} else {
performCopyMoveOperation()
}
suspend fun performCopyMoveOperationIfSufficientSpaceAvailable() {
val availableSpace = storageCalculator.availableBytes(File(sharedPreferenceUtil.prefStorage))
if (hasNotSufficientStorageSpace(availableSpace)) {
fileCopyMoveCallback?.insufficientSpaceInStorage(availableSpace)
} else {
performCopyMoveOperation()
}
}

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

@Test
fun validateZimFileCanCopyOrMoveShouldReturnFalseWhenDetectingFileSystem() =
runTest {
every { fileHandler.isBookLessThan4GB() } returns true
prepareFileSystemAndFileForMockk(fileSystemState = DetectingFileSystem)
fun validateZimFileCanCopyOrMoveShouldReturnFalseWhenDetectingFileSystem() = runTest {
every { fileHandler.isBookLessThan4GB() } returns true
prepareFileSystemAndFileForMockk(fileSystemState = DetectingFileSystem)

val result = fileHandler.validateZimFileCanCopyOrMove(storageFile)
val result = fileHandler.validateZimFileCanCopyOrMove(storageFile)

assertFalse(result)
verify { fileHandler.handleDetectingFileSystemState() }
}
assertFalse(result)
coVerify { fileHandler.handleDetectingFileSystemState() }
}

@Test
fun validateZimFileCanCopyOrMoveShouldReturnFalseWhenCannotWrite4GbFile() = runBlocking {
Expand All @@ -151,23 +150,23 @@ class CopyMoveFileHandlerTest {
val result = fileHandler.validateZimFileCanCopyOrMove(storageFile)

assertFalse(result)
verify { fileHandler.handleCannotWrite4GbFileState() }
coVerify { fileHandler.handleCannotWrite4GbFileState() }
}

@Test
fun handleDetectingFileSystemStateShouldPerformCopyMoveOperationIfBookLessThan4GB() {
fun handleDetectingFileSystemStateShouldPerformCopyMoveOperationIfBookLessThan4GB() = runTest {
fileHandler = spyk(fileHandler)
prepareFileSystemAndFileForMockk()
every { fileHandler.isBookLessThan4GB() } returns true
every { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() } just Runs
coEvery { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() } just Runs

fileHandler.handleDetectingFileSystemState()

verify { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() }
coVerify { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() }
}

@Test
fun handleDetectingFileSystemStateShouldObserveFileSystemStateIfBookGreaterThan4GB() {
fun handleDetectingFileSystemStateShouldObserveFileSystemStateIfBookGreaterThan4GB() = runTest {
fileHandler = spyk(fileHandler)
prepareFileSystemAndFileForMockk(fileSystemState = DetectingFileSystem)
every { fileHandler.isBookLessThan4GB() } returns false
Expand All @@ -178,19 +177,19 @@ class CopyMoveFileHandlerTest {
}

@Test
fun handleCannotWrite4GbFileStateShouldPerformCopyMoveOperationIfBookLessThan4GB() {
fun handleCannotWrite4GbFileStateShouldPerformCopyMoveOperationIfBookLessThan4GB() = runTest {
fileHandler = spyk(fileHandler)
prepareFileSystemAndFileForMockk()
every { fileHandler.isBookLessThan4GB() } returns true
every { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() } just Runs
coEvery { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() } just Runs

fileHandler.handleCannotWrite4GbFileState()

verify { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() }
coVerify { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() }
}

@Test
fun handleCannotWrite4GbFileStateShouldCallCallbackIfBookGreaterThan4GB() {
fun handleCannotWrite4GbFileStateShouldCallCallbackIfBookGreaterThan4GB() = runTest {
fileHandler = spyk(fileHandler)
prepareFileSystemAndFileForMockk()
every { fileHandler.isBookLessThan4GB() } returns false
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ object Versions {

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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@
package org.kiwix.kiwixmobile.core.extensions

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import java.io.File

suspend fun File.isFileExist(): Boolean = withContext(Dispatchers.IO) { exists() }

fun File.freeSpace(): Long = runBlocking {
withContext(Dispatchers.IO) {
freeSpace
}
}
suspend fun File.freeSpace(): Long = withContext(Dispatchers.IO) { freeSpace }

suspend fun File.totalSpace(): Long = withContext(Dispatchers.IO) { totalSpace }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import javax.inject.Inject

class SetPreferredStorageWithMostSpace @Inject constructor(
private val storageCalculator: StorageCalculator,
private val sharedPreferenceUtil: SharedPreferenceUtil
private val sharedPreferenceUtil: SharedPreferenceUtil,
) : SideEffect<Unit> {
override fun invokeWith(activity: AppCompatActivity) {
activity.lifecycleScope.launch {
Expand Down