Skip to content

Commit

Permalink
Merge branch 'master' into 4814-last-user
Browse files Browse the repository at this point in the history
  • Loading branch information
dogi authored Dec 2, 2024
2 parents d29f93c + b9f77df commit 9062b0f
Showing 1 changed file with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import org.ole.planet.myplanet.model.RealmTeamLog
import org.ole.planet.myplanet.model.RealmUserModel
import org.ole.planet.myplanet.service.UserProfileDbHandler
import io.realm.Realm
import io.realm.Sort
import org.ole.planet.myplanet.utilities.Utilities

class AdapterJoinedMember(private val context: Context, private val list: List<RealmUserModel>, private val mRealm: Realm, private val teamId: String) : RecyclerView.Adapter<AdapterJoinedMember.ViewHolderUser>() {
Expand Down Expand Up @@ -95,14 +96,20 @@ class AdapterJoinedMember(private val context: Context, private val list: List<R
}
}
builder.setAdapter(adapter) { _, i ->

if (position >= 0 && position < list.size) {
when (i) {
0 -> {
if (currentUser.id != list[position].id) {
reject(list[position], position)
} else {
Toast.makeText(context, R.string.cannot_remove_user, Toast.LENGTH_SHORT).show()
val nextOfKin= getNextOfKin()
if(nextOfKin!=null){
makeLeader(nextOfKin)
reject(list[position], position)
}
else {
Toast.makeText(context, R.string.cannot_remove_user, Toast.LENGTH_SHORT).show()
}
}
}
1 -> {
Expand All @@ -122,7 +129,29 @@ class AdapterJoinedMember(private val context: Context, private val list: List<R
}
}

private fun getNextOfKin(): RealmUserModel? {
val members: List<RealmMyTeam> = mRealm.where(RealmMyTeam::class.java)
.equalTo("teamId", teamId)
.equalTo("isLeader", false)
.notEqualTo("status","archived")
.sort("createdDate", Sort.DESCENDING)
.findAll()
val successor = if (members.isNotEmpty()) members?.first() else null
if(successor==null){
return null
}
else{
val user= mRealm.where(RealmUserModel::class.java).equalTo("id", successor.userId).findFirst()
return user
}
return null
}

private fun makeLeader(userModel: RealmUserModel) {
if(userModel==null){
Utilities.toast(context, context.getString(R.string.cannot_remove_user))
return
}
mRealm.executeTransaction { realm ->
val team = realm.where(RealmMyTeam::class.java)
.equalTo("teamId", teamId)
Expand All @@ -134,6 +163,7 @@ class AdapterJoinedMember(private val context: Context, private val list: List<R
.findFirst()
teamLeader?.isLeader = false
team?.isLeader = true
team?.updated=true
}
notifyDataSetChanged()
Utilities.toast(context, context.getString(R.string.leader_selected))
Expand Down

0 comments on commit 9062b0f

Please sign in to comment.