From fa47c4b87ef66e7987d41f4ab6387867e954f46a Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 7 Feb 2023 12:34:58 +0100 Subject: [PATCH] Ensure storage is computed in background, to not block display of general settings. --- .../im/vector/app/core/utils/FileUtils.kt | 2 + .../settings/VectorSettingsGeneralFragment.kt | 50 +++++++++---------- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/vector/src/main/java/im/vector/app/core/utils/FileUtils.kt b/vector/src/main/java/im/vector/app/core/utils/FileUtils.kt index c735b8b33d..63f141ef52 100644 --- a/vector/src/main/java/im/vector/app/core/utils/FileUtils.kt +++ b/vector/src/main/java/im/vector/app/core/utils/FileUtils.kt @@ -17,6 +17,7 @@ package im.vector.app.core.utils import android.content.Context +import androidx.annotation.WorkerThread import timber.log.Timber import java.io.File import java.util.Locale @@ -125,6 +126,7 @@ fun getFileExtension(fileUri: String): String? { * Size * ========================================================================================== */ +@WorkerThread fun getSizeOfFiles(root: File): Long { return root.walkTopDown() .onEnter { diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsGeneralFragment.kt b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsGeneralFragment.kt index 548a7be180..c90f55d22a 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsGeneralFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsGeneralFragment.kt @@ -250,37 +250,28 @@ class VectorSettingsGeneralFragment : // clear medias cache findPreference(VectorPreferences.SETTINGS_CLEAR_MEDIA_CACHE_PREFERENCE_KEY)!!.let { - val size = getSizeOfFiles(File(requireContext().cacheDir, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR)) + session.fileService().getCacheSize() - - it.summary = TextUtils.formatFileSize(requireContext(), size.toLong()) - - it.onPreferenceClickListener = Preference.OnPreferenceClickListener { - lifecycleScope.launch(Dispatchers.Main) { - // On UI Thread - displayLoadingView() - - Glide.get(requireContext()).clearMemory() - session.fileService().clearCache() - - var newSize: Long - - withContext(Dispatchers.IO) { - // On BG thread - Glide.get(requireContext()).clearDiskCache() - - newSize = getSizeOfFiles(File(requireContext().cacheDir, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR)) - newSize += session.fileService().getCacheSize() + lifecycleScope.launch(Dispatchers.Main) { + it.summary = getString(R.string.loading) + val size = getCacheSize() + it.summary = TextUtils.formatFileSize(requireContext(), size) + it.onPreferenceClickListener = Preference.OnPreferenceClickListener { + lifecycleScope.launch(Dispatchers.Main) { + // On UI Thread + displayLoadingView() + Glide.get(requireContext()).clearMemory() + session.fileService().clearCache() + val newSize = withContext(Dispatchers.IO) { + // On BG thread + Glide.get(requireContext()).clearDiskCache() + getCacheSize() + } + it.summary = TextUtils.formatFileSize(requireContext(), newSize) + hideLoadingView() } - - it.summary = TextUtils.formatFileSize(requireContext(), newSize) - - hideLoadingView() + false } - - false } } - // Sign out findPreference("SETTINGS_SIGN_OUT_KEY")!! .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() { super.onResume() // Refresh identity server summary