Skip to content
This repository has been archived by the owner on Jun 7, 2020. It is now read-only.

Commit

Permalink
notify member list
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhsherl committed Jan 19, 2019
1 parent 4d9727d commit 3ea8d12
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MembersAdapter(private val listener: (MemberUiModel) -> Unit, presenter: M
ViewHolder(parent.inflate(R.layout.item_member), actionsListener)

override fun onBindViewHolder(holder: ViewHolder, position: Int) =
holder.bind(dataSet[position], listener)
holder.bind(dataSet[position], position, listener)

override fun getItemCount(): Int = dataSet.size

Expand All @@ -40,26 +40,55 @@ class MembersAdapter(private val listener: (MemberUiModel) -> Unit, presenter: M

private val actionsListener = object : ViewHolder.ActionsListener {
override fun isActionsEnabled(): Boolean = enableActions
override fun onActionSelected(item: MenuItem, member: MemberUiModel) {
override fun onActionSelected(item: MenuItem, member: MemberUiModel, index: Int) {
member.apply {
when (item.itemId) {
R.id.action_member_set_owner-> {
presenter?.toggleOwner(this.userId, this.roles?.contains("owner") == true )
val isOwner = this.roles?.contains("owner") == true
presenter?.toggleOwner(this.userId, isOwner) {
if (isOwner)
dataSet[index].roles = dataSet[index].roles?.filterNot { it == "owner"}
else
dataSet[index].roles = dataSet[index].roles?.plus("owner")
notifyItemChanged(index)
}
}
R.id.action_member_set_leader-> {
presenter?.toggleLeader(this.userId, this.roles?.contains("leader") == true)
val isLeader = this.roles?.contains("leader") == true
presenter?.toggleLeader(this.userId, isLeader) {
if (isLeader)
dataSet[index].roles = dataSet[index].roles?.filterNot { it == "leader"}
else
dataSet[index].roles = dataSet[index].roles?.plus("leader")
notifyItemChanged(index)
}
}
R.id.action_member_set_moderator-> {
presenter?.toggleModerator(this.userId, this.roles?.contains("moderator") == true)
val isMod = this.roles?.contains("moderator") == true
presenter?.toggleModerator(this.userId, isMod) {
if (isMod)
dataSet[index].roles = dataSet[index].roles?.filterNot { it == "moderator" }
else
dataSet[index].roles = dataSet[index].roles?.plus("moderator")
notifyItemChanged(index)
}
}
R.id.action_member_ignore-> {
presenter?.toggleIgnore(this.userId, false)
TODO("not implemented")
// presenter?.toggleIgnore(this.userId, false){}
}
R.id.action_member_mute-> {
presenter?.toggleMute(this.username, this.muted)
presenter?.toggleMute(this.username, this.muted) {
dataSet[index].muted = !this.muted
notifyItemChanged(index)
}
}
R.id.action_member_remove-> {
presenter?.removeUser(this.userId)
presenter?.removeUser(this.userId) {
dataSet = dataSet.filterIndexed{ position, _-> position != index }
notifyItemRemoved(index)
notifyItemRangeChanged(index, dataSet.size)
}
}
else -> TODO("Not implemented")
}
Expand Down
24 changes: 14 additions & 10 deletions app/src/main/java/chat/rocket/android/members/adapter/ViewHolder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import androidx.lifecycle.Lifecycle
import androidx.recyclerview.widget.RecyclerView
import chat.rocket.android.R
import chat.rocket.android.chatroom.uimodel.MessageUiModel
import chat.rocket.android.members.ui.GroupMemberBottomSheet
import chat.rocket.android.members.uimodel.MemberUiModel
import chat.rocket.android.util.extensions.content
Expand All @@ -23,13 +23,15 @@ class ViewHolder(
private val listener: ActionsListener
) : RecyclerView.ViewHolder(itemView), MenuItem.OnMenuItemClickListener {
var data: MemberUiModel? = null
var index: Int = 0

// init {
// setupActionMenu(itemView)
// }

fun bind(memberUiModel: MemberUiModel, listener: (MemberUiModel) -> Unit) = with(itemView) {
fun bind(memberUiModel: MemberUiModel, position: Int, listener: (MemberUiModel) -> Unit) = with(itemView) {
data = memberUiModel
index = position
image_avatar.setImageURI(memberUiModel.avatarUri)
text_member.content = memberUiModel.displayName
text_member.setCompoundDrawablesRelativeWithIntrinsicBounds(DrawableHelper.getUserStatusDrawable(memberUiModel.status, context), null, null, null)
Expand All @@ -45,7 +47,7 @@ class ViewHolder(

interface ActionsListener {
fun isActionsEnabled(): Boolean
fun onActionSelected(item: MenuItem, member: MemberUiModel)
fun onActionSelected(item: MenuItem, member: MemberUiModel, index: Int)
}

internal fun setupActionMenu(view: View) {
Expand Down Expand Up @@ -74,12 +76,14 @@ class ViewHolder(
menuItems.find { it.itemId == R.id.action_member_remove }?.apply {
// if (it.roles.contains("owner")) title = "Remove Owner"
}
view.context?.let {
if (it is ContextThemeWrapper && it is AppCompatActivity) {
with(it) {
val actionsBottomSheet = GroupMemberBottomSheet()
actionsBottomSheet.addItems(menuItems, this@ViewHolder)
actionsBottomSheet.show(supportFragmentManager, null)
view.context?.let {
if (it is ContextThemeWrapper && it.baseContext is AppCompatActivity) {
with(it.baseContext as AppCompatActivity) {
if (this.lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)) {
val actionsBottomSheet = GroupMemberBottomSheet()
actionsBottomSheet.addItems(menuItems, this@ViewHolder)
actionsBottomSheet.show(supportFragmentManager, null)
}
}
}
}
Expand All @@ -90,7 +94,7 @@ class ViewHolder(

override fun onMenuItemClick(item: MenuItem): Boolean {
data?.let {
listener.onActionSelected(item, it)
listener.onActionSelected(item, it, index)
}
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class MembersPresenter @Inject constructor(
private var offset: Long = 0
private lateinit var roomType: RoomType
private lateinit var roomId: String
private var totalMembers: Long = 0
var isRoomOwner: Boolean = false

/**
Expand Down Expand Up @@ -68,6 +69,7 @@ class MembersPresenter @Inject constructor(
it.muted = muted?.find { it == username }.isNotNullNorEmpty()
}
view.showMembers(memberUiModels, members.total)
totalMembers = members.total
offset += 1 * 60L
}.ifNull {
Timber.e("Couldn't find a room with id: $roomId at current server.")
Expand All @@ -88,64 +90,64 @@ class MembersPresenter @Inject constructor(
navigator.toMemberDetails(memberUiModel.userId)
}

fun toggleOwner(userId: String, isOwner: Boolean = false) {
fun toggleOwner(userId: String, isOwner: Boolean = false, notifier: () ->Unit) {
launchUI(strategy) {
try {
if (isOwner)
retryIO(description = "removeOwner($roomId, $roomType, $userId)") { client.removeOwner(roomId, roomType, userId) }
else
retryIO(description = "addOwner($roomId, $roomType, $userId)") { client.addOwner(roomId, roomType, userId) }
offset = 0
navigator.toMembersList(roomId)
notifier()
} catch (ex: RocketChatException) {
view.showMessage(ex.message!!) // TODO Remove.
Timber.e(ex) // FIXME: Right now we are only catching the exception with Timber.
}
}
}

fun toggleLeader(userId: String, isLeader: Boolean = false) {
fun toggleLeader(userId: String, isLeader: Boolean = false, notifier: () ->Unit) {
launchUI(strategy) {
try {
if (isLeader)
retryIO(description = "removeLeader($roomId, $roomType, $userId)") { client.removeLeader(roomId, roomType, userId) }
else
retryIO(description = "addLeader($roomId, $roomType, $userId)") { client.addLeader(roomId, roomType, userId) }
navigator.toMembersList(roomId)
notifier()
} catch (ex: RocketChatException) {
view.showMessage(ex.message!!) // TODO Remove.
Timber.e(ex) // FIXME: Right now we are only catching the exception with Timber.
}
}
}

fun toggleModerator(userId: String, isModerator: Boolean = false) {
fun toggleModerator(userId: String, isModerator: Boolean = false, notifier: () ->Unit) {
launchUI(strategy) {
try {
if (isModerator)
retryIO(description = "removeModerator($roomId, $roomType, $userId)") { client.removeModerator(roomId, roomType, userId) }
else
retryIO(description = "addModerator($roomId, $roomType, $userId)") { client.addModerator(roomId, roomType, userId) }
navigator.toMembersList(roomId)
notifier()
} catch (ex: RocketChatException) {
view.showMessage(ex.message!!) // TODO Remove.
Timber.e(ex) // FIXME: Right now we are only catching the exception with Timber.
}
}
}

fun toggleIgnore(userId: String, isIgnored: Boolean = false) {
fun toggleIgnore(userId: String, isIgnored: Boolean = false, notifier: () ->Unit) {
launchUI(strategy) {
try {
retryIO(description = "ignoreUser($roomId, $userId, ${!isIgnored})") { client.ignoreUser(roomId, userId, !isIgnored) }
notifier()
} catch (ex: RocketChatException) {
view.showMessage(ex.message!!) // TODO Remove.
Timber.e(ex) // FIXME: Right now we are only catching the exception with Timber.
}
}
}

fun toggleMute(username: String?, isMuted: Boolean = false) {
fun toggleMute(username: String?, isMuted: Boolean = false, notifier: () ->Unit) {
launchUI(strategy) {
try {
if (isMuted)
Expand All @@ -156,17 +158,20 @@ class MembersPresenter @Inject constructor(
retryIO("runCommand(mute, $username, $roomId)") {
client.runCommand(Command("mute", username), roomId)
}
notifier()
} catch (ex: RocketChatException) {
view.showMessage(ex.message!!) // TODO Remove.
Timber.e(ex) // FIXME: Right now we are only catching the exception with Timber.
}
}
}

fun removeUser(userId: String) {
fun removeUser(userId: String, notifier: () ->Unit) {
launchUI(strategy) {
try {
retryIO(description = "removeUser($roomId, $roomType, $userId)") { client.removeUser(roomId, roomType, userId) }
notifier()
view.setMemberCount(--totalMembers)
} catch (ex: RocketChatException) {
view.showMessage(ex.message!!) // TODO Remove.
Timber.e(ex) // FIXME: Right now we are only catching the exception with Timber.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ interface MembersView: LoadingView, MessageView {
* @param total The total number of members.
*/
fun showMembers(dataSet: List<MemberUiModel>, total: Long)

/**
* Set count of members of a room.
*
* @param totalMembers The total number of members.
*/
fun setMemberCount(totalMembers: Long?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ class MembersFragment : Fragment(), MembersView {
ui { view_loading.isVisible = true }
}

override fun setMemberCount(totalMembers: Long?){
setupToolbar(totalMembers)
}

override fun hideLoading() {
ui { view_loading.isVisible = false }
}
Expand Down

0 comments on commit 3ea8d12

Please sign in to comment.