Skip to content

Commit

Permalink
actions: refactor RealmAnswer model to kotlin (fixes open-learning-…
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 and dogi authored Dec 20, 2023
1 parent 62b48dd commit 657edb8
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 155 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 1203
versionName "0.12.3"
versionCode 1204
versionName "0.12.4"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
142 changes: 0 additions & 142 deletions app/src/main/java/org/ole/planet/myplanet/model/RealmAnswer.java

This file was deleted.

76 changes: 76 additions & 0 deletions app/src/main/java/org/ole/planet/myplanet/model/RealmAnswer.kt
Original file line number Diff line number Diff line change
@@ -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<String>? = 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<String?, String?>, 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<RealmAnswer>): 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`
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -218,15 +218,15 @@ private boolean updateAnsDb() {
RealmList<RealmAnswer> 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);
Expand All @@ -247,17 +247,17 @@ 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)) {
mistake++;
} else {
flag = true;
}
answer.setMistakes(mistake);
answer.mistakes = mistake;
return flag;
}

Expand Down

0 comments on commit 657edb8

Please sign in to comment.