1
1
package com.example.tamaskozmer.kotlinrxexample.model
2
2
3
+ import com.example.tamaskozmer.kotlinrxexample.model.entities.AnswerListModel
4
+ import com.example.tamaskozmer.kotlinrxexample.model.entities.AnswerViewModel
3
5
import com.example.tamaskozmer.kotlinrxexample.model.entities.DetailsModel
6
+ import com.example.tamaskozmer.kotlinrxexample.model.entities.QuestionListModel
4
7
import com.example.tamaskozmer.kotlinrxexample.model.services.UserService
5
8
import io.reactivex.Single
9
+ import io.reactivex.functions.Function3
6
10
7
11
/* *
8
12
* Created by Tamas_Kozmer on 7/4/2017.
@@ -13,10 +17,28 @@ class UserRepository(
13
17
fun getUsers (page : Int ) = userService.getUsers(page)
14
18
15
19
fun getDetails (userId : Long ) : Single <DetailsModel > {
16
- // TODO
17
- return Single .create { emitter ->
18
- val detailsModel = DetailsModel (emptyList(), emptyList(), emptyList())
19
- emitter.onSuccess(detailsModel)
20
- }
20
+ return Single .zip(
21
+ userService.getQuestionsByUser(userId),
22
+ userService.getAnswersByUser(userId),
23
+ userService.getFavoritesByUser(userId),
24
+ Function3 <QuestionListModel , AnswerListModel , QuestionListModel , DetailsModel >
25
+ { questions, answers, favorites ->
26
+ createDetailsModel(questions, answers, favorites) })
27
+ }
28
+
29
+ private fun createDetailsModel (questionsModel : QuestionListModel , answersModel : AnswerListModel ,
30
+ favoritesModel : QuestionListModel ): DetailsModel {
31
+ val questions = questionsModel.items
32
+ .take(3 )
33
+
34
+ val favorites = favoritesModel.items
35
+ .take(3 )
36
+
37
+ val answers = answersModel.items
38
+ .filter { it.accepted }
39
+ .take(3 )
40
+ .map { AnswerViewModel (it.answerId, it.score, it.accepted, " TODO" ) }
41
+
42
+ return DetailsModel (questions, answers, favorites)
21
43
}
22
44
}
0 commit comments