From 0cfea40b2477ec4ad92f5f12a097d743dfd6e3e1 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 25 Nov 2020 17:57:42 +0100 Subject: [PATCH] Use when statement and modify the case to simplify the code --- .../timeline/format/NoticeEventFormatter.kt | 72 +++++++++---------- 1 file changed, 34 insertions(+), 38 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/format/NoticeEventFormatter.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/format/NoticeEventFormatter.kt index 0db6a374e9..b67a82b105 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/format/NoticeEventFormatter.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/format/NoticeEventFormatter.kt @@ -473,67 +473,63 @@ class NoticeEventFormatter @Inject constructor( val added = altAliases - prevAltAliases val removed = prevAltAliases - altAliases - return if (added.isEmpty() && removed.isEmpty() && canonicalAlias == prevCanonicalAlias) { - // in case there is no difference between the two events say something as we can't simply hide the event from here - if (event.isSentByCurrentUser()) { - sp.getString(R.string.notice_room_canonical_alias_no_change_by_you) - } else { - sp.getString(R.string.notice_room_canonical_alias_no_change, senderName) - } - } else if (added.isEmpty() && removed.isEmpty()) { - // Canonical has changed - if (canonicalAlias != null) { + return when { + added.isEmpty() && removed.isEmpty() && canonicalAlias == prevCanonicalAlias -> { + // No difference between the two events say something as we can't simply hide the event from here if (event.isSentByCurrentUser()) { - sp.getString(R.string.notice_room_canonical_alias_set_by_you, canonicalAlias) + sp.getString(R.string.notice_room_canonical_alias_no_change_by_you) } else { - sp.getString(R.string.notice_room_canonical_alias_set, senderName, canonicalAlias) - } - } else { - if (event.isSentByCurrentUser()) { - sp.getString(R.string.notice_room_canonical_alias_unset_by_you) - } else { - sp.getString(R.string.notice_room_canonical_alias_unset, senderName) + sp.getString(R.string.notice_room_canonical_alias_no_change, senderName) } } - } else if (added.isEmpty()) { - if (canonicalAlias == prevCanonicalAlias) { + added.isEmpty() && removed.isEmpty() -> { + // Canonical has changed + if (canonicalAlias != null) { + if (event.isSentByCurrentUser()) { + sp.getString(R.string.notice_room_canonical_alias_set_by_you, canonicalAlias) + } else { + sp.getString(R.string.notice_room_canonical_alias_set, senderName, canonicalAlias) + } + } else { + if (event.isSentByCurrentUser()) { + sp.getString(R.string.notice_room_canonical_alias_unset_by_you) + } else { + sp.getString(R.string.notice_room_canonical_alias_unset, senderName) + } + } + } + added.isEmpty() && canonicalAlias == prevCanonicalAlias -> { // Some alternative has been removed if (event.isSentByCurrentUser()) { sp.getQuantityString(R.plurals.notice_room_canonical_alias_alternative_removed_by_you, removed.size, removed.joinToString()) } else { sp.getQuantityString(R.plurals.notice_room_canonical_alias_alternative_removed, removed.size, senderName, removed.joinToString()) } - } else { - // Main and removed - if (event.isSentByCurrentUser()) { - sp.getString(R.string.notice_room_canonical_alias_main_and_alternative_changed_by_you) - } else { - sp.getString(R.string.notice_room_canonical_alias_main_and_alternative_changed, senderName) - } } - } else if (removed.isEmpty()) { - if (canonicalAlias == prevCanonicalAlias) { + removed.isEmpty() && canonicalAlias == prevCanonicalAlias -> { // Some alternative has been added if (event.isSentByCurrentUser()) { sp.getQuantityString(R.plurals.notice_room_canonical_alias_alternative_added_by_you, added.size, added.joinToString()) } else { sp.getQuantityString(R.plurals.notice_room_canonical_alias_alternative_added, added.size, senderName, added.joinToString()) } - } else { - // Main and added + } + canonicalAlias == prevCanonicalAlias -> { + // Alternative added and removed + if (event.isSentByCurrentUser()) { + sp.getString(R.string.notice_room_canonical_alias_alternative_changed_by_you) + } else { + sp.getString(R.string.notice_room_canonical_alias_alternative_changed, senderName) + } + } + else -> { + // Main and removed, or main and added, or main and added and removed if (event.isSentByCurrentUser()) { sp.getString(R.string.notice_room_canonical_alias_main_and_alternative_changed_by_you) } else { sp.getString(R.string.notice_room_canonical_alias_main_and_alternative_changed, senderName) } } - } else { - // Alternative added and removed - if (event.isSentByCurrentUser()) { - sp.getString(R.string.notice_room_canonical_alias_alternative_changed_by_you) - } else { - sp.getString(R.string.notice_room_canonical_alias_alternative_changed, senderName) - } } }