Skip to content

Commit 9ee76d5

Browse files
Refactor: Move "My Library" data loading to ViewModel
Refactored `BaseDashboardFragment` to use `DashboardViewModel` for fetching the "My Library" data. - Injected `DashboardViewModel` into `BaseDashboardFragment` using Hilt. - Moved the Realm query for library items from the fragment to a `suspend` function in the ViewModel. - The fragment now calls the ViewModel from a `lifecycleScope` coroutine to load the data asynchronously. - This change moves a database query off the main thread, improving performance and adhering to a better architectural pattern by separating UI and data concerns.
1 parent 83a17ba commit 9ee76d5

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

app/src/main/java/org/ole/planet/myplanet/ui/dashboard/BaseDashboardFragment.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,17 @@ import org.ole.planet.myplanet.ui.team.TeamDetailFragment
5050
import org.ole.planet.myplanet.ui.userprofile.BecomeMemberActivity
5151
import org.ole.planet.myplanet.ui.userprofile.UserProfileFragment
5252
import org.ole.planet.myplanet.utilities.Constants
53+
import androidx.fragment.app.viewModels
54+
import dagger.hilt.android.AndroidEntryPoint
5355
import org.ole.planet.myplanet.utilities.DialogUtils
5456
import org.ole.planet.myplanet.utilities.DownloadUtils
5557
import org.ole.planet.myplanet.utilities.FileUtils
5658
import org.ole.planet.myplanet.utilities.Utilities
5759

60+
@AndroidEntryPoint
5861
open class BaseDashboardFragment : BaseDashboardFragmentPlugin(), NotificationCallback,
5962
SyncListener {
63+
private val dashboardViewModel: DashboardViewModel by viewModels()
6064
private var fullName: String? = null
6165
private var params = LinearLayout.LayoutParams(250, 100)
6266
private var di: DialogUtils.CustomProgressDialog? = null
@@ -139,14 +143,7 @@ open class BaseDashboardFragment : BaseDashboardFragmentPlugin(), NotificationCa
139143
}
140144
}
141145

142-
private suspend fun myLibraryDiv(view: View) {
143-
val dbMylibrary = withContext(Dispatchers.IO) {
144-
Realm.getDefaultInstance().use { realm ->
145-
val results = RealmMyLibrary.getMyLibraryByUserId(realm, settings)
146-
realm.copyFromRealm(results)
147-
}
148-
}
149-
146+
private fun myLibraryDiv(view: View, dbMylibrary: List<RealmMyLibrary>) {
150147
view.findViewById<FlexboxLayout>(R.id.flexboxLayout).flexDirection = FlexDirection.ROW
151148
if (dbMylibrary.isEmpty()) {
152149
view.findViewById<TextView>(R.id.count_library).visibility = View.GONE
@@ -336,7 +333,8 @@ open class BaseDashboardFragment : BaseDashboardFragmentPlugin(), NotificationCa
336333
homeItemClickListener?.openCallFragment(UserProfileFragment())
337334
}
338335
viewLifecycleOwner.lifecycleScope.launch {
339-
myLibraryDiv(view)
336+
val dbMylibrary = dashboardViewModel.getMyLibraryItems(settings.getString("userId", "--"))
337+
myLibraryDiv(view, dbMylibrary)
340338
}
341339
initializeFlexBoxView(view, R.id.flexboxLayoutCourse, RealmMyCourse::class.java)
342340
initializeFlexBoxView(view, R.id.flexboxLayoutTeams, RealmMyTeam::class.java)

app/src/main/java/org/ole/planet/myplanet/ui/dashboard/DashboardViewModel.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
77
import kotlinx.coroutines.flow.StateFlow
88
import kotlinx.coroutines.flow.asStateFlow
99
import org.ole.planet.myplanet.model.RealmSubmission
10+
import org.ole.planet.myplanet.model.RealmMyLibrary
1011
import org.ole.planet.myplanet.repository.CourseRepository
1112
import org.ole.planet.myplanet.repository.LibraryRepository
1213
import org.ole.planet.myplanet.repository.NotificationRepository
@@ -65,4 +66,8 @@ class DashboardViewModel @Inject constructor(
6566
suspend fun getUnreadNotificationsSize(userId: String?): Int {
6667
return notificationRepository.getUnreadCount(userId)
6768
}
69+
70+
suspend fun getMyLibraryItems(userId: String?): List<RealmMyLibrary> {
71+
return libraryRepository.getLibraryListForUser(userId)
72+
}
6873
}

0 commit comments

Comments
 (0)