feat: Converted to ios models part II

This commit is contained in:
Deven Joshi
2021-04-14 14:56:04 +05:30
parent 97c35371af
commit 88f9bcc80c
32 changed files with 217 additions and 253 deletions
@@ -1312,7 +1312,7 @@ class ChannelClientState {
updateChannelState(channelState!.copyWith(
members: [
...channelState!.members,
member,
member!,
],
));
}));
@@ -1323,7 +1323,7 @@ class ChannelClientState {
final user = e.user;
updateChannelState(channelState!.copyWith(
members: List.from(
channelState!.members..removeWhere((m) => m!.userId == user!.id)),
channelState!.members..removeWhere((m) => m.userId == user!.id)),
));
}));
}
@@ -1549,7 +1549,7 @@ class ChannelClientState {
/// Channel members list
List<Member> get members => _channelState!.members
.map((e) => e!.copyWith(user: _channel.client.state!.users[e.user!.id]))
.map((e) => e.copyWith(user: _channel.client.state!.users[e.user!.id]))
.toList();
/// Channel members list as a stream
@@ -1664,7 +1664,7 @@ class ChannelClientState {
[],
];
final newMembers = <Member?>[
final newMembers = <Member>[
...updatedState.members,
];
+3 -3
View File
@@ -311,7 +311,7 @@ class StreamChatClient {
httpClient.unlock();
await connectUser(User.temp(id: userId), newToken);
await connectUser(User(id: userId), newToken);
try {
handler.resolve(
@@ -753,7 +753,7 @@ class StreamChatClient {
final users = channels
.expand((it) => it.members)
.map((it) => it!.user)
.map((it) => it.user)
.toList(growable: false);
state!._updateUsers(users);
@@ -956,7 +956,7 @@ class StreamChatClient {
_anonymous = true;
const uuid = Uuid();
state!.user = OwnUser.temp(id: uuid.v4());
state!.user = OwnUser(id: uuid.v4());
return connect().then((event) {
_connectCompleter!.complete(event);
@@ -76,7 +76,7 @@ abstract class ChatPersistenceClient {
getPinnedMessagesByCid(cid, messagePagination: pinnedMessagePagination),
]);
return ChannelState(
members: (data[0] as List<Member?>?)!,
members: (data[0] as List<Member>?)!,
read: (data[1] as List<Read>?)!,
channel: data[2] as ChannelModel?,
messages: (data[3] as List<Message>?)!,
@@ -212,7 +212,7 @@ abstract class ChatPersistenceClient {
])
.expand((v) => v),
...cs.read.map((r) => r.user),
...cs.members.map((m) => m!.user),
...cs.members.map((m) => m.user),
])
.expand((it) => it)
.where((it) => it != null);
@@ -15,7 +15,7 @@ class Attachment extends Equatable {
/// Constructor used for json serialization
Attachment({
String? id,
required this.type,
String? type,
this.titleLink,
String? title,
this.thumbUrl,
@@ -38,6 +38,7 @@ class Attachment extends Equatable {
UploadState? uploadState,
}) : id = id ?? const Uuid().v4(),
title = title ?? file?.name,
type = type ?? '',
localUri = file?.path != null ? Uri.parse(file!.path!) : null {
this.uploadState = uploadState ??
((assetUrl != null || imageUrl != null)
@@ -9,7 +9,7 @@ part of 'attachment.dart';
Attachment _$AttachmentFromJson(Map<String, dynamic> json) {
return Attachment(
id: json['id'] as String?,
type: json['type'] as String,
type: json['type'] as String?,
titleLink: json['title_link'] as String?,
title: json['title'] as String?,
thumbUrl: json['thumb_url'] as String?,
@@ -12,33 +12,20 @@ class ChannelModel {
ChannelModel({
this.id,
this.type,
required this.cid,
required this.config,
this.cid = '',
ChannelConfig? config,
this.createdBy,
this.frozen = false,
this.lastMessageAt,
required this.createdAt,
required this.updatedAt,
DateTime? createdAt,
DateTime? updatedAt,
this.deletedAt,
this.memberCount = 0,
this.extraData,
this.team,
});
ChannelModel.temp({
this.id,
this.type,
required this.cid,
this.createdBy,
this.frozen = false,
this.lastMessageAt,
this.deletedAt,
this.memberCount = 0,
this.extraData,
this.team,
}) : createdAt = DateTime.now(),
updatedAt = DateTime.now(),
config = ChannelConfig();
}) : config = config ?? ChannelConfig(),
createdAt = createdAt ?? DateTime.now(),
updatedAt = updatedAt ?? DateTime.now();
/// Create a new instance from a json
factory ChannelModel.fromJson(Map<String, dynamic>? json) =>
@@ -64,7 +51,7 @@ class ChannelModel {
final User? createdBy;
/// True if this channel is frozen
@JsonKey(includeIfNull: false)
@JsonKey(includeIfNull: false, defaultValue: false)
final bool frozen;
/// The date of the last message
@@ -84,7 +71,8 @@ class ChannelModel {
final DateTime? deletedAt;
/// The count of this channel members
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
@JsonKey(
includeIfNull: false, toJson: Serialization.readOnly, defaultValue: 0)
final int memberCount;
/// Map of custom channel extraData
@@ -11,20 +11,26 @@ ChannelModel _$ChannelModelFromJson(Map<String, dynamic> json) {
id: json['id'] as String?,
type: json['type'] as String?,
cid: json['cid'] as String,
config: ChannelConfig.fromJson(json['config'] as Map<String, dynamic>),
config: json['config'] == null
? null
: ChannelConfig.fromJson(json['config'] as Map<String, dynamic>),
createdBy: json['created_by'] == null
? null
: User.fromJson(json['created_by'] as Map<String, dynamic>?),
frozen: json['frozen'] as bool,
frozen: json['frozen'] as bool? ?? false,
lastMessageAt: json['last_message_at'] == null
? null
: DateTime.parse(json['last_message_at'] as String),
createdAt: DateTime.parse(json['created_at'] as String),
updatedAt: DateTime.parse(json['updated_at'] as String),
createdAt: json['created_at'] == null
? null
: DateTime.parse(json['created_at'] as String),
updatedAt: json['updated_at'] == null
? null
: DateTime.parse(json['updated_at'] as String),
deletedAt: json['deleted_at'] == null
? null
: DateTime.parse(json['deleted_at'] as String),
memberCount: json['member_count'] as int,
memberCount: json['member_count'] as int? ?? 0,
extraData: json['extra_data'] as Map<String, dynamic>?,
team: json['team'] as String?,
);
@@ -25,21 +25,26 @@ class ChannelState {
final ChannelModel? channel;
/// A paginated list of channel messages
@JsonKey(defaultValue: <Message>[])
final List<Message> messages;
/// A paginated list of channel members
final List<Member?> members;
@JsonKey(defaultValue: <Member>[])
final List<Member> members;
/// A paginated list of pinned messages
@JsonKey(defaultValue: <Message>[])
final List<Message> pinnedMessages;
/// The count of users watching the channel
final int? watcherCount;
/// A paginated list of users watching the channel
@JsonKey(defaultValue: <User>[])
final List<User> watchers;
/// The list of channel reads
@JsonKey(defaultValue: <Read>[])
final List<Read> read;
/// Create a new instance from a json
@@ -53,7 +58,7 @@ class ChannelState {
ChannelState copyWith({
ChannelModel? channel,
List<Message>? messages,
List<Member?>? members,
List<Member>? members,
List<Message>? pinnedMessages,
int? watcherCount,
List<User>? watchers,
@@ -11,23 +11,27 @@ ChannelState _$ChannelStateFromJson(Map<String, dynamic> json) {
channel: json['channel'] == null
? null
: ChannelModel.fromJson(json['channel'] as Map<String, dynamic>?),
messages: (json['messages'] as List<dynamic>)
.map((e) => Message.fromJson(e as Map<String, dynamic>?))
.toList(),
members: (json['members'] as List<dynamic>)
.map((e) =>
e == null ? null : Member.fromJson(e as Map<String, dynamic>))
.toList(),
pinnedMessages: (json['pinned_messages'] as List<dynamic>)
.map((e) => Message.fromJson(e as Map<String, dynamic>?))
.toList(),
messages: (json['messages'] as List<dynamic>?)
?.map((e) => Message.fromJson(e as Map<String, dynamic>?))
.toList() ??
[],
members: (json['members'] as List<dynamic>?)
?.map((e) => Member.fromJson(e as Map<String, dynamic>))
.toList() ??
[],
pinnedMessages: (json['pinned_messages'] as List<dynamic>?)
?.map((e) => Message.fromJson(e as Map<String, dynamic>?))
.toList() ??
[],
watcherCount: json['watcher_count'] as int?,
watchers: (json['watchers'] as List<dynamic>)
.map((e) => User.fromJson(e as Map<String, dynamic>?))
.toList(),
read: (json['read'] as List<dynamic>)
.map((e) => Read.fromJson(e as Map<String, dynamic>))
.toList(),
watchers: (json['watchers'] as List<dynamic>?)
?.map((e) => User.fromJson(e as Map<String, dynamic>?))
.toList() ??
[],
read: (json['read'] as List<dynamic>?)
?.map((e) => Read.fromJson(e as Map<String, dynamic>))
.toList() ??
[],
);
}
@@ -35,7 +39,7 @@ Map<String, dynamic> _$ChannelStateToJson(ChannelState instance) =>
<String, dynamic>{
'channel': instance.channel?.toJson(),
'messages': instance.messages.map((e) => e.toJson()).toList(),
'members': instance.members.map((e) => e?.toJson()).toList(),
'members': instance.members.map((e) => e.toJson()).toList(),
'pinned_messages':
instance.pinnedMessages.map((e) => e.toJson()).toList(),
'watcher_count': instance.watcherCount,
@@ -7,7 +7,7 @@ part 'device.g.dart';
class Device {
/// Constructor used for json serialization
Device({
required this.id,
this.id = '',
this.pushProvider,
});
@@ -85,7 +85,7 @@ EventChannel _$EventChannelFromJson(Map<String, dynamic> json) {
createdBy: json['created_by'] == null
? null
: User.fromJson(json['created_by'] as Map<String, dynamic>?),
frozen: json['frozen'] as bool,
frozen: json['frozen'] as bool? ?? false,
lastMessageAt: json['last_message_at'] == null
? null
: DateTime.parse(json['last_message_at'] as String),
@@ -94,7 +94,7 @@ EventChannel _$EventChannelFromJson(Map<String, dynamic> json) {
deletedAt: json['deleted_at'] == null
? null
: DateTime.parse(json['deleted_at'] as String),
memberCount: json['member_count'] as int,
memberCount: json['member_count'] as int? ?? 0,
extraData: json['extra_data'] as Map<String, dynamic>?,
);
}
@@ -13,14 +13,15 @@ class Member {
this.inviteAcceptedAt,
this.inviteRejectedAt,
this.invited = false,
required this.role,
this.role = '',
this.userId,
this.isModerator,
required this.createdAt,
required this.updatedAt,
DateTime? createdAt,
DateTime? updatedAt,
this.banned = false,
this.shadowBanned = false,
});
}) : createdAt = createdAt ?? DateTime.now(),
updatedAt = updatedAt ?? DateTime.now();
/// Create a new instance from a json
factory Member.fromJson(Map<String, dynamic> json) {
@@ -40,9 +41,11 @@ class Member {
final DateTime? inviteRejectedAt;
/// True if the user has been invited to the channel
@JsonKey(defaultValue: false)
final bool invited;
/// The role of the user in the channel
@JsonKey(defaultValue: '')
final String role;
/// The id of the interested user
@@ -52,9 +55,11 @@ class Member {
final bool? isModerator;
/// True if the member is banned from the channel
@JsonKey(defaultValue: false)
final bool banned;
/// True if the member is shadow banned from the channel
@JsonKey(defaultValue: false)
final bool shadowBanned;
/// The date of creation
@@ -17,14 +17,18 @@ Member _$MemberFromJson(Map<String, dynamic> json) {
inviteRejectedAt: json['invite_rejected_at'] == null
? null
: DateTime.parse(json['invite_rejected_at'] as String),
invited: json['invited'] as bool,
role: json['role'] as String,
invited: json['invited'] as bool? ?? false,
role: json['role'] as String? ?? '',
userId: json['user_id'] as String?,
isModerator: json['is_moderator'] as bool?,
createdAt: DateTime.parse(json['created_at'] as String),
updatedAt: DateTime.parse(json['updated_at'] as String),
banned: json['banned'] as bool,
shadowBanned: json['shadow_banned'] as bool,
createdAt: json['created_at'] == null
? null
: DateTime.parse(json['created_at'] as String),
updatedAt: json['updated_at'] == null
? null
: DateTime.parse(json['updated_at'] as String),
banned: json['banned'] as bool? ?? false,
shadowBanned: json['shadow_banned'] as bool? ?? false,
);
}
@@ -46,41 +46,8 @@ class Message extends Equatable {
/// Constructor used for json serialization
Message({
String? id,
required this.text,
required this.type,
this.attachments,
this.mentionedUsers,
this.silent,
this.shadowed,
this.reactionCounts,
this.reactionScores,
this.latestReactions,
this.ownReactions,
this.parentId,
this.quotedMessage,
this.quotedMessageId,
this.replyCount = 0,
this.threadParticipants,
this.showInChannel,
this.command,
required this.createdAt,
required this.updatedAt,
this.user,
this.pinned = false,
this.pinnedAt,
DateTime? pinExpires,
this.pinnedBy,
this.extraData,
this.deletedAt,
this.status = MessageSendingStatus.sent,
this.skipPush,
}) : id = id ?? const Uuid().v4(),
pinExpires = pinExpires?.toUtc();
/// Constructor for creating temporary/throwaway message with id and text
Message.temp({
String? id,
required this.text,
this.text = '',
this.type = '',
this.attachments,
this.mentionedUsers,
this.silent,
@@ -96,6 +63,8 @@ class Message extends Equatable {
this.threadParticipants,
this.showInChannel,
this.command,
DateTime? createdAt,
DateTime? updatedAt,
this.user,
this.pinned = false,
this.pinnedAt,
@@ -107,9 +76,8 @@ class Message extends Equatable {
this.skipPush,
}) : id = id ?? const Uuid().v4(),
pinExpires = pinExpires?.toUtc(),
createdAt = DateTime.now(),
updatedAt = DateTime.now(),
type = '';
createdAt = createdAt ?? DateTime.now(),
updatedAt = updatedAt ?? DateTime.now();
/// Create a new instance from a json
factory Message.fromJson(Map<String, dynamic>? json) => _$MessageFromJson(
@@ -418,12 +386,7 @@ class Message extends Equatable {
@JsonSerializable()
class TranslatedMessage extends Message {
/// Constructor used for json serialization
TranslatedMessage(this.i18n)
: super(
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
text: '',
type: '');
TranslatedMessage(this.i18n) : super();
/// Create a new instance from a json
factory TranslatedMessage.fromJson(Map<String, dynamic>? json) =>
@@ -42,8 +42,12 @@ Message _$MessageFromJson(Map<String, dynamic> json) {
.toList(),
showInChannel: json['show_in_channel'] as bool?,
command: json['command'] as String?,
createdAt: DateTime.parse(json['created_at'] as String),
updatedAt: DateTime.parse(json['updated_at'] as String),
createdAt: json['created_at'] == null
? null
: DateTime.parse(json['created_at'] as String),
updatedAt: json['updated_at'] == null
? null
: DateTime.parse(json['updated_at'] as String),
user: json['user'] == null
? null
: User.fromJson(json['user'] as Map<String, dynamic>?),
@@ -17,10 +17,10 @@ class OwnUser extends User {
this.totalUnreadCount = 0,
this.unreadChannels,
this.channelMutes = const [],
required String id,
required String role,
required DateTime createdAt,
required DateTime updatedAt,
String id = '',
String role = '',
DateTime? createdAt,
DateTime? updatedAt,
DateTime? lastActive,
bool online = false,
Map<String, dynamic> extraData = const {},
@@ -36,44 +36,34 @@ class OwnUser extends User {
banned: banned,
);
/// Create a temporary/throwaway user with an ID
OwnUser.temp({
required String id,
this.devices = const [],
this.mutes = const [],
this.totalUnreadCount = 0,
this.unreadChannels,
this.channelMutes = const [],
DateTime? lastActive,
bool online = false,
Map<String, dynamic> extraData = const {},
bool banned = false,
}) : super.temp(
id: id,
lastActive: lastActive,
online: online,
extraData: extraData,
banned: banned,
);
/// Create a new instance from a json
factory OwnUser.fromJson(Map<String, dynamic>? json) => _$OwnUserFromJson(
Serialization.moveToExtraDataFromRoot(json, topLevelFields)!);
/// List of user devices
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
@JsonKey(
includeIfNull: false,
toJson: Serialization.readOnly,
defaultValue: <Device>[])
final List<Device> devices;
/// List of users muted by the user
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
@JsonKey(
includeIfNull: false,
toJson: Serialization.readOnly,
defaultValue: <Mute>[])
final List<Mute> mutes;
/// List of users muted by the user
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
@JsonKey(
includeIfNull: false,
toJson: Serialization.readOnly,
defaultValue: <Mute>[])
final List<Mute> channelMutes;
/// Total unread messages by the user
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
@JsonKey(
includeIfNull: false, toJson: Serialization.readOnly, defaultValue: 0)
final int totalUnreadCount;
/// Total unread channels by the user
@@ -8,27 +8,34 @@ part of 'own_user.dart';
OwnUser _$OwnUserFromJson(Map<String, dynamic> json) {
return OwnUser(
devices: (json['devices'] as List<dynamic>)
.map((e) => Device.fromJson(e as Map<String, dynamic>))
.toList(),
mutes: (json['mutes'] as List<dynamic>)
.map((e) => Mute.fromJson(e as Map<String, dynamic>))
.toList(),
totalUnreadCount: json['total_unread_count'] as int,
devices: (json['devices'] as List<dynamic>?)
?.map((e) => Device.fromJson(e as Map<String, dynamic>))
.toList() ??
[],
mutes: (json['mutes'] as List<dynamic>?)
?.map((e) => Mute.fromJson(e as Map<String, dynamic>))
.toList() ??
[],
totalUnreadCount: json['total_unread_count'] as int? ?? 0,
unreadChannels: json['unread_channels'] as int?,
channelMutes: (json['channel_mutes'] as List<dynamic>)
.map((e) => Mute.fromJson(e as Map<String, dynamic>))
.toList(),
channelMutes: (json['channel_mutes'] as List<dynamic>?)
?.map((e) => Mute.fromJson(e as Map<String, dynamic>))
.toList() ??
[],
id: json['id'] as String,
role: json['role'] as String,
createdAt: DateTime.parse(json['created_at'] as String),
updatedAt: DateTime.parse(json['updated_at'] as String),
role: json['role'] as String? ?? '',
createdAt: json['created_at'] == null
? null
: DateTime.parse(json['created_at'] as String),
updatedAt: json['updated_at'] == null
? null
: DateTime.parse(json['updated_at'] as String),
lastActive: json['last_active'] == null
? null
: DateTime.parse(json['last_active'] as String),
online: json['online'] as bool,
online: json['online'] as bool? ?? false,
extraData: json['extra_data'] as Map<String, dynamic>,
banned: json['banned'] as bool,
banned: json['banned'] as bool? ?? false,
);
}
@@ -23,6 +23,7 @@ class Read {
final User user;
/// Number of unread messages
@JsonKey(defaultValue: 0)
final int unreadMessages;
/// Serialize to json
@@ -10,7 +10,7 @@ Read _$ReadFromJson(Map<String, dynamic> json) {
return Read(
lastRead: DateTime.parse(json['last_read'] as String),
user: User.fromJson(json['user'] as Map<String, dynamic>?),
unreadMessages: json['unread_messages'] as int,
unreadMessages: json['unread_messages'] as int? ?? 0,
);
}
+15 -20
View File
@@ -8,28 +8,17 @@ part 'user.g.dart';
class User {
/// Constructor used for json serialization
User({
required this.id,
required this.role,
required this.createdAt,
required this.updatedAt,
this.lastActive,
this.online = false,
this.extraData = const {},
this.banned = false,
this.teams = const [],
});
/// Use constructor for a temporary/throwaway user with an ID
User.temp({
required this.id,
this.id = '',
this.role = '',
DateTime? createdAt,
DateTime? updatedAt,
this.lastActive,
this.online = false,
this.extraData = const {},
this.banned = false,
this.teams = const [],
}) : createdAt = DateTime.now(),
updatedAt = DateTime.now();
}) : createdAt = createdAt ?? DateTime.now(),
updatedAt = updatedAt ?? DateTime.now();
/// Create a new instance from a json
factory User.fromJson(Map<String, dynamic>? json) => _$UserFromJson(
@@ -64,11 +53,15 @@ class User {
final String id;
/// User role
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
@JsonKey(
includeIfNull: false, toJson: Serialization.readOnly, defaultValue: '')
final String role;
/// User role
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
@JsonKey(
includeIfNull: false,
toJson: Serialization.readOnly,
defaultValue: <String>[])
final List<String> teams;
/// Date of user creation
@@ -84,11 +77,13 @@ class User {
final DateTime? lastActive;
/// True if user is online
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
@JsonKey(
includeIfNull: false, toJson: Serialization.readOnly, defaultValue: false)
final bool online;
/// True if user is banned from the chat
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
@JsonKey(
includeIfNull: false, toJson: Serialization.readOnly, defaultValue: false)
final bool banned;
/// Map of custom user extraData
@@ -9,16 +9,22 @@ part of 'user.dart';
User _$UserFromJson(Map<String, dynamic> json) {
return User(
id: json['id'] as String,
role: json['role'] as String,
createdAt: DateTime.parse(json['created_at'] as String),
updatedAt: DateTime.parse(json['updated_at'] as String),
role: json['role'] as String? ?? '',
createdAt: json['created_at'] == null
? null
: DateTime.parse(json['created_at'] as String),
updatedAt: json['updated_at'] == null
? null
: DateTime.parse(json['updated_at'] as String),
lastActive: json['last_active'] == null
? null
: DateTime.parse(json['last_active'] as String),
online: json['online'] as bool,
online: json['online'] as bool? ?? false,
extraData: json['extra_data'] as Map<String, dynamic>,
banned: json['banned'] as bool,
teams: (json['teams'] as List<dynamic>).map((e) => e as String).toList(),
banned: json['banned'] as bool? ?? false,
teams:
(json['teams'] as List<dynamic>?)?.map((e) => e as String).toList() ??
[],
);
}