From 5e80e93c54a94622ff7c3d0f26852e66cdb40842 Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Thu, 2 Jan 2025 18:40:19 +0300 Subject: [PATCH] migrate MyProgressFragment --- .../myplanet/ui/courses/MyProgressFragment.kt | 127 ++++++++---------- 1 file changed, 53 insertions(+), 74 deletions(-) diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/courses/MyProgressFragment.kt b/app/src/main/java/org/ole/planet/myplanet/ui/courses/MyProgressFragment.kt index 96f8b984f8..bd05c95838 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/courses/MyProgressFragment.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/courses/MyProgressFragment.kt @@ -1,29 +1,23 @@ 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 androidx.fragment.app.Fragment import androidx.recyclerview.widget.LinearLayoutManager -import com.google.gson.Gson -import com.google.gson.JsonArray -import com.google.gson.JsonObject -import io.realm.Realm -import io.realm.RealmResults +import com.google.gson.* +import io.realm.kotlin.Realm +import io.realm.kotlin.query.RealmResults +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.launch +import org.ole.planet.myplanet.MainApplication import org.ole.planet.myplanet.databinding.FragmentMyProgressBinding import org.ole.planet.myplanet.datamanager.DatabaseService -import org.ole.planet.myplanet.model.RealmAnswer -import org.ole.planet.myplanet.model.RealmCourseProgress -import org.ole.planet.myplanet.model.RealmExamQuestion -import org.ole.planet.myplanet.model.RealmMyCourse -import org.ole.planet.myplanet.model.RealmStepExam -import org.ole.planet.myplanet.model.RealmSubmission -import org.ole.planet.myplanet.model.RealmUserModel +import org.ole.planet.myplanet.model.* import org.ole.planet.myplanet.service.UserProfileDbHandler class MyProgressFragment : Fragment() { private lateinit var fragmentMyProgressBinding: FragmentMyProgressBinding + private val scope = MainApplication.applicationScope override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { fragmentMyProgressBinding = FragmentMyProgressBinding.inflate(inflater, container, false) @@ -38,39 +32,37 @@ class MyProgressFragment : Fragment() { private fun initializeData() { val realm = DatabaseService().realmInstance val user = UserProfileDbHandler(requireActivity()).userModel - val courseData = fetchCourseData(realm, user?.id) - fragmentMyProgressBinding.rvMyprogress.layoutManager = LinearLayoutManager(requireActivity()) - fragmentMyProgressBinding.rvMyprogress.adapter = AdapterMyProgress(requireActivity(), courseData) + scope.launch { + val courseData = fetchCourseData(realm, user?.id ?: "") + fragmentMyProgressBinding.rvMyprogress.apply { + layoutManager = LinearLayoutManager(requireActivity()) + adapter = AdapterMyProgress(requireActivity(), courseData) + } + } } companion object { - fun fetchCourseData(realm: Realm, userId: String?): JsonArray { - val mycourses = RealmMyCourse.getMyCourseByUserId( - userId, - realm.where(RealmMyCourse::class.java).findAll() - ) + suspend fun fetchCourseData(realm: Realm, userId: String): JsonArray { + val mycourses = realm.query(RealmMyCourse::class).find().let { RealmMyCourse.getMyCourseByUserId(userId, it) } + val arr = JsonArray() - val courseProgress = RealmCourseProgress.getCourseProgress(realm, userId) + val courseProgress = RealmCourseProgress.getCourseProgress(realm, userId).first() mycourses.forEach { course -> - val obj = JsonObject() - obj.addProperty("courseName", course.courseTitle) - obj.addProperty("courseId", course.courseId) - obj.add("progress", courseProgress[course.id]) - - val submissions = course.courseId?.let { courseId -> - realm.where(RealmSubmission::class.java) - .equalTo("userId", userId) - .contains("parentId", courseId) - .equalTo("type", "exam") - .findAll() + val obj = JsonObject().apply { + addProperty("courseName", course.courseTitle) + addProperty("courseId", course.courseId) + add("progress", courseProgress[course.id]) } - val exams = realm.where(RealmStepExam::class.java) - .equalTo("courseId", course.courseId) - .findAll() - val examIds: List = exams.map { it.id as String } - if (submissions != null) { + course.courseId.let { courseId -> + val submissions = realm.query(RealmSubmission::class, + "userId == $0 AND parentId CONTAINS $1 AND type == $2", userId, courseId, "exam" + ).find() + + val exams = realm.query(RealmStepExam::class, "courseId == $0", courseId).find() + val examIds = exams.map { it.id as String } + submissionMap(submissions, realm, examIds, obj) } arr.add(obj) @@ -80,55 +72,42 @@ class MyProgressFragment : Fragment() { private fun submissionMap(submissions: RealmResults, realm: Realm, examIds: List, obj: JsonObject) { var totalMistakes = 0 - submissions.forEach { - val answers = realm.where(RealmAnswer::class.java) - .equalTo("submissionId", it.id) - .findAll() - val mistakesMap = HashMap() - answers.forEach { r -> - val question = realm.where(RealmExamQuestion::class.java) - .equalTo("id", r.questionId) - .findFirst() - if (examIds.contains(question?.examId)) { - totalMistakes += r.mistakes - if (mistakesMap.containsKey(question?.examId)) { - mistakesMap["${examIds.indexOf(question?.examId)}"] = mistakesMap[question?.examId]!!.plus(r.mistakes) - } else { - mistakesMap["${examIds.indexOf(question?.examId)}"] = r.mistakes + val mistakesMap = HashMap() + + submissions.forEach { submission -> + val answers = realm.query(RealmAnswer::class, "submissionId == $0", submission.id).find() + + answers.forEach { answer -> + realm.query(RealmExamQuestion::class, "id == $0", answer.questionId) + .first().find()?.let { question -> + if (examIds.contains(question.examId)) { + totalMistakes += answer.mistakes + val examIndex = examIds.indexOf(question.examId).toString() + mistakesMap[examIndex] = mistakesMap.getOrDefault(examIndex, 0) + answer.mistakes + } } - } } - obj.add("stepMistake", Gson().fromJson(Gson().toJson(mistakesMap), JsonObject::class.java)) - obj.addProperty("mistakes", totalMistakes) } - } - fun getCourseProgress(courseData: JsonArray, courseId: String): JsonObject? { - courseData.forEach { element -> - val course = element.asJsonObject - if (course.get("courseId").asString == courseId) { - return course.getAsJsonObject("progress") - } + obj.apply { + add("stepMistake", Gson().fromJson(Gson().toJson(mistakesMap), JsonObject::class.java)) + addProperty("mistakes", totalMistakes) } - return null } fun countUsersWhoCompletedCourse(realm: Realm, courseId: String): Int { var completedCount = 0 - val allUsers = realm.where(RealmUserModel::class.java).findAll() + val allUsers = realm.query(RealmUserModel::class).find() allUsers.forEach { user -> val userId = user.id - val courses = RealmMyCourse.getMyCourseByUserId(userId, realm.where(RealmMyCourse::class.java).findAll()) + val courses = realm.query(RealmMyCourse::class).find() + .let { RealmMyCourse.getMyCourseByUserId(userId, it) } - val course = courses.find { it.courseId == courseId } - if (course != null) { + courses.find { it.courseId == courseId }?.let { val steps = RealmMyCourse.getCourseSteps(realm, courseId) val currentProgress = RealmCourseProgress.getCurrentProgress(steps, realm, userId, courseId) - - if (currentProgress == steps.size) { - completedCount++ - } + if (currentProgress == steps.size) completedCount++ } } return completedCount