Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ff617828 authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Refresh App Storage Size

Use flow + collectAsStateWithLifecycle to auto refresh data.

Fix: 292579670
Test: manual - try clear app storage or cache data
Test: unit test
Change-Id: I70462fdcac3edc7cb36e745ea8f4240d9874a165
parent e140ae73
Loading
Loading
Loading
Loading
+9 −7
Original line number Diff line number Diff line
@@ -22,25 +22,27 @@ import android.text.format.Formatter
import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.android.settingslib.spaprivileged.framework.common.storageStatsManager
import com.android.settingslib.spaprivileged.framework.compose.placeholder
import com.android.settingslib.spaprivileged.model.app.userHandle
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn

private const val TAG = "AppStorageSize"

@Composable
fun ApplicationInfo.getStorageSize(): State<String> {
    val context = LocalContext.current
    return produceState(initialValue = placeholder()) {
        withContext(Dispatchers.IO) {
    return remember {
        flow {
            val sizeBytes = calculateSizeBytes(context)
            value = if (sizeBytes != null) Formatter.formatFileSize(context, sizeBytes) else ""
        }
    }
            this.emit(if (sizeBytes != null) Formatter.formatFileSize(context, sizeBytes) else "")
        }.flowOn(Dispatchers.IO)
    }.collectAsStateWithLifecycle(initialValue = placeholder())
}

fun ApplicationInfo.calculateSizeBytes(context: Context): Long? {