Skip to content

Commit

Permalink
community: display leaders (fixes #1807) (#3405)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Apr 24, 2024
1 parent fc578b9 commit 82d1071
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 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 1495
versionName "0.14.95"
versionCode 1496
versionName "0.14.96"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -71,6 +70,7 @@ class ManagerSync private constructor(context: Context) {
apiInterface?.findDocs(Utilities.header, "application/json", Utilities.getUrl() + "/_users/_find", `object`)?.enqueue(object : Callback<JsonObject?> {
override fun onResponse(call: Call<JsonObject?>, response: Response<JsonObject?>) {
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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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<RealmUserModel>()
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<RealmUserModel> {
val leadersList = mutableListOf<RealmUserModel>()
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
}
}

0 comments on commit 82d1071

Please sign in to comment.