Skip to content

Commit

Permalink
Fix widget not updated on log in or log out
Browse files Browse the repository at this point in the history
  • Loading branch information
Drumber committed Nov 8, 2024
1 parent 6f118bc commit 054d6a5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -63,6 +65,8 @@ class MainActivity : BaseActivity(R.layout.activity_main) {

private val binding: ActivityMainBinding by viewBinding()

private val updateLibraryWidget by inject<UpdateLibraryWidgetUseCase>()

private lateinit var navController: NavController

private var overrideStartDestination: Int? = null
Expand Down Expand Up @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,22 @@ 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
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
Expand All @@ -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()

Expand Down Expand Up @@ -330,6 +337,7 @@ class LibraryAppWidget : GlanceAppWidget(), KoinComponent {
}

private suspend fun loadData(): List<LibraryEntryWithModification> {
if (!isLoggedIn()) return emptyList()
return try {
libraryRepository.getLibraryEntriesWithModificationsByStatus(
listOf(LibraryStatus.Current)
Expand All @@ -340,11 +348,18 @@ class LibraryAppWidget : GlanceAppWidget(), KoinComponent {
}
}

@OptIn(ExperimentalCoroutinesApi::class)
private fun getDataFlow(): Flow<List<LibraryEntryWithModification>> {
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()
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/layout/activity_authentication.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/title_login"
android:textAppearance="?attr/textAppearanceHeadlineMedium"
android:textColor="?attr/colorSecondary"
Expand All @@ -63,6 +64,7 @@
android:id="@+id/tv_additional_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:visibility="gone"
tools:text="Your access token has expired. Please log in again."
tools:visibility="visible"
Expand Down

0 comments on commit 054d6a5

Please sign in to comment.