From 6ff90cd8dbe1b37a1f06888dbabaf9ce66121ce7 Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Sat, 28 Dec 2024 20:14:22 +0300 Subject: [PATCH] migrate courseDetailFragment --- .../planet/myplanet/model/RealmMyCourse.kt | 2 +- .../ui/courses/CourseDetailFragment.kt | 41 +++++++++++-------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/org/ole/planet/myplanet/model/RealmMyCourse.kt b/app/src/main/java/org/ole/planet/myplanet/model/RealmMyCourse.kt index 98cf0979a5..c026c35027 100644 --- a/app/src/main/java/org/ole/planet/myplanet/model/RealmMyCourse.kt +++ b/app/src/main/java/org/ole/planet/myplanet/model/RealmMyCourse.kt @@ -175,7 +175,7 @@ class RealmMyCourse : RealmObject { settings.edit().putString("concatenated_links", gson.toJson(existingConcatenatedLinks)).apply() } - fun getCourseSteps(realm: Realm, courseId: String): List { + fun getCourseSteps(realm: Realm, courseId: String?): List { return realm.query("id == $0", courseId).first().find()?.courseSteps?.toList() ?: emptyList() } diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/courses/CourseDetailFragment.kt b/app/src/main/java/org/ole/planet/myplanet/ui/courses/CourseDetailFragment.kt index 96d7cc530f..3b10d34831 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/courses/CourseDetailFragment.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/courses/CourseDetailFragment.kt @@ -1,24 +1,17 @@ package org.ole.planet.myplanet.ui.courses import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup +import android.view.* import android.widget.TextView import androidx.recyclerview.widget.LinearLayoutManager -import io.realm.Realm -import org.ole.planet.myplanet.MainApplication -import org.ole.planet.myplanet.R +import io.realm.kotlin.Realm +import kotlinx.coroutines.* +import org.ole.planet.myplanet.* import org.ole.planet.myplanet.base.BaseContainerFragment import org.ole.planet.myplanet.callback.OnRatingChangeListener import org.ole.planet.myplanet.databinding.FragmentCourseDetailBinding import org.ole.planet.myplanet.datamanager.DatabaseService -import org.ole.planet.myplanet.model.RealmMyCourse -import org.ole.planet.myplanet.model.RealmMyCourse.Companion.getCourseSteps -import org.ole.planet.myplanet.model.RealmMyLibrary -import org.ole.planet.myplanet.model.RealmRating.Companion.getRatingsById -import org.ole.planet.myplanet.model.RealmStepExam.Companion.getNoOfExam -import org.ole.planet.myplanet.model.RealmUserModel +import org.ole.planet.myplanet.model.* import org.ole.planet.myplanet.service.UserProfileDbHandler import org.ole.planet.myplanet.utilities.Markdown.setMarkdownText @@ -29,6 +22,8 @@ class CourseDetailFragment : BaseContainerFragment(), OnRatingChangeListener { var courses: RealmMyCourse? = null var user: RealmUserModel? = null var id: String? = null + private val scope = CoroutineScope(Dispatchers.Main + Job()) + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) if (arguments != null) { @@ -40,7 +35,9 @@ class CourseDetailFragment : BaseContainerFragment(), OnRatingChangeListener { fragmentCourseDetailBinding = FragmentCourseDetailBinding.inflate(inflater, container, false) dbService = DatabaseService() cRealm = dbService.realmInstance - courses = cRealm.where(RealmMyCourse::class.java).equalTo("courseId", id).findFirst() + courses = mRealm.query(RealmMyCourse::class, "courseId == $0", id ?: "") + .first() + .find() user = UserProfileDbHandler(requireContext()).userModel return fragmentCourseDetailBinding.root } @@ -58,10 +55,18 @@ class CourseDetailFragment : BaseContainerFragment(), OnRatingChangeListener { setTextViewVisibility(fragmentCourseDetailBinding.language, courses?.languageOfInstruction, fragmentCourseDetailBinding.ltLanguage) val markdownContentWithLocalPaths = CourseStepFragment.prependBaseUrlToImages(courses?.description, "file://" + MainApplication.context.getExternalFilesDir(null) + "/ole/") setMarkdownText(fragmentCourseDetailBinding.description, markdownContentWithLocalPaths) - fragmentCourseDetailBinding.noOfExams.text = context?.getString(R.string.number_placeholder, getNoOfExam(cRealm, id)) - val resources: List = cRealm.where(RealmMyLibrary::class.java).equalTo("courseId", id).equalTo("resourceOffline", false).isNotNull("resourceLocalAddress").findAll() + scope.launch { + val examCount = RealmStepExam.getNoOfExam(mRealm, id) + fragmentCourseDetailBinding.noOfExams.text = context?.getString(R.string.number_placeholder, examCount) + } + val resources = mRealm.query(RealmMyLibrary::class, + "courseId == $0 AND resourceOffline == false AND resourceLocalAddress != null", id ?: "" + ).find() setResourceButton(resources, fragmentCourseDetailBinding.btnResources) - val downloadedResources: List = cRealm.where(RealmMyLibrary::class.java).equalTo("resourceOffline", true).equalTo("courseId", id).isNotNull("resourceLocalAddress").findAll() + val downloadedResources = mRealm.query(RealmMyLibrary::class, + "courseId == $0 AND resourceOffline == true AND resourceLocalAddress != null", + id ?: "" + ).find() setOpenResourceButton(downloadedResources, fragmentCourseDetailBinding.btnOpen) onRatingChanged() setStepsList() @@ -76,13 +81,13 @@ class CourseDetailFragment : BaseContainerFragment(), OnRatingChangeListener { } private fun setStepsList() { - val steps = getCourseSteps(cRealm, courses?.courseId) + val steps = RealmMyCourse.getCourseSteps(cRealm, courses?.courseId) fragmentCourseDetailBinding.stepsList.layoutManager = LinearLayoutManager(activity) fragmentCourseDetailBinding.stepsList.adapter = AdapterSteps(requireActivity(), steps, cRealm) } override fun onRatingChanged() { - val `object` = getRatingsById(cRealm, "course", courses?.courseId, user?.id) + val `object` = RealmRating.getRatingsById(cRealm, "course", courses?.courseId, user?.id) setRatings(`object`) }