Kotlin style

This commit is contained in:
Benoit Marty 2019-08-05 17:35:26 +02:00
parent 097e9714ff
commit f2a3bdb68e
4 changed files with 103 additions and 115 deletions

View File

@ -821,106 +821,101 @@ class RoomDetailFragment :
textComposerViewModel.process(TextComposerActions.QueryUsers(query)) textComposerViewModel.process(TextComposerActions.QueryUsers(query))
} }
private fun handleActions(actionData: ActionsHandler.ActionData) { private fun handleActions(action: SimpleAction) {
when (actionData.actionId) { when (action) {
MessageMenuViewModel.ACTION_ADD_REACTION -> { is SimpleAction.AddReaction -> {
val eventId = actionData.data?.toString() ?: return startActivityForResult(EmojiReactionPickerActivity.intent(requireContext(), action.eventId), REACTION_SELECT_REQUEST_CODE)
startActivityForResult(EmojiReactionPickerActivity.intent(requireContext(), eventId), REACTION_SELECT_REQUEST_CODE)
} }
MessageMenuViewModel.ACTION_VIEW_REACTIONS -> { is SimpleAction.ViewReactions -> {
val messageInformationData = actionData.data as? MessageInformationData ViewReactionBottomSheet.newInstance(roomDetailArgs.roomId, action.messageInformationData)
?: return
ViewReactionBottomSheet.newInstance(roomDetailArgs.roomId, messageInformationData)
.show(requireActivity().supportFragmentManager, "DISPLAY_REACTIONS") .show(requireActivity().supportFragmentManager, "DISPLAY_REACTIONS")
} }
MessageMenuViewModel.ACTION_COPY -> { is SimpleAction.Copy -> {
//I need info about the current selected message :/ //I need info about the current selected message :/
copyToClipboard(requireContext(), actionData.data?.toString() ?: "", false) copyToClipboard(requireContext(), action.content, false)
val msg = requireContext().getString(R.string.copied_to_clipboard) val msg = requireContext().getString(R.string.copied_to_clipboard)
showSnackWithMessage(msg, Snackbar.LENGTH_SHORT) showSnackWithMessage(msg, Snackbar.LENGTH_SHORT)
} }
MessageMenuViewModel.ACTION_DELETE -> { is SimpleAction.Delete -> {
val eventId = actionData.data?.toString() ?: return roomDetailViewModel.process(RoomDetailActions.RedactAction(action.eventId, context?.getString(R.string.event_redacted_by_user_reason)))
roomDetailViewModel.process(RoomDetailActions.RedactAction(eventId, context?.getString(R.string.event_redacted_by_user_reason)))
} }
MessageMenuViewModel.ACTION_SHARE -> { is SimpleAction.Share -> {
//TODO current data communication is too limited //TODO current data communication is too limited
//Need to now the media type //Need to now the media type
actionData.data?.toString()?.let { //TODO bad, just POC
//TODO bad, just POC BigImageViewer.imageLoader().loadImage(
BigImageViewer.imageLoader().loadImage( action.hashCode(),
actionData.hashCode(), Uri.parse(action.imageUrl),
Uri.parse(it), object : ImageLoader.Callback {
object : ImageLoader.Callback { override fun onFinish() {}
override fun onFinish() {}
override fun onSuccess(image: File?) {
if (image != null)
shareMedia(requireContext(), image, "image/*")
}
override fun onFail(error: Exception?) {}
override fun onCacheHit(imageType: Int, image: File?) {}
override fun onCacheMiss(imageType: Int, image: File?) {}
override fun onProgress(progress: Int) {}
override fun onStart() {}
override fun onSuccess(image: File?) {
if (image != null)
shareMedia(requireContext(), image, "image/*")
} }
) override fun onFail(error: Exception?) {}
}
override fun onCacheHit(imageType: Int, image: File?) {}
override fun onCacheMiss(imageType: Int, image: File?) {}
override fun onProgress(progress: Int) {}
override fun onStart() {}
}
)
} }
MessageMenuViewModel.VIEW_SOURCE, is SimpleAction.ViewSource -> {
MessageMenuViewModel.VIEW_DECRYPTED_SOURCE -> {
val view = LayoutInflater.from(requireContext()).inflate(R.layout.dialog_event_content, null) val view = LayoutInflater.from(requireContext()).inflate(R.layout.dialog_event_content, null)
view.findViewById<TextView>(R.id.event_content_text_view)?.let { view.findViewById<TextView>(R.id.event_content_text_view)?.let {
it.text = actionData.data?.toString() ?: "" it.text = action.content
} }
AlertDialog.Builder(requireActivity()) AlertDialog.Builder(requireActivity())
.setView(view) .setView(view)
.setPositiveButton(R.string.ok) { dialog, id -> dialog.cancel() } .setPositiveButton(R.string.ok, null)
.show() .show()
} }
MessageMenuViewModel.ACTION_QUICK_REACT -> { is SimpleAction.ViewDecryptedSource -> {
//eventId,ClickedOn,Add val view = LayoutInflater.from(requireContext()).inflate(R.layout.dialog_event_content, null)
(actionData.data as? Triple<String, String, Boolean>)?.let { (eventId, clickedOn, add) -> view.findViewById<TextView>(R.id.event_content_text_view)?.let {
roomDetailViewModel.process(RoomDetailActions.UpdateQuickReactAction(eventId, clickedOn, add)) it.text = action.content
} }
AlertDialog.Builder(requireActivity())
.setView(view)
.setPositiveButton(R.string.ok, null)
.show()
} }
MessageMenuViewModel.ACTION_EDIT -> { is SimpleAction.QuickReact -> {
val eventId = actionData.data.toString() //eventId,ClickedOn,Add
roomDetailViewModel.process(RoomDetailActions.EnterEditMode(eventId)) roomDetailViewModel.process(RoomDetailActions.UpdateQuickReactAction(action.eventId, action.clickedOn, action.add))
} }
MessageMenuViewModel.ACTION_QUOTE -> { is SimpleAction.Edit -> {
val eventId = actionData.data.toString() roomDetailViewModel.process(RoomDetailActions.EnterEditMode(action.eventId))
roomDetailViewModel.process(RoomDetailActions.EnterQuoteMode(eventId))
} }
MessageMenuViewModel.ACTION_REPLY -> { is SimpleAction.Quote -> {
val eventId = actionData.data.toString() roomDetailViewModel.process(RoomDetailActions.EnterQuoteMode(action.eventId))
roomDetailViewModel.process(RoomDetailActions.EnterReplyMode(eventId))
} }
MessageMenuViewModel.ACTION_COPY_PERMALINK -> { is SimpleAction.Reply -> {
val eventId = actionData.data.toString() roomDetailViewModel.process(RoomDetailActions.EnterReplyMode(action.eventId))
val permalink = PermalinkFactory.createPermalink(roomDetailArgs.roomId, eventId) }
is SimpleAction.CopyPermalink -> {
val permalink = PermalinkFactory.createPermalink(roomDetailArgs.roomId, action.eventId)
copyToClipboard(requireContext(), permalink, false) copyToClipboard(requireContext(), permalink, false)
showSnackWithMessage(requireContext().getString(R.string.copied_to_clipboard), Snackbar.LENGTH_SHORT) showSnackWithMessage(requireContext().getString(R.string.copied_to_clipboard), Snackbar.LENGTH_SHORT)
} }
MessageMenuViewModel.ACTION_RESEND -> { is SimpleAction.Resend -> {
val eventId = actionData.data.toString() roomDetailViewModel.process(RoomDetailActions.ResendMessage(action.eventId))
roomDetailViewModel.process(RoomDetailActions.ResendMessage(eventId))
} }
MessageMenuViewModel.ACTION_REMOVE -> { is SimpleAction.Remove -> {
val eventId = actionData.data.toString() roomDetailViewModel.process(RoomDetailActions.RemoveFailedEcho(action.eventId))
roomDetailViewModel.process(RoomDetailActions.RemoveFailedEcho(eventId))
} }
else -> { else -> {
Toast.makeText(context, "Action ${actionData.actionId} not implemented", Toast.LENGTH_LONG).show() Toast.makeText(context, "Action $action is not implemented yet", Toast.LENGTH_LONG).show()
} }
} }
} }

View File

@ -17,6 +17,7 @@ package im.vector.riotx.features.home.room.detail.timeline.action
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import im.vector.riotx.core.extensions.postLiveEvent
import im.vector.riotx.core.utils.LiveEvent import im.vector.riotx.core.utils.LiveEvent
import javax.inject.Inject import javax.inject.Inject
@ -25,15 +26,10 @@ import javax.inject.Inject
*/ */
class ActionsHandler @Inject constructor() : ViewModel() { class ActionsHandler @Inject constructor() : ViewModel() {
data class ActionData( val actionCommandEvent = MutableLiveData<LiveEvent<SimpleAction>>()
val actionId: String,
val data: Any?
)
val actionCommandEvent = MutableLiveData<LiveEvent<ActionData>>() fun fireAction(action: SimpleAction) {
actionCommandEvent.postLiveEvent(action)
fun fireAction(actionId: String, data: Any? = null) {
actionCommandEvent.value = LiveEvent(ActionData(actionId,data))
} }
} }

View File

@ -89,7 +89,7 @@ class MessageActionsBottomSheet : VectorBaseBottomSheetDialogFragment() {
} }
menuActionFragment.interactionListener = object : MessageMenuFragment.InteractionListener { menuActionFragment.interactionListener = object : MessageMenuFragment.InteractionListener {
override fun didSelectMenuAction(simpleAction: SimpleAction) { override fun didSelectMenuAction(simpleAction: SimpleAction) {
actionHandlerModel.fireAction(simpleAction.uid, simpleAction.data) actionHandlerModel.fireAction(simpleAction)
dismiss() dismiss()
} }
} }
@ -105,7 +105,7 @@ class MessageActionsBottomSheet : VectorBaseBottomSheetDialogFragment() {
quickReactionFragment.interactionListener = object : QuickReactionFragment.InteractionListener { quickReactionFragment.interactionListener = object : QuickReactionFragment.InteractionListener {
override fun didQuickReactWith(clickedOn: String, add: Boolean, eventId: String) { override fun didQuickReactWith(clickedOn: String, add: Boolean, eventId: String) {
actionHandlerModel.fireAction(MessageMenuViewModel.ACTION_QUICK_REACT, Triple(eventId, clickedOn, add)) actionHandlerModel.fireAction(SimpleAction.QuickReact(eventId, clickedOn, add))
dismiss() dismiss()
} }
} }

View File

@ -15,6 +15,8 @@
*/ */
package im.vector.riotx.features.home.room.detail.timeline.action package im.vector.riotx.features.home.room.detail.timeline.action
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import com.airbnb.mvrx.* import com.airbnb.mvrx.*
import com.squareup.inject.assisted.Assisted import com.squareup.inject.assisted.Assisted
import com.squareup.inject.assisted.AssistedInject import com.squareup.inject.assisted.AssistedInject
@ -36,7 +38,24 @@ import im.vector.riotx.core.utils.isSingleEmoji
import im.vector.riotx.features.home.room.detail.timeline.item.MessageInformationData import im.vector.riotx.features.home.room.detail.timeline.item.MessageInformationData
data class SimpleAction(val uid: String, val titleRes: Int, val iconResId: Int?, val data: Any? = null) sealed class SimpleAction(@StringRes val titleRes: Int, @DrawableRes val iconResId: Int) {
data class AddReaction(val eventId: String) : SimpleAction(R.string.message_add_reaction, R.drawable.ic_add_reaction)
data class Copy(val content: String) : SimpleAction(R.string.copy, R.drawable.ic_copy)
data class Edit(val eventId: String) : SimpleAction(R.string.edit, R.drawable.ic_edit)
data class Quote(val eventId: String) : SimpleAction(R.string.quote, R.drawable.ic_quote)
data class Reply(val eventId: String) : SimpleAction(R.string.reply, R.drawable.ic_reply)
data class Share(val imageUrl: String?) : SimpleAction(R.string.share, R.drawable.ic_share)
data class Resend(val eventId: String) : SimpleAction(R.string.global_retry, R.drawable.ic_refresh_cw)
data class Remove(val eventId: String) : SimpleAction(R.string.remove, R.drawable.ic_trash)
data class Delete(val eventId: String) : SimpleAction(R.string.delete, R.drawable.ic_delete)
data class Cancel(val eventId: String) : SimpleAction(R.string.cancel, R.drawable.ic_close_round)
data class ViewSource(val content: String) : SimpleAction(R.string.view_source, R.drawable.ic_view_source)
data class ViewDecryptedSource(val content: String) : SimpleAction(R.string.view_decrypted_source, R.drawable.ic_view_source)
data class CopyPermalink(val eventId: String) : SimpleAction(R.string.permalink, R.drawable.ic_permalink)
data class Flag(val eventId: String) : SimpleAction(R.string.report_content, R.drawable.ic_flag)
data class QuickReact(val eventId: String, val clickedOn: String, val add: Boolean) : SimpleAction(0, 0)
data class ViewReactions(val messageInformationData: MessageInformationData) : SimpleAction(R.string.message_view_reaction, R.drawable.ic_view_reactions)
}
data class MessageMenuState( data class MessageMenuState(
val roomId: String, val roomId: String,
@ -68,24 +87,6 @@ class MessageMenuViewModel @AssistedInject constructor(@Assisted initialState: M
private val informationData: MessageInformationData = initialState.informationData private val informationData: MessageInformationData = initialState.informationData
companion object : MvRxViewModelFactory<MessageMenuViewModel, MessageMenuState> { companion object : MvRxViewModelFactory<MessageMenuViewModel, MessageMenuState> {
const val ACTION_ADD_REACTION = "add_reaction"
const val ACTION_COPY = "copy"
const val ACTION_EDIT = "edit"
const val ACTION_QUOTE = "quote"
const val ACTION_REPLY = "reply"
const val ACTION_SHARE = "share"
const val ACTION_RESEND = "resend"
const val ACTION_REMOVE = "remove"
const val ACTION_DELETE = "delete"
const val ACTION_CANCEL = "cancel"
const val VIEW_SOURCE = "VIEW_SOURCE"
const val VIEW_DECRYPTED_SOURCE = "VIEW_DECRYPTED_SOURCE"
const val ACTION_COPY_PERMALINK = "ACTION_COPY_PERMALINK"
const val ACTION_FLAG = "ACTION_FLAG"
const val ACTION_QUICK_REACT = "ACTION_QUICK_REACT"
const val ACTION_VIEW_REACTIONS = "ACTION_VIEW_REACTIONS"
override fun create(viewModelContext: ViewModelContext, state: MessageMenuState): MessageMenuViewModel? { override fun create(viewModelContext: ViewModelContext, state: MessageMenuState): MessageMenuViewModel? {
val fragment: MessageMenuFragment = (viewModelContext as FragmentViewModelContext).fragment() val fragment: MessageMenuFragment = (viewModelContext as FragmentViewModelContext).fragment()
return fragment.messageMenuViewModelFactory.create(state) return fragment.messageMenuViewModelFactory.create(state)
@ -115,52 +116,48 @@ class MessageMenuViewModel @AssistedInject constructor(@Assisted initialState: M
return arrayListOf<SimpleAction>().apply { return arrayListOf<SimpleAction>().apply {
if (event.root.sendState.hasFailed()) { if (event.root.sendState.hasFailed()) {
if (canRetry(event)) { if (canRetry(event)) {
add(SimpleAction(ACTION_RESEND, R.string.global_retry, R.drawable.ic_refresh_cw, eventId)) add(SimpleAction.Resend(eventId))
} }
add(SimpleAction(ACTION_REMOVE, R.string.remove, R.drawable.ic_trash, eventId)) add(SimpleAction.Remove(eventId))
} else if (event.root.sendState.isSending()) { } else if (event.root.sendState.isSending()) {
//TODO is uploading attachment? //TODO is uploading attachment?
if (canCancel(event)) { if (canCancel(event)) {
add(SimpleAction(ACTION_CANCEL, R.string.cancel, R.drawable.ic_close_round, eventId)) add(SimpleAction.Cancel(eventId))
} }
} else { } else {
if (!event.root.isRedacted()) { if (!event.root.isRedacted()) {
if (canReply(event, messageContent)) { if (canReply(event, messageContent)) {
add(SimpleAction(ACTION_REPLY, R.string.reply, R.drawable.ic_reply, eventId)) add(SimpleAction.Reply(eventId))
} }
if (canEdit(event, session.myUserId)) { if (canEdit(event, session.myUserId)) {
add(SimpleAction(ACTION_EDIT, R.string.edit, R.drawable.ic_edit, eventId)) add(SimpleAction.Edit(eventId))
} }
if (canRedact(event, session.myUserId)) { if (canRedact(event, session.myUserId)) {
add(SimpleAction(ACTION_DELETE, R.string.delete, R.drawable.ic_delete, eventId)) add(SimpleAction.Delete(eventId))
} }
if (canCopy(type)) { if (canCopy(type)) {
//TODO copy images? html? see ClipBoard //TODO copy images? html? see ClipBoard
add(SimpleAction(ACTION_COPY, R.string.copy, R.drawable.ic_copy, messageContent!!.body)) add(SimpleAction.Copy(messageContent!!.body))
} }
if (event.canReact()) { if (event.canReact()) {
add(SimpleAction(ACTION_ADD_REACTION, R.string.message_add_reaction, R.drawable.ic_add_reaction, eventId)) add(SimpleAction.AddReaction(eventId))
} }
if (canQuote(event, messageContent)) { if (canQuote(event, messageContent)) {
add(SimpleAction(ACTION_QUOTE, R.string.quote, R.drawable.ic_quote, eventId)) add(SimpleAction.Quote(eventId))
} }
if (canViewReactions(event)) { if (canViewReactions(event)) {
add(SimpleAction(ACTION_VIEW_REACTIONS, R.string.message_view_reaction, R.drawable.ic_view_reactions, informationData)) add(SimpleAction.ViewReactions(informationData))
} }
if (canShare(type)) { if (canShare(type)) {
if (messageContent is MessageImageContent) { if (messageContent is MessageImageContent) {
add( add(SimpleAction.Share(session.contentUrlResolver().resolveFullSize(messageContent.url)))
SimpleAction(ACTION_SHARE,
R.string.share, R.drawable.ic_share,
session.contentUrlResolver().resolveFullSize(messageContent.url))
)
} }
//TODO //TODO
} }
@ -174,17 +171,17 @@ class MessageMenuViewModel @AssistedInject constructor(@Assisted initialState: M
} }
} }
add(SimpleAction(VIEW_SOURCE, R.string.view_source, R.drawable.ic_view_source, event.root.toContentStringWithIndent())) add(SimpleAction.ViewSource(event.root.toContentStringWithIndent()))
if (event.isEncrypted()) { if (event.isEncrypted()) {
val decryptedContent = event.root.toClearContentStringWithIndent() val decryptedContent = event.root.toClearContentStringWithIndent()
?: stringProvider.getString(R.string.encryption_information_decryption_error) ?: stringProvider.getString(R.string.encryption_information_decryption_error)
add(SimpleAction(VIEW_DECRYPTED_SOURCE, R.string.view_decrypted_source, R.drawable.ic_view_source, decryptedContent)) add(SimpleAction.ViewDecryptedSource(decryptedContent))
} }
add(SimpleAction(ACTION_COPY_PERMALINK, R.string.permalink, R.drawable.ic_permalink, event.root.eventId)) add(SimpleAction.CopyPermalink(eventId))
if (session.myUserId != event.root.senderId && event.root.getClearType() == EventType.MESSAGE) { if (session.myUserId != event.root.senderId && event.root.getClearType() == EventType.MESSAGE) {
//not sent by me //not sent by me
add(SimpleAction(ACTION_FLAG, R.string.report_content, R.drawable.ic_flag, event.root.eventId)) add(SimpleAction.Flag(eventId))
} }
} }
} }