Skip to content

Commit

Permalink
actions: refactor RealmMyLife model to kotlin (fixes #2807) (#2815)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Dec 6, 2023
1 parent cc7d935 commit bef16d4
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 140 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 1193
versionName "0.11.93"
versionCode 1194
versionName "0.11.94"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
127 changes: 0 additions & 127 deletions app/src/main/java/org/ole/planet/myplanet/model/RealmMyLife.java

This file was deleted.

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

import android.content.SharedPreferences
import io.realm.Realm
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey

open class RealmMyLife : RealmObject {
@PrimaryKey
private var _id: String? = null
@JvmField
var imageId: String? = null
@JvmField
var userId: String? = null
@JvmField
var title: String? = null
@JvmField
var isVisible = false
@JvmField
var weight = 0

constructor(imageId: String?, userId: String?, title: String?) {
this.imageId = imageId
this.userId = userId
this.title = title
isVisible = true
}

constructor()

fun get_id(): String? {
return _id
}

fun set_id(_id: String?) {
this._id = _id
}

companion object {
fun getMyLifeByUserId(mRealm: Realm, settings: SharedPreferences): List<RealmMyLife> {
val userId = settings.getString("userId", "--")
return getMyLifeByUserId(mRealm, userId)
}

@JvmStatic
fun getMyLifeByUserId(mRealm: Realm, userId: String?): List<RealmMyLife> {
return mRealm.where(RealmMyLife::class.java).equalTo("userId", userId).findAll()
.sort("weight")
}

fun createMyLife(myLife: RealmMyLife, mRealm: Realm, _id: String?) {
if (!mRealm.isInTransaction) mRealm.beginTransaction()
myLife.set_id(_id)
mRealm.commitTransaction()
}

@JvmStatic
fun updateWeight(weight: Int, _id: String?, realm: Realm, userId: String?) {
realm.executeTransaction { realm ->
var currentWeight = -1
val myLifeList = getMyLifeByUserId(realm, userId)
for (item in myLifeList) {
if (item.get_id()!!.contains(_id!!)) {
currentWeight = item.weight
item.weight = weight
}
}
for (item in myLifeList) {
if (currentWeight != -1 && item.weight == weight && !item.get_id()!!.contains(_id!!)) {
item.weight = currentWeight
}
}
}
}

@JvmStatic
fun updateVisibility(isVisible: Boolean, _id: String?, realm: Realm, userId: String?) {
realm.executeTransaction { realm ->
val myLifeList = getMyLifeByUserId(realm, userId)
for (item in myLifeList) {
if (item.get_id()!!.contains(_id!!)) {
item.isVisible = isVisible
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ open class BaseDashboardFragmentPlugin : BaseContainerFragment() {
itemMyLifeBinding.tvCount.visibility = View.GONE
}

handleClickMyLife(title, v)
if (title != null) {
handleClickMyLife(title, v)
}
return v
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof org.ole.planet.myplanet.ui.mylife.AdapterMyLife.ViewHolderMyLife) {
Utilities.log("On bind " + position);
((ViewHolderMyLife) holder).title.setText(myLifeList.get(position).getTitle());
((ViewHolderMyLife) holder).imageView.setImageResource(context.getResources().getIdentifier(myLifeList.get(position).getImageId(), "drawable", context.getPackageName()));
Fragment fragment = find_fragment(myLifeList.get(position).getImageId());
((ViewHolderMyLife) holder).title.setText(myLifeList.get(position).title);
((ViewHolderMyLife) holder).imageView.setImageResource(context.getResources().getIdentifier(myLifeList.get(position).imageId, "drawable", context.getPackageName()));
Fragment fragment = find_fragment(myLifeList.get(position).imageId);
if (fragment != null) {
((ViewHolderMyLife) holder).imageView.setOnClickListener(view -> transactionFragment(fragment, view));
}
Expand All @@ -111,22 +111,22 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int
mDragStartListener.onStartDrag(holder);
return false;
});
((ViewHolderMyLife) holder).visibility.setOnClickListener(view -> updateVisibility(holder, holder.getAdapterPosition(), myLifeList.get(holder.getAdapterPosition()).isVisible()));
if (!myLifeList.get(position).isVisible())
((ViewHolderMyLife) holder).visibility.setOnClickListener(view -> updateVisibility(holder, holder.getAdapterPosition(), myLifeList.get(holder.getAdapterPosition()).isVisible));
if (!myLifeList.get(position).isVisible)
changeVisibility(holder, R.drawable.ic_visibility, HIDE);
else changeVisibility(holder, R.drawable.ic_visibility_off, SHOW);

}
}

public void updateVisibility(final RecyclerView.ViewHolder holder, final int position, final boolean isVisible) {
mRealm.executeTransactionAsync(realm -> RealmMyLife.updateVisibility(!isVisible, myLifeList.get(position).get_id(), realm, myLifeList.get(position).getUserId()), () -> new Handler(Looper.getMainLooper()).post(() -> {
mRealm.executeTransactionAsync(realm -> RealmMyLife.updateVisibility(!isVisible, myLifeList.get(position).get_id(), realm, myLifeList.get(position).userId), () -> new Handler(Looper.getMainLooper()).post(() -> {
if (isVisible) {
changeVisibility(holder, R.drawable.ic_visibility, HIDE);
Utilities.toast(context, myLifeList.get(position).getTitle() + context.getString(R.string.is_now_hidden));
Utilities.toast(context, myLifeList.get(position).title + context.getString(R.string.is_now_hidden));
} else {
changeVisibility(holder, R.drawable.ic_visibility_off, SHOW);
Utilities.toast(context, myLifeList.get(position).getTitle() + context.getString(R.string.is_now_shown));
Utilities.toast(context, myLifeList.get(position).title + context.getString(R.string.is_now_shown));
}
}), error -> Utilities.log(String.valueOf(error)));
}
Expand All @@ -147,7 +147,7 @@ public int getItemCount() {

@Override
public boolean onItemMove(int fromPosition, int toPosition) {
RealmMyLife.updateWeight(toPosition + 1, myLifeList.get(fromPosition).get_id(), mRealm, myLifeList.get(fromPosition).getUserId());
RealmMyLife.updateWeight(toPosition + 1, myLifeList.get(fromPosition).get_id(), mRealm, myLifeList.get(fromPosition).userId);
notifyItemMoved(fromPosition, toPosition);
return true;
}
Expand Down Expand Up @@ -178,7 +178,7 @@ public void onItemSelected() {
@Override
public void onItemClear(RecyclerView.ViewHolder holder) {
itemView.setBackgroundColor(0);
if (!myLifeList.get(holder.getAdapterPosition()).isVisible())
if (!myLifeList.get(holder.getAdapterPosition()).isVisible)
((ViewHolderMyLife) holder).rv_item_container.setAlpha(HIDE);
}
}
Expand Down

0 comments on commit bef16d4

Please sign in to comment.