Ensure storage is computed in background, to not block display of general settings.

This commit is contained in:
Benoit Marty 2023-02-07 12:34:58 +01:00
parent 48641769d9
commit fa47c4b87e
2 changed files with 25 additions and 27 deletions

View file

@ -17,6 +17,7 @@
package im.vector.app.core.utils package im.vector.app.core.utils
import android.content.Context import android.content.Context
import androidx.annotation.WorkerThread
import timber.log.Timber import timber.log.Timber
import java.io.File import java.io.File
import java.util.Locale import java.util.Locale
@ -125,6 +126,7 @@ fun getFileExtension(fileUri: String): String? {
* Size * Size
* ========================================================================================== */ * ========================================================================================== */
@WorkerThread
fun getSizeOfFiles(root: File): Long { fun getSizeOfFiles(root: File): Long {
return root.walkTopDown() return root.walkTopDown()
.onEnter { .onEnter {

View file

@ -250,37 +250,28 @@ class VectorSettingsGeneralFragment :
// clear medias cache // clear medias cache
findPreference<VectorPreference>(VectorPreferences.SETTINGS_CLEAR_MEDIA_CACHE_PREFERENCE_KEY)!!.let { findPreference<VectorPreference>(VectorPreferences.SETTINGS_CLEAR_MEDIA_CACHE_PREFERENCE_KEY)!!.let {
val size = getSizeOfFiles(File(requireContext().cacheDir, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR)) + session.fileService().getCacheSize() lifecycleScope.launch(Dispatchers.Main) {
it.summary = getString(R.string.loading)
it.summary = TextUtils.formatFileSize(requireContext(), size.toLong()) val size = getCacheSize()
it.summary = TextUtils.formatFileSize(requireContext(), size)
it.onPreferenceClickListener = Preference.OnPreferenceClickListener { it.onPreferenceClickListener = Preference.OnPreferenceClickListener {
lifecycleScope.launch(Dispatchers.Main) { lifecycleScope.launch(Dispatchers.Main) {
// On UI Thread // On UI Thread
displayLoadingView() displayLoadingView()
Glide.get(requireContext()).clearMemory() Glide.get(requireContext()).clearMemory()
session.fileService().clearCache() session.fileService().clearCache()
val newSize = withContext(Dispatchers.IO) {
var newSize: Long
withContext(Dispatchers.IO) {
// On BG thread // On BG thread
Glide.get(requireContext()).clearDiskCache() Glide.get(requireContext()).clearDiskCache()
getCacheSize()
newSize = getSizeOfFiles(File(requireContext().cacheDir, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR))
newSize += session.fileService().getCacheSize()
} }
it.summary = TextUtils.formatFileSize(requireContext(), newSize) it.summary = TextUtils.formatFileSize(requireContext(), newSize)
hideLoadingView() hideLoadingView()
} }
false false
} }
} }
}
// Sign out // Sign out
findPreference<VectorPreference>("SETTINGS_SIGN_OUT_KEY")!! findPreference<VectorPreference>("SETTINGS_SIGN_OUT_KEY")!!
.onPreferenceClickListener = Preference.OnPreferenceClickListener { .onPreferenceClickListener = Preference.OnPreferenceClickListener {
@ -292,6 +283,11 @@ class VectorSettingsGeneralFragment :
} }
} }
private suspend fun getCacheSize(): Long = withContext(Dispatchers.IO) {
getSizeOfFiles(File(requireContext().cacheDir, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR)) +
session.fileService().getCacheSize()
}
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
// Refresh identity server summary // Refresh identity server summary