In one of my android app, at first I want to call an api, which will return a list of item, and the item will be shown in a RecyclerView. I also need to call another api for each item of the RecyclerView to fetch description of that item and show the description to each item according to their id. How can I resolve this scenario.
Subject.kt
data class Subject(
val subject: String,
val subjectCode:String,
val subjectIcon: String,
val progress: Double = 0.0,
)
SubjectRepository.kt
interface SubjectRepository {
fun getAllSubject(): Observable<List<Subject>>
fun getProgressAnalysisOf(subjectCode: String): Observable<Double>
}
SubjectViewModel.kt
class HomeViewModel @Inject constructor(
private val subjectRepository: SubjectRepository,
) : BaseViewModel() {
val subjectList = MutableLiveData<List<Subject>>()
val progress = MutableLiveData<Double>
fun getAllSubject() {
compositeDisposable += subjectRepository.getAllSubject()
.performOnBackgroundOutputOnMain()
.doOnSubscribe { loader.value = true }
.doAfterTerminate { loader.value = false }
.subscribe({
subjectList.value = it
}, {
handleException(it)
})
}
fun getProgressAnalysisOf(subjectCode: String) {
compositeDisposable += subjectRepository. getProgressAnalysisOf(subjectCode)
.performOnBackgroundOutputOnMain()
.doOnSubscribe { loader.value = true }
.doAfterTerminate { loader.value = false }
.subscribe({
progress.value = it
}, {
handleException(it)
})
}
}
first I need to call getAllSubject() which will return a list of Subject and I show this list in my RecyclerView. And for each subject of the recycler view, I have to call getProgressAnalysisOf(subjectCode) and update the item of that recyclerView.