-
Notifications
You must be signed in to change notification settings - Fork 31
courses: smoother data tag view modelling (fixes #13082) #12867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dogi
merged 15 commits into
master
from
feature-courses-viewmodel-orchestration-10508090116705126768
Apr 21, 2026
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
23e22ac
Refactor Courses loading logic into CoursesViewModel
dogi 83d68c3
Refactor Courses loading logic into CoursesViewModel
dogi d2bff57
Refactor Courses loading logic into CoursesViewModel
dogi 23a8246
Refactor Courses loading logic into CoursesViewModel
dogi f2767e1
Refactor Courses loading logic into CoursesViewModel
dogi ac6a65d
Refactor Courses loading logic into CoursesViewModel
dogi 5546e34
Refactor Courses loading logic into CoursesViewModel
dogi 8629758
Refactor Courses loading logic into CoursesViewModel
dogi 4b5680d
Merge branch 'master' into feature-courses-viewmodel-orchestration-10…
Okuro3499 e2d43b2
courses: smoother repository tag filtering (fixes #12977) (#12953)
dogi 46591dd
Merge branch 'master' into feature-courses-viewmodel-orchestration-10…
Okuro3499 55282c5
Merge branch 'master' into feature-courses-viewmodel-orchestration-10…
Okuro3499 53dceda
Merge branch 'master' into feature-courses-viewmodel-orchestration-10…
Okuro3499 8c135fb
Update build.gradle
dogi c9e55d2
Merge branch 'master' into feature-courses-viewmodel-orchestration-10…
dogi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,30 +5,44 @@ import com.google.gson.JsonObject | |
| import dagger.hilt.android.lifecycle.HiltViewModel | ||
| import java.util.HashMap | ||
| import javax.inject.Inject | ||
| import androidx.lifecycle.viewModelScope | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.withContext | ||
| import kotlinx.coroutines.async | ||
| import kotlinx.coroutines.coroutineScope | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import org.ole.planet.myplanet.model.Course | ||
| import org.ole.planet.myplanet.model.RealmMyCourse | ||
| import org.ole.planet.myplanet.model.RealmTag | ||
| import org.ole.planet.myplanet.model.Tag | ||
| import org.ole.planet.myplanet.repository.CoursesRepository | ||
| import org.ole.planet.myplanet.utils.DispatcherProvider | ||
|
|
||
| data class CoursesUiState( | ||
| val courses: List<Course> = emptyList(), | ||
| val map: HashMap<String?, JsonObject> = HashMap(), | ||
| val progressMap: HashMap<String?, JsonObject>? = null | ||
| val progressMap: HashMap<String?, JsonObject>? = null, | ||
| val tagsMap: Map<String, List<Tag>> = emptyMap() | ||
| ) | ||
|
|
||
| @HiltViewModel | ||
| class CoursesViewModel @Inject constructor() : ViewModel() { | ||
| class CoursesViewModel @Inject constructor( | ||
| private val coursesRepository: CoursesRepository, | ||
| private val dispatcherProvider: DispatcherProvider | ||
| ) : ViewModel() { | ||
|
|
||
| private val _coursesState = MutableStateFlow(CoursesUiState()) | ||
| val coursesState: StateFlow<CoursesUiState> = _coursesState | ||
|
|
||
| fun processCourses( | ||
| private fun processCourses( | ||
| isMyCourseLib: Boolean, | ||
| userId: String?, | ||
| validCourses: List<RealmMyCourse>, | ||
| myCourses: List<RealmMyCourse>, | ||
| map: HashMap<String?, JsonObject>, | ||
| progressMap: HashMap<String?, JsonObject>? | ||
| progressMap: HashMap<String?, JsonObject>?, | ||
| tagsMap: Map<String, List<Tag>> | ||
| ) { | ||
| val sortedCourseList = if (isMyCourseLib) { | ||
| myCourses.forEach { it.isMyCourse = true } | ||
|
|
@@ -39,7 +53,56 @@ class CoursesViewModel @Inject constructor() : ViewModel() { | |
| } | ||
|
|
||
| val mappedCourses = sortedCourseList.map { it.toCourse() } | ||
| _coursesState.value = CoursesUiState(mappedCourses, map, progressMap) | ||
| _coursesState.value = CoursesUiState(mappedCourses, map, progressMap, tagsMap) | ||
| } | ||
|
|
||
| fun loadCourses(isMyCourseLib: Boolean, userId: String?) { | ||
| viewModelScope.launch { | ||
| withContext(dispatcherProvider.io) { | ||
| try { | ||
| val allCourses = coursesRepository.getAllCourses() | ||
| val validCourses = allCourses.filter { !it.courseTitle.isNullOrBlank() } | ||
|
|
||
| val myCourses = if (isMyCourseLib) { | ||
| coursesRepository.getMyCourses(userId, validCourses) | ||
| } else { | ||
| emptyList() | ||
| } | ||
|
|
||
| val (map, progressMap) = coroutineScope { | ||
| val ratingsDeferred = async { coursesRepository.getCourseRatings(userId) } | ||
| val progressDeferred = async { coursesRepository.getCourseProgress(userId) } | ||
| Pair(ratingsDeferred.await(), progressDeferred.await()) | ||
| } | ||
|
|
||
| val allCourseIds = validCourses.mapNotNull { it.courseId } | ||
| val tagsMap = coursesRepository.getCourseTagsBulk(allCourseIds) | ||
| .mapValues { entry -> entry.value.map { it.toTag() } } | ||
|
|
||
| processCourses(isMyCourseLib, userId, validCourses, myCourses, map, progressMap, tagsMap) | ||
| } catch (e: Exception) { | ||
| e.printStackTrace() | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun filterCourses(isMyCourseLib: Boolean, userId: String?, searchText: String, selectedGrade: String, selectedSubject: String, tagNames: List<String>) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| viewModelScope.launch { | ||
| withContext(dispatcherProvider.io) { | ||
| val filteredCourses = coursesRepository.filterCourses(searchText, selectedGrade, selectedSubject, tagNames) | ||
| val myCourses = filteredCourses.filter { it.userId?.contains(userId) == true } | ||
|
|
||
| val (map, progressMap) = coroutineScope { | ||
| val ratingsDeferred = async { coursesRepository.getCourseRatings(userId) } | ||
| val progressDeferred = async { coursesRepository.getCourseProgress(userId) } | ||
| Pair(ratingsDeferred.await(), progressDeferred.await()) | ||
| } | ||
|
|
||
| val tagsMap = _coursesState.value.tagsMap | ||
| processCourses(isMyCourseLib, userId, filteredCourses, myCourses, map, progressMap, tagsMap) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun RealmMyCourse.toCourse(): Course { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function with many parameters (count = 7): processCourses [qlty:function-parameters]