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

Commit

Permalink
filter menuitem on roles
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhsherl committed Feb 10, 2019
1 parent 0e384f0 commit 8282965
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class ChatDetailsPresenter @Inject constructor(
navigator.toFileList(chatRoomId)
}

fun toMembers(chatRoomId: String) {
navigator.toMembersList(chatRoomId)
fun toMembers(chatRoomId: String, isOwner: Boolean, isMod: Boolean) {
navigator.toMembersList(chatRoomId, isOwner, isMod)
}

fun toMentions(chatRoomId: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,27 @@ fun newInstance(
chatRoomId: String,
chatRoomType: String,
isSubscribed: Boolean,
disableMenu: Boolean
disableMenu: Boolean,
isOwner: Boolean,
isMod: Boolean
): ChatDetailsFragment {
return ChatDetailsFragment().apply {
arguments = Bundle(4).apply {
putString(BUNDLE_CHAT_ROOM_ID, chatRoomId)
putString(BUNDLE_CHAT_ROOM_TYPE, chatRoomType)
putBoolean(BUNDLE_IS_SUBSCRIBED, isSubscribed)
putBoolean(BUNDLE_DISABLE_MENU, disableMenu)
putBoolean(BUNDLE_CHAT_ROOM_OWNER, isOwner)
putBoolean(BUNDLE_CHAT_ROOM_MOD, isMod)
}
}
}

internal const val TAG_CHAT_DETAILS_FRAGMENT = "ChatDetailsFragment"

private const val BUNDLE_CHAT_ROOM_ID = "BUNDLE_CHAT_ROOM_ID"
private const val BUNDLE_CHAT_ROOM_MOD = "BUNDLE_CHAT_ROOM_MOD"
private const val BUNDLE_CHAT_ROOM_OWNER = "BUNDLE_CHAT_ROOM_OWNER"
private const val BUNDLE_CHAT_ROOM_TYPE = "BUNDLE_CHAT_ROOM_TYPE"
private const val BUNDLE_IS_SUBSCRIBED = "BUNDLE_IS_SUBSCRIBED"
private const val BUNDLE_DISABLE_MENU = "BUNDLE_DISABLE_MENU"
Expand All @@ -61,6 +67,8 @@ class ChatDetailsFragment : Fragment(), ChatDetailsView {
private var chatRoomId: String? = null
private var chatRoomType: String? = null
private var isSubscribed: Boolean = true
private var isOwner: Boolean = false
private var isMod: Boolean = false
private var disableMenu: Boolean = false

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -72,6 +80,8 @@ class ChatDetailsFragment : Fragment(), ChatDetailsView {
chatRoomType = bundle.getString(BUNDLE_CHAT_ROOM_TYPE)
isSubscribed = bundle.getBoolean(BUNDLE_IS_SUBSCRIBED)
disableMenu = bundle.getBoolean(BUNDLE_DISABLE_MENU)
isOwner = bundle.getBoolean(BUNDLE_CHAT_ROOM_OWNER)
isMod = bundle.getBoolean(BUNDLE_CHAT_ROOM_MOD)
} else {
requireNotNull(bundle) { "no arguments supplied when the fragment was instantiated" }
}
Expand Down Expand Up @@ -135,7 +145,7 @@ class ChatDetailsFragment : Fragment(), ChatDetailsView {
getString(R.string.title_members),
R.drawable.ic_people_outline_black_24dp
) {
presenter.toMembers(chatRoomId!!)
presenter.toMembers(chatRoomId!!, isOwner, isMod)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,25 @@ class ChatRoomNavigator(internal val activity: ChatRoomActivity) {
chatRoomId: String,
chatRoomType: String,
isChatRoomSubscribed: Boolean,
isMenuDisabled: Boolean
isMenuDisabled: Boolean,
isOwner: Boolean,
isModerator: Boolean
) {
activity.addFragmentBackStack(TAG_CHAT_DETAILS_FRAGMENT, R.id.fragment_container) {
chat.rocket.android.chatdetails.ui.newInstance(
chatRoomId,
chatRoomType,
isChatRoomSubscribed,
isMenuDisabled
isMenuDisabled,
isOwner,
isModerator
)
}
}

fun toMembersList(chatRoomId: String) {
fun toMembersList(chatRoomId: String, isOwner: Boolean, isMod: Boolean) {
activity.addFragmentBackStack(TAG_MEMBERS_FRAGMENT, R.id.fragment_container) {
chat.rocket.android.members.ui.newInstance(chatRoomId)
chat.rocket.android.members.ui.newInstance(chatRoomId, isOwner, isMod)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package chat.rocket.android.chatroom.presentation

import android.graphics.Bitmap
import android.net.Uri
import android.util.Log
import chat.rocket.android.R
import chat.rocket.android.analytics.AnalyticsManager
import chat.rocket.android.analytics.event.SubscriptionTypeEvent
Expand Down Expand Up @@ -142,16 +141,12 @@ class ChatRoomPresenter @Inject constructor(
} else {
emptyList()
}
Log.d("OPOPOP",chatRoles.toString())
} catch (ex: Exception) {
Timber.e(ex)
chatRoles = emptyList()
} finally {
// User has at least an 'owner' or 'moderator' role.
val isOwner = isOwner()
val isMod= isModerator()
val isLeader = isLeader()
// val canModerate = isOwnerOrMod()
val canModerate = isOwner() || isModerator()
// Can post anyway if has the 'post-readonly' permission on server.
val room = dbManager.getRoom(roomId)
room?.let {
Expand All @@ -160,11 +155,8 @@ class ChatRoomPresenter @Inject constructor(
launchUI(strategy) {
view.onRoomUpdated(roomUiModel = roomUiModel.copy(
broadcast = chatIsBroadcast,
isOwner = isOwner,
isLeader = isLeader,
isMod = isMod,
canModerate = isOwner || isMod,
writable = roomUiModel.writable || isOwner || isLeader
canModerate = canModerate,
writable = roomUiModel.writable || canModerate
))
}
}
Expand Down Expand Up @@ -202,12 +194,6 @@ class ChatRoomPresenter @Inject constructor(
chatRoomId?.let { manager.removeRoomChannel(it) }
}

// private fun isOwnerOrMod(): Boolean {
// return chatRoles.firstOrNull { it.user.username == currentLoggedUsername }?.roles?.any {
// it == "owner" || it == "moderator"
// } ?: false
// }

private fun isOwner(): Boolean {
return chatRoles.find { it.user.username == currentLoggedUsername }?.roles?.any {
it == "owner"
Expand All @@ -220,12 +206,6 @@ class ChatRoomPresenter @Inject constructor(
} ?: false
}

private fun isLeader(): Boolean {
return chatRoles.find { it.user.username == currentLoggedUsername }?.roles?.any {
it == "leader"
} ?: false
}

fun loadMessages(
chatRoomId: String,
chatRoomType: String,
Expand Down Expand Up @@ -940,7 +920,7 @@ class ChatRoomPresenter @Inject constructor(
isSubscribed: Boolean,
isMenuDisabled: Boolean
) {
navigator.toChatDetails(chatRoomId, chatRoomType, isSubscribed, isMenuDisabled)
navigator.toChatDetails(chatRoomId, chatRoomType, isSubscribed, isMenuDisabled, isOwner(), isModerator())
}

fun loadChatRoomsSuggestions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ data class RoomUiModel(
val status: UserStatus? = null,
val username: String? = null,
val broadcast: Boolean = false,
val isOwner: Boolean = false,
val isLeader: Boolean = false,
val isMod: Boolean = false,
val canModerate: Boolean = false,
val writable: Boolean = true,
val muted: List<String> = emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class CreateChannelFragment : Fragment(), CreateChannelView, ActionMode.Callback
if (it.username != null) {
processSelectedMember(it.username)
}
}, null)
}, null, true, false)
private val compositeDisposable = CompositeDisposable()
private var channelType: String = RoomType.CHANNEL
private var isChannelReadOnly: Boolean = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import chat.rocket.android.members.presentation.MembersPresenter
import chat.rocket.android.members.uimodel.MemberUiModel
import chat.rocket.android.util.extensions.inflate

class MembersAdapter(private val listener: (MemberUiModel) -> Unit, presenter: MembersPresenter?) :
class MembersAdapter(private val listener: (MemberUiModel) -> Unit, presenter: MembersPresenter?, private val isOwner: Boolean, private val isMod: Boolean) :
RecyclerView.Adapter<ViewHolder>() {
private var dataSet: List<MemberUiModel> = ArrayList()
private val enableActions: Boolean = true

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
ViewHolder(parent.inflate(R.layout.item_member), actionsListener)
ViewHolder(parent.inflate(R.layout.item_member), actionsListener, isOwner, isMod)

override fun onBindViewHolder(holder: ViewHolder, position: Int) =
holder.bind(dataSet[position], position, listener)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ import kotlinx.android.synthetic.main.item_member.view.*

class ViewHolder(
itemView: View,
private val listener: ActionsListener
private val listener: ActionsListener,
private val isOwner: Boolean,
private val isMod: Boolean
) : RecyclerView.ViewHolder(itemView), MenuItem.OnMenuItemClickListener {
var data: MemberUiModel? = null
var index: Int = 0

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

fun bind(memberUiModel: MemberUiModel, position: Int, listener: (MemberUiModel) -> Unit) = with(itemView) {
data = memberUiModel
index = position
Expand All @@ -54,8 +52,11 @@ class ViewHolder(
view.setOnLongClickListener{
data?.let {
var menuItems = view.context.inflate(R.menu.group_member_actions).toList()
// if(!isRoomOwner) menuItems = menuItems.filter { it.itemId == R.id.action_member_mute || it.itemId == R.id.action_member_ignore }
menuItems.find { it.itemId == R.id.action_member_set_owner }?.apply {
if (!isOwner && !isMod)
menuItems = menuItems.filter { it.itemId == R.id.action_member_mute || it.itemId == R.id.action_member_ignore }
else if (isMod)
menuItems = menuItems.filter { it.itemId == R.id.action_member_mute || it.itemId == R.id.action_member_ignore || it.itemId == R.id.action_member_remove}
menuItems.find { it.itemId == R.id.action_member_set_owner }?.apply {
if (it.roles?.contains("owner") == true) title = "Remove as Owner"
}
menuItems.find { it.itemId == R.id.action_member_set_leader }?.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ import kotlinx.android.synthetic.main.app_bar_chat_room.*
import kotlinx.android.synthetic.main.fragment_members.*
import javax.inject.Inject

fun newInstance(chatRoomId: String): Fragment {
fun newInstance(chatRoomId: String, isOwner: Boolean, isMod: Boolean): Fragment {
return MembersFragment().apply {
arguments = Bundle(1).apply {
putString(BUNDLE_CHAT_ROOM_ID, chatRoomId)
putBoolean(BUNDLE_CHAT_ROOM_OWNER, isOwner)
putBoolean(BUNDLE_CHAT_ROOM_MOD, isMod)
}
}
}

internal const val TAG_MEMBERS_FRAGMENT = "MembersFragment"
private const val BUNDLE_CHAT_ROOM_ID = "chat_room_id"
private const val BUNDLE_CHAT_ROOM_MOD = "chat_room_mod"
private const val BUNDLE_CHAT_ROOM_OWNER = "chat_room_owner"

class MembersFragment : Fragment(), MembersView {
@Inject
Expand All @@ -46,6 +50,8 @@ class MembersFragment : Fragment(), MembersView {
private lateinit var adapter: MembersAdapter
private val linearLayoutManager = LinearLayoutManager(context)
private lateinit var chatRoomId: String
private var isOwner: Boolean = false
private var isMod: Boolean = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -54,6 +60,9 @@ class MembersFragment : Fragment(), MembersView {
val bundle = arguments
if (bundle != null) {
chatRoomId = bundle.getString(BUNDLE_CHAT_ROOM_ID)
isOwner = bundle.getBoolean(BUNDLE_CHAT_ROOM_OWNER)
isMod = bundle.getBoolean(BUNDLE_CHAT_ROOM_MOD)

} else {
requireNotNull(bundle) { "no arguments supplied when the fragment was instantiated" }
}
Expand Down Expand Up @@ -125,7 +134,7 @@ class MembersFragment : Fragment(), MembersView {

private fun setupRecyclerView() {
ui {
adapter = MembersAdapter ({ memberUiModel -> presenter.toMemberDetails(memberUiModel) }, presenter)
adapter = MembersAdapter ({ memberUiModel -> presenter.toMemberDetails(memberUiModel) }, presenter, isOwner, isMod)
recycler_view.layoutManager = LinearLayoutManager(context)
recycler_view.addItemDecoration(
DividerItemDecoration(
Expand Down

0 comments on commit 8282965

Please sign in to comment.