From 657edb86459d54e1a96b401fc12d24ab86fd2ec9 Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Wed, 20 Dec 2023 06:45:36 +0300 Subject: [PATCH] actions: refactor `RealmAnswer` model to kotlin (fixes #2772) (#2777) Co-authored-by: dogi --- app/build.gradle | 4 +- .../planet/myplanet/model/RealmAnswer.java | 142 ------------------ .../ole/planet/myplanet/model/RealmAnswer.kt | 76 ++++++++++ .../myplanet/ui/exam/TakeExamFragment.java | 22 +-- 4 files changed, 89 insertions(+), 155 deletions(-) delete mode 100644 app/src/main/java/org/ole/planet/myplanet/model/RealmAnswer.java create mode 100644 app/src/main/java/org/ole/planet/myplanet/model/RealmAnswer.kt diff --git a/app/build.gradle b/app/build.gradle index 12c1f2d719..c99f9eeed3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,8 +9,8 @@ android { applicationId "org.ole.planet.myplanet" minSdkVersion 21 targetSdkVersion 34 - versionCode 1203 - versionName "0.12.3" + versionCode 1204 + versionName "0.12.4" ndkVersion '21.3.6528147' testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true diff --git a/app/src/main/java/org/ole/planet/myplanet/model/RealmAnswer.java b/app/src/main/java/org/ole/planet/myplanet/model/RealmAnswer.java deleted file mode 100644 index ece12f0a61..0000000000 --- a/app/src/main/java/org/ole/planet/myplanet/model/RealmAnswer.java +++ /dev/null @@ -1,142 +0,0 @@ -package org.ole.planet.myplanet.model; - -import android.text.TextUtils; - -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; - -import org.ole.planet.myplanet.utilities.Utilities; - -import java.util.HashMap; - -import io.realm.RealmList; -import io.realm.RealmObject; -import io.realm.annotations.PrimaryKey; - -public class RealmAnswer extends RealmObject { - @PrimaryKey - private String id; - private String value; - private RealmList valueChoices; - private int mistakes = 0; - private boolean passed; - private int grade; - private String examId; - private String questionId; - private String submissionId; - - public static JsonArray serializeRealmAnswer(RealmList answers) { - Utilities.log("Ans size " + answers.size()); - JsonArray array = new JsonArray(); - for (RealmAnswer ans : answers) { - array.add(createObject(ans)); - } - return array; - } - - private static JsonObject createObject(RealmAnswer ans) { - JsonObject object = new JsonObject(); - if (!TextUtils.isEmpty(ans.getValue())) { - object.addProperty("value", ans.getValue()); - } else { - object.add("value", ans.getValueChoicesArray()); - } - object.addProperty("mistakes", ans.getMistakes()); - object.addProperty("passed", ans.isPassed()); - return object; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public JsonArray getValueChoicesArray() { - JsonArray array = new JsonArray(); - if (valueChoices == null) { - return array; - } - for (String choice : valueChoices) { - array.add(new Gson().fromJson(choice, JsonObject.class)); - } - return array; - } - - public RealmList getValueChoices() { - return valueChoices; - } - - public void setValueChoices(HashMap map, boolean isLastAnsvalid) { - if (!isLastAnsvalid) this.valueChoices.clear(); - for (String key : map.keySet()) { - JsonObject ob = new JsonObject(); - ob.addProperty("id", map.get(key)); - ob.addProperty("text", key); - this.valueChoices.add(new Gson().toJson(ob)); - } - } - - public void setValueChoices(RealmList valueChoices) { - this.valueChoices = valueChoices; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } - - public int getMistakes() { - return mistakes; - } - - public void setMistakes(int mistakes) { - this.mistakes = mistakes; - } - - public boolean isPassed() { - return passed; - } - - public void setPassed(boolean passed) { - this.passed = passed; - } - - public int getGrade() { - return grade; - } - - public void setGrade(int grade) { - this.grade = grade; - } - - public String getExamId() { - return examId; - } - - public void setExamId(String examId) { - this.examId = examId; - } - - public String getQuestionId() { - return questionId; - } - - public void setQuestionId(String questionId) { - this.questionId = questionId; - } - - public String getSubmissionId() { - return submissionId; - } - - public void setSubmissionId(String submissionId) { - this.submissionId = submissionId; - } -} diff --git a/app/src/main/java/org/ole/planet/myplanet/model/RealmAnswer.kt b/app/src/main/java/org/ole/planet/myplanet/model/RealmAnswer.kt new file mode 100644 index 0000000000..649401c455 --- /dev/null +++ b/app/src/main/java/org/ole/planet/myplanet/model/RealmAnswer.kt @@ -0,0 +1,76 @@ +package org.ole.planet.myplanet.model + +import android.text.TextUtils +import com.google.gson.Gson +import com.google.gson.JsonArray +import com.google.gson.JsonObject +import io.realm.RealmList +import io.realm.RealmObject +import io.realm.annotations.PrimaryKey +import org.ole.planet.myplanet.utilities.Utilities + +open class RealmAnswer : RealmObject() { + @PrimaryKey + var id: String? = null + @JvmField + var value: String? = null + @JvmField + var valueChoices: RealmList? = null + @JvmField + var mistakes = 0 + @JvmField + var isPassed = false + @JvmField + var grade = 0 + @JvmField + var examId: String? = null + @JvmField + var questionId: String? = null + @JvmField + var submissionId: String? = null + val valueChoicesArray: JsonArray + get() { + val array = JsonArray() + if (valueChoices == null) { + return array + } + for (choice in valueChoices!!) { + array.add(Gson().fromJson(choice, JsonObject::class.java)) + } + return array + } + + fun setValueChoices(map: HashMap, isLastAnsvalid: Boolean) { + if (!isLastAnsvalid) valueChoices!!.clear() + for (key in map.keys) { + val ob = JsonObject() + ob.addProperty("id", map[key]) + ob.addProperty("text", key) + valueChoices!!.add(Gson().toJson(ob)) + } + } + + companion object { + @JvmStatic + fun serializeRealmAnswer(answers: RealmList): JsonArray { + Utilities.log("Ans size " + answers.size) + val array = JsonArray() + for (ans in answers) { + array.add(createObject(ans)) + } + return array + } + + private fun createObject(ans: RealmAnswer): JsonObject { + val `object` = JsonObject() + if (!TextUtils.isEmpty(ans.value)) { + `object`.addProperty("value", ans.value) + } else { + `object`.add("value", ans.valueChoicesArray) + } + `object`.addProperty("mistakes", ans.mistakes) + `object`.addProperty("passed", ans.isPassed) + return `object` + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/exam/TakeExamFragment.java b/app/src/main/java/org/ole/planet/myplanet/ui/exam/TakeExamFragment.java index 8aebcfed19..a065a54661 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/exam/TakeExamFragment.java +++ b/app/src/main/java/org/ole/planet/myplanet/ui/exam/TakeExamFragment.java @@ -114,7 +114,7 @@ public void startExam(RealmExamQuestion question) { fragmentTakeExamBinding.llCheckbox.setVisibility(View.GONE); clearAnswer(); if (sub.getAnswers().size() > currentIndex) { - ans = sub.getAnswers().get(currentIndex).getValue(); + ans = sub.getAnswers().get(currentIndex).value; } if (question.getType().equalsIgnoreCase("select")) { fragmentTakeExamBinding.groupChoices.setVisibility(View.VISIBLE); @@ -218,15 +218,15 @@ private boolean updateAnsDb() { RealmList list = sub.getAnswers(); RealmAnswer answer = createAnswer(list); RealmExamQuestion que = mRealm.copyFromRealm(questions.get(currentIndex)); - answer.setQuestionId(que.getId()); - answer.setValue(ans); + answer.questionId = que.getId(); + answer.value = ans; answer.setValueChoices(listAns, isLastAnsvalid); - answer.setSubmissionId(sub.getId()); - Submit_id = answer.getSubmissionId(); + answer.submissionId = sub.getId(); + Submit_id = answer.submissionId; if (que.getCorrectChoice().size() == 0) { - answer.setGrade(0); - answer.setMistakes(0); + answer.grade = 0; + answer.mistakes = 0; flag = true; } else { flag = checkCorrectAns(answer, que); @@ -247,9 +247,9 @@ else if (list.size() > currentIndex && !isLastAnsvalid) { private boolean checkCorrectAns(RealmAnswer answer, RealmExamQuestion que) { boolean flag = false; - answer.setPassed(que.getCorrectChoice().contains(ans.toLowerCase())); - answer.setGrade(1); - int mistake = answer.getMistakes(); + answer.isPassed = que.getCorrectChoice().contains(ans.toLowerCase()); + answer.grade = 1; + int mistake = answer.mistakes; String[] selectedAns = listAns.values().toArray(new String[0]); String[] correctChoices = que.getCorrectChoice().toArray(new String[0]); if (!isEqual(selectedAns, correctChoices)) { @@ -257,7 +257,7 @@ private boolean checkCorrectAns(RealmAnswer answer, RealmExamQuestion que) { } else { flag = true; } - answer.setMistakes(mistake); + answer.mistakes = mistake; return flag; }