From 2267671e8b372c6bd9caa929bfa06f8600293e93 Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Wed, 6 Dec 2023 22:00:17 +0300 Subject: [PATCH] actions: refactor `RealmCourseProgress` model to kotlin (fixes #2773) (#2778) Co-authored-by: dogi --- app/build.gradle | 4 +- .../myplanet/model/RealmCourseProgress.java | 201 ------------------ .../myplanet/model/RealmCourseProgress.kt | 126 +++++++++++ .../myplanet/service/UploadManager.java | 2 +- .../ui/course/CourseStepFragment.java | 16 +- .../myplanet/ui/exam/BaseExamFragment.java | 2 +- 6 files changed, 138 insertions(+), 213 deletions(-) delete mode 100644 app/src/main/java/org/ole/planet/myplanet/model/RealmCourseProgress.java create mode 100644 app/src/main/java/org/ole/planet/myplanet/model/RealmCourseProgress.kt diff --git a/app/build.gradle b/app/build.gradle index 5066f6e78d..5d763847e7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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 diff --git a/app/src/main/java/org/ole/planet/myplanet/model/RealmCourseProgress.java b/app/src/main/java/org/ole/planet/myplanet/model/RealmCourseProgress.java deleted file mode 100644 index c8a1fe8857..0000000000 --- a/app/src/main/java/org/ole/planet/myplanet/model/RealmCourseProgress.java +++ /dev/null @@ -1,201 +0,0 @@ -package org.ole.planet.myplanet.model; - -import com.google.gson.Gson; -import com.google.gson.JsonObject; - -import org.ole.planet.myplanet.utilities.JsonUtils; -import org.ole.planet.myplanet.utilities.Utilities; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - -import io.realm.Realm; -import io.realm.RealmObject; -import io.realm.RealmResults; -import io.realm.Sort; -import io.realm.annotations.PrimaryKey; - -public class RealmCourseProgress extends RealmObject { - @PrimaryKey - private String id; - private String _id; - private String createdOn; - private long createdDate; - private long updatedDate; - - private String _rev; - - private int stepNum; - - private boolean passed; - - private String userId; - - private String courseId; - - private String parentCode; - - public static JsonObject serializeProgress(RealmCourseProgress progress) { - JsonObject object = new JsonObject(); - object.addProperty("userId", progress.getUserId()); - object.addProperty("parentCode", progress.getParentCode()); - object.addProperty("courseId", progress.getCourseId()); - object.addProperty("passed", progress.getPassed()); - object.addProperty("stepNum", progress.getStepNum()); - object.addProperty("createdOn", progress.getCreatedOn()); - object.addProperty("createdDate", progress.getCreatedDate()); - object.addProperty("updatedDate", progress.getUpdatedDate()); - return object; - } - - public String getCreatedOn() { - return createdOn; - } - - public static HashMap getCourseProgress(Realm mRealm, String userId) { - List r = RealmMyCourse.getMyCourseByUserId(userId, mRealm.where(RealmMyCourse.class).findAll()); - - HashMap map = new HashMap<>(); - for (RealmMyCourse course : r) { - JsonObject object = new JsonObject(); - List steps = RealmCourseStep.getSteps(mRealm, course.courseId); - object.addProperty("max", steps.size()); - - object.addProperty("current", getCurrentProgress(steps, mRealm, userId, course.courseId)); - if (RealmMyCourse.isMyCourse(userId, course.courseId, mRealm)) - map.put(course.courseId, object); - } - return map; - } - - public static List getPassedCourses(Realm mRealm, String userId) { - - RealmResults progresses = mRealm.where(RealmCourseProgress.class).equalTo("userId", userId).equalTo("passed", true).findAll(); - List list = new ArrayList<>(); - for (RealmCourseProgress progress : progresses) { - Utilities.log("Course id certified " + progress.getCourseId()); - RealmSubmission sub = mRealm.where(RealmSubmission.class).contains("parentId", progress.getCourseId()).equalTo("userId", userId).sort("lastUpdateTime", Sort.DESCENDING).findFirst(); - if (sub != null) list.add(sub); - } - return list; - } - - public static int getCurrentProgress(List steps, Realm mRealm, String userId, String courseId) { - int i; - for (i = 0; i < steps.size(); i++) { - RealmCourseProgress progress = mRealm.where(RealmCourseProgress.class).equalTo("stepNum", i + 1).equalTo("userId", userId).equalTo("courseId", courseId).findFirst(); - if (progress == null) { - break; - } - } - return i; - } - - public String get_rev() { - return _rev; - } - - public void set_rev(String _rev) { - this._rev = _rev; - } - - public int getStepNum() { - return stepNum; - } - - public void setStepNum(int stepNum) { - this.stepNum = stepNum; - } - - public boolean getPassed() { - return passed; - } - - public void setPassed(boolean passed) { - this.passed = passed; - } - - public String get_id() { - return _id; - } - - public void set_id(String _id) { - this._id = _id; - } - - public String getUserId() { - return userId; - } - - public void setUserId(String userId) { - this.userId = userId; - } - - public String getCourseId() { - return courseId; - } - - public void setCourseId(String courseId) { - this.courseId = courseId; - } - - public String getParentCode() { - return parentCode; - } - - public void setParentCode(String parentCode) { - this.parentCode = parentCode; - } - - public static void insert(Realm mRealm, JsonObject act) { - if (!mRealm.isInTransaction()) mRealm.beginTransaction(); - Utilities.log("insert course progresss " + new Gson().toJson(act)); - RealmCourseProgress courseProgress = mRealm.where(RealmCourseProgress.class).equalTo("_id", JsonUtils.getString("_id", act)).findFirst(); - if (courseProgress == null) - courseProgress = mRealm.createObject(RealmCourseProgress.class, JsonUtils.getString("_id", act)); - courseProgress.set_rev(JsonUtils.getString("_rev", act)); - courseProgress.set_id(JsonUtils.getString("_id", act)); - courseProgress.setPassed(JsonUtils.getBoolean("passed", act)); - courseProgress.setStepNum(JsonUtils.getInt("stepNum", act)); - courseProgress.setUserId(JsonUtils.getString("userId", act)); - courseProgress.setParentCode(JsonUtils.getString("parentCode", act)); - courseProgress.setCourseId(JsonUtils.getString("courseId", act)); - courseProgress.setCreatedOn(JsonUtils.getString("createdOn", act)); - courseProgress.setCreatedDate(JsonUtils.getLong("createdDate", act)); - courseProgress.setUpdatedDate(JsonUtils.getLong("updatedDate", act)); - mRealm.commitTransaction(); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public void setCreatedOn(String createdOn) { - this.createdOn = createdOn; - } - - public long getCreatedDate() { - return createdDate; - } - - public void setCreatedDate(long createdDate) { - this.createdDate = createdDate; - } - - public long getUpdatedDate() { - return updatedDate; - } - - public void setUpdatedDate(long updatedDate) { - this.updatedDate = updatedDate; - } - - public boolean isPassed() { - return passed; - } -} diff --git a/app/src/main/java/org/ole/planet/myplanet/model/RealmCourseProgress.kt b/app/src/main/java/org/ole/planet/myplanet/model/RealmCourseProgress.kt new file mode 100644 index 0000000000..9d6447155e --- /dev/null +++ b/app/src/main/java/org/ole/planet/myplanet/model/RealmCourseProgress.kt @@ -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 { + val r = getMyCourseByUserId(userId, mRealm.where(RealmMyCourse::class.java).findAll()) + val map = HashMap() + 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 { + val progresses = mRealm.where(RealmCourseProgress::class.java).equalTo("userId", userId).equalTo("passed", true).findAll() + val list: MutableList = 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, 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() + } + } +} diff --git a/app/src/main/java/org/ole/planet/myplanet/service/UploadManager.java b/app/src/main/java/org/ole/planet/myplanet/service/UploadManager.java index 3169b2ab4d..a599788b5f 100644 --- a/app/src/main/java/org/ole/planet/myplanet/service/UploadManager.java +++ b/app/src/main/java/org/ole/planet/myplanet/service/UploadManager.java @@ -211,7 +211,7 @@ public void uploadCourseProgress() { List 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)); diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/course/CourseStepFragment.java b/app/src/main/java/org/ole/planet/myplanet/ui/course/CourseStepFragment.java index 25979b6b65..a179cfdc90 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/course/CourseStepFragment.java +++ b/app/src/main/java/org/ole/planet/myplanet/ui/course/CourseStepFragment.java @@ -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(); } diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/exam/BaseExamFragment.java b/app/src/main/java/org/ole/planet/myplanet/ui/exam/BaseExamFragment.java index 6c4187fe37..f808353611 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/exam/BaseExamFragment.java +++ b/app/src/main/java/org/ole/planet/myplanet/ui/exam/BaseExamFragment.java @@ -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(); } }