diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/team/TeamFragment.kt b/app/src/main/java/org/ole/planet/myplanet/ui/team/TeamFragment.kt index 5cae9b86f5..6f02668b73 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/team/TeamFragment.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/team/TeamFragment.kt @@ -199,8 +199,9 @@ class TeamFragment : Fragment(), AdapterTeamList.OnClickTeamItem { .notEqualTo("status", "archived") .contains("name", charSequence.toString(), Case.INSENSITIVE) val (list, conditionApplied) = getList(query) + val sortedList = sortTeams(list) val adapterTeamList = AdapterTeamList( - activity as Context, list, mRealm, childFragmentManager + activity as Context, sortedList, mRealm, childFragmentManager ) adapterTeamList.setTeamListener(this@TeamFragment) fragmentTeamBinding.rvTeamList.adapter = adapterTeamList @@ -249,6 +250,17 @@ class TeamFragment : Fragment(), AdapterTeamList.OnClickTeamItem { } } + private fun sortTeams(list: List): List { + val user = user?.id + return list.sortedWith(compareByDescending { team -> + when { + RealmMyTeam.isTeamLeader(team.teamId, user, mRealm) -> 3 + team.isMyTeam(user, mRealm) -> 2 + else -> 1 + } + }) + } + override fun onEditTeam(team: RealmMyTeam?) { createTeamAlert(team!!) } @@ -257,7 +269,8 @@ class TeamFragment : Fragment(), AdapterTeamList.OnClickTeamItem { activity?.runOnUiThread { val query = mRealm.where(RealmMyTeam::class.java).isEmpty("teamId").notEqualTo("status", "archived") val (filteredList, conditionApplied) = getList(query) - val adapterTeamList = AdapterTeamList(activity as Context, filteredList, mRealm, childFragmentManager).apply { + val sortedList = sortTeams(filteredList) + val adapterTeamList = AdapterTeamList(activity as Context, sortedList, mRealm, childFragmentManager).apply { setType(type) setTeamListener(this@TeamFragment) }