Skip to content

Commit 6b8b04b

Browse files
committed
Fix background refresh functionality
Closes #38
1 parent ef4107a commit 6b8b04b

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

app/src/main/java/com/melonhead/mangadexfollower/work_manager/RefreshWorker.kt

+5-7
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@ package com.melonhead.mangadexfollower.work_manager
33
import android.content.Context
44
import androidx.work.CoroutineWorker
55
import androidx.work.WorkerParameters
6+
import com.melonhead.feature_manga_list.usecases.RefreshMangaUseCase
67
import com.melonhead.lib_app_events.AppEventsRepository
78
import com.melonhead.lib_app_events.events.UserEvent
89
import com.melonhead.lib_logging.Clog
910
import org.koin.core.component.KoinComponent
1011
import org.koin.core.component.inject
1112

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

1517
override suspend fun doWork(): Result {
16-
return if (appEventsRepository.postEvent(UserEvent.RefreshManga)) {
17-
Result.success()
18-
} else {
19-
Clog.e("RefreshWorker: Failed to post RefreshManga event", RuntimeException("RefreshWorker: Failed to post RefreshManga event"))
20-
Result.failure()
21-
}
18+
refreshMangaUseCase.invoke()
19+
return Result.success()
2220
}
2321
}

feature-manga-list/src/main/java/com/melonhead/feature_manga_list/MangaRepository.kt

+11-5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ internal interface MangaRepository {
3333
val manga: Flow<List<UIManga>>
3434
val refreshStatus: Flow<MangaRefreshStatus>
3535
suspend fun getChapterData(mangaId: String,chapterId: String): List<String>?
36+
@Deprecated("Use UserEvent.RefreshManga event instead of using this directly")
37+
suspend fun forceRefresh()
3638
}
3739

3840
internal class MangaRepositoryImpl(
@@ -64,6 +66,7 @@ internal class MangaRepositoryImpl(
6466
private var isLoggedIn: Boolean = false
6567

6668
init {
69+
Clog.i("MangaRepository init")
6770
externalScope.launch {
6871
// refresh manga on login
6972
try {
@@ -74,17 +77,17 @@ internal class MangaRepositoryImpl(
7477
is AuthenticationEvent.LoggedIn -> {
7578
if (!isLoggedIn) {
7679
isLoggedIn = true
77-
forceRefresh()
80+
refreshMangaThrottled(Unit)
7881
}
7982
}
8083
is AuthenticationEvent.LoggedOut -> {
8184
isLoggedIn = false
8285
}
8386
is AppLifecycleEvent.AppForegrounded -> {
84-
forceRefresh()
87+
refreshMangaThrottled(Unit)
8588
}
8689
is UserEvent.RefreshManga -> {
87-
forceRefresh()
90+
refreshMangaThrottled(Unit)
8891
}
8992
is UserEvent.SetMarkChapterRead -> {
9093
markChapterRead(it.mangaId, it.chapterId, it.read)
@@ -102,10 +105,13 @@ internal class MangaRepositoryImpl(
102105
e.printStackTrace()
103106
}
104107
}
108+
109+
refreshMangaThrottled(Unit)
105110
}
106111

107-
private fun forceRefresh() {
108-
if (isLoggedIn) refreshMangaThrottled(Unit)
112+
@Deprecated("Use UserEvent.RefreshManga event instead of using this directly")
113+
override suspend fun forceRefresh() {
114+
refreshMangaThrottled(Unit)
109115
}
110116

111117
private fun generateUIManga(dbSeries: List<MangaEntity>, dbChapters: List<ChapterEntity>): List<UIManga> {

feature-manga-list/src/main/java/com/melonhead/feature_manga_list/di/FeatureMangaListModule.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import com.melonhead.data_user.di.DataUserModule
77
import com.melonhead.feature_manga_list.MangaRepository
88
import com.melonhead.feature_manga_list.MangaRepositoryImpl
99
import com.melonhead.feature_manga_list.navigation.MangaListScreenResolver
10+
import com.melonhead.feature_manga_list.usecases.RefreshMangaUseCase
11+
import com.melonhead.feature_manga_list.usecases.RefreshMangaUseCaseImpl
1012
import com.melonhead.feature_manga_list.viewmodels.MangaListViewModel
1113
import com.melonhead.lib_app_context.di.LibAppContextModule
1214
import com.melonhead.lib_app_events.di.LibAppEventsModule
@@ -28,7 +30,7 @@ val FeatureMangaListModule = module {
2830
includes(DataAtHomeModule)
2931
includes(DataMangaModule)
3032

31-
single<MangaRepository> {
33+
single<MangaRepository>(createdAtStart = true) {
3234
MangaRepositoryImpl(
3335
get(),
3436
get(),
@@ -48,4 +50,5 @@ val FeatureMangaListModule = module {
4850
viewModel { MangaListViewModel(get(), get(), get(), get()) }
4951

5052
single<MangaListScreenResolver>(createdAtStart = true) { MangaListScreenResolver() }
53+
factory<RefreshMangaUseCase> { RefreshMangaUseCaseImpl(get()) }
5154
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.melonhead.feature_manga_list.usecases
2+
3+
import com.melonhead.feature_manga_list.MangaRepository
4+
5+
@Deprecated("Use UserEvent.RefreshManga event instead of using this directly")
6+
interface RefreshMangaUseCase {
7+
suspend fun invoke()
8+
}
9+
10+
@Suppress("DEPRECATION") // This is the only valid use case of forceRefresh
11+
internal class RefreshMangaUseCaseImpl(
12+
private val mangaRepository: MangaRepository,
13+
): RefreshMangaUseCase {
14+
override suspend fun invoke() {
15+
mangaRepository.forceRefresh()
16+
}
17+
}

lib-app-events/src/main/java/com/melonhead/lib_app_events/AppEventsRepository.kt

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.melonhead.lib_app_events
22

33
import com.melonhead.lib_app_events.events.AppEvent
44
import com.melonhead.lib_logging.Clog
5+
import kotlinx.coroutines.Job
56
import kotlinx.coroutines.flow.Flow
67
import kotlinx.coroutines.flow.MutableSharedFlow
78
import kotlinx.coroutines.flow.asSharedFlow

0 commit comments

Comments
 (0)