From 02104698ea41a2bfa3e8f9f15056b0cbd0d3f870 Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Tue, 5 Dec 2023 21:16:19 +0300 Subject: [PATCH] actions: refactor `RealmAchievement` model to kotlin (fixes #2757) (#2761) Co-authored-by: dogi --- app/build.gradle | 4 +- .../myplanet/model/RealmAchievement.java | 174 ------------------ .../planet/myplanet/model/RealmAchievement.kt | 134 ++++++++++++++ .../ui/userprofile/AchievementFragment.java | 10 +- .../userprofile/EditAchievementFragment.java | 16 +- 5 files changed, 149 insertions(+), 189 deletions(-) delete mode 100644 app/src/main/java/org/ole/planet/myplanet/model/RealmAchievement.java create mode 100644 app/src/main/java/org/ole/planet/myplanet/model/RealmAchievement.kt diff --git a/app/build.gradle b/app/build.gradle index db71d1f812..ab03663aa2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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 diff --git a/app/src/main/java/org/ole/planet/myplanet/model/RealmAchievement.java b/app/src/main/java/org/ole/planet/myplanet/model/RealmAchievement.java deleted file mode 100644 index 46bbcc89b5..0000000000 --- a/app/src/main/java/org/ole/planet/myplanet/model/RealmAchievement.java +++ /dev/null @@ -1,174 +0,0 @@ -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 org.ole.planet.myplanet.utilities.JsonUtils; -import org.ole.planet.myplanet.utilities.Utilities; - -import io.realm.Realm; -import io.realm.RealmList; -import io.realm.RealmObject; -import io.realm.annotations.PrimaryKey; - -public class RealmAchievement extends RealmObject { - private RealmList achievements; - - private RealmList references; - - private String purpose; - - private String achievementsHeader; - - private String sendToNation; - - private String _rev; - - @PrimaryKey - private String _id; - - private String goals; - - public static JsonObject serialize(RealmAchievement sub) { - JsonObject object = new JsonObject(); - object.addProperty("_id", sub.get_id()); - if (!TextUtils.isEmpty(sub.get_rev())) object.addProperty("_rev", sub.get_rev()); - object.addProperty("goals", sub.getGoals()); - object.addProperty("purpose", sub.getPurpose()); - object.addProperty("achievementsHeader", sub.getAchievementsHeader()); - object.add("references", sub.getreferencesArray()); - object.add("achievements", sub.getAchievementsArray()); - Utilities.log(new Gson().toJson(object)); - return object; - } - - public void setAchievements(RealmList achievements) { - this.achievements = achievements; - } - - public RealmList getReferences() { - return references; - } - - public void setReferences(RealmList references) { - this.references = references; - } - - public static JsonObject createReference(String name, EditText relation, EditText phone, EditText email) { - JsonObject ob = new JsonObject(); - ob.addProperty("name", name); - ob.addProperty("phone", phone.getText().toString()); - ob.addProperty("relationship", relation.getText().toString()); - ob.addProperty("email", email.getText().toString()); - return ob; - } - - public String getPurpose() { - return purpose; - } - - public void setPurpose(String purpose) { - this.purpose = purpose; - } - - public String getAchievementsHeader() { - return achievementsHeader; - } - - public void setAchievementsHeader(String achievementsHeader) { - this.achievementsHeader = achievementsHeader; - } - - public String getSendToNation() { - return sendToNation; - } - - public void setSendToNation(String sendToNation) { - this.sendToNation = sendToNation; - } - - public String get_rev() { - return _rev; - } - - public void set_rev(String _rev) { - this._rev = _rev; - } - - public String get_id() { - return _id; - } - - public void set_id(String _id) { - this._id = _id; - } - - public String getGoals() { - return goals; - } - - public void setGoals(String goals) { - this.goals = goals; - } - - public static void insert(Realm mRealm, JsonObject act) { - RealmAchievement achievement = mRealm.where(RealmAchievement.class).equalTo("_id", JsonUtils.getString("_id", act)).findFirst(); - if (achievement == null) - achievement = mRealm.createObject(RealmAchievement.class, JsonUtils.getString("_id", act)); - achievement.set_rev(JsonUtils.getString("_rev", act)); - achievement.setPurpose(JsonUtils.getString("purpose", act)); - achievement.setGoals(JsonUtils.getString("goals", act)); - achievement.setAchievementsHeader(JsonUtils.getString("achievementsHeader", act)); - achievement.setreferences(JsonUtils.getJsonArray("references", act)); - achievement.setAchievements(JsonUtils.getJsonArray("achievements", act)); - } - - public RealmList getreferences() { - return references; - } - - public RealmList getAchievements() { - return achievements; - } - - public JsonArray getAchievementsArray() { - JsonArray array = new JsonArray(); - for (String s : achievements) { - JsonElement ob = new Gson().fromJson(s, JsonElement.class); - array.add(ob); - } - return array; - } - - public JsonArray getreferencesArray() { - JsonArray array = new JsonArray(); - for (String s : references) { - JsonElement ob = new Gson().fromJson(s, JsonElement.class); - array.add(ob); - } - return array; - } - - public void setAchievements(JsonArray ac) { - achievements = new RealmList(); - for (JsonElement el : ac) { - String achi = new Gson().toJson(el); - if (!achievements.contains(achi)) achievements.add(achi); - } - } - - public void setreferences(JsonArray of) { - references = new RealmList(); - if (of == null) return; - for (JsonElement el : of) { - Utilities.log("Set references"); - String e = new Gson().toJson(el); - if (!references.contains(e)) references.add(e); - } - } -} \ No newline at end of file diff --git a/app/src/main/java/org/ole/planet/myplanet/model/RealmAchievement.kt b/app/src/main/java/org/ole/planet/myplanet/model/RealmAchievement.kt new file mode 100644 index 0000000000..6c9decd640 --- /dev/null +++ b/app/src/main/java/org/ole/planet/myplanet/model/RealmAchievement.kt @@ -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? = null + @JvmField + var references: RealmList? = 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? { + 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)) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/userprofile/AchievementFragment.java b/app/src/main/java/org/ole/planet/myplanet/ui/userprofile/AchievementFragment.java index e9ef509b57..71db6eff04 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/userprofile/AchievementFragment.java +++ b/app/src/main/java/org/ole/planet/myplanet/ui/userprofile/AchievementFragment.java @@ -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) { @@ -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) { diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/userprofile/EditAchievementFragment.java b/app/src/main/java/org/ole/planet/myplanet/ui/userprofile/EditAchievementFragment.java index b0d5166818..27c664cf51 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/userprofile/EditAchievementFragment.java +++ b/app/src/main/java/org/ole/planet/myplanet/ui/userprofile/EditAchievementFragment.java @@ -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(); @@ -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() + ""; } } \ No newline at end of file