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
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 {

View file

@ -250,37 +250,28 @@ class VectorSettingsGeneralFragment :
// clear medias cache
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()
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<VectorPreference>("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