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"> <component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173"> <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"> <option name="OTHER_INDENT_OPTIONS">
<value> <value>
<option name="INDENT_SIZE" value="2" /> <option name="INDENT_SIZE" value="2" />
@ -34,25 +32,11 @@
<option name="JD_PRESERVE_LINE_FEEDS" value="true" /> <option name="JD_PRESERVE_LINE_FEEDS" value="true" />
</JavaCodeStyleSettings> </JavaCodeStyleSettings>
<JetCodeStyleSettings> <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" /> <option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</JetCodeStyleSettings> </JetCodeStyleSettings>
<XML> <editorconfig>
<option name="XML_LEGACY_SETTINGS_IMPORTED" value="true" /> <option name="ENABLED" value="false" />
</XML> </editorconfig>
<ADDITIONAL_INDENT_OPTIONS fileType="php"> <ADDITIONAL_INDENT_OPTIONS fileType="php">
<option name="INDENT_SIZE" value="2" /> <option name="INDENT_SIZE" value="2" />
<option name="CONTINUATION_INDENT_SIZE" value="4" /> <option name="CONTINUATION_INDENT_SIZE" value="4" />
@ -319,16 +303,19 @@
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" /> <option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
<option name="LINE_COMMENT_AT_FIRST_COLUMN" value="false" /> <option name="LINE_COMMENT_AT_FIRST_COLUMN" value="false" />
<option name="LINE_COMMENT_ADD_SPACE" value="true" /> <option name="LINE_COMMENT_ADD_SPACE" value="true" />
<option name="KEEP_BLANK_LINES_IN_DECLARATIONS" value="1" /> <option name="SPACE_AROUND_EQUALITY_OPERATORS" value="false" />
<option name="KEEP_BLANK_LINES_IN_CODE" value="1" /> <option name="CALL_PARAMETERS_WRAP" value="0" />
<option name="KEEP_BLANK_LINES_BEFORE_RBRACE" value="0" /> <option name="CALL_PARAMETERS_LPAREN_ON_NEXT_LINE" value="false" />
<option name="ALIGN_MULTILINE_PARAMETERS" value="false" /> <option name="CALL_PARAMETERS_RPAREN_ON_NEXT_LINE" value="false" />
<option name="METHOD_ANNOTATION_WRAP" value="1" /> <option name="METHOD_PARAMETERS_WRAP" value="0" />
<option name="FIELD_ANNOTATION_WRAP" value="1" /> <option name="METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE" value="false" />
<option name="ENUM_CONSTANTS_WRAP" value="2" /> <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> <indentOptions>
<option name="INDENT_SIZE" value="2" /> <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" /> <option name="TAB_SIZE" value="2" />
</indentOptions> </indentOptions>
</codeStyleSettings> </codeStyleSettings>

View File

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

View File

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

View File

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

View File

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

View File

@ -35,11 +35,7 @@ fun File.freeSpace(): Long = runBlocking {
} }
} }
fun File.totalSpace(): Long = runBlocking { suspend fun File.totalSpace(): Long = withContext(Dispatchers.IO) { totalSpace }
withContext(Dispatchers.IO) {
totalSpace
}
}
suspend fun File.canReadFile(): Boolean = withContext(Dispatchers.IO) { canRead() } 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.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import androidx.preference.EditTextPreference import androidx.preference.EditTextPreference
import androidx.preference.ListPreference import androidx.preference.ListPreference
import androidx.preference.Preference import androidx.preference.Preference
@ -97,11 +98,12 @@ abstract class CorePrefsFragment :
@Inject @Inject
internal var libkiwixBookmarks: LibkiwixBookmarks? = null internal var libkiwixBookmarks: LibkiwixBookmarks? = null
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
lifecycleScope.launch {
coreComponent coreComponent
.activityComponentBuilder() .activityComponentBuilder()
.activity(requireActivity()) .activity(requireActivity())
.build() .build()
.inject(this) .inject(this@CorePrefsFragment)
addPreferencesFromResource(R.xml.preferences) addPreferencesFromResource(R.xml.preferences)
setStorage() setStorage()
setUpSettings() setUpSettings()
@ -113,6 +115,7 @@ abstract class CorePrefsFragment :
) )
} }
} }
}
private fun setupZoom() { private fun setupZoom() {
val textZoom = findPreference<Preference>(INTERNAL_TEXT_ZOOM) val textZoom = findPreference<Preference>(INTERNAL_TEXT_ZOOM)
@ -129,7 +132,7 @@ abstract class CorePrefsFragment :
textZoom?.summary = getString(R.string.percentage, sharedPreferenceUtil?.textZoom) textZoom?.summary = getString(R.string.percentage, sharedPreferenceUtil?.textZoom)
} }
protected abstract fun setStorage() protected abstract suspend fun setStorage()
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
preferenceScreen.sharedPreferences preferenceScreen.sharedPreferences
@ -448,6 +451,7 @@ abstract class CorePrefsFragment :
@Suppress("NestedBlockDepth") @Suppress("NestedBlockDepth")
fun onStorageDeviceSelected(storageDevice: StorageDevice) { fun onStorageDeviceSelected(storageDevice: StorageDevice) {
lifecycleScope.launch {
sharedPreferenceUtil?.let { sharedPreferenceUtil -> sharedPreferenceUtil?.let { sharedPreferenceUtil ->
sharedPreferenceUtil.putPrefStorage( sharedPreferenceUtil.putPrefStorage(
sharedPreferenceUtil.getPublicDirectoryPath(storageDevice.name) sharedPreferenceUtil.getPublicDirectoryPath(storageDevice.name)
@ -459,7 +463,7 @@ abstract class CorePrefsFragment :
setShowStorageOption() setShowStorageOption()
setStorage() setStorage()
} }
return }
} }
private fun setShowStorageOption() { private fun setShowStorageOption() {

View File

@ -33,15 +33,15 @@ class StorageCalculator @Inject constructor(
fun calculateAvailableSpace(file: File = File(sharedPreferenceUtil.prefStorage)): String = fun calculateAvailableSpace(file: File = File(sharedPreferenceUtil.prefStorage)): String =
Bytes(availableBytes(file)).humanReadable 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 Bytes(totalBytes(file)).humanReadable
fun calculateUsedSpace(file: File): String = suspend fun calculateUsedSpace(file: File): String =
Bytes(totalBytes(file) - availableBytes(file)).humanReadable Bytes(totalBytes(file) - availableBytes(file)).humanReadable
fun availableBytes(file: File = File(sharedPreferenceUtil.prefStorage)) = fun availableBytes(file: File = File(sharedPreferenceUtil.prefStorage)) =
if (file.isFileExist()) file.freeSpace() if (file.isFileExist()) file.freeSpace()
else 0L 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) sharedPreferenceUtil?.putPrefExternalLinkPopup(false)
} }
override fun setStorage() { override suspend fun setStorage() {
findPreference<Preference>("pref_storage")?.let(preferenceScreen::removePreference) findPreference<Preference>("pref_storage")?.let(preferenceScreen::removePreference)
} }
} }