Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AppControl: Long pressing refresh force reloads the pkg cache #948

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package eu.darken.sdmse.common.pkgs
import eu.darken.sdmse.common.collections.mutate
import eu.darken.sdmse.common.coroutine.AppScope
import eu.darken.sdmse.common.debug.Bugs
import eu.darken.sdmse.common.debug.logging.Logging.Priority.DEBUG
import eu.darken.sdmse.common.debug.logging.Logging.Priority.INFO
import eu.darken.sdmse.common.debug.logging.Logging.Priority.VERBOSE
import eu.darken.sdmse.common.debug.logging.log
import eu.darken.sdmse.common.debug.logging.logTag
Expand Down Expand Up @@ -40,7 +42,10 @@ class PkgRepo @Inject constructor(
data class CacheContainer(
val isInitialized: Boolean = false,
val pkgData: Map<CacheKey, CachedInfo> = emptyMap(),
)
) {
internal val pkgCount: Int
get() = pkgData.count { it.value.data != null }
}

private val cacheLock = Mutex()
private val pkgCache = MutableStateFlow(CacheContainer())
Expand All @@ -52,7 +57,7 @@ class PkgRepo @Inject constructor(
.onStart {
cacheLock.withLock {
if (!pkgCache.value.isInitialized) {
log(TAG) { "Init due to pkgs subscription" }
log(TAG, INFO) { "Init due to pkgs subscription" }
load()
}
}
Expand All @@ -62,7 +67,7 @@ class PkgRepo @Inject constructor(
init {
pkgEventListener.events
.onEach {
log(TAG) { "Refreshing package cache due to event: $it" }
log(TAG, INFO) { "Refreshing package cache due to event: $it" }
Bugs.leaveBreadCrumb("Installed package data has changed")
refresh()
}
Expand All @@ -78,7 +83,7 @@ class PkgRepo @Inject constructor(
}

private suspend fun generatePkgcache(): Map<CacheKey, CachedInfo> {
log(TAG) { "Generating package cache" }
log(TAG, INFO) { "Generating package cache" }
return gatewaySwitch.useRes {
pkgOps.useRes {
val sourceMap: Map<KClass<out PkgDataSource>, Collection<Installed>> = pkgSources.associate { source ->
Expand Down Expand Up @@ -114,8 +119,8 @@ class PkgRepo @Inject constructor(
mergedData.putAll(extraPkgs)
}

log(TAG) { "Pkgs total: ${mergedData.size}" }
mergedData.values.forEach { log(TAG, VERBOSE) { "Installed package: $it" } }
log(TAG, INFO) { "Pkgs total: ${mergedData.size}" }
mergedData.values.forEach { log(TAG, DEBUG) { "Installed package: $it" } }

mergedData
}
Expand Down Expand Up @@ -172,8 +177,9 @@ class PkgRepo @Inject constructor(
}

suspend fun refresh(): Collection<Installed> = cacheLock.withLock {
log(TAG) { "refresh()" }
log(TAG) { "refresh()... (before=${pkgCache.value.pkgCount})" }
load()
log(TAG, INFO) { "...refresh()ed (after=${pkgCache.value.pkgCount})" }
pkgCache.value.pkgData.mapNotNull { it.value.data }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ class AppControl @Inject constructor(
internalData.value = null

val currentUserHandle = userManager.currentUser().handle

val appInfos = pkgRepo.currentPkgs()
val pkgs = if (task.refreshPkgCache) pkgRepo.refresh() else pkgRepo.currentPkgs()
val appInfos = pkgs
.filter { it.userHandle == currentUserHandle }
.map { it.toAppInfo() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.parcelize.Parcelize
@Parcelize
data class AppControlScanTask(
val pkgIdFilter: Set<Pkg.Id> = emptySet(),
val refreshPkgCache: Boolean = false,
) : AppControlTask {

@Parcelize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,16 @@ class AppControlListFragment : Fragment3(R.layout.appcontrol_list_fragment) {
}
}

ui.refreshAction.setOnClickListener {
tracker.clearSelection()
vm.refresh()
ui.refreshAction.apply {
setOnClickListener {
tracker.clearSelection()
vm.refresh()
}
setOnLongClickListener {
tracker.clearSelection()
vm.refresh(refreshPkgCache = true)
true
}
}

super.onViewCreated(view, savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ class AppControlListViewModel @Inject constructor(
settings.listFilter.update { old -> old.copy(tags = emptySet()) }
}

fun refresh() = launch {
fun refresh(refreshPkgCache: Boolean = false) = launch {
log(TAG) { "refresh()" }
appControl.submit(AppControlScanTask())
appControl.submit(AppControlScanTask(refreshPkgCache = refreshPkgCache))
}

fun exclude(items: Collection<AppControlListAdapter.Item>) = launch {
Expand Down