Skip to content

Commit

Permalink
migrate realmMyCourse and course progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 committed Dec 10, 2024
1 parent 443631a commit 3f4ff4a
Show file tree
Hide file tree
Showing 2 changed files with 559 additions and 285 deletions.
203 changes: 107 additions & 96 deletions app/src/main/java/org/ole/planet/myplanet/model/RealmCourseProgress.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@ package org.ole.planet.myplanet.model

import com.google.gson.JsonObject
import com.opencsv.CSVWriter
import io.realm.Realm
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import io.realm.kotlin.Realm
import io.realm.kotlin.RealmConfiguration
import io.realm.kotlin.ext.query
import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.query.Sort
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.PrimaryKey
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.toList
import org.ole.planet.myplanet.MainApplication.Companion.context
import org.ole.planet.myplanet.model.RealmMyCourse.Companion.getCourseSteps
import org.ole.planet.myplanet.model.RealmMyCourse.Companion.getMyCourseByUserId
Expand All @@ -14,124 +25,124 @@ import java.io.File
import java.io.FileWriter
import java.io.IOException

open class RealmCourseProgress : RealmObject() {
class RealmCourseProgress : RealmObject {
@PrimaryKey
var id: String? = null
var _id: String? = null
var createdOn: String? = null
var id: String = ""
var _id: String = ""
var createdOn: String = ""
var createdDate: Long = 0
var updatedDate: Long = 0
var _rev: String? = null
var stepNum = 0
var passed = false
var userId: String? = null
var courseId: String? = null
var parentCode: String? = null
var _rev: String = ""
var stepNum: Int = 0
var passed: Boolean = false
var userId: String = ""
var courseId: String = ""
var parentCode: String = ""

companion object {
val progressDataList: MutableList<Array<String>> = mutableListOf()
@JvmStatic
private val progressDataList = mutableListOf<Array<String>>()

fun serializeProgress(progress: RealmCourseProgress): JsonObject {
val `object` = JsonObject()
`object`.addProperty("userId", progress.userId)
`object`.addProperty("parentCode", progress.parentCode)
`object`.addProperty("courseId", progress.courseId)
`object`.addProperty("passed", progress.passed)
`object`.addProperty("stepNum", progress.stepNum)
`object`.addProperty("createdOn", progress.createdOn)
`object`.addProperty("createdDate", progress.createdDate)
`object`.addProperty("updatedDate", progress.updatedDate)
return `object`
return JsonObject().apply {
addProperty("userId", progress.userId)
addProperty("parentCode", progress.parentCode)
addProperty("courseId", progress.courseId)
addProperty("passed", progress.passed)
addProperty("stepNum", progress.stepNum)
addProperty("createdOn", progress.createdOn)
addProperty("createdDate", progress.createdDate)
addProperty("updatedDate", progress.updatedDate)
}
}

@JvmStatic
fun getCourseProgress(mRealm: Realm, userId: String?): HashMap<String?, JsonObject> {
val r = getMyCourseByUserId(userId, mRealm.where(RealmMyCourse::class.java).findAll())
val map = HashMap<String?, JsonObject>()
for (course in r) {
val `object` = JsonObject()
val steps = getCourseSteps(mRealm, course.courseId)
`object`.addProperty("max", steps.size)
`object`.addProperty("current", getCurrentProgress(steps, mRealm, userId, course.courseId))
if (isMyCourse(userId, course.courseId, mRealm)) map[course.courseId] = `object`
suspend fun getCourseProgress(realm: Realm, userId: String): Flow<Map<String, JsonObject>> {
return getMyCourseByUserId(userId).map { courses ->
courses.map { course ->
getCourseSteps(course.courseId).first().let { steps ->
if (isMyCourse(userId, course.courseId)) {
course.courseId to JsonObject().apply {
addProperty("max", steps.size)
addProperty("current", getCurrentProgress(steps, realm, userId, course.courseId))
}
} else null
}
}.filterNotNull().toMap()
}
return map
}

// @JvmStatic
// fun getPassedCourses(mRealm: Realm, userId: String?): List<RealmSubmission> {
// val progresses = mRealm.where(RealmCourseProgress::class.java).equalTo("userId", userId).equalTo("passed", true).findAll()
// val list: MutableList<RealmSubmission> = ArrayList()
// for (progress in progresses) {
// Utilities.log("Course id certified " + progress.courseId)
// val sub = progress.courseId?.let {
// mRealm.where(RealmSubmission::class.java)
// .contains("parentId", it).equalTo("userId", userId)
// .sort("lastUpdateTime", Sort.DESCENDING).findFirst()
// fun getPassedCourses(realm: Realm, userId: String): Flow<List<RealmSubmission>> {
// return realm.query<RealmCourseProgress>("userId == $0 AND passed == true", userId)
// .asFlow()
// .map { progresses ->
// progresses.list.mapNotNull { progress ->
// realm.query<RealmSubmission>("parentId CONTAINS $0 AND userId == $1",
// progress.courseId, userId).sort("lastUpdateTime", Sort.DESCENDING)
// .first().find()
// }
// }
// if (sub != null) list.add(sub)
// }
// return list
// }

@JvmStatic
fun getCurrentProgress(steps: List<RealmCourseStep?>?, mRealm: Realm, userId: String?, courseId: String?): Int {
var i = 0
while (i < (steps?.size ?: 0)) {
mRealm.where(RealmCourseProgress::class.java).equalTo("stepNum", i + 1).equalTo("userId", userId).equalTo("courseId", courseId)
.findFirst()
?: break
i++
fun getCurrentProgress(steps: List<RealmCourseStep>, realm: Realm, userId: String, courseId: String): Int {
var currentStep = 0
while (currentStep < steps.size) {
val progress = realm.query<RealmCourseProgress>(
"stepNum == $0 AND userId == $1 AND courseId == $2", currentStep + 1, userId, courseId
).first().find() ?: break
currentStep++
}
return i
return currentStep
}

@JvmStatic
fun insert(mRealm: Realm, act: JsonObject?) {
if (!mRealm.isInTransaction) {
mRealm.beginTransaction()
}
var courseProgress = mRealm.where(RealmCourseProgress::class.java).equalTo("_id", JsonUtils.getString("_id", act)).findFirst()
if (courseProgress == null) {
courseProgress = mRealm.createObject(RealmCourseProgress::class.java, JsonUtils.getString("_id", act))
}
courseProgress?._rev = JsonUtils.getString("_rev", act)
courseProgress?._id = JsonUtils.getString("_id", act)
courseProgress?.passed = JsonUtils.getBoolean("passed", act)
courseProgress?.stepNum = JsonUtils.getInt("stepNum", act)
courseProgress?.userId = JsonUtils.getString("userId", act)
courseProgress?.parentCode = JsonUtils.getString("parentCode", act)
courseProgress?.courseId = JsonUtils.getString("courseId", act)
courseProgress?.createdOn = JsonUtils.getString("createdOn", act)
courseProgress?.createdDate = JsonUtils.getLong("createdDate", act)
courseProgress?.updatedDate = JsonUtils.getLong("updatedDate", act)
mRealm.commitTransaction()
suspend fun insert(realm: Realm, act: JsonObject) {
realm.write {
val courseProgress = query<RealmCourseProgress>("_id == $0",
JsonUtils.getString("_id", act)).first().find()
?: RealmCourseProgress().apply {
id = JsonUtils.getString("_id", act)
}

copyToRealm(courseProgress.apply {
_rev = JsonUtils.getString("_rev", act)
_id = JsonUtils.getString("_id", act)
passed = JsonUtils.getBoolean("passed", act)
stepNum = JsonUtils.getInt("stepNum", act)
userId = JsonUtils.getString("userId", act)
parentCode = JsonUtils.getString("parentCode", act)
courseId = JsonUtils.getString("courseId", act)
createdOn = JsonUtils.getString("createdOn", act)
createdDate = JsonUtils.getLong("createdDate", act)
updatedDate = JsonUtils.getLong("updatedDate", act)
})

val csvRow = arrayOf(
JsonUtils.getString("_id", act),
JsonUtils.getString("_rev", act),
JsonUtils.getBoolean("passed", act).toString(),
JsonUtils.getInt("stepNum", act).toString(),
JsonUtils.getString("userId", act),
JsonUtils.getString("parentCode", act),
JsonUtils.getString("courseId", act),
JsonUtils.getString("createdOn", act),
JsonUtils.getLong("createdDate", act).toString(),
JsonUtils.getLong("updatedDate", act).toString()
)
progressDataList.add(csvRow)
val csvRow = arrayOf(
JsonUtils.getString("_id", act),
JsonUtils.getString("_rev", act),
JsonUtils.getBoolean("passed", act).toString(),
JsonUtils.getInt("stepNum", act).toString(),
JsonUtils.getString("userId", act),
JsonUtils.getString("parentCode", act),
JsonUtils.getString("courseId", act),
JsonUtils.getString("createdOn", act),
JsonUtils.getLong("createdDate", act).toString(),
JsonUtils.getLong("updatedDate", act).toString()
)
progressDataList.add(csvRow)
}
}

fun writeCsv(filePath: String, data: List<Array<String>>) {
try {
val file = File(filePath)
file.parentFile?.mkdirs()
val writer = CSVWriter(FileWriter(file))
writer.writeNext(arrayOf("progressId", "progress_rev", "passed", "stepNum", "userId", "parentCode", "courseId", "createdOn", "createdDate", "updatedDate"))
for (row in data) {
writer.writeNext(row)
CSVWriter(FileWriter(file)).use { writer ->
writer.writeNext(arrayOf(
"progressId", "progress_rev", "passed", "stepNum", "userId", "parentCode",
"courseId", "createdOn", "createdDate", "updatedDate"
))
data.forEach { row ->
writer.writeNext(row)
}
}
writer.close()
} catch (e: IOException) {
e.printStackTrace()
}
Expand Down
Loading

0 comments on commit 3f4ff4a

Please sign in to comment.