-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
- Loading branch information
Showing
3 changed files
with
33 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 30 additions & 17 deletions
47
app/src/main/java/org/ole/planet/myplanet/ui/community/LeadersFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |