feat(all): Make ConversationType an enhanced enum.

Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
This commit is contained in:
Ikjot Singh Dhody 2023-07-14 14:55:55 +05:30
parent 008e816d70
commit 06eab1d6f5
5 changed files with 42 additions and 60 deletions

View File

@ -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,

View File

@ -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.

View File

@ -105,8 +105,8 @@ void setupBackgroundEventHandler() {
EventTypeMatcher<GetPagedSharedMediaCommand>(performGetPagedSharedMedia),
EventTypeMatcher<GetReactionsForMessageCommand>(performGetReactions),
EventTypeMatcher<RequestAvatarForJidCommand>(performRequestAvatarForJid),
EventTypeMatcher<DebugCommand>(performDebugCommand),
EventTypeMatcher<JoinGroupchatCommand>(performJoinGroupchat),
EventTypeMatcher<DebugCommand>(performDebugCommand),
]);
GetIt.I.registerSingleton<EventHandler>(handler);
@ -244,7 +244,7 @@ Future<void> 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<void> performAddConversation(
contactId,
await css.getProfilePicturePathForJid(command.jid),
await css.getContactDisplayName(contactId),
GroupchatDetails(
command.jid,
'',
'',
),
null,
);
sendEvent(
@ -592,11 +588,7 @@ Future<void> performAddContact(
contactId,
await css.getProfilePicturePathForJid(jid),
await css.getContactDisplayName(contactId),
GroupchatDetails(
jid,
'',
'',
),
null,
);
sendEvent(

View File

@ -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<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) {
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

View File

@ -65,7 +65,7 @@ class NewConversationBloc
jid: event.jid,
avatarUrl: event.avatarUrl,
lastMessageBody: '',
conversationType: conversationTypeToString(event.type),
conversationType: event.type.value,
),
);