Skip to content

Commit

Permalink
fix: 🐛 fix/Json Parsing Issue
Browse files Browse the repository at this point in the history
- Added tryParse methods in `MessageType`, `MessageStatus` in `enumaration.dart`
- Fixed `fromJson()`, `toJson()` methods in `message.dart`, `reaction.dart`, `reply_message.dart`
  • Loading branch information
dhyash-simform committed May 31, 2024
1 parent aa04ada commit 9587b2d
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 28 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## [1.3.2] (Unreleased)

* **Fix**: [177](https://github.com/SimformSolutionsPvtLtd/flutter_chatview/pull/177) Fixed
json serializable of models (Message, Reaction and Reply Message).
* **Fix**: [139](https://github.com/SimformSolutionsPvtLtd/flutter_chatview/issues/139) Added
support to customize view for the reply of any message.
* **Fix**: [174](https://github.com/SimformSolutionsPvtLtd/flutter_chatview/issues/174) Fix
Expand Down Expand Up @@ -32,7 +34,6 @@
* **Feat**: [161](https://github.com/SimformSolutionsPvtLtd/flutter_chatview/pull/161) Added
field to set top padding of chat text field.


## [1.3.1]

* **Feat**: [105](https://github.com/SimformSolutionsPvtLtd/flutter_chatview/pull/105) Allow user
Expand Down
48 changes: 35 additions & 13 deletions lib/src/models/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,47 @@ class Message {
}

factory Message.fromJson(Map<String, dynamic> json) => Message(
id: json["id"],
message: json["message"],
createdAt: json["createdAt"],
sendBy: json["sendBy"],
replyMessage: ReplyMessage.fromJson(json["reply_message"]),
reaction: Reaction.fromJson(json["reaction"]),
messageType: json["message_type"],
voiceMessageDuration: json["voice_message_duration"],
status: json['status']);
id: json['id']?.toString() ?? '',
message: json['message']?.toString() ?? '',
createdAt:
DateTime.tryParse(json['createdAt'].toString()) ?? DateTime.now(),
sendBy: json['sendBy']?.toString() ?? '',
replyMessage: json['reply_message'] == null
? const ReplyMessage()
: ReplyMessage.fromJson(
json['reply_message'] is Map<String, dynamic>
? json['reply_message'] as Map<String, dynamic>
: <String, dynamic>{},
),
reaction: json['reaction'] == null
? null
: Reaction.fromJson(
json['reaction'] is Map<String, dynamic>
? json['reaction'] as Map<String, dynamic>
: <String, dynamic>{},
),
messageType: MessageType.tryParse(json['message_type']?.toString()) ??
MessageType.text,
voiceMessageDuration: json['voice_message_duration'] == null
? null
: Duration(
microseconds:
int.tryParse(json['voice_message_duration'].toString()) ??
0,
),
status: MessageStatus.tryParse(json['status']?.toString()) ??
MessageStatus.pending,
);

Map<String, dynamic> toJson() => {
'id': id,
'message': message,
'createdAt': createdAt,
'createdAt': createdAt.toIso8601String(),
'sendBy': sendBy,
'reply_message': replyMessage.toJson(),
'reaction': reaction.toJson(),
'message_type': messageType,
'voice_message_duration': voiceMessageDuration,
'status': status.name
'message_type': messageType.name,
'voice_message_duration': voiceMessageDuration?.inMicroseconds,
'status': status.name,
};
}
29 changes: 25 additions & 4 deletions lib/src/models/reaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,31 @@ class Reaction {
required this.reactedUserIds,
});

factory Reaction.fromJson(Map<String, dynamic> json) => Reaction(
reactions: json['reactions'],
reactedUserIds: json['reactedUserIds'],
);
factory Reaction.fromJson(Map<String, dynamic> json) {
final reactions = <String>[];
final reactionsList = json['reactions'] is List<dynamic>
? json['reactions'] as List<dynamic>
: <dynamic>[];

for (var i = 0; i < reactionsList.length; i++) {
final reaction = reactionsList[i]?.toString();
if (reaction?.isEmpty ?? true) continue;
reactions.add(reaction!);
}

final reactedUserIds = <String>[];
final reactedUserIdList = json['reactedUserIds'] is List<dynamic>
? json['reactedUserIds'] as List<dynamic>
: <dynamic>[];

for (var i = 0; i < reactedUserIdList.length; i++) {
final reactedUserId = reactedUserIdList[i]?.toString();
if (reactedUserId?.isEmpty ?? true) continue;
reactedUserIds.add(reactedUserId!);
}

return Reaction(reactions: reactions, reactedUserIds: reactedUserIds);
}

/// Provides list of reaction in single message.
final List<String> reactions;
Expand Down
22 changes: 14 additions & 8 deletions lib/src/models/reply_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,26 @@ class ReplyMessage {
});

factory ReplyMessage.fromJson(Map<String, dynamic> json) => ReplyMessage(
message: json['message'],
replyBy: json['replyBy'],
replyTo: json['replyTo'],
messageType: json["message_type"],
messageId: json["id"],
voiceMessageDuration: json["voiceMessageDuration"],
message: json['message']?.toString() ?? '',
replyBy: json['replyBy']?.toString() ?? '',
replyTo: json['replyTo']?.toString() ?? '',
messageType: MessageType.tryParse(json['message_type']?.toString()) ??
MessageType.text,
messageId: json['id']?.toString() ?? '',
voiceMessageDuration: json['voiceMessageDuration'] == null
? null
: Duration(
microseconds:
int.tryParse(json['voiceMessageDuration'].toString()) ?? 0,
),
);

Map<String, dynamic> toJson() => {
'message': message,
'replyBy': replyBy,
'replyTo': replyTo,
'message_type': messageType,
'message_type': messageType.name,
'id': messageId,
'voiceMessageDuration': voiceMessageDuration,
'voiceMessageDuration': voiceMessageDuration?.inMicroseconds,
};
}
39 changes: 37 additions & 2 deletions lib/src/values/enumaration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,22 @@ enum MessageType {

/// Only supported on android and ios
voice,
custom
custom;

static MessageType? tryParse(String? value) {
final type = value?.trim().toLowerCase();
if (type?.isEmpty ?? true) return null;
if (type == image.name) {
return image;
} else if (type == text.name) {
return text;
} else if (type == voice.name) {
return voice;
} else if (type == custom.name) {
return custom;
}
return null;
}
}

/// Events, Wheter the user is still typing a message or has
Expand All @@ -36,7 +51,27 @@ enum TypeWriterStatus { typing, typed }

/// [MessageStatus] defines the current state of the message
/// if you are sender sending a message then, the
enum MessageStatus { read, delivered, undelivered, pending }
enum MessageStatus {
read,
delivered,
undelivered,
pending;

static MessageStatus? tryParse(String? value) {
final status = value?.trim().toLowerCase();
if (status?.isEmpty ?? true) return null;
if (status == read.name) {
return read;
} else if (status == delivered.name) {
return delivered;
} else if (status == undelivered.name) {
return undelivered;
} else if (status == pending.name) {
return pending;
}
return null;
}
}

/// Types of states
enum ChatViewState { hasMessages, noData, loading, error }
Expand Down

0 comments on commit 9587b2d

Please sign in to comment.