From a4ead543299a12165373d2255eb0081be6fbde3b Mon Sep 17 00:00:00 2001 From: MM20 <15646950+MM2-0@users.noreply.github.com> Date: Sat, 29 Jan 2022 19:57:02 +0100 Subject: [PATCH] SearchGridView: don't crash if column preference is faulty --- .../launcher2/ui/legacy/search/SearchGridView.kt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) 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() } }