Code review fixes.

This commit is contained in:
onurays 2020-02-14 15:04:25 +03:00
parent fd135e1eeb
commit 030d6824e3
3 changed files with 42 additions and 35 deletions

View file

@ -157,6 +157,11 @@ data class Event(
*/
fun isRedacted() = unsignedData?.redactedEvent != null
/**
* Tells if the event is redacted by the user himself.
*/
fun isRedactedBySameUser() = senderId == unsignedData?.redactedEvent?.senderId
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

View file

@ -788,14 +788,14 @@ class RoomDetailFragment @Inject constructor(
.show()
}
private fun promptConfirmationToRedactEvent(eventId: String, askForReason: Boolean) {
private fun promptConfirmationToRedactEvent(action: EventSharedAction.Redact) {
val layout = requireActivity().layoutInflater.inflate(R.layout.dialog_delete_event, null)
val reasonCheckBox = layout.findViewById<MaterialCheckBox>(R.id.deleteEventReasonCheck)
val reasonTextInputLayout = layout.findViewById<TextInputLayout>(R.id.deleteEventReasonTextInputLayout)
val reasonInput = layout.findViewById<TextInputEditText>(R.id.deleteEventReasonInput)
reasonCheckBox.isVisible = askForReason
reasonTextInputLayout.isVisible = askForReason
reasonCheckBox.isVisible = action.askForReason
reasonTextInputLayout.isVisible = action.askForReason
reasonCheckBox.setOnCheckedChangeListener { _, isChecked -> reasonTextInputLayout.isEnabled = isChecked }
@ -804,10 +804,10 @@ class RoomDetailFragment @Inject constructor(
.setView(layout)
.setPositiveButton(R.string.remove) { _, _ ->
val reason = reasonInput.text.toString()
.takeIf { askForReason }
.takeIf { action.askForReason }
?.takeIf { reasonCheckBox.isChecked }
?.takeIf { it.isNotBlank() }
roomDetailViewModel.handle(RoomDetailAction.RedactAction(eventId, reason))
roomDetailViewModel.handle(RoomDetailAction.RedactAction(action.eventId, reason))
}
.setNegativeButton(R.string.cancel, null)
.show()
@ -1125,7 +1125,7 @@ class RoomDetailFragment @Inject constructor(
showSnackWithMessage(getString(R.string.copied_to_clipboard), Snackbar.LENGTH_SHORT)
}
is EventSharedAction.Redact -> {
promptConfirmationToRedactEvent(action.eventId, action.askForReason)
promptConfirmationToRedactEvent(action)
}
is EventSharedAction.Share -> {
// TODO current data communication is too limited

View file

@ -168,27 +168,25 @@ class MessageActionsViewModel @AssistedInject constructor(@Assisted
}
private fun computeMessageBody(timelineEvent: TimelineEvent): CharSequence {
if (timelineEvent.root.isRedacted()) {
return getRedactionReason(timelineEvent)
}
return when (timelineEvent.root.getClearType()) {
EventType.MESSAGE,
EventType.ENCRYPTED,
EventType.STICKER -> {
when (timelineEvent.root.isRedacted()) {
true -> getRedactionReason(timelineEvent)
false -> {
val messageContent: MessageContent? = timelineEvent.getLastMessageContent()
if (messageContent is MessageTextContent && messageContent.format == MessageFormat.FORMAT_MATRIX_HTML) {
val html = messageContent.formattedBody
?.takeIf { it.isNotBlank() }
?.let { htmlCompressor.compress(it) }
?: messageContent.body
val messageContent: MessageContent? = timelineEvent.getLastMessageContent()
if (messageContent is MessageTextContent && messageContent.format == MessageFormat.FORMAT_MATRIX_HTML) {
val html = messageContent.formattedBody
?.takeIf { it.isNotBlank() }
?.let { htmlCompressor.compress(it) }
?: messageContent.body
eventHtmlRenderer.get().render(html)
} else if (messageContent is MessageVerificationRequestContent) {
stringProvider.getString(R.string.verification_request)
} else {
messageContent?.body
}
}
eventHtmlRenderer.get().render(html)
} else if (messageContent is MessageVerificationRequestContent) {
stringProvider.getString(R.string.verification_request)
} else {
messageContent?.body
}
}
EventType.STATE_ROOM_NAME,
@ -206,26 +204,30 @@ class MessageActionsViewModel @AssistedInject constructor(@Assisted
} ?: ""
}
private fun getRedactionReason(timelineEvent: TimelineEvent) =
(timelineEvent
private fun getRedactionReason(timelineEvent: TimelineEvent): String {
return (timelineEvent
.root
.unsignedData
?.redactedEvent
?.content
?.get("reason") as? String)
?.takeIf { it.isNotBlank() }
?.let { reason ->
when (timelineEvent.root.senderId == timelineEvent.root.unsignedData?.redactedEvent?.senderId) {
true -> stringProvider.getString(R.string.event_redacted_by_user_reason_with_reason, reason)
false -> stringProvider.getString(R.string.event_redacted_by_admin_reason_with_reason, reason)
}
}
?: run {
when (timelineEvent.root.senderId == timelineEvent.root.unsignedData?.redactedEvent?.senderId) {
true -> stringProvider.getString(R.string.event_redacted_by_user_reason)
false -> stringProvider.getString(R.string.event_redacted_by_admin_reason)
.let { reason ->
if (reason == null) {
if (timelineEvent.root.isRedactedBySameUser()) {
stringProvider.getString(R.string.event_redacted_by_user_reason)
} else {
stringProvider.getString(R.string.event_redacted_by_admin_reason)
}
} else {
if (timelineEvent.root.isRedactedBySameUser()) {
stringProvider.getString(R.string.event_redacted_by_user_reason_with_reason, reason)
} else {
stringProvider.getString(R.string.event_redacted_by_admin_reason_with_reason, reason)
}
}
}
}
private fun actionsForEvent(timelineEvent: TimelineEvent): List<EventSharedAction> {
val messageContent: MessageContent? = timelineEvent.annotations?.editSummary?.aggregatedContent.toModel()