@@ -31,62 +31,54 @@ Some of the features of the app include
31
31
viewModel. getPosts()
32
32
```
33
33
34
- ### [ ViewModel] ( posts/src/main/java/com/karntrehan/posts/list/ListViewModel.kt )
34
+ ### [ ViewModel] ( posts/src/main/java/com/karntrehan/posts/list/viewmodel/ ListViewModel.kt )
35
35
``` java
36
36
fun getPosts() {
37
37
if (postsOutcome. value == null )
38
- repo. fetchPosts(compositeDisposable )
38
+ repo. fetchPosts()
39
39
}
40
40
```
41
41
42
- ### [ Repository] ( posts/src/main/java/com/karntrehan/posts/list/ListRepository.kt )
42
+ ### [ Repository] ( posts/src/main/java/com/karntrehan/posts/list/model/ ListRepository.kt )
43
43
``` java
44
44
val postFetchOutcome: PublishSubject<Outcome<List<PostWithUser > > > = PublishSubject . create< Outcome<List<PostWithUser > > > ()
45
45
46
- private val TAG = " ListRepository"
47
-
48
- fun fetchPosts(compositeDisposable: CompositeDisposable ) {
46
+ override fun fetchPosts() {
49
47
postFetchOutcome. loading(true )
50
48
// Observe changes to the db
51
- postDb . postDao() . getAll ()
52
- .performOnBackOutOnMain()
49
+ local . getPostsWithUsers ()
50
+ .performOnBackOutOnMain(scheduler )
53
51
.subscribe({ retailers - >
54
52
postFetchOutcome. success(retailers)
55
53
if (remoteFetch)
56
- refreshPosts(compositeDisposable )
54
+ refreshPosts()
57
55
remoteFetch = false
58
- }, { error - > handleError(error) }
56
+ }, { error - > handleError(error) })
59
57
.addTo(compositeDisposable)
60
- )
61
58
}
62
59
63
- fun refreshPosts(compositeDisposable : CompositeDisposable ) {
60
+ override fun refreshPosts() {
64
61
postFetchOutcome. loading(true )
65
- Flowable . zip(
66
- postService . getUsers(),
67
- postService . getPosts(),
68
- BiFunction<List<User > , List<Post > , Unit > { t1, t2 - > saveUsersAndPosts(t1, t2) }
69
- )
70
- .performOnBackOutOnMain()
71
- .subscribe({}, { error - > handleError(error) })
72
- .addTo(compositeDisposable)
62
+ Flowable . zip(
63
+ remote . getUsers(),
64
+ remote . getPosts(),
65
+ BiFunction<List<User > , List<Post > , Unit > { t1, t2 - > saveUsersAndPosts(t1, t2) }
66
+ )
67
+ .performOnBackOutOnMain(scheduler )
68
+ .subscribe({}, { error - > handleError(error) })
69
+ .addTo(compositeDisposable)
73
70
}
74
71
75
- private fun saveUsersAndPosts(users: List<User > , posts: List<Post > ) {
76
- Completable . fromAction {
77
- postDb. userDao(). insertAll(users)
78
- postDb. postDao(). insertAll(posts)
79
- }
80
- .performOnBackOutOnMain()
81
- .subscribe()
72
+ override fun saveUsersAndPosts(users: List<User > , posts: List<Post > ) {
73
+ local. saveUsersAndPosts(users, posts)
82
74
}
83
75
84
- private fun handleError(error: Throwable ) {
76
+ override fun handleError(error: Throwable ) {
85
77
postFetchOutcome. failed(error)
86
78
}
87
79
```
88
80
89
- ### [ ViewModel] ( posts/src/main/java/com/karntrehan/posts/list/ListViewModel.kt ) ###
81
+ ### [ ViewModel] ( posts/src/main/java/com/karntrehan/posts/list/viewmodel/ ListViewModel.kt ) ###
90
82
``` java
91
83
val postsOutcome: LiveData<Outcome<List<Post > > > by lazy {
92
84
// Convert publish subject to livedata
@@ -133,7 +125,7 @@ To run all the instrumented tests, run `./gradlew connectedAndroidTest`. This w
133
125
* [ Dagger 2] ( https://google.github.io/dagger/ )
134
126
* [ Retrofit] ( http://square.github.io/retrofit/ )
135
127
* [ OkHttp] ( http://square.github.io/okhttp/ )
136
- * [ Picasso] ( square.github.io/picasso/ )
128
+ * [ Picasso] ( http:// square.github.io/picasso/)
137
129
* [ Stetho] ( http://facebook.github.io/stetho/ )
138
130
* [ Room] ( https://developer.android.com/topic/libraries/architecture/room.html )
139
131
* [ ViewModel] ( https://developer.android.com/topic/libraries/architecture/viewmodel.html )
0 commit comments