Skip to content

Commit

Permalink
fix: 🐛 added copyWith methods for model
Browse files Browse the repository at this point in the history
  • Loading branch information
apurva010 committed Jun 6, 2024
1 parent 0d936fa commit dc914eb
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 43 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## [2.0.0] (Unreleased)

* **Fix**: [177](https://github.com/SimformSolutionsPvtLtd/flutter_chatview/pull/177) Fixed
* **Breaking**: [177](https://github.com/SimformSolutionsPvtLtd/flutter_chatview/pull/177) Fixed
json serializable of models and added copyWith method (Message, Reaction and Reply Message).
* **Fix**: [182](https://github.com/SimformSolutionsPvtLtd/flutter_chatview/issues/182) Fix
send message not working when user start texting after newLine.
Expand Down
23 changes: 21 additions & 2 deletions lib/src/models/chat_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,34 @@ class ChatUser {
id: json["id"],
name: json["name"],
profilePhoto: json["profilePhoto"],
imageType: json["imageType"],
imageType: ImageType.tryParse(json['imageType']?.toString()) ??
ImageType.network,
defaultAvatarImage: json["defaultAvatarImage"],
);

Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'profilePhoto': profilePhoto,
'imageType': imageType,
'imageType': imageType.name,
'defaultAvatarImage': defaultAvatarImage,
};

ChatUser copyWith({
String? id,
String? name,
String? profilePhoto,
ImageType? imageType,
String? defaultAvatarImage,
bool forceNullValue = false,
}) {
return ChatUser(
id: id ?? this.id,
name: name ?? this.name,
imageType: imageType ?? this.imageType,
profilePhoto:
forceNullValue ? profilePhoto : profilePhoto ?? this.profilePhoto,
defaultAvatarImage: defaultAvatarImage ?? this.defaultAvatarImage,
);
}
}
59 changes: 38 additions & 21 deletions lib/src/models/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,18 @@ class Message {
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>{},
),
replyMessage: json['reply_message'] is Map<String, dynamic>
? ReplyMessage.fromJson(json['reply_message'])
: const ReplyMessage(),
reaction: json['reaction'] is Map<String, dynamic>
? Reaction.fromJson(json['reaction'])
: null,
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,
),
voiceMessageDuration: Duration(
microseconds:
int.tryParse(json['voice_message_duration'].toString()) ?? 0,
),
status: MessageStatus.tryParse(json['status']?.toString()) ??
MessageStatus.pending,
);
Expand All @@ -134,4 +123,32 @@ class Message {
'voice_message_duration': voiceMessageDuration?.inMicroseconds,
'status': status.name,
};

Message copyWith({
String? id,
GlobalKey? key,
String? message,
DateTime? createdAt,
String? sendBy,
ReplyMessage? replyMessage,
Reaction? reaction,
MessageType? messageType,
Duration? voiceMessageDuration,
MessageStatus? status,
bool forceNullValue = false,
}) {
return Message(
id: id ?? this.message,
message: message ?? this.message,
createdAt: createdAt ?? this.createdAt,
sendBy: sendBy ?? this.sendBy,
messageType: messageType ?? this.messageType,
voiceMessageDuration: forceNullValue
? voiceMessageDuration
: voiceMessageDuration ?? this.voiceMessageDuration,
reaction: reaction ?? this.reaction,
replyMessage: replyMessage ?? this.replyMessage,
status: status ?? this.status,
);
}
}
37 changes: 24 additions & 13 deletions lib/src/models/reaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@ class Reaction {
});

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 reactions = <String>[
for (var i = 0; i < reactionsList.length; i++)
if (reactionsList[i]?.toString().isNotEmpty ?? false)
reactionsList[i]!.toString()
];

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!);
}
final reactedUserIds = <String>[
for (var i = 0; i < reactedUserIdList.length; i++)
if (reactedUserIdList[i]?.toString().isNotEmpty ?? false)
reactedUserIdList[i]!.toString()
];

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

/// Provides list of reaction in single message.
Expand All @@ -40,4 +41,14 @@ class Reaction {
'reactions': reactions,
'reactedUserIds': reactedUserIds,
};

Reaction copyWith({
List<String>? reactions,
List<String>? reactedUserIds,
}) {
return Reaction(
reactions: reactions ?? this.reactions,
reactedUserIds: reactedUserIds ?? this.reactedUserIds,
);
}
}
31 changes: 25 additions & 6 deletions lib/src/models/reply_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,10 @@ class ReplyMessage {
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,
),
voiceMessageDuration: Duration(
microseconds:
int.tryParse(json['voiceMessageDuration'].toString()) ?? 0,
),
);

Map<String, dynamic> toJson() => {
Expand All @@ -70,4 +68,25 @@ class ReplyMessage {
'id': messageId,
'voiceMessageDuration': voiceMessageDuration?.inMicroseconds,
};

ReplyMessage copyWith({
String? messageId,
String? message,
String? replyTo,
String? replyBy,
MessageType? messageType,
Duration? voiceMessageDuration,
bool forceNullValue = false,
}) {
return ReplyMessage(
messageId: messageId ?? this.messageId,
message: message ?? this.message,
replyTo: replyTo ?? this.replyTo,
replyBy: replyBy ?? this.replyBy,
messageType: messageType ?? this.messageType,
voiceMessageDuration: forceNullValue
? voiceMessageDuration
: voiceMessageDuration ?? this.voiceMessageDuration,
);
}
}
13 changes: 13 additions & 0 deletions lib/src/values/enumeration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ enum ImageType {
bool get isAsset => this == ImageType.asset;

bool get isBase64 => this == ImageType.base64;

static ImageType? tryParse(String? value) {
final type = value?.trim().toLowerCase();
if (type?.isEmpty ?? true) return null;
if (type == asset.name) {
return asset;
} else if (type == network.name) {
return network;
} else if (type == base64.name) {
return base64;
}
return null;
}
}

extension ChatViewStateExtension on ChatViewState {
Expand Down

0 comments on commit dc914eb

Please sign in to comment.