Skip to content

Commit

Permalink
actions: refactor RealmAchievement model to kotlin (fixes #2757) (#…
Browse files Browse the repository at this point in the history
…2761)

Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Dec 5, 2023
1 parent 0710d06 commit 0210469
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 189 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 1184
versionName "0.11.84"
versionCode 1185
versionName "0.11.85"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
174 changes: 0 additions & 174 deletions app/src/main/java/org/ole/planet/myplanet/model/RealmAchievement.java

This file was deleted.

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

import android.text.TextUtils
import android.widget.EditText
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import io.realm.Realm
import io.realm.RealmList
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import org.ole.planet.myplanet.utilities.JsonUtils
import org.ole.planet.myplanet.utilities.Utilities

open class RealmAchievement : RealmObject() {
@JvmField
var achievements: RealmList<String>? = null
@JvmField
var references: RealmList<String>? = null
@JvmField
var purpose: String? = null
@JvmField
var achievementsHeader: String? = null
@JvmField
var sendToNation: String? = null
private var _rev: String? = null

@PrimaryKey
private var _id: String? = null
@JvmField
var goals: 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
}

fun getreferences(): RealmList<String>? {
return references
}

val achievementsArray: JsonArray
get() {
val array = JsonArray()
for (s in achievements!!) {
val ob = Gson().fromJson(s, JsonElement::class.java)
array.add(ob)
}
return array
}

fun getreferencesArray(): JsonArray {
val array = JsonArray()
for (s in references!!) {
val ob = Gson().fromJson(s, JsonElement::class.java)
array.add(ob)
}
return array
}

fun setAchievements(ac: JsonArray) {
achievements = RealmList()
for (el in ac) {
val achi = Gson().toJson(el)
if (!achievements!!.contains(achi)) achievements!!.add(achi)
}
}

fun setreferences(of: JsonArray?) {
references = RealmList()
if (of == null) return
for (el in of) {
Utilities.log("Set references")
val e = Gson().toJson(el)
if (!references!!.contains(e)) references!!.add(e)
}
}

companion object {
@JvmStatic
fun serialize(sub: RealmAchievement): JsonObject {
val `object` = JsonObject()
`object`.addProperty("_id", sub.get_id())
if (!TextUtils.isEmpty(sub.get_rev())) `object`.addProperty("_rev", sub.get_rev())
`object`.addProperty("goals", sub.goals)
`object`.addProperty("purpose", sub.purpose)
`object`.addProperty("achievementsHeader", sub.achievementsHeader)
`object`.add("references", sub.getreferencesArray())
`object`.add("achievements", sub.achievementsArray)
Utilities.log(Gson().toJson(`object`))
return `object`
}

@JvmStatic
fun createReference(
name: String?,
relation: EditText,
phone: EditText,
email: EditText
): JsonObject {
val ob = JsonObject()
ob.addProperty("name", name)
ob.addProperty("phone", phone.text.toString())
ob.addProperty("relationship", relation.text.toString())
ob.addProperty("email", email.text.toString())
return ob
}

fun insert(mRealm: Realm, act: JsonObject?) {
var achievement = mRealm.where(RealmAchievement::class.java)
.equalTo("_id", JsonUtils.getString("_id", act)).findFirst()
if (achievement == null) achievement = mRealm.createObject(
RealmAchievement::class.java, JsonUtils.getString("_id", act)
)
achievement!!.set_rev(JsonUtils.getString("_rev", act))
achievement.purpose = JsonUtils.getString("purpose", act)
achievement.goals = JsonUtils.getString("goals", act)
achievement.achievementsHeader = JsonUtils.getString("achievementsHeader", act)
achievement.setreferences(JsonUtils.getJsonArray("references", act))
achievement.setAchievements(JsonUtils.getJsonArray("achievements", act))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
fragmentAchievementBinding.tvFirstName.setText(user.getFirstName());
fragmentAchievementBinding.tvName.setText(String.format("%s %s %s", user.getFirstName(), user.getMiddleName(), user.getLastName()));
if (achievement != null) {
fragmentAchievementBinding.tvGoals.setText(achievement.getGoals());
fragmentAchievementBinding.tvPurpose.setText(achievement.getPurpose());
fragmentAchievementBinding.tvAchievementHeader.setText(achievement.getAchievementsHeader());
fragmentAchievementBinding.tvGoals.setText(achievement.goals);
fragmentAchievementBinding.tvPurpose.setText(achievement.purpose);
fragmentAchievementBinding.tvAchievementHeader.setText(achievement.achievementsHeader);
fragmentAchievementBinding.llAchievement.removeAllViews();
for (String s : achievement.getAchievements()) {
for (String s : achievement.achievements) {
rowAchievementBinding = RowAchievementBinding.inflate(LayoutInflater.from(MainApplication.context));
JsonElement ob = new Gson().fromJson(s, JsonElement.class);
if (ob instanceof JsonObject) {
Expand Down Expand Up @@ -124,7 +124,7 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
}

private void createAchievementList() {
for (String s : achievement.getAchievements()) {
for (String s : achievement.achievements) {
rowAchievementBinding = RowAchievementBinding.inflate(LayoutInflater.from(MainApplication.context));
JsonElement ob = new Gson().fromJson(s, JsonElement.class);
if (ob instanceof JsonObject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ public void initializeData() {
} else {
achievementArray = achievement.getAchievementsArray();
referenceArray = achievement.getreferencesArray();
fragmentEditAchievementBinding.etAchievement.setText(achievement.getAchievementsHeader());
fragmentEditAchievementBinding.etPurpose.setText(achievement.getPurpose());
fragmentEditAchievementBinding.etGoals.setText(achievement.getGoals());
fragmentEditAchievementBinding.cbSendToNation.setChecked(Boolean.parseBoolean(achievement.getSendToNation()));
fragmentEditAchievementBinding.etAchievement.setText(achievement.achievementsHeader);
fragmentEditAchievementBinding.etPurpose.setText(achievement.purpose);
fragmentEditAchievementBinding.etGoals.setText(achievement.goals);
fragmentEditAchievementBinding.cbSendToNation.setChecked(Boolean.parseBoolean(achievement.sendToNation));
}
fragmentEditAchievementBinding.txtDob.setText(TextUtils.isEmpty(user.getDob()) ? getString(R.string.birth_date) : TimeUtils.getFormatedDate(user.getDob(), "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));
resourceArray = new JsonArray();
Expand Down Expand Up @@ -299,11 +299,11 @@ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup
public void setUserInfo() {}

public void setAchievementInfo() {
achievement.setAchievementsHeader(fragmentEditAchievementBinding.etAchievement.getText().toString().trim());
achievement.setGoals(fragmentEditAchievementBinding.etGoals.getText().toString().trim());
achievement.setPurpose(fragmentEditAchievementBinding.etPurpose.getText().toString().trim());
achievement.achievementsHeader = fragmentEditAchievementBinding.etAchievement.getText().toString().trim();
achievement.goals = fragmentEditAchievementBinding.etGoals.getText().toString().trim();
achievement.purpose = fragmentEditAchievementBinding.etPurpose.getText().toString().trim();
achievement.setAchievements(achievementArray);
achievement.setreferences(referenceArray);
achievement.setSendToNation(fragmentEditAchievementBinding.cbSendToNation.isChecked() + "");
achievement.sendToNation = fragmentEditAchievementBinding.cbSendToNation.isChecked() + "";
}
}

0 comments on commit 0210469

Please sign in to comment.