move total space to IO Thread

This commit is contained in:
CalebK 2024-11-07 09:10:21 +03:00 committed by Kelson
parent 154389f459
commit cff3901491
9 changed files with 62 additions and 65 deletions

View File

@ -1,7 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<option name="USE_SAME_INDENTS" value="true" />
<option name="IGNORE_SAME_INDENTS_FOR_LANGUAGES" value="true" />
<option name="OTHER_INDENT_OPTIONS">
<value>
<option name="INDENT_SIZE" value="2" />
@ -34,25 +32,11 @@
<option name="JD_PRESERVE_LINE_FEEDS" value="true" />
</JavaCodeStyleSettings>
<JetCodeStyleSettings>
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
<value />
</option>
<option name="PACKAGES_IMPORT_LAYOUT">
<value>
<package name="" alias="false" withSubpackages="true" />
<package name="java" alias="false" withSubpackages="true" />
<package name="javax" alias="false" withSubpackages="true" />
<package name="kotlin" alias="false" withSubpackages="true" />
<package name="" alias="true" withSubpackages="true" />
</value>
</option>
<option name="NAME_COUNT_TO_USE_STAR_IMPORT" value="2147483647" />
<option name="NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS" value="2147483647" />
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</JetCodeStyleSettings>
<XML>
<option name="XML_LEGACY_SETTINGS_IMPORTED" value="true" />
</XML>
<editorconfig>
<option name="ENABLED" value="false" />
</editorconfig>
<ADDITIONAL_INDENT_OPTIONS fileType="php">
<option name="INDENT_SIZE" value="2" />
<option name="CONTINUATION_INDENT_SIZE" value="4" />
@ -319,16 +303,19 @@
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
<option name="LINE_COMMENT_AT_FIRST_COLUMN" value="false" />
<option name="LINE_COMMENT_ADD_SPACE" value="true" />
<option name="KEEP_BLANK_LINES_IN_DECLARATIONS" value="1" />
<option name="KEEP_BLANK_LINES_IN_CODE" value="1" />
<option name="KEEP_BLANK_LINES_BEFORE_RBRACE" value="0" />
<option name="ALIGN_MULTILINE_PARAMETERS" value="false" />
<option name="METHOD_ANNOTATION_WRAP" value="1" />
<option name="FIELD_ANNOTATION_WRAP" value="1" />
<option name="ENUM_CONSTANTS_WRAP" value="2" />
<option name="SPACE_AROUND_EQUALITY_OPERATORS" value="false" />
<option name="CALL_PARAMETERS_WRAP" value="0" />
<option name="CALL_PARAMETERS_LPAREN_ON_NEXT_LINE" value="false" />
<option name="CALL_PARAMETERS_RPAREN_ON_NEXT_LINE" value="false" />
<option name="METHOD_PARAMETERS_WRAP" value="0" />
<option name="METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE" value="false" />
<option name="METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE" value="false" />
<option name="EXTENDS_LIST_WRAP" value="0" />
<option name="METHOD_CALL_CHAIN_WRAP" value="0" />
<option name="ASSIGNMENT_WRAP" value="0" />
<indentOptions>
<option name="INDENT_SIZE" value="2" />
<option name="CONTINUATION_INDENT_SIZE" value="4" />
<option name="CONTINUATION_INDENT_SIZE" value="2" />
<option name="TAB_SIZE" value="2" />
</indentOptions>
</codeStyleSettings>

View File

@ -22,6 +22,7 @@ import android.os.Build
import android.os.Bundle
import android.os.Environment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
import eu.mhutti1.utils.storage.StorageDevice
@ -30,6 +31,7 @@ import io.reactivex.Flowable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.launch
import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.extensions.getFreeSpace
import org.kiwix.kiwixmobile.core.extensions.getUsedSpace
@ -52,7 +54,7 @@ class KiwixPrefsFragment : CorePrefsFragment() {
setMangeExternalStoragePermission()
}
override fun setStorage() {
override suspend fun setStorage() {
sharedPreferenceUtil?.let {
if (storageDisposable?.isDisposed == false) {
// update the storage when user switch to other storage.

View File

@ -25,6 +25,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import eu.mhutti1.utils.storage.adapter.StorageAdapter
@ -56,7 +57,8 @@ class StorageSelectDialog : DialogFragment() {
StorageDelegate(
storageCalculator,
sharedPreferenceUtil,
shouldShowCheckboxSelected
lifecycleScope,
shouldShowCheckboxSelected,
) {
onSelectAction?.invoke(it)
dismiss()

View File

@ -21,6 +21,7 @@ package eu.mhutti1.utils.storage.adapter
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import eu.mhutti1.utils.storage.StorageDevice
import kotlinx.coroutines.CoroutineScope
import org.kiwix.kiwixmobile.core.base.adapter.AdapterDelegate
import org.kiwix.kiwixmobile.core.databinding.ItemStoragePreferenceBinding
import org.kiwix.kiwixmobile.core.extensions.ViewGroupExtensions.viewBinding
@ -30,6 +31,7 @@ import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
class StorageDelegate(
private val storageCalculator: StorageCalculator,
private val sharedPreferenceUtil: SharedPreferenceUtil,
private val lifecycleScope: CoroutineScope,
private val shouldShowCheckboxSelected: Boolean,
private val onClickAction: (StorageDevice) -> Unit
) : AdapterDelegate<StorageDevice> {
@ -37,6 +39,7 @@ class StorageDelegate(
return StorageViewHolder(
parent.viewBinding(ItemStoragePreferenceBinding::inflate, false),
storageCalculator,
lifecycleScope,
sharedPreferenceUtil,
shouldShowCheckboxSelected,
onClickAction

View File

@ -24,6 +24,8 @@ import android.text.SpannableStringBuilder
import android.text.style.AbsoluteSizeSpan
import android.view.View.VISIBLE
import eu.mhutti1.utils.storage.StorageDevice
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.base.adapter.BaseViewHolder
import org.kiwix.kiwixmobile.core.databinding.ItemStoragePreferenceBinding
@ -43,6 +45,7 @@ const val STORAGE_TITLE_TEXTVIEW_SIZE = 15
internal class StorageViewHolder(
private val itemStoragePreferenceBinding: ItemStoragePreferenceBinding,
private val storageCalculator: StorageCalculator,
private val lifecycleScope: CoroutineScope,
private val sharedPreferenceUtil: SharedPreferenceUtil,
private val shouldShowCheckboxSelected: Boolean,
private val onClickAction: (StorageDevice) -> Unit

View File

@ -35,11 +35,7 @@ fun File.freeSpace(): Long = runBlocking {
}
}
fun File.totalSpace(): Long = runBlocking {
withContext(Dispatchers.IO) {
totalSpace
}
}
suspend fun File.totalSpace(): Long = withContext(Dispatchers.IO) { totalSpace }
suspend fun File.canReadFile(): Boolean = withContext(Dispatchers.IO) { canRead() }

View File

@ -34,6 +34,7 @@ import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import androidx.preference.EditTextPreference
import androidx.preference.ListPreference
import androidx.preference.Preference
@ -97,20 +98,22 @@ abstract class CorePrefsFragment :
@Inject
internal var libkiwixBookmarks: LibkiwixBookmarks? = null
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
coreComponent
.activityComponentBuilder()
.activity(requireActivity())
.build()
.inject(this)
addPreferencesFromResource(R.xml.preferences)
setStorage()
setUpSettings()
setupZoom()
sharedPreferenceUtil?.let {
LanguageUtils(requireActivity()).changeFont(
requireActivity(),
it
)
lifecycleScope.launch {
coreComponent
.activityComponentBuilder()
.activity(requireActivity())
.build()
.inject(this@CorePrefsFragment)
addPreferencesFromResource(R.xml.preferences)
setStorage()
setUpSettings()
setupZoom()
sharedPreferenceUtil?.let {
LanguageUtils(requireActivity()).changeFont(
requireActivity(),
it
)
}
}
}
@ -129,7 +132,7 @@ abstract class CorePrefsFragment :
textZoom?.summary = getString(R.string.percentage, sharedPreferenceUtil?.textZoom)
}
protected abstract fun setStorage()
protected abstract suspend fun setStorage()
override fun onResume() {
super.onResume()
preferenceScreen.sharedPreferences
@ -448,18 +451,19 @@ abstract class CorePrefsFragment :
@Suppress("NestedBlockDepth")
fun onStorageDeviceSelected(storageDevice: StorageDevice) {
sharedPreferenceUtil?.let { sharedPreferenceUtil ->
sharedPreferenceUtil.putPrefStorage(
sharedPreferenceUtil.getPublicDirectoryPath(storageDevice.name)
)
sharedPreferenceUtil.putStoragePosition(
if (storageDevice.isInternal) INTERNAL_SELECT_POSITION
else EXTERNAL_SELECT_POSITION
)
setShowStorageOption()
setStorage()
lifecycleScope.launch {
sharedPreferenceUtil?.let { sharedPreferenceUtil ->
sharedPreferenceUtil.putPrefStorage(
sharedPreferenceUtil.getPublicDirectoryPath(storageDevice.name)
)
sharedPreferenceUtil.putStoragePosition(
if (storageDevice.isInternal) INTERNAL_SELECT_POSITION
else EXTERNAL_SELECT_POSITION
)
setShowStorageOption()
setStorage()
}
}
return
}
private fun setShowStorageOption() {

View File

@ -33,15 +33,15 @@ class StorageCalculator @Inject constructor(
fun calculateAvailableSpace(file: File = File(sharedPreferenceUtil.prefStorage)): String =
Bytes(availableBytes(file)).humanReadable
fun calculateTotalSpace(file: File = File(sharedPreferenceUtil.prefStorage)): String =
suspend fun calculateTotalSpace(file: File = File(sharedPreferenceUtil.prefStorage)): String =
Bytes(totalBytes(file)).humanReadable
fun calculateUsedSpace(file: File): String =
suspend fun calculateUsedSpace(file: File): String =
Bytes(totalBytes(file) - availableBytes(file)).humanReadable
fun availableBytes(file: File = File(sharedPreferenceUtil.prefStorage)) =
if (file.isFileExist()) file.freeSpace()
else 0L
fun totalBytes(file: File) = if (file.isFileExist()) file.totalSpace() else 0L
suspend fun totalBytes(file: File) = if (file.isFileExist()) file.totalSpace() else 0L
}

View File

@ -50,7 +50,7 @@ class CustomPrefsFragment : CorePrefsFragment() {
sharedPreferenceUtil?.putPrefExternalLinkPopup(false)
}
override fun setStorage() {
override suspend fun setStorage() {
findPreference<Preference>("pref_storage")?.let(preferenceScreen::removePreference)
}
}