Skip to content

Commit

Permalink
migrate courseDetailFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 committed Dec 28, 2024
1 parent f061879 commit 6ff90cd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class RealmMyCourse : RealmObject {
settings.edit().putString("concatenated_links", gson.toJson(existingConcatenatedLinks)).apply()
}

fun getCourseSteps(realm: Realm, courseId: String): List<RealmCourseStep> {
fun getCourseSteps(realm: Realm, courseId: String?): List<RealmCourseStep> {
return realm.query<RealmMyCourse>("id == $0", courseId).first().find()?.courseSteps?.toList()
?: emptyList()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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) {
Expand All @@ -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>(RealmMyCourse::class, "courseId == $0", id ?: "")
.first()
.find()
user = UserProfileDbHandler(requireContext()).userModel
return fragmentCourseDetailBinding.root
}
Expand All @@ -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<RealmMyLibrary> = 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>(RealmMyLibrary::class,
"courseId == $0 AND resourceOffline == false AND resourceLocalAddress != null", id ?: ""
).find()
setResourceButton(resources, fragmentCourseDetailBinding.btnResources)
val downloadedResources: List<RealmMyLibrary> = cRealm.where(RealmMyLibrary::class.java).equalTo("resourceOffline", true).equalTo("courseId", id).isNotNull("resourceLocalAddress").findAll()
val downloadedResources = mRealm.query<RealmMyLibrary>(RealmMyLibrary::class,
"courseId == $0 AND resourceOffline == true AND resourceLocalAddress != null",
id ?: ""
).find()
setOpenResourceButton(downloadedResources, fragmentCourseDetailBinding.btnOpen)
onRatingChanged()
setStepsList()
Expand All @@ -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`)
}

Expand Down

0 comments on commit 6ff90cd

Please sign in to comment.