Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 17d099b

Browse files
Update FlowTestObserver and renaming
1 parent 54aba21 commit 17d099b

File tree

6 files changed

+138
-83
lines changed

6 files changed

+138
-83
lines changed

‎Tutorial2-1FlowBasics/src/main/java/com/smarttoolfactory/tutorial2_1flowbasics/chapter4_single_source_of_truth/data/repository/PostRepository.kt‎

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,37 @@ import io.reactivex.rxjava3.core.Completable
55
import io.reactivex.rxjava3.core.Single
66

77
/**
8-
* This repository contains no data save, delete or fetch logic with RxJava3.
8+
* This repository contains no data save, delete or fetch logic with Coroutines.
99
*
1010
* All business logic for creating offline-first or offline-last approach is moved to UseCase
1111
*/
12-
interface PostRepositoryRxJava3 {
13-
14-
fun getPostEntitiesFromLocal(): Single<List<PostEntity>>
12+
interface PostRepository {
1513

16-
fun fetchEntitiesFromRemote(): Single<List<PostEntity>>
14+
suspendfun fetchEntitiesFromRemote(): List<PostEntity>
1715

18-
fun isCacheExpired(): Boolean
16+
suspendfun getPostEntitiesFromLocal(): List<PostEntity>
1917

20-
fun savePostEntities(postEntities: List<PostEntity>): Completable
18+
suspendfun savePostEntity(postEntities: List<PostEntity>)
2119

22-
fun deletePostEntities(): Completable
20+
suspendfun deletePostEntities()
2321

22+
suspend fun isCacheExpired(): Boolean
2423
}
2524

2625
/**
27-
* This repository contains no data save, delete or fetch logic with Coroutines.
26+
* This repository contains no data save, delete or fetch logic with RxJava3.
2827
*
2928
* All business logic for creating offline-first or offline-last approach is moved to UseCase
3029
*/
31-
interface PostRepository {
32-
33-
suspend fun getPostEntitiesFromLocal(): List<PostEntity>
30+
interface PostRepositoryRxJava3 {
3431

35-
suspendfun fetchEntitiesFromRemote(): List<PostEntity>
32+
fun fetchEntitiesFromRemote(): Single<List<PostEntity>>
3633

37-
suspendfun isCacheExpired(): Boolean
34+
fun getPostEntitiesFromLocal(): Single<List<PostEntity>>
3835

39-
suspendfun savePostEntity(postEntities: List<PostEntity>)
36+
fun savePostEntities(postEntities: List<PostEntity>): Completable
4037

41-
suspendfun deletePostEntities()
38+
fun deletePostEntities(): Completable
4239

43-
}
40+
fun isCacheExpired(): Boolean
41+
}

‎Tutorial2-1FlowBasics/src/main/java/com/smarttoolfactory/tutorial2_1flowbasics/chapter4_single_source_of_truth/domain/GetPostsUseCaseFlow.kt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class GetPostsUseCaseFlow(
5151
repository.savePostEntity(it)
5252
repository.getPostEntitiesFromLocal()
5353
}
54-
5554
}
5655
.flowOn(dispatcherProvider.ioDispatcher)
5756
.catch { cause ->
@@ -132,6 +131,8 @@ class GetPostsUseCaseFlow(
132131
throw EmptyDataException("Data is available in neither in remote nor local source!")
133132
}
134133
}
134+
135+
135136
// 🔥 Section 3 Convert result to state of UI
136137
.map { postList ->
137138

‎Tutorial2-1FlowBasics/src/main/java/com/smarttoolfactory/tutorial2_1flowbasics/chapter4_single_source_of_truth/post_list/PostViewModel.kt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class PostViewModel(
2121
private val getPostsUseCase: GetPostsUseCaseFlow
2222
) : AbstractPostViewModel() {
2323

24-
2524
override fun getPosts() {
2625
getPostsUseCase.getPostFlowOfflineFirst()
2726
.onStart {

‎Tutorial2-1FlowBasics/src/main/java/com/smarttoolfactory/tutorial2_1flowbasics/data/model/ViewState.kt‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.smarttoolfactory.tutorial2_1flowbasics.data.model
22

3+
import kotlinx.coroutines.CoroutineDispatcher
4+
import kotlinx.coroutines.flow.*
5+
36

47
class ViewState<T>(
58
val status: Status,
@@ -20,4 +23,12 @@ enum class Status {
2023
LOADING,
2124
SUCCESS,
2225
ERROR
26+
}
27+
28+
fun <T> Flow<T>.convertToFlowViewState(dispatcher: CoroutineDispatcher): Flow<ViewState<T>> {
29+
30+
return this
31+
.map { postList -> ViewState(status = Status.SUCCESS, data = postList) }
32+
.catch { cause: Throwable -> emitAll(flowOf(ViewState(Status.ERROR, error = cause))) }
33+
.flowOn(dispatcher)
2334
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /