Skip to content

Commit 1f5c587

Browse files
committedJul 28, 2017
Request the questions belonging to answers to get the title
1 parent 61de364 commit 1f5c587

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed
 

‎app/src/main/java/com/example/tamaskozmer/kotlinrxexample/di/modules/ApplicationModule.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.example.tamaskozmer.kotlinrxexample.di.modules
22

33
import com.example.tamaskozmer.kotlinrxexample.CustomApplication
44
import com.example.tamaskozmer.kotlinrxexample.model.UserRepository
5+
import com.example.tamaskozmer.kotlinrxexample.model.services.QuestionService
56
import com.example.tamaskozmer.kotlinrxexample.model.services.UserService
67
import com.google.gson.Gson
78
import dagger.Module
@@ -30,5 +31,6 @@ class ApplicationModule(val application: CustomApplication) {
3031
@Singleton
3132
fun provideUserRepository(retrofit: Retrofit) =
3233
UserRepository(
33-
retrofit.create(UserService::class.java))
34+
retrofit.create(UserService::class.java),
35+
retrofit.create(QuestionService::class.java))
3436
}

‎app/src/main/java/com/example/tamaskozmer/kotlinrxexample/model/UserRepository.kt

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.example.tamaskozmer.kotlinrxexample.model
22

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
75
import com.example.tamaskozmer.kotlinrxexample.model.services.UserService
86
import io.reactivex.Single
97
import io.reactivex.functions.Function3
@@ -12,32 +10,57 @@ import io.reactivex.functions.Function3
1210
* Created by Tamas_Kozmer on 7/4/2017.
1311
*/
1412
class UserRepository(
15-
private val userService: UserService) {
13+
private val userService: UserService,
14+
private val questionService: QuestionService) {
1615

1716
fun getUsers(page: Int) = userService.getUsers(page)
1817

1918
fun getDetails(userId: Long) : Single<DetailsModel> {
2019
return Single.zip(
2120
userService.getQuestionsByUser(userId),
22-
userService.getAnswersByUser(userId),
21+
getAnswers(userId),
2322
userService.getFavoritesByUser(userId),
24-
Function3<QuestionListModel, AnswerListModel, QuestionListModel, DetailsModel>
23+
Function3<QuestionListModel, List<AnswerViewModel>, QuestionListModel, DetailsModel>
2524
{ questions, answers, favorites ->
2625
createDetailsModel(questions, answers, favorites) })
2726
}
2827

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>,
3054
favoritesModel: QuestionListModel): DetailsModel {
3155
val questions = questionsModel.items
3256
.take(3)
3357

3458
val favorites = favoritesModel.items
3559
.take(3)
3660

37-
val answers = answersModel.items
61+
val answers = answersModel
3862
.filter { it.accepted }
3963
.take(3)
40-
.map { AnswerViewModel(it.answerId, it.score, it.accepted, "TODO") }
4164

4265
return DetailsModel(questions, answers, favorites)
4366
}

0 commit comments

Comments
 (0)
Please sign in to comment.