add copywith and merge to ownuser
This commit is contained in:
@@ -3,6 +3,7 @@ import 'package:stream_chat/src/core/models/device.dart';
|
|||||||
import 'package:stream_chat/src/core/models/mute.dart';
|
import 'package:stream_chat/src/core/models/mute.dart';
|
||||||
import 'package:stream_chat/src/core/models/user.dart';
|
import 'package:stream_chat/src/core/models/user.dart';
|
||||||
import 'package:stream_chat/src/core/util/serializer.dart';
|
import 'package:stream_chat/src/core/util/serializer.dart';
|
||||||
|
import 'package:stream_chat/stream_chat.dart';
|
||||||
|
|
||||||
part 'own_user.g.dart';
|
part 'own_user.g.dart';
|
||||||
|
|
||||||
@@ -25,6 +26,7 @@ class OwnUser extends User {
|
|||||||
bool online = false,
|
bool online = false,
|
||||||
Map<String, Object?> extraData = const {},
|
Map<String, Object?> extraData = const {},
|
||||||
bool banned = false,
|
bool banned = false,
|
||||||
|
List<String> teams = const [],
|
||||||
}) : super(
|
}) : super(
|
||||||
id: id,
|
id: id,
|
||||||
role: role,
|
role: role,
|
||||||
@@ -34,6 +36,7 @@ class OwnUser extends User {
|
|||||||
online: online,
|
online: online,
|
||||||
extraData: extraData,
|
extraData: extraData,
|
||||||
banned: banned,
|
banned: banned,
|
||||||
|
teams: teams,
|
||||||
);
|
);
|
||||||
|
|
||||||
/// Create a new instance from a json
|
/// Create a new instance from a json
|
||||||
@@ -50,8 +53,69 @@ class OwnUser extends User {
|
|||||||
online: user.online,
|
online: user.online,
|
||||||
banned: user.banned,
|
banned: user.banned,
|
||||||
extraData: user.extraData,
|
extraData: user.extraData,
|
||||||
|
teams: user.teams,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/// Creates a copy of [OwnUser] with specified attributes overridden.
|
||||||
|
@override
|
||||||
|
OwnUser copyWith({
|
||||||
|
String? id,
|
||||||
|
String? role,
|
||||||
|
DateTime? createdAt,
|
||||||
|
DateTime? updatedAt,
|
||||||
|
DateTime? lastActive,
|
||||||
|
bool? online,
|
||||||
|
Map<String, Object?>? extraData,
|
||||||
|
bool? banned,
|
||||||
|
List<String>? teams,
|
||||||
|
List<Mute>? channelMutes,
|
||||||
|
List<Device>? devices,
|
||||||
|
List<Mute>? mutes,
|
||||||
|
int? totalUnreadCount,
|
||||||
|
int? unreadChannels,
|
||||||
|
}) =>
|
||||||
|
OwnUser(
|
||||||
|
id: id ?? this.id,
|
||||||
|
banned: banned ?? this.banned,
|
||||||
|
role: role ?? this.role,
|
||||||
|
createdAt: createdAt ?? this.createdAt,
|
||||||
|
updatedAt: updatedAt ?? this.updatedAt,
|
||||||
|
lastActive: lastActive ?? this.lastActive,
|
||||||
|
online: online ?? this.online,
|
||||||
|
extraData: extraData ?? this.extraData,
|
||||||
|
teams: teams ?? this.teams,
|
||||||
|
channelMutes: channelMutes ?? this.channelMutes,
|
||||||
|
devices: devices ?? this.devices,
|
||||||
|
mutes: mutes ?? this.mutes,
|
||||||
|
totalUnreadCount: totalUnreadCount ?? this.totalUnreadCount,
|
||||||
|
unreadChannels: unreadChannels ?? this.unreadChannels,
|
||||||
|
);
|
||||||
|
|
||||||
|
/// Returns a new [OwnUser] that is a combination of this ownUser
|
||||||
|
/// and the given [other] ownUser.
|
||||||
|
OwnUser merge(OwnUser? other) {
|
||||||
|
if (other == null) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
return copyWith(
|
||||||
|
banned: other.banned,
|
||||||
|
channelMutes: other.channelMutes,
|
||||||
|
createdAt: other.createdAt,
|
||||||
|
devices: other.devices,
|
||||||
|
extraData: other.extraData,
|
||||||
|
id: other.id,
|
||||||
|
lastActive: other.lastActive,
|
||||||
|
mutes: other.mutes,
|
||||||
|
online: other.online,
|
||||||
|
role: other.role,
|
||||||
|
teams: other.teams,
|
||||||
|
totalUnreadCount: other.totalUnreadCount,
|
||||||
|
unreadChannels: other.unreadChannels,
|
||||||
|
updatedAt: other.updatedAt,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/// List of user devices
|
/// List of user devices
|
||||||
@JsonKey(includeIfNull: false, defaultValue: <Device>[])
|
@JsonKey(includeIfNull: false, defaultValue: <Device>[])
|
||||||
final List<Device> devices;
|
final List<Device> devices;
|
||||||
|
|||||||
@@ -47,9 +47,10 @@ class User extends Equatable {
|
|||||||
|
|
||||||
/// User role
|
/// User role
|
||||||
@JsonKey(
|
@JsonKey(
|
||||||
includeIfNull: false,
|
includeIfNull: false,
|
||||||
toJson: Serializer.readOnly,
|
toJson: Serializer.readOnly,
|
||||||
defaultValue: <String>[])
|
defaultValue: <String>[],
|
||||||
|
)
|
||||||
final List<String> teams;
|
final List<String> teams;
|
||||||
|
|
||||||
/// Date of user creation
|
/// Date of user creation
|
||||||
|
|||||||
@@ -41,5 +41,43 @@ void main() {
|
|||||||
expect(ownUser.banned, user.banned);
|
expect(ownUser.banned, user.banned);
|
||||||
expect(ownUser.extraData, user.extraData);
|
expect(ownUser.extraData, user.extraData);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('copyWith', () {
|
||||||
|
final user = OwnUser.fromJson(jsonFixture('own_user.json'));
|
||||||
|
var newUser = user.copyWith();
|
||||||
|
|
||||||
|
expect(newUser.id, user.id);
|
||||||
|
expect(newUser.role, user.role);
|
||||||
|
expect(newUser.name, user.name);
|
||||||
|
|
||||||
|
newUser = user.copyWith(
|
||||||
|
id: 'test',
|
||||||
|
role: 'test',
|
||||||
|
extraData: {
|
||||||
|
'name': 'test',
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(newUser.id, 'test');
|
||||||
|
expect(newUser.role, 'test');
|
||||||
|
expect(newUser.name, 'test');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('merge', () {
|
||||||
|
final user = OwnUser.fromJson(jsonFixture('own_user.json'));
|
||||||
|
final newUser = user.merge(OwnUser(
|
||||||
|
id: 'test',
|
||||||
|
role: 'test',
|
||||||
|
extraData: const {
|
||||||
|
'name': 'test',
|
||||||
|
},
|
||||||
|
banned: true,
|
||||||
|
));
|
||||||
|
|
||||||
|
expect(newUser.id, 'test');
|
||||||
|
expect(newUser.role, 'test');
|
||||||
|
expect(newUser.name, 'test');
|
||||||
|
expect(newUser.banned, true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user