From 5484511d965dade9daad6cdda1e8547bda0d34ef Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Tue, 17 May 2022 18:26:44 +0200 Subject: [PATCH] Migrate remaining chat models to kotlin data classes Signed-off-by: Andy Scherzinger --- .../talk/models/json/chat/ChatMessage.kt | 111 ++++++++++-------- .../talk/models/json/chat/ChatOCS.kt | 2 +- .../models/json/conversations/Conversation.kt | 13 +- .../EnumSystemMessageTypeConverter.kt | 1 + 4 files changed, 76 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/models/json/chat/ChatMessage.kt b/app/src/main/java/com/nextcloud/talk/models/json/chat/ChatMessage.kt index e06011a11..04d23d056 100644 --- a/app/src/main/java/com/nextcloud/talk/models/json/chat/ChatMessage.kt +++ b/app/src/main/java/com/nextcloud/talk/models/json/chat/ChatMessage.kt @@ -176,8 +176,7 @@ data class ChatMessage( "file".toByteArray() ) ) { - - // TODO: this selectedIndividualHashMap stuff needs to be analyzed and most likely be refactored! + // FIX-ME: this selectedIndividualHashMap stuff needs to be analyzed and most likely be refactored! // it just feels wrong to fill this here inside getImageUrl() selectedIndividualHashMap = individualHashMap if (!isVoiceMessage) { @@ -189,8 +188,8 @@ data class ChatMessage( ) } else { Log.e( - TAG, "activeUser or activeUser.getBaseUrl() were null when trying to " + - "getImageUrl()" + TAG, + "activeUser or activeUser.getBaseUrl() were null when trying to getImageUrl()" ) } } @@ -203,22 +202,21 @@ data class ChatMessage( } fun getCalculateMessageType(): MessageType { - if (!TextUtils.isEmpty(systemMessage)) { - return MessageType.SYSTEM_MESSAGE - } - if (isVoiceMessage) { - return MessageType.VOICE_MESSAGE - } - if (hasFileAttachment()) { - return MessageType.SINGLE_NC_ATTACHMENT_MESSAGE - } - return if (hasGeoLocation()) { + return if (!TextUtils.isEmpty(systemMessage)) { + MessageType.SYSTEM_MESSAGE + } else if (isVoiceMessage) { + MessageType.VOICE_MESSAGE + } else if (hasFileAttachment()) { + MessageType.SINGLE_NC_ATTACHMENT_MESSAGE + } else if (hasGeoLocation()) { MessageType.SINGLE_NC_GEOLOCATION_MESSAGE - } else MessageType.REGULAR_TEXT_MESSAGE + } else { + MessageType.REGULAR_TEXT_MESSAGE + } } override fun getId(): String { - return Integer.toString(jsonMessageId) + return jsonMessageId.toString() } override fun getText(): String { @@ -227,29 +225,41 @@ data class ChatMessage( /*} else if (getCalculateMessageType().equals(MessageType.SINGLE_LINK_MESSAGE)) { if (actorId.equals(activeUser.getUserId())) { - return (NextcloudTalkApplication.Companion.getSharedApplication().getString(R.string.nc_sent_a_link_you)); + return ( + NextcloudTalkApplication + .Companion.getSharedApplication() + .getString(R.string.nc_sent_a_link_you) + ); } else { - return (String.format(NextcloudTalkApplication.Companion.getSharedApplication().getResources().getString(R.string.nc_sent_a_link), - !TextUtils.isEmpty(actorDisplayName) ? actorDisplayName : NextcloudTalkApplication.Companion.getSharedApplication().getString(R.string.nc_guest))); + return (String.format(NextcloudTalkApplication. + Companion. + getSharedApplication(). + getResources(). + getString(R.string.nc_sent_a_link), + !TextUtils.isEmpty(actorDisplayName) ? actorDisplayName : NextcloudTalkApplication. + Companion. + getSharedApplication(). + getString(R.string.nc_guest)) + ); }*/ val lastMessageDisplayText: String get() { if (getCalculateMessageType() == MessageType.REGULAR_TEXT_MESSAGE || getCalculateMessageType() == MessageType.SYSTEM_MESSAGE || - getCalculateMessageType() == MessageType.SINGLE_LINK_MESSAGE) { + getCalculateMessageType() == MessageType.SINGLE_LINK_MESSAGE + ) { return text } else { if (MessageType.SINGLE_LINK_GIPHY_MESSAGE == getCalculateMessageType() || MessageType.SINGLE_LINK_TENOR_MESSAGE == getCalculateMessageType() || - MessageType.SINGLE_LINK_GIF_MESSAGE == getCalculateMessageType()) { + MessageType.SINGLE_LINK_GIF_MESSAGE == getCalculateMessageType() + ) { return if (actorId == activeUser!!.userId) { sharedApplication!!.getString(R.string.nc_sent_a_gif_you) } else { String.format( sharedApplication!!.resources.getString(R.string.nc_sent_a_gif), - if (!TextUtils.isEmpty(actorDisplayName)) actorDisplayName else sharedApplication!!.getString( - R.string.nc_guest - ) + getNullsafeActorDisplayName() ) } } else if (MessageType.SINGLE_NC_ATTACHMENT_MESSAGE == getCalculateMessageType()) { @@ -258,9 +268,7 @@ data class ChatMessage( } else { String.format( sharedApplication!!.resources.getString(R.string.nc_sent_an_attachment), - if (!TextUtils.isEmpty(actorDisplayName)) actorDisplayName else sharedApplication!!.getString( - R.string.nc_guest - ) + getNullsafeActorDisplayName() ) } } else if (MessageType.SINGLE_NC_GEOLOCATION_MESSAGE == getCalculateMessageType()) { @@ -269,9 +277,7 @@ data class ChatMessage( } else { String.format( sharedApplication!!.resources.getString(R.string.nc_sent_location), - if (!TextUtils.isEmpty(actorDisplayName)) actorDisplayName else sharedApplication!!.getString( - R.string.nc_guest - ) + getNullsafeActorDisplayName() ) } } else if (MessageType.VOICE_MESSAGE == getCalculateMessageType()) { @@ -280,17 +286,29 @@ data class ChatMessage( } else { String.format( sharedApplication!!.resources.getString(R.string.nc_sent_voice), - if (!TextUtils.isEmpty(actorDisplayName)) actorDisplayName else sharedApplication!!.getString( - R.string.nc_guest - ) + getNullsafeActorDisplayName() ) } /*} else if (getCalculateMessageType().equals(MessageType.SINGLE_LINK_MESSAGE)) { if (actorId.equals(activeUser.getUserId())) { - return (NextcloudTalkApplication.Companion.getSharedApplication().getString(R.string.nc_sent_a_link_you)); + return ( + NextcloudTalkApplication + .Companion + .getSharedApplication() + .getString(R.string.nc_sent_a_link_you) + ); } else { - return (String.format(NextcloudTalkApplication.Companion.getSharedApplication().getResources().getString(R.string.nc_sent_a_link), - !TextUtils.isEmpty(actorDisplayName) ? actorDisplayName : NextcloudTalkApplication.Companion.getSharedApplication().getString(R.string.nc_guest))); + return (String.format( + NextcloudTalkApplication + .Companion + .getSharedApplication() + .getResources() + .getString(R.string.nc_sent_a_link), + !TextUtils.isEmpty(actorDisplayName) ? actorDisplayName : NextcloudTalkApplication. + Companion. + getSharedApplication(). + getString(R.string.nc_guest)) + ); }*/ } else if (MessageType.SINGLE_LINK_AUDIO_MESSAGE == getCalculateMessageType()) { return if (actorId == activeUser!!.userId) { @@ -298,9 +316,7 @@ data class ChatMessage( } else { String.format( sharedApplication!!.resources.getString(R.string.nc_sent_an_audio), - if (!TextUtils.isEmpty(actorDisplayName)) actorDisplayName else sharedApplication!!.getString( - R.string.nc_guest - ) + getNullsafeActorDisplayName() ) } } else if (MessageType.SINGLE_LINK_VIDEO_MESSAGE == getCalculateMessageType()) { @@ -309,9 +325,7 @@ data class ChatMessage( } else { String.format( sharedApplication!!.resources.getString(R.string.nc_sent_a_video), - if (!TextUtils.isEmpty(actorDisplayName)) actorDisplayName else sharedApplication!!.getString( - R.string.nc_guest - ) + getNullsafeActorDisplayName() ) } } else if (MessageType.SINGLE_LINK_IMAGE_MESSAGE == getCalculateMessageType()) { @@ -320,9 +334,7 @@ data class ChatMessage( } else { String.format( sharedApplication!!.resources.getString(R.string.nc_sent_an_image), - if (!TextUtils.isEmpty(actorDisplayName)) actorDisplayName else sharedApplication!!.getString( - R.string.nc_guest - ) + getNullsafeActorDisplayName() ) } } @@ -330,6 +342,12 @@ data class ChatMessage( return "" } + private fun getNullsafeActorDisplayName() = if (!TextUtils.isEmpty(actorDisplayName)) { + actorDisplayName + } else { + sharedApplication!!.getString(R.string.nc_guest) + } + override fun getUser(): IUser { return object : IUser { override fun getId(): String { @@ -369,7 +387,7 @@ data class ChatMessage( } override fun getCreatedAt(): Date { - return Date(timestamp * 1000L) + return Date(timestamp * MILLIES) } override fun getSystemMessage(): String { @@ -450,5 +468,6 @@ data class ChatMessage( companion object { private const val TAG = "ChatMessage" + private const val MILLIES: Long = 1000L } } diff --git a/app/src/main/java/com/nextcloud/talk/models/json/chat/ChatOCS.kt b/app/src/main/java/com/nextcloud/talk/models/json/chat/ChatOCS.kt index d4b6f1bf5..5e36b98a7 100644 --- a/app/src/main/java/com/nextcloud/talk/models/json/chat/ChatOCS.kt +++ b/app/src/main/java/com/nextcloud/talk/models/json/chat/ChatOCS.kt @@ -37,4 +37,4 @@ data class ChatOCS( ) : Parcelable { // This constructor is added to work with the 'com.bluelinelabs.logansquare.annotation.JsonObject' constructor() : this(null, null) -} \ No newline at end of file +} diff --git a/app/src/main/java/com/nextcloud/talk/models/json/conversations/Conversation.kt b/app/src/main/java/com/nextcloud/talk/models/json/conversations/Conversation.kt index b230a35b1..23d89bbe4 100644 --- a/app/src/main/java/com/nextcloud/talk/models/json/conversations/Conversation.kt +++ b/app/src/main/java/com/nextcloud/talk/models/json/conversations/Conversation.kt @@ -131,10 +131,14 @@ data class Conversation( get() = ConversationType.ROOM_PUBLIC_CALL == type val isGuest: Boolean - get() = ParticipantType.GUEST == participantType || ParticipantType.GUEST_MODERATOR == participantType || ParticipantType.USER_FOLLOWING_LINK == participantType + get() = ParticipantType.GUEST == participantType || + ParticipantType.GUEST_MODERATOR == participantType || + ParticipantType.USER_FOLLOWING_LINK == participantType val isParticipantOwnerOrModerator: Boolean - get() = ParticipantType.OWNER == participantType || ParticipantType.GUEST_MODERATOR == participantType || ParticipantType.MODERATOR == participantType + get() = ParticipantType.OWNER == participantType || + ParticipantType.GUEST_MODERATOR == participantType || + ParticipantType.MODERATOR == participantType private fun isLockedOneToOne(conversationUser: UserEntity): Boolean { return type == ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL && @@ -150,7 +154,8 @@ data class Conversation( } fun isLobbyViewApplicable(conversationUser: UserEntity): Boolean { - return !canModerate(conversationUser) && (type == ConversationType.ROOM_GROUP_CALL || type == ConversationType.ROOM_PUBLIC_CALL) + return !canModerate(conversationUser) && + (type == ConversationType.ROOM_GROUP_CALL || type == ConversationType.ROOM_PUBLIC_CALL) } fun isNameEditable(conversationUser: UserEntity): Boolean { @@ -189,4 +194,4 @@ data class Conversation( enum class ConversationType { DUMMY, ROOM_TYPE_ONE_TO_ONE_CALL, ROOM_GROUP_CALL, ROOM_PUBLIC_CALL, ROOM_SYSTEM } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/nextcloud/talk/models/json/converters/EnumSystemMessageTypeConverter.kt b/app/src/main/java/com/nextcloud/talk/models/json/converters/EnumSystemMessageTypeConverter.kt index c20d09046..caa75ec2b 100644 --- a/app/src/main/java/com/nextcloud/talk/models/json/converters/EnumSystemMessageTypeConverter.kt +++ b/app/src/main/java/com/nextcloud/talk/models/json/converters/EnumSystemMessageTypeConverter.kt @@ -171,6 +171,7 @@ class EnumSystemMessageTypeConverter : StringBasedTypeConverter