Skip to content

Commit

Permalink
Fix background refresh functionality
Browse files Browse the repository at this point in the history
Closes #38
  • Loading branch information
julieminer committed Aug 26, 2024
1 parent ef4107a commit 6b8b04b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ package com.melonhead.mangadexfollower.work_manager
import android.content.Context
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.melonhead.feature_manga_list.usecases.RefreshMangaUseCase
import com.melonhead.lib_app_events.AppEventsRepository
import com.melonhead.lib_app_events.events.UserEvent
import com.melonhead.lib_logging.Clog
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

class RefreshWorker(appContext: Context, workerParams: WorkerParameters): CoroutineWorker(appContext, workerParams), KoinComponent {
private val appEventsRepository: AppEventsRepository by inject()
@Suppress("DEPRECATION") // Necessary to await the result of the refresh
private val refreshMangaUseCase: RefreshMangaUseCase by inject()

override suspend fun doWork(): Result {
return if (appEventsRepository.postEvent(UserEvent.RefreshManga)) {
Result.success()
} else {
Clog.e("RefreshWorker: Failed to post RefreshManga event", RuntimeException("RefreshWorker: Failed to post RefreshManga event"))
Result.failure()
}
refreshMangaUseCase.invoke()
return Result.success()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ internal interface MangaRepository {
val manga: Flow<List<UIManga>>
val refreshStatus: Flow<MangaRefreshStatus>
suspend fun getChapterData(mangaId: String,chapterId: String): List<String>?
@Deprecated("Use UserEvent.RefreshManga event instead of using this directly")
suspend fun forceRefresh()
}

internal class MangaRepositoryImpl(
Expand Down Expand Up @@ -64,6 +66,7 @@ internal class MangaRepositoryImpl(
private var isLoggedIn: Boolean = false

init {
Clog.i("MangaRepository init")
externalScope.launch {
// refresh manga on login
try {
Expand All @@ -74,17 +77,17 @@ internal class MangaRepositoryImpl(
is AuthenticationEvent.LoggedIn -> {
if (!isLoggedIn) {
isLoggedIn = true
forceRefresh()
refreshMangaThrottled(Unit)
}
}
is AuthenticationEvent.LoggedOut -> {
isLoggedIn = false
}
is AppLifecycleEvent.AppForegrounded -> {
forceRefresh()
refreshMangaThrottled(Unit)
}
is UserEvent.RefreshManga -> {
forceRefresh()
refreshMangaThrottled(Unit)
}
is UserEvent.SetMarkChapterRead -> {
markChapterRead(it.mangaId, it.chapterId, it.read)
Expand All @@ -102,10 +105,13 @@ internal class MangaRepositoryImpl(
e.printStackTrace()
}
}

refreshMangaThrottled(Unit)
}

private fun forceRefresh() {
if (isLoggedIn) refreshMangaThrottled(Unit)
@Deprecated("Use UserEvent.RefreshManga event instead of using this directly")
override suspend fun forceRefresh() {
refreshMangaThrottled(Unit)
}

private fun generateUIManga(dbSeries: List<MangaEntity>, dbChapters: List<ChapterEntity>): List<UIManga> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.melonhead.data_user.di.DataUserModule
import com.melonhead.feature_manga_list.MangaRepository
import com.melonhead.feature_manga_list.MangaRepositoryImpl
import com.melonhead.feature_manga_list.navigation.MangaListScreenResolver
import com.melonhead.feature_manga_list.usecases.RefreshMangaUseCase
import com.melonhead.feature_manga_list.usecases.RefreshMangaUseCaseImpl
import com.melonhead.feature_manga_list.viewmodels.MangaListViewModel
import com.melonhead.lib_app_context.di.LibAppContextModule
import com.melonhead.lib_app_events.di.LibAppEventsModule
Expand All @@ -28,7 +30,7 @@ val FeatureMangaListModule = module {
includes(DataAtHomeModule)
includes(DataMangaModule)

single<MangaRepository> {
single<MangaRepository>(createdAtStart = true) {
MangaRepositoryImpl(
get(),
get(),
Expand All @@ -48,4 +50,5 @@ val FeatureMangaListModule = module {
viewModel { MangaListViewModel(get(), get(), get(), get()) }

single<MangaListScreenResolver>(createdAtStart = true) { MangaListScreenResolver() }
factory<RefreshMangaUseCase> { RefreshMangaUseCaseImpl(get()) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.melonhead.feature_manga_list.usecases

import com.melonhead.feature_manga_list.MangaRepository

@Deprecated("Use UserEvent.RefreshManga event instead of using this directly")
interface RefreshMangaUseCase {
suspend fun invoke()
}

@Suppress("DEPRECATION") // This is the only valid use case of forceRefresh
internal class RefreshMangaUseCaseImpl(
private val mangaRepository: MangaRepository,
): RefreshMangaUseCase {
override suspend fun invoke() {
mangaRepository.forceRefresh()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.melonhead.lib_app_events

import com.melonhead.lib_app_events.events.AppEvent
import com.melonhead.lib_logging.Clog
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
Expand Down

0 comments on commit 6b8b04b

Please sign in to comment.