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
5
- import com.example.tamaskozmer.kotlinrxexample.model.entities.DetailsModel
6
- import com.example.tamaskozmer.kotlinrxexample.model.entities.QuestionListModel
3
+ import com.example.tamaskozmer.kotlinrxexample.model.entities.*
4
+ import com.example.tamaskozmer.kotlinrxexample.model.services.QuestionService
7
5
import com.example.tamaskozmer.kotlinrxexample.model.services.UserService
8
6
import io.reactivex.Single
9
7
import io.reactivex.functions.Function3
@@ -12,32 +10,57 @@ import io.reactivex.functions.Function3
12
10
* Created by Tamas_Kozmer on 7/4/2017.
13
11
*/
14
12
class UserRepository (
15
- private val userService : UserService ) {
13
+ private val userService : UserService ,
14
+ private val questionService : QuestionService ) {
16
15
17
16
fun getUsers (page : Int ) = userService.getUsers(page)
18
17
19
18
fun getDetails (userId : Long ) : Single <DetailsModel > {
20
19
return Single .zip(
21
20
userService.getQuestionsByUser(userId),
22
- userService.getAnswersByUser (userId),
21
+ getAnswers (userId),
23
22
userService.getFavoritesByUser(userId),
24
- Function3 <QuestionListModel , AnswerListModel , QuestionListModel , DetailsModel >
23
+ Function3 <QuestionListModel , List < AnswerViewModel > , QuestionListModel , DetailsModel >
25
24
{ questions, answers, favorites ->
26
25
createDetailsModel(questions, answers, favorites) })
27
26
}
28
27
29
- private fun createDetailsModel (questionsModel : QuestionListModel , answersModel : AnswerListModel ,
28
+ private fun getAnswers (userId : Long ) : Single <List <AnswerViewModel >> {
29
+ return userService.getAnswersByUser(userId)
30
+ .flatMap { answerListModel: AnswerListModel ->
31
+ mapAnswersToAnswerViewModels(answerListModel.items) }
32
+ }
33
+
34
+ private fun mapAnswersToAnswerViewModels (answers : List <Answer >): Single <List <AnswerViewModel >> {
35
+ val ids = answers
36
+ .map { it.questionId.toString() }
37
+ .joinToString(separator = " ;" )
38
+
39
+ val questionsListModel = questionService.getQuestionById(ids)
40
+
41
+ return questionsListModel
42
+ .map { questionListModel: QuestionListModel ? ->
43
+ addTitlesToAnswers(answers, questionListModel?.items ? : emptyList()) }
44
+ }
45
+
46
+ private fun addTitlesToAnswers (answers : List <Answer >, questions : List <Question >) : List <AnswerViewModel > {
47
+ return answers.map { (answerId, questionId, score, accepted) ->
48
+ val question = questions.find { it.questionId == questionId }
49
+ AnswerViewModel (answerId, score, accepted, question?.title ? : " Unknown" )
50
+ }
51
+ }
52
+
53
+ private fun createDetailsModel (questionsModel : QuestionListModel , answersModel : List <AnswerViewModel >,
30
54
favoritesModel : QuestionListModel ): DetailsModel {
31
55
val questions = questionsModel.items
32
56
.take(3 )
33
57
34
58
val favorites = favoritesModel.items
35
59
.take(3 )
36
60
37
- val answers = answersModel.items
61
+ val answers = answersModel
38
62
.filter { it.accepted }
39
63
.take(3 )
40
- .map { AnswerViewModel (it.answerId, it.score, it.accepted, " TODO" ) }
41
64
42
65
return DetailsModel (questions, answers, favorites)
43
66
}
0 commit comments