refactor(android): centralize widget display logic

This commit is contained in:
Harsh Shandilya 2024-02-13 21:39:19 +05:30
parent 62f643135b
commit 8d78e1910a
4 changed files with 10 additions and 12 deletions

View file

@ -31,15 +31,18 @@ import dev.msfjarvis.claw.common.theme.LightThemeColors
import dev.msfjarvis.claw.model.UIPost import dev.msfjarvis.claw.model.UIPost
import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
class SavedPostsWidget(private val posts: List<UIPost>) : GlanceAppWidget() { class SavedPostsWidget(private val posts: Flow<List<UIPost>>) : GlanceAppWidget() {
override suspend fun provideGlance(context: Context, id: GlanceId) { override suspend fun provideGlance(context: Context, id: GlanceId) {
val postWindow = posts.first().take(50).toImmutableList()
provideContent { provideContent {
GlanceTheme( GlanceTheme(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) GlanceTheme.colors if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) GlanceTheme.colors
else ColorProviders(light = LightThemeColors, dark = DarkThemeColors) else ColorProviders(light = LightThemeColors, dark = DarkThemeColors)
) { ) {
WidgetHost(posts.toImmutableList()) WidgetHost(postWindow)
} }
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright © 2023 Harsh Shandilya. * Copyright © 2023-2024 Harsh Shandilya.
* Use of this source code is governed by an MIT-style * Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at * license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT. * https://opensource.org/licenses/MIT.
@ -14,8 +14,6 @@ import com.deliveryhero.whetstone.Whetstone
import com.deliveryhero.whetstone.broadcastreceiver.ContributesBroadcastReceiverInjector import com.deliveryhero.whetstone.broadcastreceiver.ContributesBroadcastReceiverInjector
import dev.msfjarvis.claw.android.viewmodel.ClawViewModel import dev.msfjarvis.claw.android.viewmodel.ClawViewModel
import javax.inject.Inject import javax.inject.Inject
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
@ContributesBroadcastReceiverInjector @ContributesBroadcastReceiverInjector
class WidgetReceiver : GlanceAppWidgetReceiver() { class WidgetReceiver : GlanceAppWidgetReceiver() {
@ -23,7 +21,7 @@ class WidgetReceiver : GlanceAppWidgetReceiver() {
@Inject lateinit var viewModel: ClawViewModel @Inject lateinit var viewModel: ClawViewModel
override val glanceAppWidget: GlanceAppWidget override val glanceAppWidget: GlanceAppWidget
get() = SavedPostsWidget(runBlocking { viewModel.savedPosts.first().take(50) }) get() = SavedPostsWidget(viewModel.savedPosts)
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
Whetstone.inject(this, context) Whetstone.inject(this, context)

View file

@ -53,7 +53,6 @@ import kotlinx.collections.immutable.toImmutableMap
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.shareIn import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -161,10 +160,7 @@ constructor(
} else { } else {
savedPostsRepository.savePost(post.toSavedPost()) savedPostsRepository.savePost(post.toSavedPost())
} }
val newPosts = savedPosts.first() withContext(mainDispatcher) { SavedPostsWidget(savedPosts).updateAll(getApplication()) }
withContext(mainDispatcher) {
SavedPostsWidget(newPosts.take(50)).updateAll(getApplication())
}
} }
} }

View file

@ -23,6 +23,7 @@ import dev.msfjarvis.claw.model.fromSavedPost
import dev.msfjarvis.claw.model.toSavedPost import dev.msfjarvis.claw.model.toSavedPost
import javax.inject.Inject import javax.inject.Inject
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
/** /**
* WorkManager-backed [CoroutineWorker] that gets all the posts from [SavedPostsRepository], fetches * WorkManager-backed [CoroutineWorker] that gets all the posts from [SavedPostsRepository], fetches
@ -47,7 +48,7 @@ constructor(
.map { result -> result.value.toSavedPost() } .map { result -> result.value.toSavedPost() }
.let { savedPostsRepository.savePosts(it) } .let { savedPostsRepository.savePosts(it) }
SavedPostsWidget( SavedPostsWidget(
savedPostsRepository.savedPosts.first().take(50).map(UIPost.Companion::fromSavedPost) savedPostsRepository.savedPosts.map { it.map(UIPost.Companion::fromSavedPost) }
) )
.updateAll(applicationContext) .updateAll(applicationContext)
return Result.success() return Result.success()