Shortened lines

This commit is contained in:
Benoit Marty 2019-12-04 19:17:23 +01:00
parent e0e778909d
commit 415511f3e0
1 changed files with 48 additions and 58 deletions

View File

@ -29,7 +29,7 @@ import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
class NoticeEventFormatter @Inject constructor(private val sessionHolder: ActiveSessionHolder, class NoticeEventFormatter @Inject constructor(private val sessionHolder: ActiveSessionHolder,
private val stringProvider: StringProvider) { private val sp: StringProvider) {
fun format(timelineEvent: TimelineEvent): CharSequence? { fun format(timelineEvent: TimelineEvent): CharSequence? {
return when (val type = timelineEvent.root.getClearType()) { return when (val type = timelineEvent.root.getClearType()) {
@ -77,22 +77,22 @@ class NoticeEventFormatter @Inject constructor(private val sessionHolder: Active
private fun formatRoomNameEvent(event: Event, senderName: String?): CharSequence? { private fun formatRoomNameEvent(event: Event, senderName: String?): CharSequence? {
val content = event.getClearContent().toModel<RoomNameContent>() ?: return null val content = event.getClearContent().toModel<RoomNameContent>() ?: return null
return if (content.name.isNullOrBlank()) { return if (content.name.isNullOrBlank()) {
stringProvider.getString(R.string.notice_room_name_removed, senderName) sp.getString(R.string.notice_room_name_removed, senderName)
} else { } else {
stringProvider.getString(R.string.notice_room_name_changed, senderName, content.name) sp.getString(R.string.notice_room_name_changed, senderName, content.name)
} }
} }
private fun formatRoomTombstoneEvent(senderName: String?): CharSequence? { private fun formatRoomTombstoneEvent(senderName: String?): CharSequence? {
return stringProvider.getString(R.string.notice_room_update, senderName) return sp.getString(R.string.notice_room_update, senderName)
} }
private fun formatRoomTopicEvent(event: Event, senderName: String?): CharSequence? { private fun formatRoomTopicEvent(event: Event, senderName: String?): CharSequence? {
val content = event.getClearContent().toModel<RoomTopicContent>() ?: return null val content = event.getClearContent().toModel<RoomTopicContent>() ?: return null
return if (content.topic.isNullOrEmpty()) { return if (content.topic.isNullOrEmpty()) {
stringProvider.getString(R.string.notice_room_topic_removed, senderName) sp.getString(R.string.notice_room_topic_removed, senderName)
} else { } else {
stringProvider.getString(R.string.notice_room_topic_changed, senderName, content.topic) sp.getString(R.string.notice_room_topic_changed, senderName, content.topic)
} }
} }
@ -100,12 +100,12 @@ class NoticeEventFormatter @Inject constructor(private val sessionHolder: Active
val historyVisibility = event.getClearContent().toModel<RoomHistoryVisibilityContent>()?.historyVisibility ?: return null val historyVisibility = event.getClearContent().toModel<RoomHistoryVisibilityContent>()?.historyVisibility ?: return null
val formattedVisibility = when (historyVisibility) { val formattedVisibility = when (historyVisibility) {
RoomHistoryVisibility.SHARED -> stringProvider.getString(R.string.notice_room_visibility_shared) RoomHistoryVisibility.SHARED -> sp.getString(R.string.notice_room_visibility_shared)
RoomHistoryVisibility.INVITED -> stringProvider.getString(R.string.notice_room_visibility_invited) RoomHistoryVisibility.INVITED -> sp.getString(R.string.notice_room_visibility_invited)
RoomHistoryVisibility.JOINED -> stringProvider.getString(R.string.notice_room_visibility_joined) RoomHistoryVisibility.JOINED -> sp.getString(R.string.notice_room_visibility_joined)
RoomHistoryVisibility.WORLD_READABLE -> stringProvider.getString(R.string.notice_room_visibility_world_readable) RoomHistoryVisibility.WORLD_READABLE -> sp.getString(R.string.notice_room_visibility_world_readable)
} }
return stringProvider.getString(R.string.notice_made_future_room_visibility, senderName, formattedVisibility) return sp.getString(R.string.notice_made_future_room_visibility, senderName, formattedVisibility)
} }
private fun formatCallEvent(event: Event, senderName: String?): CharSequence? { private fun formatCallEvent(event: Event, senderName: String?): CharSequence? {
@ -114,13 +114,13 @@ class NoticeEventFormatter @Inject constructor(private val sessionHolder: Active
val content = event.getClearContent().toModel<CallInviteContent>() ?: return null val content = event.getClearContent().toModel<CallInviteContent>() ?: return null
val isVideoCall = content.offer.sdp == CallInviteContent.Offer.SDP_VIDEO val isVideoCall = content.offer.sdp == CallInviteContent.Offer.SDP_VIDEO
return if (isVideoCall) { return if (isVideoCall) {
stringProvider.getString(R.string.notice_placed_video_call, senderName) sp.getString(R.string.notice_placed_video_call, senderName)
} else { } else {
stringProvider.getString(R.string.notice_placed_voice_call, senderName) sp.getString(R.string.notice_placed_voice_call, senderName)
} }
} }
EventType.CALL_ANSWER == event.type -> stringProvider.getString(R.string.notice_answered_call, senderName) EventType.CALL_ANSWER == event.type -> sp.getString(R.string.notice_answered_call, senderName)
EventType.CALL_HANGUP == event.type -> stringProvider.getString(R.string.notice_ended_call, senderName) EventType.CALL_HANGUP == event.type -> sp.getString(R.string.notice_ended_call, senderName)
else -> null else -> null
} }
} }
@ -142,11 +142,11 @@ class NoticeEventFormatter @Inject constructor(private val sessionHolder: Active
if (eventContent?.displayName != prevEventContent?.displayName) { if (eventContent?.displayName != prevEventContent?.displayName) {
val displayNameText = when { val displayNameText = when {
prevEventContent?.displayName.isNullOrEmpty() -> prevEventContent?.displayName.isNullOrEmpty() ->
stringProvider.getString(R.string.notice_display_name_set, event.senderId, eventContent?.displayName) sp.getString(R.string.notice_display_name_set, event.senderId, eventContent?.displayName)
eventContent?.displayName.isNullOrEmpty() -> eventContent?.displayName.isNullOrEmpty() ->
stringProvider.getString(R.string.notice_display_name_removed, event.senderId, prevEventContent?.displayName) sp.getString(R.string.notice_display_name_removed, event.senderId, prevEventContent?.displayName)
else -> else ->
stringProvider.getString(R.string.notice_display_name_changed_from, event.senderId, prevEventContent?.displayName, eventContent?.displayName) sp.getString(R.string.notice_display_name_changed_from, event.senderId, prevEventContent?.displayName, eventContent?.displayName)
} }
displayText.append(displayNameText) displayText.append(displayNameText)
} }
@ -154,15 +154,15 @@ class NoticeEventFormatter @Inject constructor(private val sessionHolder: Active
if (eventContent?.avatarUrl != prevEventContent?.avatarUrl) { if (eventContent?.avatarUrl != prevEventContent?.avatarUrl) {
val displayAvatarText = if (displayText.isNotEmpty()) { val displayAvatarText = if (displayText.isNotEmpty()) {
displayText.append(" ") displayText.append(" ")
stringProvider.getString(R.string.notice_avatar_changed_too) sp.getString(R.string.notice_avatar_changed_too)
} else { } else {
stringProvider.getString(R.string.notice_avatar_url_changed, senderName) sp.getString(R.string.notice_avatar_url_changed, senderName)
} }
displayText.append(displayAvatarText) displayText.append(displayAvatarText)
} }
if (displayText.isEmpty()) { if (displayText.isEmpty()) {
displayText.append( displayText.append(
stringProvider.getString(R.string.notice_member_no_changes, senderName) sp.getString(R.string.notice_member_no_changes, senderName)
) )
} }
return displayText.toString() return displayText.toString()
@ -179,73 +179,63 @@ class NoticeEventFormatter @Inject constructor(private val sessionHolder: Active
val userWhoHasAccepted = eventContent.thirdPartyInvite?.signed?.mxid ?: event.stateKey val userWhoHasAccepted = eventContent.thirdPartyInvite?.signed?.mxid ?: event.stateKey
val threePidDisplayName = eventContent.thirdPartyInvite?.displayName ?: "" val threePidDisplayName = eventContent.thirdPartyInvite?.displayName ?: ""
eventContent.safeReason?.let { reason -> eventContent.safeReason?.let { reason ->
stringProvider.getString(R.string.notice_room_third_party_registered_invite_with_reason, userWhoHasAccepted, threePidDisplayName, reason) sp.getString(R.string.notice_room_third_party_registered_invite_with_reason, userWhoHasAccepted, threePidDisplayName, reason)
} } ?: sp.getString(R.string.notice_room_third_party_registered_invite, userWhoHasAccepted, threePidDisplayName)
?: stringProvider.getString(R.string.notice_room_third_party_registered_invite, userWhoHasAccepted, threePidDisplayName)
} }
event.stateKey == selfUserId -> event.stateKey == selfUserId ->
eventContent.safeReason?.let { reason -> eventContent.safeReason?.let { reason ->
stringProvider.getString(R.string.notice_room_invite_you_with_reason, senderDisplayName, reason) sp.getString(R.string.notice_room_invite_you_with_reason, senderDisplayName, reason)
} } ?: sp.getString(R.string.notice_room_invite_you, senderDisplayName)
?: stringProvider.getString(R.string.notice_room_invite_you, senderDisplayName)
event.stateKey.isNullOrEmpty() -> event.stateKey.isNullOrEmpty() ->
eventContent.safeReason?.let { reason -> eventContent.safeReason?.let { reason ->
stringProvider.getString(R.string.notice_room_invite_no_invitee_with_reason, senderDisplayName, reason) sp.getString(R.string.notice_room_invite_no_invitee_with_reason, senderDisplayName, reason)
} } ?: sp.getString(R.string.notice_room_invite_no_invitee, senderDisplayName)
?: stringProvider.getString(R.string.notice_room_invite_no_invitee, senderDisplayName)
else -> else ->
eventContent.safeReason?.let { reason -> eventContent.safeReason?.let { reason ->
stringProvider.getString(R.string.notice_room_invite, senderDisplayName, targetDisplayName, reason) sp.getString(R.string.notice_room_invite, senderDisplayName, targetDisplayName, reason)
} } ?: sp.getString(R.string.notice_room_invite, senderDisplayName, targetDisplayName)
?: stringProvider.getString(R.string.notice_room_invite, senderDisplayName, targetDisplayName)
} }
} }
Membership.JOIN -> Membership.JOIN ->
eventContent.safeReason?.let { reason -> eventContent.safeReason?.let { reason ->
stringProvider.getString(R.string.notice_room_join_with_reason, senderDisplayName, reason) sp.getString(R.string.notice_room_join_with_reason, senderDisplayName, reason)
} }
?: stringProvider.getString(R.string.notice_room_join, senderDisplayName) ?: sp.getString(R.string.notice_room_join, senderDisplayName)
Membership.LEAVE -> Membership.LEAVE ->
// 2 cases here: this member may have left voluntarily or they may have been "left" by someone else ie. kicked // 2 cases here: this member may have left voluntarily or they may have been "left" by someone else ie. kicked
if (event.senderId == event.stateKey) { if (event.senderId == event.stateKey) {
if (prevEventContent?.membership == Membership.INVITE) { if (prevEventContent?.membership == Membership.INVITE) {
eventContent.safeReason?.let { reason -> eventContent.safeReason?.let { reason ->
stringProvider.getString(R.string.notice_room_reject_with_reason, senderDisplayName, reason) sp.getString(R.string.notice_room_reject_with_reason, senderDisplayName, reason)
} } ?: sp.getString(R.string.notice_room_reject, senderDisplayName)
?: stringProvider.getString(R.string.notice_room_reject, senderDisplayName)
} else { } else {
eventContent.safeReason?.let { reason -> eventContent.safeReason?.let { reason ->
stringProvider.getString(R.string.notice_room_leave_with_reason, senderDisplayName, reason) sp.getString(R.string.notice_room_leave_with_reason, senderDisplayName, reason)
} } ?: sp.getString(R.string.notice_room_leave, senderDisplayName)
?: stringProvider.getString(R.string.notice_room_leave, senderDisplayName)
} }
} else if (prevEventContent?.membership == Membership.INVITE) { } else if (prevEventContent?.membership == Membership.INVITE) {
eventContent.safeReason?.let { reason -> eventContent.safeReason?.let { reason ->
stringProvider.getString(R.string.notice_room_withdraw_with_reason, senderDisplayName, targetDisplayName, reason) sp.getString(R.string.notice_room_withdraw_with_reason, senderDisplayName, targetDisplayName, reason)
} } ?: sp.getString(R.string.notice_room_withdraw, senderDisplayName, targetDisplayName)
?: stringProvider.getString(R.string.notice_room_withdraw, senderDisplayName, targetDisplayName)
} else if (prevEventContent?.membership == Membership.JOIN) { } else if (prevEventContent?.membership == Membership.JOIN) {
eventContent.safeReason?.let { reason -> eventContent.safeReason?.let { reason ->
stringProvider.getString(R.string.notice_room_kick_with_reason, senderDisplayName, targetDisplayName, reason) sp.getString(R.string.notice_room_kick_with_reason, senderDisplayName, targetDisplayName, reason)
} } ?: sp.getString(R.string.notice_room_kick, senderDisplayName, targetDisplayName)
?: stringProvider.getString(R.string.notice_room_kick, senderDisplayName, targetDisplayName)
} else if (prevEventContent?.membership == Membership.BAN) { } else if (prevEventContent?.membership == Membership.BAN) {
eventContent.safeReason?.let { reason -> eventContent.safeReason?.let { reason ->
stringProvider.getString(R.string.notice_room_unban_with_reason, senderDisplayName, targetDisplayName, reason) sp.getString(R.string.notice_room_unban_with_reason, senderDisplayName, targetDisplayName, reason)
} } ?: sp.getString(R.string.notice_room_unban, senderDisplayName, targetDisplayName)
?: stringProvider.getString(R.string.notice_room_unban, senderDisplayName, targetDisplayName)
} else { } else {
null null
} }
Membership.BAN -> Membership.BAN ->
eventContent.safeReason?.let { reason -> eventContent.safeReason?.let {
stringProvider.getString(R.string.notice_room_ban_with_reason, senderDisplayName, targetDisplayName, reason) sp.getString(R.string.notice_room_ban_with_reason, senderDisplayName, targetDisplayName, it)
} } ?: sp.getString(R.string.notice_room_ban, senderDisplayName, targetDisplayName)
?: stringProvider.getString(R.string.notice_room_ban, senderDisplayName, targetDisplayName)
Membership.KNOCK -> Membership.KNOCK ->
eventContent.safeReason?.let { reason -> eventContent.safeReason?.let { reason ->
stringProvider.getString(R.string.notice_room_kick_with_reason, senderDisplayName, targetDisplayName, reason) sp.getString(R.string.notice_room_kick_with_reason, senderDisplayName, targetDisplayName, reason)
} ?: stringProvider.getString(R.string.notice_room_kick, senderDisplayName, targetDisplayName) } ?: sp.getString(R.string.notice_room_kick, senderDisplayName, targetDisplayName)
else -> null else -> null
} }
} }
@ -253,8 +243,8 @@ class NoticeEventFormatter @Inject constructor(private val sessionHolder: Active
private fun formatJoinRulesEvent(event: Event, senderName: String?): CharSequence? { private fun formatJoinRulesEvent(event: Event, senderName: String?): CharSequence? {
val content = event.getClearContent().toModel<RoomJoinRulesContent>() ?: return null val content = event.getClearContent().toModel<RoomJoinRulesContent>() ?: return null
return when (content.joinRules) { return when (content.joinRules) {
RoomJoinRules.INVITE -> stringProvider.getString(R.string.room_join_rules_invite, senderName) RoomJoinRules.INVITE -> sp.getString(R.string.room_join_rules_invite, senderName)
RoomJoinRules.PUBLIC -> stringProvider.getString(R.string.room_join_rules_public, senderName) RoomJoinRules.PUBLIC -> sp.getString(R.string.room_join_rules_public, senderName)
else -> null else -> null
} }
} }