Skip to content

Commit

Permalink
actions: refactor RealmCourseProgress model to kotlin (fixes #2773) (
Browse files Browse the repository at this point in the history
…#2778)

Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Dec 6, 2023
1 parent bef16d4 commit 2267671
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 213 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 21
targetSdkVersion 34
versionCode 1194
versionName "0.11.94"
versionCode 1195
versionName "0.11.95"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down

This file was deleted.

126 changes: 126 additions & 0 deletions app/src/main/java/org/ole/planet/myplanet/model/RealmCourseProgress.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package org.ole.planet.myplanet.model

import com.google.gson.Gson
import com.google.gson.JsonObject
import io.realm.Realm
import io.realm.RealmObject
import io.realm.Sort
import io.realm.annotations.PrimaryKey
import org.ole.planet.myplanet.model.RealmMyCourse
import org.ole.planet.myplanet.model.RealmMyCourse.Companion.getMyCourseByUserId
import org.ole.planet.myplanet.model.RealmMyCourse.Companion.isMyCourse
import org.ole.planet.myplanet.utilities.JsonUtils
import org.ole.planet.myplanet.utilities.Utilities

open class RealmCourseProgress : RealmObject() {
@PrimaryKey
var id: String? = null
private var _id: String? = null
@JvmField
var createdOn: String? = null
@JvmField
var createdDate: Long = 0
@JvmField
var updatedDate: Long = 0
private var _rev: String? = null
@JvmField
var stepNum = 0
@JvmField
var passed = false
@JvmField
var userId: String? = null
@JvmField
var courseId: String? = null
@JvmField
var parentCode: String? = null
fun get_rev(): String? {
return _rev
}

fun set_rev(_rev: String?) {
this._rev = _rev
}

fun get_id(): String? {
return _id
}

fun set_id(_id: String?) {
this._id = _id
}

companion object {
@JvmStatic
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`
}

@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 = RealmCourseStep.getSteps(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`
}
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 = mRealm.where(RealmSubmission::class.java)
.contains("parentId", progress.courseId).equalTo("userId", userId)
.sort("lastUpdateTime", Sort.DESCENDING).findFirst()
if (sub != null) list.add(sub)
}
return list
}

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

fun insert(mRealm: Realm, act: JsonObject?) {
if (!mRealm.isInTransaction) mRealm.beginTransaction()
Utilities.log("insert course progresss " + Gson().toJson(act))
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!!.set_rev(JsonUtils.getString("_rev", act))
courseProgress.set_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()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public void uploadCourseProgress() {
List<RealmCourseProgress> data = realm.where(RealmCourseProgress.class).isNull("_id").findAll();
for (RealmCourseProgress sub : data) {
try {
if (sub.getUserId().startsWith("guest")) continue;
if (sub.userId.startsWith("guest")) continue;
JsonObject object = apiInterface.postDoc(Utilities.getHeader(), "application/json", Utilities.getUrl() + "/courses_progress", RealmCourseProgress.serializeProgress(sub)).execute().body();
if (object != null) {
sub.set_id(JsonUtils.getString("id", object));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,19 @@ public void saveCourseProgress() {
RealmCourseProgress courseProgress = mRealm.where(RealmCourseProgress.class).equalTo("courseId", step.courseId).equalTo("userId", user.getId()).equalTo("stepNum", stepNumber).findFirst();
if (courseProgress == null) {
courseProgress = mRealm.createObject(RealmCourseProgress.class, UUID.randomUUID().toString());
courseProgress.setCreatedDate(new Date().getTime());
courseProgress.createdDate = new Date().getTime();
}

courseProgress.setCourseId(step.courseId);
courseProgress.setStepNum(stepNumber);
courseProgress.courseId = step.courseId;
courseProgress.stepNum = stepNumber;

if (stepExams.size() == 0) {
courseProgress.setPassed(true);
courseProgress.passed = true;
}
courseProgress.setCreatedOn(user.getPlanetCode());
courseProgress.setUpdatedDate(new Date().getTime());
courseProgress.setParentCode(user.getParentCode());
courseProgress.setUserId(user.getId());
courseProgress.createdOn = user.getPlanetCode();
courseProgress.updatedDate = new Date().getTime();
courseProgress.parentCode = user.getParentCode();
courseProgress.userId = user.getId();
mRealm.commitTransaction();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private void saveCourseProgress() {
RealmCourseProgress progress = mRealm.where(RealmCourseProgress.class).equalTo("courseId", exam.getCourseId()).equalTo("stepNum", stepNumber).findFirst();
if (progress != null) {
if (!mRealm.isInTransaction()) mRealm.beginTransaction();
progress.setPassed(sub.getStatus().equals("graded"));
progress.passed = sub.getStatus().equals("graded");
mRealm.commitTransaction();
}
}
Expand Down

0 comments on commit 2267671

Please sign in to comment.