From c80a6905c36388a91fdcb880213e6b6dbc33518b Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Wed, 20 Dec 2023 07:07:19 +0300 Subject: [PATCH] actions: refactor `RealmNews` model to kotlin (fixes #2809) (#2813) Co-authored-by: dogi --- app/build.gradle | 4 +- .../myplanet/base/BaseNewsFragment.java | 2 +- .../ole/planet/myplanet/model/RealmNews.java | 372 ------------------ .../ole/planet/myplanet/model/RealmNews.kt | 232 +++++++++++ .../myplanet/service/UploadManager.java | 10 +- .../planet/myplanet/ui/news/AdapterNews.java | 52 +-- .../myplanet/ui/news/NewsDetailActivity.kt | 19 +- .../planet/myplanet/ui/news/NewsFragment.java | 6 +- .../myplanet/ui/news/ReplyActivity.java | 2 +- .../DiscussionListFragment.java | 6 +- 10 files changed, 282 insertions(+), 423 deletions(-) delete mode 100644 app/src/main/java/org/ole/planet/myplanet/model/RealmNews.java create mode 100644 app/src/main/java/org/ole/planet/myplanet/model/RealmNews.kt diff --git a/app/build.gradle b/app/build.gradle index c0ff9ac2a..b61e58f61 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,8 +9,8 @@ android { applicationId "org.ole.planet.myplanet" minSdkVersion 21 targetSdkVersion 34 - versionCode 1205 - versionName "0.12.5" + versionCode 1206 + versionName "0.12.6" ndkVersion '21.3.6528147' testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true diff --git a/app/src/main/java/org/ole/planet/myplanet/base/BaseNewsFragment.java b/app/src/main/java/org/ole/planet/myplanet/base/BaseNewsFragment.java index 2e92af236..437d0783c 100644 --- a/app/src/main/java/org/ole/planet/myplanet/base/BaseNewsFragment.java +++ b/app/src/main/java/org/ole/planet/myplanet/base/BaseNewsFragment.java @@ -70,7 +70,7 @@ public void onDestroy() { @Override public void showReply(RealmNews news, boolean fromLogin) { - startActivity(new Intent(getActivity(), ReplyActivity.class).putExtra("id", news.getId()).putExtra("fromLogin", fromLogin)); + startActivity(new Intent(getActivity(), ReplyActivity.class).putExtra("id", news.id).putExtra("fromLogin", fromLogin)); } public abstract void setData(List list); diff --git a/app/src/main/java/org/ole/planet/myplanet/model/RealmNews.java b/app/src/main/java/org/ole/planet/myplanet/model/RealmNews.java deleted file mode 100644 index e89257686..000000000 --- a/app/src/main/java/org/ole/planet/myplanet/model/RealmNews.java +++ /dev/null @@ -1,372 +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.JsonElement; -import com.google.gson.JsonObject; - -import org.ole.planet.myplanet.utilities.JsonUtils; -import org.ole.planet.myplanet.utilities.Utilities; - -import java.util.Date; -import java.util.HashMap; -import java.util.UUID; - -import io.realm.Realm; -import io.realm.RealmList; -import io.realm.RealmObject; -import io.realm.annotations.PrimaryKey; - -public class RealmNews extends RealmObject { - @PrimaryKey - private String id; - private String _id; - private String _rev; - private String userId; - private String user; - private String message; - private String docType; - private String viewableBy; - private String viewableId; - private String avatar; - private String replyTo; - private String userName; - private String messagePlanetCode; - private String messageType; - private long updatedDate; - private long time; - private String createdOn; - private String parentCode; - private RealmList imageUrls; - private String images; - private RealmList labels; - private String viewIn; - - public static void insert(Realm mRealm, JsonObject doc) { - Utilities.log("sync nnews " + new Gson().toJson(doc)); - RealmNews news = mRealm.where(RealmNews.class).equalTo("_id", JsonUtils.getString("_id", doc)).findFirst(); - if (news == null) { - news = mRealm.createObject(RealmNews.class, JsonUtils.getString("_id", doc)); - } - news.set_rev(JsonUtils.getString("_rev", doc)); - news.set_id(JsonUtils.getString("_id", doc)); - news.setViewableBy(JsonUtils.getString("viewableBy", doc)); - news.setDocType(JsonUtils.getString("docType", doc)); - news.setAvatar(JsonUtils.getString("avatar", doc)); - news.setUpdatedDate(JsonUtils.getLong("updatedDate", doc)); - news.setViewableId(JsonUtils.getString("viewableId", doc)); - news.setCreatedOn(JsonUtils.getString("createdOn", doc)); - news.setMessageType(JsonUtils.getString("messageType", doc)); - news.setMessagePlanetCode(JsonUtils.getString("messagePlanetCode", doc)); - news.setReplyTo(JsonUtils.getString("replyTo", doc)); - news.setParentCode(JsonUtils.getString("parentCode", doc)); - JsonObject user = JsonUtils.getJsonObject("user", doc); - news.setUser(new Gson().toJson(JsonUtils.getJsonObject("user", doc))); - news.setUserId(JsonUtils.getString("_id", user)); - news.setUserName(JsonUtils.getString("name", user)); - news.setTime(JsonUtils.getLong("time", doc)); - JsonArray images = JsonUtils.getJsonArray("images", doc); - String message = JsonUtils.getString("message", doc); - news.setMessage(message); - news.setImages(new Gson().toJson(images)); - JsonArray labels = JsonUtils.getJsonArray("labels", doc); - news.setViewIn(new Gson().toJson(JsonUtils.getJsonArray("viewIn", doc))); - news.setLabels(labels); - } - - public String getImages() { - return images; - } - - public JsonArray getImagesArray() { - return images == null ? new JsonArray() : new Gson().fromJson(images, JsonArray.class); - } - - public void setImages(String images) { - this.images = images; - - } - - public String getViewIn() { - return viewIn; - } - - public void setViewIn(String viewIn) { - this.viewIn = viewIn; - } - - public RealmList getLabels() { - return labels; - } - - public JsonArray getLabelsArray() { - JsonArray array = new JsonArray(); - for (String s : this.labels) { - array.add(s); - } - return array; - } - - public void setLabels(RealmList labels) { - this.labels = labels; - } - - public void addLabel(String label) { - if (!this.labels.contains(label)) { - Utilities.log("Added"); - this.labels.add(label); - } - } - - public String getMessagePlanetCode() { - return messagePlanetCode; - } - - public void setMessagePlanetCode(String messagePlanetCode) { - this.messagePlanetCode = messagePlanetCode; - } - - public void setLabels(JsonArray images) { - this.labels = new RealmList<>(); - for (JsonElement ob : images) { - this.labels.add(ob.getAsString()); - } - } - - public String getMessageType() { - return messageType; - } - - public void setMessageType(String messageType) { - this.messageType = messageType; - } - - public long getUpdatedDate() { - return updatedDate; - } - - public void setUpdatedDate(long updatedDate) { - this.updatedDate = updatedDate; - } - - public String getUserName() { - return userName; - } - - public void setUserName(String userName) { - this.userName = userName; - } - - public String getDocType() { - return docType; - } - - public void setDocType(String docType) { - this.docType = docType; - } - - public String getViewableBy() { - return viewableBy; - } - - public void setViewableBy(String viewableBy) { - this.viewableBy = viewableBy; - } - - public String getViewableId() { - return viewableId; - } - - public void setViewableId(String viewableId) { - this.viewableId = viewableId; - } - - public String getAvatar() { - return avatar; - } - - public void setAvatar(String avatar) { - this.avatar = avatar; - } - - public void setUser(String user) { - this.user = user; - } - - public String getUser() { - return user; - } - - public static JsonObject serializeNews(RealmNews news, RealmUserModel user) { - JsonObject object = new JsonObject(); - object.addProperty("message", news.getMessage()); - if (news.get_id() != null) object.addProperty("_id", news.get_id()); - if (news.get_rev() != null) object.addProperty("_rev", news.get_rev()); - object.addProperty("time", news.getTime()); - object.addProperty("createdOn", news.getCreatedOn()); - object.addProperty("docType", news.getDocType()); - addViewIn(object, news); - object.addProperty("avatar", news.getAvatar()); - object.addProperty("messageType", news.getMessageType()); - object.addProperty("messagePlanetCode", news.getMessagePlanetCode()); - object.addProperty("createdOn", news.getCreatedOn()); - object.addProperty("replyTo", news.getReplyTo()); - object.addProperty("parentCode", news.getParentCode()); - object.add("images", news.getImagesArray()); - object.add("labels", news.getLabelsArray()); - object.add("user", new Gson().fromJson(news.getUser(), JsonObject.class)); - return object; - } - - private static void addViewIn(JsonObject object, RealmNews news) { - if (!TextUtils.isEmpty(news.getViewableId())) { - object.addProperty("viewableId", news.getViewableId()); - object.addProperty("viewableBy", news.getViewableBy()); - } - if (!TextUtils.isEmpty(news.getViewIn())) { - JsonArray ar = new Gson().fromJson(news.getViewIn(), JsonArray.class); - if (ar.size() > 0) object.add("viewIn", ar); - } - } - - public String getReplyTo() { - return replyTo; - } - - public void setReplyTo(String replyTo) { - this.replyTo = replyTo; - } - - public static RealmNews createNews(HashMap map, Realm mRealm, RealmUserModel user, RealmList imageUrls) { - if (!mRealm.isInTransaction()) mRealm.beginTransaction(); - RealmNews news = mRealm.createObject(RealmNews.class, UUID.randomUUID().toString()); - news.setMessage(map.get("message")); - news.setTime(new Date().getTime()); - news.setCreatedOn(user.getPlanetCode()); - news.setAvatar(""); - news.setDocType("message"); - news.setUserName(user.getName()); - news.setParentCode(user.getParentCode()); - news.setMessagePlanetCode(map.get("messagePlanetCode")); - news.setMessageType(map.get("messageType")); - news.setViewIn(getViewInJson(map)); - try { - news.setUpdatedDate(Long.parseLong(map.get("updatedDate"))); - } catch (Exception e) { - } - news.setUserId(user.getId()); - news.setReplyTo(map.containsKey("replyTo") ? map.get("replyTo") : ""); - news.setUser(new Gson().toJson(user.serialize())); - news.setImageUrls(imageUrls); - mRealm.commitTransaction(); - return news; - } - - public static String getViewInJson(HashMap map) { - JsonArray viewInArray = new JsonArray(); - if (!TextUtils.isEmpty(map.get("viewInId"))) { - JsonObject object = new JsonObject(); - object.addProperty("_id", map.get("viewInId")); - object.addProperty("section", map.get("viewInSection")); - viewInArray.add(object); - } - return new Gson().toJson(viewInArray); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String get_id() { - return _id; - } - - public void set_id(String _id) { - this._id = _id; - } - - public String get_rev() { - return _rev; - } - - public void set_rev(String _rev) { - this._rev = _rev; - } - - public String getUserId() { - return userId; - } - - public void setUserId(String userId) { - this.userId = userId; - } - - public String getMessage() { - return message; - } - - public String getMessageWithoutMarkdown() { - String ms = message; - Utilities.log(ms); - for (JsonElement ob : getImagesArray()) { - ms = ms.replace(JsonUtils.getString("markdown", ob.getAsJsonObject()), ""); - } - return ms; - } - - public boolean isCommunityNews() { - JsonArray array = new Gson().fromJson(getViewIn(), JsonArray.class); - boolean isCommunity = false; - for (JsonElement e : array) { - JsonObject object = e.getAsJsonObject(); - if (object.has("section") && object.get("section").getAsString().equalsIgnoreCase("community")) { - isCommunity = true; - break; - } - } - return isCommunity; - } - - public void setMessage(String message) { - this.message = message; - } - - public long getTime() { - return time; - } - - public void setTime(long time) { - this.time = time; - } - - public String getCreatedOn() { - return createdOn; - } - - public void setCreatedOn(String createdOn) { - this.createdOn = createdOn; - } - - public String getParentCode() { - return parentCode; - } - - public void setParentCode(String parentCode) { - this.parentCode = parentCode; - } - - public RealmList getImageUrls() { - return imageUrls; - } - - public void setImageUrls(RealmList imageUrls) { - this.imageUrls = imageUrls; - } - -} diff --git a/app/src/main/java/org/ole/planet/myplanet/model/RealmNews.kt b/app/src/main/java/org/ole/planet/myplanet/model/RealmNews.kt new file mode 100644 index 000000000..5c936be03 --- /dev/null +++ b/app/src/main/java/org/ole/planet/myplanet/model/RealmNews.kt @@ -0,0 +1,232 @@ +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.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 +import java.util.Date +import java.util.UUID + +open class RealmNews : RealmObject() { + @JvmField + @PrimaryKey + var id: String? = null + private var _id: String? = null + private var _rev: String? = null + @JvmField + var userId: String? = null + @JvmField + var user: String? = null + @JvmField + var message: String? = null + @JvmField + var docType: String? = null + @JvmField + var viewableBy: String? = null + @JvmField + var viewableId: String? = null + @JvmField + var avatar: String? = null + @JvmField + var replyTo: String? = null + @JvmField + var userName: String? = null + @JvmField + var messagePlanetCode: String? = null + @JvmField + var messageType: String? = null + @JvmField + var updatedDate: Long = 0 + @JvmField + var time: Long = 0 + @JvmField + var createdOn: String? = null + @JvmField + var parentCode: String? = null + @JvmField + var imageUrls: RealmList? = null + @JvmField + var images: String? = null + @JvmField + var labels: RealmList? = null + @JvmField + var viewIn: String? = null + val imagesArray: JsonArray + get() = if (images == null) JsonArray() else Gson().fromJson(images, JsonArray::class.java) + val labelsArray: JsonArray + get() { + val array = JsonArray() + for (s in labels!!) { + array.add(s) + } + return array + } + + fun addLabel(label: String?) { + if (!labels!!.contains(label)) { + Utilities.log("Added") + labels!!.add(label) + } + } + + fun setLabels(images: JsonArray) { + labels = RealmList() + for (ob in images) { + labels!!.add(ob.asString) + } + } + + fun get_id(): String? { + return _id + } + + fun set_id(_id: String?) { + this._id = _id + } + + fun get_rev(): String? { + return _rev + } + + fun set_rev(_rev: String?) { + this._rev = _rev + } + + val messageWithoutMarkdown: String? + get() { + var ms = message + Utilities.log(ms) + for (ob in imagesArray) { + ms = ms!!.replace(JsonUtils.getString("markdown", ob.asJsonObject), "") + } + return ms + } + val isCommunityNews: Boolean + get() { + val array = Gson().fromJson(viewIn, JsonArray::class.java) + var isCommunity = false + for (e in array) { + val `object` = e.asJsonObject + if (`object`.has("section") && `object`["section"].asString.equals( + "community", + ignoreCase = true + ) + ) { + isCommunity = true + break + } + } + return isCommunity + } + + companion object { + fun insert(mRealm: Realm, doc: JsonObject?) { + Utilities.log("sync nnews " + Gson().toJson(doc)) + var news = mRealm.where(RealmNews::class.java).equalTo("_id", JsonUtils.getString("_id", doc)).findFirst() + if (news == null) { + news = mRealm.createObject(RealmNews::class.java, JsonUtils.getString("_id", doc)) + } + news!!.set_rev(JsonUtils.getString("_rev", doc)) + news.set_id(JsonUtils.getString("_id", doc)) + news.viewableBy = JsonUtils.getString("viewableBy", doc) + news.docType = JsonUtils.getString("docType", doc) + news.avatar = JsonUtils.getString("avatar", doc) + news.updatedDate = JsonUtils.getLong("updatedDate", doc) + news.viewableId = JsonUtils.getString("viewableId", doc) + news.createdOn = JsonUtils.getString("createdOn", doc) + news.messageType = JsonUtils.getString("messageType", doc) + news.messagePlanetCode = JsonUtils.getString("messagePlanetCode", doc) + news.replyTo = JsonUtils.getString("replyTo", doc) + news.parentCode = JsonUtils.getString("parentCode", doc) + val user = JsonUtils.getJsonObject("user", doc) + news.user = Gson().toJson(JsonUtils.getJsonObject("user", doc)) + news.userId = JsonUtils.getString("_id", user) + news.userName = JsonUtils.getString("name", user) + news.time = JsonUtils.getLong("time", doc) + val images = JsonUtils.getJsonArray("images", doc) + val message = JsonUtils.getString("message", doc) + news.message = message + news.images = Gson().toJson(images) + val labels = JsonUtils.getJsonArray("labels", doc) + news.viewIn = Gson().toJson(JsonUtils.getJsonArray("viewIn", doc)) + news.setLabels(labels) + } + + @JvmStatic + fun serializeNews(news: RealmNews, user: RealmUserModel?): JsonObject { + val `object` = JsonObject() + `object`.addProperty("message", news.message) + if (news.get_id() != null) `object`.addProperty("_id", news.get_id()) + if (news.get_rev() != null) `object`.addProperty("_rev", news.get_rev()) + `object`.addProperty("time", news.time) + `object`.addProperty("createdOn", news.createdOn) + `object`.addProperty("docType", news.docType) + addViewIn(`object`, news) + `object`.addProperty("avatar", news.avatar) + `object`.addProperty("messageType", news.messageType) + `object`.addProperty("messagePlanetCode", news.messagePlanetCode) + `object`.addProperty("createdOn", news.createdOn) + `object`.addProperty("replyTo", news.replyTo) + `object`.addProperty("parentCode", news.parentCode) + `object`.add("images", news.imagesArray) + `object`.add("labels", news.labelsArray) + `object`.add("user", Gson().fromJson(news.user, JsonObject::class.java)) + return `object` + } + + private fun addViewIn(`object`: JsonObject, news: RealmNews) { + if (!TextUtils.isEmpty(news.viewableId)) { + `object`.addProperty("viewableId", news.viewableId) + `object`.addProperty("viewableBy", news.viewableBy) + } + if (!TextUtils.isEmpty(news.viewIn)) { + val ar = Gson().fromJson(news.viewIn, JsonArray::class.java) + if (ar.size() > 0) `object`.add("viewIn", ar) + } + } + + @JvmStatic + fun createNews(map: HashMap, mRealm: Realm, user: RealmUserModel, imageUrls: RealmList?): RealmNews { + if (!mRealm.isInTransaction) mRealm.beginTransaction() + val news = mRealm.createObject(RealmNews::class.java, UUID.randomUUID().toString()) + news.message = map["message"] + news.time = Date().time + news.createdOn = user.planetCode + news.avatar = "" + news.docType = "message" + news.userName = user.name + news.parentCode = user.parentCode + news.messagePlanetCode = map["messagePlanetCode"] + news.messageType = map["messageType"] + news.viewIn = getViewInJson(map) + try { + news.updatedDate = map["updatedDate"]!!.toLong() + } catch (e: Exception) { + e.printStackTrace() + } + news.userId = user.id + news.replyTo = if (map.containsKey("replyTo")) map["replyTo"] else "" + news.user = Gson().toJson(user.serialize()) + news.imageUrls = imageUrls + mRealm.commitTransaction() + return news + } + + fun getViewInJson(map: HashMap): String { + val viewInArray = JsonArray() + if (!TextUtils.isEmpty(map["viewInId"])) { + val `object` = JsonObject() + `object`.addProperty("_id", map["viewInId"]) + `object`.addProperty("section", map["viewInSection"]) + viewInArray.add(`object`) + } + return Gson().toJson(viewInArray) + } + } +} diff --git a/app/src/main/java/org/ole/planet/myplanet/service/UploadManager.java b/app/src/main/java/org/ole/planet/myplanet/service/UploadManager.java index bb37cf293..fd233398b 100644 --- a/app/src/main/java/org/ole/planet/myplanet/service/UploadManager.java +++ b/app/src/main/java/org/ole/planet/myplanet/service/UploadManager.java @@ -449,12 +449,12 @@ public void uploadNews() { final RealmResults activities = realm.where(RealmNews.class).findAll(); for (RealmNews act : activities) { try { - if (act.getUserId().startsWith("guest")) continue; + if (act.userId.startsWith("guest")) continue; JsonObject object = RealmNews.serializeNews(act, userModel); JsonArray image = act.getImagesArray(); RealmUserModel user = realm.where(RealmUserModel.class).equalTo("id", pref.getString("userId", "")).findFirst(); - if (act.getImageUrls() != null) { - for (String imageobject : act.getImageUrls()) { + if (act.imageUrls != null) { + for (String imageobject : act.imageUrls) { JsonObject imgObject = new Gson().fromJson(imageobject, JsonObject.class); JsonObject ob = createImage(user, imgObject); JsonObject response = apiInterface.postDoc(Utilities.getHeader(), "application/json", Utilities.getUrl() + "/resources", ob).execute().body(); @@ -481,7 +481,7 @@ public void uploadNews() { image.add(resourceObject); } } - act.setImages(new Gson().toJson(image)); + act.images = new Gson().toJson(image); object.add("images", image); Response newsUploadResponse; if (TextUtils.isEmpty(act.get_id())) { @@ -490,7 +490,7 @@ public void uploadNews() { newsUploadResponse = apiInterface.putDoc(Utilities.getHeader(), "application/json", Utilities.getUrl() + "/news/" + act.get_id(), object).execute(); } if (newsUploadResponse.body() != null) { - act.getImageUrls().clear(); + act.imageUrls.clear(); act.set_id(JsonUtils.getString("id", newsUploadResponse.body())); act.set_rev(JsonUtils.getString("rev", newsUploadResponse.body())); } diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/news/AdapterNews.java b/app/src/main/java/org/ole/planet/myplanet/ui/news/AdapterNews.java index 2ae1d6f94..f0cc7fdbc 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/news/AdapterNews.java +++ b/app/src/main/java/org/ole/planet/myplanet/ui/news/AdapterNews.java @@ -104,22 +104,22 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int positi viewHolder.bind(position); RealmNews news = getNews(holder, position); if (news.isValid()) { - RealmUserModel userModel = mRealm.where(RealmUserModel.class).equalTo("id", news.getUserId()).findFirst(); + RealmUserModel userModel = mRealm.where(RealmUserModel.class).equalTo("id", news.userId).findFirst(); if (userModel != null && currentUser != null) { viewHolder.rowNewsBinding.tvName.setText(userModel.toString()); Utilities.loadImage(userModel.getUserImage(), viewHolder.rowNewsBinding.imgUser); showHideButtons(userModel, holder); } else { - viewHolder.rowNewsBinding.tvName.setText(news.getUserName()); + viewHolder.rowNewsBinding.tvName.setText(news.userName); viewHolder.rowNewsBinding.llEditDelete.setVisibility(View.GONE); } showShareButton(holder, news); viewHolder.rowNewsBinding.tvMessage.setText(news.getMessageWithoutMarkdown()); - viewHolder.rowNewsBinding.tvDate.setText(TimeUtils.formatDate(news.getTime())); - if(Objects.equals(news.getUserId(), currentUser.get_id())){ + viewHolder.rowNewsBinding.tvDate.setText(TimeUtils.formatDate(news.time)); + if(Objects.equals(news.userId, currentUser.get_id())){ viewHolder.rowNewsBinding.imgDelete.setOnClickListener(view -> new AlertDialog.Builder(context).setMessage(R.string.delete_record).setPositiveButton(R.string.ok, (dialogInterface, i) -> deletePost(news, context)).setNegativeButton(R.string.cancel, null).show()); - viewHolder.rowNewsBinding.imgEdit.setOnClickListener(view -> showEditAlert(news.getId(), true)); + viewHolder.rowNewsBinding.imgEdit.setOnClickListener(view -> showEditAlert(news.id, true)); viewHolder.rowNewsBinding.btnAddLabel.setVisibility(fromLogin ? View.GONE : View.VISIBLE); } else{ viewHolder.rowNewsBinding.imgEdit.setVisibility(View.GONE); @@ -133,7 +133,7 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int positi showReplyButton(holder, news, position); if(news.isCommunityNews()) { - holder.itemView.setOnClickListener(v -> context.startActivity(new Intent(context, NewsDetailActivity.class).putExtra("newsId", list.get(position).getId()))); + holder.itemView.setOnClickListener(v -> context.startActivity(new Intent(context, NewsDetailActivity.class).putExtra("newsId", list.get(position).id))); } addLabels(holder, news); showChips(holder, news); @@ -164,23 +164,23 @@ private void showChips(RecyclerView.ViewHolder holder, RealmNews news) { viewHolder.rowNewsBinding.fbChips.removeAllViews(); final ChipCloud chipCloud = new ChipCloud(context, viewHolder.rowNewsBinding.fbChips, config); - for (String s : news.getLabels()) { + for (String s : news.labels) { chipCloud.addChip(getLabel(s)); chipCloud.setDeleteListener((i, s1) -> { if (!mRealm.isInTransaction()) mRealm.beginTransaction(); - news.getLabels().remove(Constants.LABELS.get(s1)); + news.labels.remove(Constants.LABELS.get(s1)); mRealm.commitTransaction(); - viewHolder.rowNewsBinding.btnAddLabel.setEnabled(news.getLabels().size() < 3); + viewHolder.rowNewsBinding.btnAddLabel.setEnabled(news.labels.size() < 3); }); } - viewHolder.rowNewsBinding.btnAddLabel.setEnabled(news.getLabels().size() < 3); + viewHolder.rowNewsBinding.btnAddLabel.setEnabled(news.labels.size() < 3); } private void loadImage(RecyclerView.ViewHolder holder, RealmNews news) { ViewHolderNews viewHolder = (ViewHolderNews) holder; - if (news.getImageUrls() != null && news.getImageUrls().size() > 0) { + if (news.imageUrls != null && news.imageUrls.size() > 0) { try { - JsonObject imgObject = new Gson().fromJson(news.getImageUrls().get(0), JsonObject.class); + JsonObject imgObject = new Gson().fromJson(news.imageUrls.get(0), JsonObject.class); viewHolder.rowNewsBinding.imgNews.setVisibility(View.VISIBLE); Glide.with(context).load(new File(JsonUtils.getString("imageUrl", imgObject))).into(viewHolder.rowNewsBinding.imgNews); } catch (Exception e) { @@ -193,7 +193,7 @@ private void loadImage(RecyclerView.ViewHolder holder, RealmNews news) { private void loadRemoteImage(RecyclerView.ViewHolder holder, RealmNews news) { ViewHolderNews viewHolder = (ViewHolderNews) holder; - Utilities.log(news.getImages()); + Utilities.log(news.images); if (news.getImagesArray().size() > 0) { JsonObject ob = news.getImagesArray().get(0).getAsJsonObject(); String resourceId = JsonUtils.getString("resourceId", ob.getAsJsonObject()); @@ -216,15 +216,15 @@ private void showReplyButton(RecyclerView.ViewHolder holder, RealmNews finalNews ViewHolderNews viewHolder = (ViewHolderNews) holder; if (this.listener == null || this.fromLogin) viewHolder.rowNewsBinding.btnShowReply.setVisibility(View.GONE); - viewHolder.rowNewsBinding.btnReply.setOnClickListener(view -> showEditAlert(finalNews.getId(), false)); - List replies = mRealm.where(RealmNews.class).sort("time", Sort.DESCENDING).equalTo("replyTo", finalNews.getId(), Case.INSENSITIVE).findAll(); + viewHolder.rowNewsBinding.btnReply.setOnClickListener(view -> showEditAlert(finalNews.id, false)); + List replies = mRealm.where(RealmNews.class).sort("time", Sort.DESCENDING).equalTo("replyTo", finalNews.id, Case.INSENSITIVE).findAll(); viewHolder.rowNewsBinding.btnShowReply.setText(String.format(context.getString(R.string.show_replies) + " (%d)", replies.size())); viewHolder.rowNewsBinding.btnShowReply.setVisibility(replies.size() > 0 ? View.VISIBLE : View.GONE); if (position == 0 && parentNews != null) viewHolder.rowNewsBinding.btnShowReply.setVisibility(View.GONE); viewHolder.rowNewsBinding.btnShowReply.setOnClickListener(view -> { - sharedPreferences.setREPLIEDNEWSID1(finalNews.getId()); + sharedPreferences.setREPLIEDNEWSID1(finalNews.id); if (listener != null) { listener.showReply(finalNews, fromLogin); } @@ -238,7 +238,7 @@ public void showEditAlert(String id, boolean isEdit) { LinearLayout llImage = v.findViewById(R.id.ll_alert_image); v.findViewById(R.id.add_news_image).setOnClickListener(view -> listener.addImage(llImage)); RealmNews news = mRealm.where(RealmNews.class).equalTo("id", id).findFirst(); - if (isEdit) et.setText(news.getMessage() + ""); + if (isEdit) et.setText(news.message + ""); new AlertDialog.Builder(context).setTitle(isEdit ? R.string.edit_post : R.string.reply).setIcon(R.drawable.ic_edit).setView(v).setPositiveButton(R.string.button_submit, (dialogInterface, i) -> { String s = et.getText().toString(); if (isEdit) { @@ -251,11 +251,11 @@ public void postReply(String s, RealmNews news) { if (!mRealm.isInTransaction()) mRealm.beginTransaction(); HashMap map = new HashMap<>(); map.put("message", s); - map.put("viewableBy", news.getViewableBy()); - map.put("viewableId", news.getViewableId()); - map.put("replyTo", news.getId()); - map.put("messageType", news.getMessageType()); - map.put("messagePlanetCode", news.getMessagePlanetCode()); + map.put("viewableBy", news.viewableBy); + map.put("viewableId", news.viewableId); + map.put("replyTo", news.id); + map.put("messageType", news.messageType); + map.put("messagePlanetCode", news.messagePlanetCode); RealmNews.createNews(map, mRealm, currentUser, imageList); notifyDataSetChanged(); } @@ -266,7 +266,7 @@ private void editPost(String s, RealmNews news) { return; } if (!mRealm.isInTransaction()) mRealm.beginTransaction(); - news.setMessage(s); + news.message = s; mRealm.commitTransaction(); notifyDataSetChanged(); } @@ -301,7 +301,7 @@ private void showHideButtons(RealmUserModel userModel, RecyclerView.ViewHolder h private void deletePost(RealmNews news, Context context) { if (!mRealm.isInTransaction()) mRealm.beginTransaction(); - if (Objects.equals(news.getId(), sharedPreferences.getREPLIEDNEWSID1())) { + if (Objects.equals(news.id, sharedPreferences.getREPLIEDNEWSID1())) { if (isFromNewsFragment) { list.remove(news); } @@ -350,14 +350,14 @@ public void showShareButton(RecyclerView.ViewHolder holder, RealmNews news) { ViewHolderNews viewHolder = (ViewHolderNews) holder; viewHolder.rowNewsBinding.btnShare.setVisibility((news.isCommunityNews() || fromLogin) ? View.GONE : View.VISIBLE); viewHolder.rowNewsBinding.btnShare.setOnClickListener(view -> { - JsonArray array = new Gson().fromJson(news.getViewIn(), JsonArray.class); + JsonArray array = new Gson().fromJson(news.viewIn, JsonArray.class); JsonObject ob = new JsonObject(); ob.addProperty("section", "community"); ob.addProperty("_id", currentUser.getPlanetCode() + "@" + currentUser.getParentCode()); ob.addProperty("sharedDate", Calendar.getInstance().getTimeInMillis()); array.add(ob); if (!mRealm.isInTransaction()) mRealm.beginTransaction(); - news.setViewIn(new Gson().toJson(array)); + news.viewIn = new Gson().toJson(array); mRealm.commitTransaction(); Utilities.toast(context, context.getString(R.string.shared_to_community)); viewHolder.rowNewsBinding.btnShare.setVisibility(View.GONE); diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/news/NewsDetailActivity.kt b/app/src/main/java/org/ole/planet/myplanet/ui/news/NewsDetailActivity.kt index 89b504338..39c27870b 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/news/NewsDetailActivity.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/news/NewsDetailActivity.kt @@ -55,18 +55,17 @@ class NewsDetailActivity : BaseActivity() { private fun initViews() { title = news?.userName - var msg: String = news!!.message + var msg: String? = news!!.message - if (news!!.imageUrls != null && news!!.imageUrls.size > 0) { + if (news!!.imageUrls != null && news!!.imageUrls!!.size > 0) { msg = loadLocalImage(); } else { news?.imagesArray?.forEach { val ob = it.asJsonObject val resourceId = JsonUtils.getString("resourceId", ob.asJsonObject) val markDown = JsonUtils.getString("markdown", ob.asJsonObject) - val library = - realm.where(RealmMyLibrary::class.java).equalTo("_id", resourceId).findFirst() - msg = msg.replace( + val library = realm.where(RealmMyLibrary::class.java).equalTo("_id", resourceId).findFirst() + msg = msg?.replace( markDown, "", false @@ -74,7 +73,7 @@ class NewsDetailActivity : BaseActivity() { } loadImage() } - msg = msg.replace( + msg = msg!!.replace( "\n", "

" ) @@ -89,13 +88,13 @@ class NewsDetailActivity : BaseActivity() { } private fun loadLocalImage(): String { - var msg: String = news!!.message + var msg: String? = news!!.message try { - val imgObject = Gson().fromJson(news!!.imageUrls[0], JsonObject::class.java) + val imgObject = Gson().fromJson(news!!.imageUrls!![0], JsonObject::class.java) activityNewsDetailBinding.img.visibility = View.VISIBLE Glide.with(this@NewsDetailActivity) .load(File(JsonUtils.getString("imageUrl", imgObject))).into(activityNewsDetailBinding.img) - news!!.imageUrls.forEach { + news!!.imageUrls!!.forEach { val imageObject = Gson().fromJson(it, JsonObject::class.java) msg += "
getNewsList() { List allNews = mRealm.where(RealmNews.class).sort("time", Sort.DESCENDING).isEmpty("replyTo").equalTo("docType", "message", Case.INSENSITIVE).findAll(); List list = new ArrayList<>(); for (RealmNews news : allNews) { - if (!TextUtils.isEmpty(news.getViewableBy()) && news.getViewableBy().equalsIgnoreCase("community")) { + if (!TextUtils.isEmpty(news.viewableBy) && news.viewableBy.equalsIgnoreCase("community")) { list.add(news); continue; } - if (!TextUtils.isEmpty(news.getViewIn())) { - JsonArray ar = new Gson().fromJson(news.getViewIn(), JsonArray.class); + if (!TextUtils.isEmpty(news.viewIn)) { + JsonArray ar = new Gson().fromJson(news.viewIn, JsonArray.class); for (JsonElement e : ar) { JsonObject ob = e.getAsJsonObject(); if (ob != null && ob.has("_id") && ob.get("_id").getAsString().equalsIgnoreCase(user != null ? user.getPlanetCode() + "@" + user.getParentCode() : "")) { diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/news/ReplyActivity.java b/app/src/main/java/org/ole/planet/myplanet/ui/news/ReplyActivity.java index 9d991a550..8f511f713 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/news/ReplyActivity.java +++ b/app/src/main/java/org/ole/planet/myplanet/ui/news/ReplyActivity.java @@ -76,7 +76,7 @@ private void showData(String id) { @Override public void showReply(RealmNews news, boolean fromLogin) { - startActivity(new Intent(this, ReplyActivity.class).putExtra("id", news.getId())); + startActivity(new Intent(this, ReplyActivity.class).putExtra("id", news.id)); } @Override diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/team/teamDiscussion/DiscussionListFragment.java b/app/src/main/java/org/ole/planet/myplanet/ui/team/teamDiscussion/DiscussionListFragment.java index df1e87132..75ee593d9 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/team/teamDiscussion/DiscussionListFragment.java +++ b/app/src/main/java/org/ole/planet/myplanet/ui/team/teamDiscussion/DiscussionListFragment.java @@ -70,10 +70,10 @@ private List getNews() { List realmNewsList = mRealm.where(RealmNews.class).isEmpty("replyTo").sort("time", Sort.DESCENDING).findAll(); List list = new ArrayList<>(); for (RealmNews news : realmNewsList) { - if (!TextUtils.isEmpty(news.getViewableBy()) && news.getViewableBy().equalsIgnoreCase("teams") && news.getViewableId().equalsIgnoreCase(team.get_id())) { + if (!TextUtils.isEmpty(news.viewableBy) && news.viewableBy.equalsIgnoreCase("teams") && news.viewableId.equalsIgnoreCase(team.get_id())) { list.add(news); - } else if (!TextUtils.isEmpty(news.getViewIn())) { - JsonArray ar = new Gson().fromJson(news.getViewIn(), JsonArray.class); + } else if (!TextUtils.isEmpty(news.viewIn)) { + JsonArray ar = new Gson().fromJson(news.viewIn, JsonArray.class); for (JsonElement e : ar) { JsonObject ob = e.getAsJsonObject(); if (ob.get("_id").getAsString().equalsIgnoreCase(team.get_id())) {