From 06eab1d6f5a139a6fc61b221ef0d18385c26cf4f Mon Sep 17 00:00:00 2001 From: Ikjot Singh Dhody Date: Fri, 14 Jul 2023 14:55:55 +0530 Subject: [PATCH] feat(all): Make ConversationType an enhanced enum. Signed-off-by: Ikjot Singh Dhody --- lib/service/conversation.dart | 4 +-- lib/service/database/helpers.dart | 34 -------------------- lib/service/events.dart | 16 +++------- lib/shared/models/conversation.dart | 46 ++++++++++++++++++++------- lib/ui/bloc/newconversation_bloc.dart | 2 +- 5 files changed, 42 insertions(+), 60 deletions(-) diff --git a/lib/service/conversation.dart b/lib/service/conversation.dart index 0d88d81d..c98a1d5a 100644 --- a/lib/service/conversation.dart +++ b/lib/service/conversation.dart @@ -88,7 +88,7 @@ class ConversationService { } GroupchatDetails? groupchatDetails; - if (c['type'] == 'groupchat') { + if (c['type'] == ConversationType.groupchat.value) { groupchatDetails = await gs.getGroupchatDetailsByJid(c['jid']! as String); } @@ -272,7 +272,7 @@ class ConversationService { _conversationCache![newConversation.jid] = newConversation; } - if (type == ConversationType.groupchat) { + if (type == ConversationType.groupchat && groupchatDetails != null) { await gs.addGroupchatDetailsFromData( jid, groupchatDetails!.nick, diff --git a/lib/service/database/helpers.dart b/lib/service/database/helpers.dart index d9a5984d..84bca60f 100644 --- a/lib/service/database/helpers.dart +++ b/lib/service/database/helpers.dart @@ -10,40 +10,6 @@ bool stringToBool(String s) => s == 'true' ? true : false; String intToString(int i) => '$i'; int stringToInt(String s) => int.parse(s); -String conversationTypeToString(ConversationType type) { - switch (type) { - case ConversationType.chat: - { - return 'chat'; - } - case ConversationType.note: - { - return 'note'; - } - case ConversationType.groupchat: - { - return 'groupchat'; - } - } -} - -ConversationType stringToConversationType(String type) { - switch (type) { - case 'chat': - { - return ConversationType.chat; - } - case 'note': - { - return ConversationType.note; - } - default: - { - return ConversationType.groupchat; - } - } -} - /// Given a map [map], extract all key-value pairs from [map] where the key starts with /// [prefix]. Combine those key-value pairs into a new map, where the leading [prefix] /// is removed from all key names. diff --git a/lib/service/events.dart b/lib/service/events.dart index 09177c8a..9f56f8f8 100644 --- a/lib/service/events.dart +++ b/lib/service/events.dart @@ -105,8 +105,8 @@ void setupBackgroundEventHandler() { EventTypeMatcher(performGetPagedSharedMedia), EventTypeMatcher(performGetReactions), EventTypeMatcher(performRequestAvatarForJid), - EventTypeMatcher(performDebugCommand), EventTypeMatcher(performJoinGroupchat), + EventTypeMatcher(performDebugCommand), ]); GetIt.I.registerSingleton(handler); @@ -244,7 +244,7 @@ Future performAddConversation( final newConversation = await cs.addConversationFromData( command.title, null, - stringToConversationType(command.conversationType), + ConversationType.fromString(command.conversationType), command.avatarUrl, command.jid, 0, @@ -255,11 +255,7 @@ Future performAddConversation( contactId, await css.getProfilePicturePathForJid(command.jid), await css.getContactDisplayName(contactId), - GroupchatDetails( - command.jid, - '', - '', - ), + null, ); sendEvent( @@ -592,11 +588,7 @@ Future performAddContact( contactId, await css.getProfilePicturePathForJid(jid), await css.getContactDisplayName(contactId), - GroupchatDetails( - jid, - '', - '', - ), + null, ); sendEvent( diff --git a/lib/shared/models/conversation.dart b/lib/shared/models/conversation.dart index 29478459..8f53677e 100644 --- a/lib/shared/models/conversation.dart +++ b/lib/shared/models/conversation.dart @@ -40,28 +40,52 @@ class ConversationMessageConverter }; } -enum ConversationType { +// enum ConversationType { +// chat('chat'), +// note('note'), +// groupchat('groupchat'); + +// const ConversationType(this.value); + +// /// The identifier of the enum value. +// final String value; + +// static ConversationType? fromInt(String value) { +// switch (value) { +// case 'chat': +// return ConversationType.chat; +// case 'note': +// return ConversationType.note; +// case 'groupchat': +// return ConversationType.groupchat; +// } + +// return null; +// } +// } + +enum ConversationType implements Comparable { chat('chat'), note('note'), groupchat('groupchat'); const ConversationType(this.value); - - /// The identifier of the enum value. final String value; - static ConversationType? fromInt(String value) { + static ConversationType fromString(String value) { switch (value) { - case 'chat': - return ConversationType.chat; - case 'note': - return ConversationType.note; case 'groupchat': return ConversationType.groupchat; + case 'note': + return ConversationType.note; + default: + return ConversationType.chat; } - - return null; } + + // TODO: What to do here? + @override + int compareTo(ConversationType other) => 0; } class ConversationTypeConverter @@ -70,7 +94,7 @@ class ConversationTypeConverter @override ConversationType fromJson(String json) { - return ConversationType.fromInt(json)!; + return ConversationType.fromString(json)!; } @override diff --git a/lib/ui/bloc/newconversation_bloc.dart b/lib/ui/bloc/newconversation_bloc.dart index 1c5171f8..9f9f8242 100644 --- a/lib/ui/bloc/newconversation_bloc.dart +++ b/lib/ui/bloc/newconversation_bloc.dart @@ -65,7 +65,7 @@ class NewConversationBloc jid: event.jid, avatarUrl: event.avatarUrl, lastMessageBody: '', - conversationType: conversationTypeToString(event.type), + conversationType: event.type.value, ), );