diff --git a/ui/src/main/java/de/mm20/launcher2/ui/legacy/search/SearchGridView.kt b/ui/src/main/java/de/mm20/launcher2/ui/legacy/search/SearchGridView.kt index 848b68e5..4f357a35 100644 --- a/ui/src/main/java/de/mm20/launcher2/ui/legacy/search/SearchGridView.kt +++ b/ui/src/main/java/de/mm20/launcher2/ui/legacy/search/SearchGridView.kt @@ -35,19 +35,28 @@ class SearchGridView : ViewGroup, KoinComponent { val dataStore: LauncherDataStore by inject() + private val columnCountPreference = dataStore.data + .map { it.grid.columnCount.takeIf { it > 0 } ?: 5 } + .distinctUntilChanged() + var job: Job? = null override fun onAttachedToWindow() { super.onAttachedToWindow() job?.cancel() lifecycleScope.launch { lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { - dataStore.data.map { it.grid.columnCount }.distinctUntilChanged().collectLatest { + columnCountPreference.collectLatest { columnCount = it } } } } + override fun onDetachedFromWindow() { + super.onDetachedFromWindow() + job?.cancel() + } + var columnCount: Int = 1 set(value) { @@ -116,7 +125,7 @@ class SearchGridView : ViewGroup, KoinComponent { init { columnCount = runBlocking { - dataStore.data.map { it.grid.columnCount }.first().takeIf { it > 0 } ?: 5 + columnCountPreference.first() } }