From 82d107116635f58a83d7d5df678774bb23206af9 Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Wed, 24 Apr 2024 21:12:27 +0300 Subject: [PATCH] community: display leaders (fixes #1807) (#3405) Co-authored-by: dogi --- app/build.gradle | 4 +- .../myplanet/datamanager/ManagerSync.kt | 2 +- .../myplanet/ui/community/LeadersFragment.kt | 47 ++++++++++++------- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 3781ec82d0..8ab2479bbf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,8 +9,8 @@ android { applicationId "org.ole.planet.myplanet" minSdkVersion 21 targetSdkVersion 34 - versionCode 1495 - versionName "0.14.95" + versionCode 1496 + versionName "0.14.96" ndkVersion '21.3.6528147' testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true diff --git a/app/src/main/java/org/ole/planet/myplanet/datamanager/ManagerSync.kt b/app/src/main/java/org/ole/planet/myplanet/datamanager/ManagerSync.kt index 848fe3fbb5..d7b1298ded 100644 --- a/app/src/main/java/org/ole/planet/myplanet/datamanager/ManagerSync.kt +++ b/app/src/main/java/org/ole/planet/myplanet/datamanager/ManagerSync.kt @@ -9,7 +9,6 @@ import io.realm.Realm import org.ole.planet.myplanet.MainApplication import org.ole.planet.myplanet.callback.SyncListener import org.ole.planet.myplanet.model.RealmUserModel.Companion.populateUsersTable -import org.ole.planet.myplanet.ui.sync.SyncActivity import org.ole.planet.myplanet.utilities.AndroidDecrypter.Companion.AndroidDecrypter import org.ole.planet.myplanet.utilities.Constants.PREFS_NAME import org.ole.planet.myplanet.utilities.JsonUtils @@ -71,6 +70,7 @@ class ManagerSync private constructor(context: Context) { apiInterface?.findDocs(Utilities.header, "application/json", Utilities.getUrl() + "/_users/_find", `object`)?.enqueue(object : Callback { override fun onResponse(call: Call, response: Response) { if (response.body() != null) { + settings.edit().putString("communityLeaders", "${response.body()}").apply() val array = JsonUtils.getJsonArray("docs", response.body()) if (array.size() > 0) { settings.edit().putString("user_admin", Gson().toJson(array[0])).apply() diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/community/LeadersFragment.kt b/app/src/main/java/org/ole/planet/myplanet/ui/community/LeadersFragment.kt index 0b94511887..26b11b71b9 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/community/LeadersFragment.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/community/LeadersFragment.kt @@ -1,41 +1,54 @@ package org.ole.planet.myplanet.ui.community +import android.content.* import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup +import android.view.* import androidx.fragment.app.Fragment import androidx.recyclerview.widget.GridLayoutManager +import org.json.* import org.ole.planet.myplanet.R import org.ole.planet.myplanet.databinding.FragmentMembersBinding -import org.ole.planet.myplanet.datamanager.DatabaseService -import org.ole.planet.myplanet.model.RealmMyTeam import org.ole.planet.myplanet.model.RealmUserModel +import org.ole.planet.myplanet.utilities.Constants.PREFS_NAME class LeadersFragment : Fragment() { private lateinit var fragmentMembersBinding: FragmentMembersBinding - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? - ): View { + lateinit var settings: SharedPreferences + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { fragmentMembersBinding = FragmentMembersBinding.inflate(inflater, container, false) + settings = requireActivity().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) return fragmentMembersBinding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - val mRealm = DatabaseService(requireActivity()).realmInstance - val leaders = mRealm.where(RealmMyTeam::class.java).equalTo("isLeader", true).findAll() - if (leaders.isEmpty()) { + val leaders = settings.getString("communityLeaders", "") + if (leaders.isNullOrEmpty()) { fragmentMembersBinding.tvNodata.text = getString(R.string.no_data_available) } else { + val leadersList = parseLeadersJson(leaders) fragmentMembersBinding.rvMember.layoutManager = GridLayoutManager(activity, 2) - val list = ArrayList() - for (team in leaders) { - val model = - mRealm.where(RealmUserModel::class.java).equalTo("id", team.userId).findFirst() - if (model != null && !list.contains(model)) list.add(model) + fragmentMembersBinding.rvMember.adapter = AdapterLeader(requireActivity(), leadersList) + } + } + + private fun parseLeadersJson(jsonString: String): List { + val leadersList = mutableListOf() + try { + val jsonObject = JSONObject(jsonString) + val docsArray = jsonObject.getJSONArray("docs") + for (i in 0 until docsArray.length()) { + val docObject = docsArray.getJSONObject(i) + val user = RealmUserModel() + user.name = docObject.getString("name") + user.firstName = docObject.getString("firstName") + user.lastName = docObject.getString("lastName") + user.email = docObject.getString("email") + leadersList.add(user) } - fragmentMembersBinding.rvMember.adapter = AdapterLeader(requireActivity(), list) + } catch (e: JSONException) { + e.printStackTrace() } + return leadersList } } \ No newline at end of file