From 054d6a53c402a4a8dbf1ff3f1bd16c10373a8315 Mon Sep 17 00:00:00 2001 From: Lars <29163322+Drumber@users.noreply.github.com> Date: Fri, 8 Nov 2024 12:01:43 +0100 Subject: [PATCH] Fix widget not updated on log in or log out --- .../drumber/kitsune/ui/main/MainActivity.kt | 5 +++++ .../kitsune/ui/widget/LibraryAppWidget.kt | 21 ++++++++++++++++--- .../res/layout/activity_authentication.xml | 2 ++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/github/drumber/kitsune/ui/main/MainActivity.kt b/app/src/main/java/io/github/drumber/kitsune/ui/main/MainActivity.kt index b57fcb4c..feb79c73 100644 --- a/app/src/main/java/io/github/drumber/kitsune/ui/main/MainActivity.kt +++ b/app/src/main/java/io/github/drumber/kitsune/ui/main/MainActivity.kt @@ -38,6 +38,7 @@ import io.github.drumber.kitsune.constants.IntentAction.SHORTCUT_LIBRARY import io.github.drumber.kitsune.constants.IntentAction.SHORTCUT_SEARCH import io.github.drumber.kitsune.constants.IntentAction.SHORTCUT_SETTINGS import io.github.drumber.kitsune.databinding.ActivityMainBinding +import io.github.drumber.kitsune.domain.work.UpdateLibraryWidgetUseCase import io.github.drumber.kitsune.preference.KitsunePref import io.github.drumber.kitsune.preference.StartPagePref import io.github.drumber.kitsune.preference.getDestinationId @@ -55,6 +56,7 @@ import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch +import org.koin.android.ext.android.inject import org.koin.androidx.viewmodel.ext.android.viewModel class MainActivity : BaseActivity(R.layout.activity_main) { @@ -63,6 +65,8 @@ class MainActivity : BaseActivity(R.layout.activity_main) { private val binding: ActivityMainBinding by viewBinding() + private val updateLibraryWidget by inject() + private lateinit var navController: NavController private var overrideStartDestination: Int? = null @@ -91,6 +95,7 @@ class MainActivity : BaseActivity(R.layout.activity_main) { repeatOnLifecycle(Lifecycle.State.STARTED) { viewModel.isLoggedInFlow.collectLatest { isLoggedIn -> if (initialLoginState != isLoggedIn) { + updateLibraryWidget(this@MainActivity) startNewMainActivity() } } diff --git a/app/src/main/java/io/github/drumber/kitsune/ui/widget/LibraryAppWidget.kt b/app/src/main/java/io/github/drumber/kitsune/ui/widget/LibraryAppWidget.kt index dce611c3..5a0cc66b 100644 --- a/app/src/main/java/io/github/drumber/kitsune/ui/widget/LibraryAppWidget.kt +++ b/app/src/main/java/io/github/drumber/kitsune/ui/widget/LibraryAppWidget.kt @@ -70,7 +70,10 @@ import io.github.drumber.kitsune.data.presentation.model.library.LibraryEntry import io.github.drumber.kitsune.data.presentation.model.library.LibraryEntryWithModification import io.github.drumber.kitsune.data.presentation.model.library.LibraryStatus import io.github.drumber.kitsune.data.presentation.model.media.identifier +import io.github.drumber.kitsune.data.repository.AccessTokenRepository +import io.github.drumber.kitsune.data.repository.AccessTokenRepository.AccessTokenState import io.github.drumber.kitsune.data.repository.LibraryRepository +import io.github.drumber.kitsune.domain.auth.IsUserLoggedInUseCase import io.github.drumber.kitsune.domain.library.UpdateLibraryEntryProgressUseCase import io.github.drumber.kitsune.preference.KitsunePref import io.github.drumber.kitsune.ui.details.DetailsFragmentArgs @@ -78,9 +81,11 @@ import io.github.drumber.kitsune.ui.main.MainActivity import io.github.drumber.kitsune.ui.widget.KitsuneWidgetTheme.applyTheme import io.github.drumber.kitsune.util.logE import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.cancelFutureOnCancellation import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.emptyFlow +import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch import kotlinx.coroutines.suspendCancellableCoroutine @@ -97,6 +102,8 @@ class LibraryAppWidget : GlanceAppWidget(), KoinComponent { private const val POSTER_IMG_HEIGHT = 85 } + private val isLoggedIn: IsUserLoggedInUseCase by inject() + private val accessTokenRepository: AccessTokenRepository by inject() private val libraryRepository: LibraryRepository by inject() private val updateLibraryEntryProgress: UpdateLibraryEntryProgressUseCase by inject() @@ -330,6 +337,7 @@ class LibraryAppWidget : GlanceAppWidget(), KoinComponent { } private suspend fun loadData(): List { + if (!isLoggedIn()) return emptyList() return try { libraryRepository.getLibraryEntriesWithModificationsByStatus( listOf(LibraryStatus.Current) @@ -340,11 +348,18 @@ class LibraryAppWidget : GlanceAppWidget(), KoinComponent { } } + @OptIn(ExperimentalCoroutinesApi::class) private fun getDataFlow(): Flow> { return try { - libraryRepository.getLibraryEntriesWithModificationsByStatusAsFlow( - listOf(LibraryStatus.Current) - ).map { it.take(LibraryWidget.MAX_ITEM_COUNT) } + accessTokenRepository.accessTokenState.flatMapLatest { state -> + if (state == AccessTokenState.PRESENT) { + libraryRepository.getLibraryEntriesWithModificationsByStatusAsFlow( + listOf(LibraryStatus.Current) + ).map { it.take(LibraryWidget.MAX_ITEM_COUNT) } + } else { + emptyFlow() + } + } } catch (e: Exception) { logE("Failed to get library entries flow.", e) emptyFlow() diff --git a/app/src/main/res/layout/activity_authentication.xml b/app/src/main/res/layout/activity_authentication.xml index 7c57ca4d..d09c5713 100644 --- a/app/src/main/res/layout/activity_authentication.xml +++ b/app/src/main/res/layout/activity_authentication.xml @@ -54,6 +54,7 @@