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 ae41d05

Browse files
Add RxJava3 for DataSources, Repository, UseCase and ViewModel
1 parent fe84d1c commit ae41d05

File tree

13 files changed

+432
-127
lines changed

13 files changed

+432
-127
lines changed

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

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ package com.smarttoolfactory.tutorial2_1flowbasics.chapter4_single_source_of_tru
33
import android.os.Bundle
44
import androidx.appcompat.app.AppCompatActivity
55
import com.smarttoolfactory.tutorial2_1flowbasics.R
6-
import com.smarttoolfactory.tutorial2_1flowbasics.data.model.PostEntity
7-
import com.smarttoolfactory.tutorial2_1flowbasics.di.ServiceLocator
8-
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
9-
import io.reactivex.rxjava3.core.Single
10-
import io.reactivex.rxjava3.schedulers.Schedulers
116
import kotlinx.coroutines.*
127
import kotlin.coroutines.CoroutineContext
138

@@ -24,87 +19,6 @@ class Activity4SingleSourceOfTruth : AppCompatActivity(), CoroutineScope {
2419
super.onCreate(savedInstanceState)
2520
setContentView(R.layout.activity4_single_source_of_truth)
2621
job = Job()
27-
28-
// examineDaoWithRxJava()
29-
3022
}
3123

32-
/**
33-
* Function to test what happens when there is no post in DB with Single, Maybe and Observable
34-
* classes of RxJava2
35-
*/
36-
private fun examineDaoWithRxJava() {
37-
38-
val serviceLocator = ServiceLocator(application)
39-
40-
val postDaoRxJava = serviceLocator.providePostDaoRxJava()
41-
42-
// SINGLE
43-
val disposableSingle = postDaoRxJava.getPostByIdSingle(1)
44-
.subscribeOn(Schedulers.io())
45-
.observeOn(AndroidSchedulers.mainThread())
46-
// .onErrorResumeNext {
47-
// _ -> Single.error(AssertionError("ERROR"))
48-
// }
49-
.onErrorResumeNext {
50-
Single.just(PostEntity(1, 1, "Title", "Body"))
51-
}
52-
.doOnError { throwable ->
53-
println("🔥 MainActivity onCreate() doOnError() throwable: $throwable")
54-
}
55-
.subscribe(
56-
{ postEntity ->
57-
println("🍎 MainActivity onCreate() getPostByIdSingle() onNext(): $postEntity")
58-
},
59-
{
60-
println("⏰ MainActivity onCreate() getPostByIdSingle() onError: $it")
61-
}
62-
)
63-
64-
// MAYBE
65-
// val disposableMaybe = postDaoRxJava.getPostListByIdMaybe(1)
66-
// .subscribeOn(Schedulers.io())
67-
// .observeOn(AndroidSchedulers.mainThread())
68-
// .doOnError { throwable ->
69-
// println("🔥 MainActivity onCreate() doOnError() throwable: $throwable")
70-
// }
71-
// .doOnComplete {
72-
// println("🔥 MainActivity onCreate() doOnComplete()")
73-
//
74-
// }
75-
// .subscribe(
76-
// { postEntity ->
77-
// println("🍎 MainActivity onCreate() getPostListByIdMaybe() onNext(): $postEntity")
78-
// },
79-
// {
80-
// println("🍏 MainActivity onCreate() getPostListByIdMaybe() onError: $it")
81-
// },
82-
// {
83-
// println("⏰ MainActivity onCreate() getPostListByIdMaybe() onComplete")
84-
// }
85-
// )
86-
87-
// OBSERVABLE
88-
// val disposableObservable = postDaoRxJava.getPostListById(1)
89-
// .subscribeOn(Schedulers.io())
90-
// .observeOn(AndroidSchedulers.mainThread())
91-
// .doOnError { throwable ->
92-
// println("🔥 MainActivity onCreate() doOnError() throwable: $throwable")
93-
// }
94-
// .doOnComplete {
95-
// println("🔥 MainActivity onCreate() doOnComplete()")
96-
//
97-
// }
98-
// .subscribe(
99-
// { postEntity ->
100-
// println("🍎 MainActivity onCreate() getPostListById() onNext(): $postEntity")
101-
// },
102-
// {
103-
// println("🍏 MainActivity onCreate() getPostListById() onError: $it")
104-
// },
105-
// {
106-
// println("⏰ MainActivity onCreate() getPostListById() onComplete")
107-
// }
108-
// )
109-
}
11024
}

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
11
package com.smarttoolfactory.tutorial2_1flowbasics.chapter4_single_source_of_truth.data.repository
22

33
import com.smarttoolfactory.tutorial2_1flowbasics.data.model.PostEntity
4-
import kotlinx.coroutines.flow.Flow
4+
import io.reactivex.rxjava3.core.Completable
5+
import io.reactivex.rxjava3.core.Single
56

7+
/**
8+
* This repository contains no data save, delete or fetch logic with RxJava3.
9+
*
10+
* All business logic for creating offline-first or offline-last approach is moved to UseCase
11+
*/
12+
interface PostRepositoryRxJava3 {
13+
14+
fun getPostEntitiesFromLocal(): Single<List<PostEntity>>
615

16+
fun fetchEntitiesFromRemote(): Single<List<PostEntity>>
717

18+
fun isCacheExpired(): Boolean
819

9-
interfacePostRepositoryRxJava {
20+
funsavePostEntities(postEntities:List<PostEntity>): Completable
1021

22+
fun deletePostEntities(): Completable
1123

1224
}
1325

1426
/**
15-
* This repository contains no data save, delete or retieve logic. All business logic for creating offline-first
16-
* or offline-last approach is moved to UseCase
27+
* This repository contains no data save, delete or fetch logic with Coroutines.
28+
*
29+
* All business logic for creating offline-first or offline-last approach is moved to UseCase
1730
*/
1831
interface PostRepository {
1932

2033
suspend fun getPostEntitiesFromLocal(): List<PostEntity>
2134

2235
suspend fun fetchEntitiesFromRemote(): List<PostEntity>
2336

24-
suspend fun isCacheExpired():Boolean
37+
suspend fun isCacheExpired():Boolean
2538

2639
suspend fun savePostEntity(postEntities: List<PostEntity>)
2740

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

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.smarttoolfactory.tutorial2_1flowbasics.chapter4_single_source_of_truth.data.repository
22

3-
import com.smarttoolfactory.tutorial2_1flowbasics.chapter4_single_source_of_truth.data.source.Cache
4-
import com.smarttoolfactory.tutorial2_1flowbasics.chapter4_single_source_of_truth.data.source.RemotePostDataSource
5-
import com.smarttoolfactory.tutorial2_1flowbasics.chapter4_single_source_of_truth.data.source.LocalPostDataSource
3+
import com.smarttoolfactory.tutorial2_1flowbasics.chapter4_single_source_of_truth.data.source.*
4+
import com.smarttoolfactory.tutorial2_1flowbasics.chapter4_single_source_of_truth_rxjava3.data.source.Cache
5+
import com.smarttoolfactory.tutorial2_1flowbasics.chapter4_single_source_of_truth_rxjava3.data.source.LocalPostDataSourceRxJava3
6+
import com.smarttoolfactory.tutorial2_1flowbasics.chapter4_single_source_of_truth_rxjava3.data.source.RemotePostDataSourceRxJava3
67
import com.smarttoolfactory.tutorial2_1flowbasics.data.mapper.DTOtoEntityMapper
78
import com.smarttoolfactory.tutorial2_1flowbasics.data.model.PostEntity
9+
import io.reactivex.rxjava3.core.Completable
10+
import io.reactivex.rxjava3.core.Single
811

912

1013
/**
@@ -49,4 +52,37 @@ class PostRepositoryImpl(
4952
override suspend fun deletePostEntities() {
5053
localPostDataSource.deletePostEntities()
5154
}
52-
}
55+
}
56+
57+
class PostRepoRxJava3Impl(
58+
private val localPostDataSource: LocalPostDataSourceRxJava3,
59+
private val remotePostDataSource: RemotePostDataSourceRxJava3,
60+
private val mapper: DTOtoEntityMapper,
61+
private val cache: Cache
62+
) : PostRepositoryRxJava3 {
63+
64+
override fun getPostEntitiesFromLocal(): Single<List<PostEntity>> {
65+
return localPostDataSource.getPostEntities()
66+
}
67+
68+
override fun fetchEntitiesFromRemote(): Single<List<PostEntity>> {
69+
return remotePostDataSource.getPostDTOs()
70+
.map {
71+
mapper.map(it)
72+
}
73+
}
74+
75+
override fun isCacheExpired(): Boolean {
76+
return cache.isDirty
77+
}
78+
79+
override fun savePostEntities(postEntities: List<PostEntity>): Completable {
80+
return localPostDataSource.saveEntities(postEntities).andThen(Completable.fromCallable {
81+
cache.saveCacheTime()
82+
})
83+
}
84+
85+
override fun deletePostEntities(): Completable {
86+
return localPostDataSource.deletePostEntities()
87+
}
88+
}

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
package com.smarttoolfactory.tutorial2_1flowbasics.chapter4_single_source_of_truth.data.source
22

3+
import com.smarttoolfactory.tutorial2_1flowbasics.chapter4_single_source_of_truth_rxjava3.data.source.LocalPostDataSourceRxJava3
4+
import com.smarttoolfactory.tutorial2_1flowbasics.chapter4_single_source_of_truth_rxjava3.data.source.PostDataSource
5+
import com.smarttoolfactory.tutorial2_1flowbasics.chapter4_single_source_of_truth_rxjava3.data.source.RemotePostDataSourceRxJava3
36
import com.smarttoolfactory.tutorial2_1flowbasics.data.api.PostApi
7+
import com.smarttoolfactory.tutorial2_1flowbasics.data.api.PostApiRxJava
48
import com.smarttoolfactory.tutorial2_1flowbasics.data.db.PostDao
9+
import com.smarttoolfactory.tutorial2_1flowbasics.data.db.PostDaoRxJava
510
import com.smarttoolfactory.tutorial2_1flowbasics.data.model.PostDTO
611
import com.smarttoolfactory.tutorial2_1flowbasics.data.model.PostEntity
12+
import io.reactivex.rxjava3.core.Completable
13+
import io.reactivex.rxjava3.core.Single
714

815
interface PostDataSource
916

17+
/*
18+
Coroutines
19+
*/
20+
1021
interface RemotePostDataSource : PostDataSource {
1122
suspend fun getPostDTOs(): List<PostDTO>
1223
}
@@ -40,6 +51,47 @@ class LocalDataSourceImpl(private val postDao: PostDao) : LocalPostDataSource {
4051
}
4152
}
4253

54+
/*
55+
RxJava3
56+
*/
57+
58+
interface RemotePostDataSourceRxJava3 : PostDataSource {
59+
fun getPostDTOs(): Single<List<PostDTO>>
60+
}
61+
62+
interface LocalPostDataSourceRxJava3 : PostDataSource {
63+
64+
fun getPostEntities(): Single<List<PostEntity>>
65+
fun saveEntities(posts: List<PostEntity>): Completable
66+
fun deletePostEntities(): Completable
67+
}
68+
69+
class RemoteDataSourceRxJava3Impl(private val postApi: PostApiRxJava) :
70+
RemotePostDataSourceRxJava3 {
71+
72+
override fun getPostDTOs(): Single<List<PostDTO>> {
73+
return postApi.getPostsSingle()
74+
}
75+
}
76+
77+
class LocalDataSourceRxJava3Impl(private val postDao: PostDaoRxJava) : LocalPostDataSourceRxJava3 {
78+
79+
override fun getPostEntities(): Single<List<PostEntity>> {
80+
return postDao.getPostsSingleNonNull()
81+
}
82+
83+
override fun saveEntities(posts: List<PostEntity>): Completable {
84+
return postDao.savePosts(posts)
85+
}
86+
87+
override fun deletePostEntities(): Completable {
88+
return postDao.deleteAll()
89+
}
90+
}
91+
92+
/*
93+
Cache
94+
*/
4395
class Cache(private val postDao: PostDao) {
4496

4597
private val expireDuration = 30 * 60 * 1000
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import com.smarttoolfactory.tutorial2_1flowbasics.data.model.ViewState
1010
import com.smarttoolfactory.tutorial2_1flowbasics.util.EmptyDataException
1111
import kotlinx.coroutines.flow.*
1212

13-
class GetPostsUseCase(
13+
class GetPostsUseCaseFlow(
1414
private val repository: PostRepository,
1515
private val entityToPostMapper: EntityToPostMapper,
1616
private val dispatcherProvider: DispatcherProvider
@@ -40,13 +40,15 @@ class GetPostsUseCase(
4040
return flow { emit(repository.fetchEntitiesFromRemote()) }
4141
.map {
4242
println("🍏 getPostFlowOfflineLast() First map in thread: ${Thread.currentThread().name}")
43-
if (it.isEmpty()) {
44-
repository.getPostEntitiesFromLocal()
43+
44+
if (it.isNullOrEmpty()) {
45+
throw EmptyDataException("No data is available!")
4546
} else {
4647
repository.deletePostEntities()
4748
repository.savePostEntity(it)
4849
repository.getPostEntitiesFromLocal()
4950
}
51+
5052
}
5153
.flowOn(dispatcherProvider.ioDispatcher)
5254
.catch { cause ->

0 commit comments

Comments
(0)

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