Skip to content

Commit

Permalink
migrate MyProgressFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 committed Jan 2, 2025
1 parent 7888348 commit 5e80e93
Showing 1 changed file with 53 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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>(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<String> = exams.map { it.id as String }

if (submissions != null) {
course.courseId.let { courseId ->
val submissions = realm.query<RealmSubmission>(RealmSubmission::class,
"userId == $0 AND parentId CONTAINS $1 AND type == $2", userId, courseId, "exam"
).find()

val exams = realm.query<RealmStepExam>(RealmStepExam::class, "courseId == $0", courseId).find()
val examIds = exams.map { it.id as String }

submissionMap(submissions, realm, examIds, obj)
}
arr.add(obj)
Expand All @@ -80,55 +72,42 @@ class MyProgressFragment : Fragment() {

private fun submissionMap(submissions: RealmResults<RealmSubmission>, realm: Realm, examIds: List<String>, obj: JsonObject) {
var totalMistakes = 0
submissions.forEach {
val answers = realm.where(RealmAnswer::class.java)
.equalTo("submissionId", it.id)
.findAll()
val mistakesMap = HashMap<String, Int>()
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<String, Int>()

submissions.forEach { submission ->
val answers = realm.query<RealmAnswer>(RealmAnswer::class, "submissionId == $0", submission.id).find()

answers.forEach { answer ->
realm.query<RealmExamQuestion>(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>(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>(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
Expand Down

0 comments on commit 5e80e93

Please sign in to comment.