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