-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…exchange#2772) (open-learning-exchange#2777) Co-authored-by: dogi <[email protected]>
- Loading branch information
Showing
4 changed files
with
89 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 0 additions & 142 deletions
142
app/src/main/java/org/ole/planet/myplanet/model/RealmAnswer.java
This file was deleted.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
app/src/main/java/org/ole/planet/myplanet/model/RealmAnswer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters