feat(ui): minor fixes, add support for name in user.dart

Signed-off-by: xsahil03x <[email protected]>
This commit is contained in:
Sahil Kumar
2021-08-05 16:37:23 +05:30
committed by xsahil03x
parent 27893e147a
commit 46828764c1
5 changed files with 111 additions and 94 deletions
@@ -20,6 +20,8 @@ class OwnUser extends User {
this.channelMutes = const [], this.channelMutes = const [],
required String id, required String id,
String? role, String? role,
String? name,
String? image,
DateTime? createdAt, DateTime? createdAt,
DateTime? updatedAt, DateTime? updatedAt,
DateTime? lastActive, DateTime? lastActive,
@@ -28,10 +30,11 @@ class OwnUser extends User {
bool banned = false, bool banned = false,
List<String> teams = const [], List<String> teams = const [],
String? language, String? language,
String? image,
}) : super( }) : super(
id: id, id: id,
role: role, role: role,
name: name,
image: image,
createdAt: createdAt, createdAt: createdAt,
updatedAt: updatedAt, updatedAt: updatedAt,
lastActive: lastActive, lastActive: lastActive,
@@ -40,7 +43,6 @@ class OwnUser extends User {
banned: banned, banned: banned,
teams: teams, teams: teams,
language: language, language: language,
image: image,
); );
/// Create a new instance from a json /// Create a new instance from a json
@@ -51,6 +53,8 @@ class OwnUser extends User {
factory OwnUser.fromUser(User user) => OwnUser( factory OwnUser.fromUser(User user) => OwnUser(
id: user.id, id: user.id,
role: user.role, role: user.role,
name: user.name,
image: user.image,
createdAt: user.createdAt, createdAt: user.createdAt,
updatedAt: user.updatedAt, updatedAt: user.updatedAt,
lastActive: user.lastActive, lastActive: user.lastActive,
@@ -59,7 +63,6 @@ class OwnUser extends User {
extraData: user.extraData, extraData: user.extraData,
teams: user.teams, teams: user.teams,
language: user.language, language: user.language,
image: user.image,
); );
/// Creates a copy of [OwnUser] with specified attributes overridden. /// Creates a copy of [OwnUser] with specified attributes overridden.
@@ -67,6 +70,8 @@ class OwnUser extends User {
OwnUser copyWith({ OwnUser copyWith({
String? id, String? id,
String? role, String? role,
String? name,
String? image,
DateTime? createdAt, DateTime? createdAt,
DateTime? updatedAt, DateTime? updatedAt,
DateTime? lastActive, DateTime? lastActive,
@@ -80,48 +85,51 @@ class OwnUser extends User {
int? totalUnreadCount, int? totalUnreadCount,
int? unreadChannels, int? unreadChannels,
String? language, String? language,
String? image,
}) => }) =>
OwnUser( OwnUser(
id: id ?? this.id, id: id ?? this.id,
banned: banned ?? this.banned, role: role ?? this.role,
role: role ?? this.role, /* if null, it will be retrieved from extraData['name']*/
createdAt: createdAt ?? this.createdAt, name: name,
updatedAt: updatedAt ?? this.updatedAt, /* if null, it will be retrieved from extraData['image']*/
lastActive: lastActive ?? this.lastActive, image: image,
online: online ?? this.online, banned: banned ?? this.banned,
extraData: extraData ?? this.extraData, createdAt: createdAt ?? this.createdAt,
teams: teams ?? this.teams, updatedAt: updatedAt ?? this.updatedAt,
channelMutes: channelMutes ?? this.channelMutes, lastActive: lastActive ?? this.lastActive,
devices: devices ?? this.devices, online: online ?? this.online,
mutes: mutes ?? this.mutes, extraData: extraData ?? this.extraData,
totalUnreadCount: totalUnreadCount ?? this.totalUnreadCount, teams: teams ?? this.teams,
unreadChannels: unreadChannels ?? this.unreadChannels, channelMutes: channelMutes ?? this.channelMutes,
language: language ?? this.language, devices: devices ?? this.devices,
image: image // if null, it will be retrieved from extraData['image'] mutes: mutes ?? this.mutes,
); totalUnreadCount: totalUnreadCount ?? this.totalUnreadCount,
unreadChannels: unreadChannels ?? this.unreadChannels,
language: language ?? this.language,
);
/// Returns a new [OwnUser] that is a combination of this ownUser /// Returns a new [OwnUser] that is a combination of this ownUser
/// and the given [other] ownUser. /// and the given [other] ownUser.
OwnUser merge(OwnUser? other) { OwnUser merge(OwnUser? other) {
if (other == null) return this; if (other == null) return this;
return copyWith( return copyWith(
id: other.id,
role: other.role,
name: other.name,
image: other.image,
banned: other.banned, banned: other.banned,
channelMutes: other.channelMutes, channelMutes: other.channelMutes,
createdAt: other.createdAt, createdAt: other.createdAt,
devices: other.devices, devices: other.devices,
extraData: other.extraData, extraData: other.extraData,
id: other.id,
lastActive: other.lastActive, lastActive: other.lastActive,
mutes: other.mutes, mutes: other.mutes,
online: other.online, online: other.online,
role: other.role,
teams: other.teams, teams: other.teams,
totalUnreadCount: other.totalUnreadCount, totalUnreadCount: other.totalUnreadCount,
unreadChannels: other.unreadChannels, unreadChannels: other.unreadChannels,
updatedAt: other.updatedAt, updatedAt: other.updatedAt,
language: other.language, language: other.language,
image: other.image,
); );
} }
@@ -40,6 +40,5 @@ OwnUser _$OwnUserFromJson(Map<String, dynamic> json) {
(json['teams'] as List<dynamic>?)?.map((e) => e as String).toList() ?? (json['teams'] as List<dynamic>?)?.map((e) => e as String).toList() ??
[], [],
language: json['language'] as String?, language: json['language'] as String?,
image: json['image'] as String?,
); );
} }
@@ -9,6 +9,18 @@ part 'user.g.dart';
class User extends Equatable { class User extends Equatable {
/// Creates a new user. /// Creates a new user.
/// ///
/// {@template name}
/// If an [name] is provided it will be set on [extraData] with a `key`
/// of 'name'.
///
/// For example:
/// ```dart
/// final user = User(id: 'id', name: 'Sahil Kumar');
/// print(user.name == user.extraData['name']); // true
/// ```
/// {@endtemplate}
///
/// {@template image}
/// If an [image] is provided it will be set on [extraData] with a `key` /// If an [image] is provided it will be set on [extraData] with a `key`
/// of 'image'. /// of 'image'.
/// ///
@@ -17,9 +29,11 @@ class User extends Equatable {
/// final user = User(id: 'id', image: 'https://getstream.io/image.png'); /// final user = User(id: 'id', image: 'https://getstream.io/image.png');
/// print(user.image == user.extraData['image']); // true /// print(user.image == user.extraData['image']); // true
/// ``` /// ```
/// {@endtemplate}
User({ User({
required this.id, required this.id,
this.role, this.role,
String? name,
String? image, String? image,
DateTime? createdAt, DateTime? createdAt,
DateTime? updatedAt, DateTime? updatedAt,
@@ -29,13 +43,14 @@ class User extends Equatable {
this.banned = false, this.banned = false,
this.teams = const [], this.teams = const [],
this.language, this.language,
}) : _image = image, }) : createdAt = createdAt ?? DateTime.now(),
createdAt = createdAt ?? DateTime.now(),
updatedAt = updatedAt ?? DateTime.now(), updatedAt = updatedAt ?? DateTime.now(),
/*For backwards compatibility, set 'name', 'image' in [extraData].*/
// For backwards compatibalitity, set 'image' on [extraData]. extraData = {
extraData = ...extraData,
(image != null) ? {...extraData, 'image': image} : extraData; if (name != null) 'name': name,
if (image != null) 'image': image,
};
/// Create a new instance from json. /// Create a new instance from json.
factory User.fromJson(Map<String, dynamic> json) => factory User.fromJson(Map<String, dynamic> json) =>
@@ -54,62 +69,32 @@ class User extends Equatable {
'banned', 'banned',
'teams', 'teams',
'language', 'language',
'image',
]; ];
/// User id. /// User id.
final String id; final String id;
/// User role. /// Shortcut for user name.
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final String? role;
/// Image for user. This is also set on `extraData['image']`.
/// ///
/// {@template image} /// {@macro name}
/// There are a few ways to set an image. @JsonKey(ignore: true)
/// String get name {
/// Setting an image by passing in an image argument: if (extraData.containsKey('name')) {
/// ```dart final name = extraData['name']! as String;
/// final user = User( if (name.isNotEmpty) return name;
/// id: 'id', }
/// image: 'https://getstream.io/image', return id;
/// ); }
/// ```
///
/// Or by directly setting it in [extraData], for example:
/// ```dart
/// final user = User(
/// id: 'id',
/// extraData: const {'image': 'https://getstream.io/image'},
/// );
///
/// ```
/// Parsing json with an 'image' key will automatically set the `image`
/// property and `extraData['image']` key/value.
///
/// ```dart
/// final user = User.fromJson({
/// id: 'id',
/// image: 'https://getstream.io/image', // key: image
/// });
///
/// print(user.image == user.extraData['image']); // true
/// ```
/// {@endtemplate}
final String? _image;
/// Shortcut for user image. /// Shortcut for user image.
/// ///
/// {@macro image} /// {@macro image}
@JsonKey(ignore: true)
String? get image => extraData['image'] as String?;
/// User role.
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
String? get image { final String? role;
if (_image != null) {
return _image;
} else {
return extraData['image'] as String?;
}
}
/// User teams /// User teams
@JsonKey( @JsonKey(
@@ -152,15 +137,6 @@ class User extends Equatable {
@JsonKey(includeIfNull: false) @JsonKey(includeIfNull: false)
final String? language; final String? language;
/// Shortcut for user name.
String get name {
if (extraData.containsKey('name')) {
final name = extraData['name']! as String;
if (name.isNotEmpty) return name;
}
return id;
}
/// List of users to list of userIds. /// List of users to list of userIds.
static List<String>? toIds(List<User>? users) => static List<String>? toIds(List<User>? users) =>
users?.map((u) => u.id).toList(); users?.map((u) => u.id).toList();
@@ -174,6 +150,8 @@ class User extends Equatable {
User copyWith({ User copyWith({
String? id, String? id,
String? role, String? role,
String? name,
String? image,
DateTime? createdAt, DateTime? createdAt,
DateTime? updatedAt, DateTime? updatedAt,
DateTime? lastActive, DateTime? lastActive,
@@ -182,11 +160,14 @@ class User extends Equatable {
bool? banned, bool? banned,
List<String>? teams, List<String>? teams,
String? language, String? language,
String? image,
}) => }) =>
User( User(
id: id ?? this.id, id: id ?? this.id,
role: role ?? this.role, role: role ?? this.role,
/* if null, it will be retrieved from extraData['name']*/
name: name,
/* if null, it will be retrieved from extraData['image']*/
image: image,
createdAt: createdAt ?? this.createdAt, createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt, updatedAt: updatedAt ?? this.updatedAt,
lastActive: lastActive ?? this.lastActive, lastActive: lastActive ?? this.lastActive,
@@ -195,9 +176,8 @@ class User extends Equatable {
banned: banned ?? this.banned, banned: banned ?? this.banned,
teams: teams ?? this.teams, teams: teams ?? this.teams,
language: language ?? this.language, language: language ?? this.language,
image: image, // if null, it will be retrieved from extraData['image']
); );
@override @override
List<Object?> get props => [id]; List<Object?> get props => [id, role];
} }
@@ -10,7 +10,6 @@ User _$UserFromJson(Map<String, dynamic> json) {
return User( return User(
id: json['id'] as String, id: json['id'] as String,
role: json['role'] as String?, role: json['role'] as String?,
image: json['image'] as String?,
createdAt: json['created_at'] == null createdAt: json['created_at'] == null
? null ? null
: DateTime.parse(json['created_at'] as String), : DateTime.parse(json['created_at'] as String),
@@ -42,7 +41,6 @@ Map<String, dynamic> _$UserToJson(User instance) {
} }
writeNotNull('role', readonly(instance.role)); writeNotNull('role', readonly(instance.role));
writeNotNull('image', readonly(instance.image));
writeNotNull('teams', readonly(instance.teams)); writeNotNull('teams', readonly(instance.teams));
writeNotNull('created_at', readonly(instance.createdAt)); writeNotNull('created_at', readonly(instance.createdAt));
writeNotNull('updated_at', readonly(instance.updatedAt)); writeNotNull('updated_at', readonly(instance.updatedAt));
@@ -45,9 +45,9 @@ void main() {
final user = User( final user = User(
id: id, id: id,
role: role, role: role,
name: name,
image: image, image: image,
extraData: const { extraData: const {
'name': name,
'extraDataStringTest': extraDataStringTest, 'extraDataStringTest': extraDataStringTest,
'extraDataIntTest': extraDataIntTest, 'extraDataIntTest': extraDataIntTest,
'extraDataDoubleTest': extraDataDoubleTest, 'extraDataDoubleTest': extraDataDoubleTest,
@@ -93,9 +93,7 @@ void main() {
newUser = user.copyWith( newUser = user.copyWith(
id: 'test', id: 'test',
role: 'test', role: 'test',
extraData: { name: 'test',
'name': 'test',
},
image: 'https://stream.io/new-image', image: 'https://stream.io/new-image',
online: false, online: false,
banned: false, banned: false,
@@ -120,6 +118,40 @@ void main() {
expect(newUser.language, 'it'); expect(newUser.language, 'it');
}); });
test('name property and extraData manipulation', () {
final user = User(id: id, name: name);
expect(user.name, name);
expect(user.extraData['name'], name);
expect(user.toJson(), {'id': id, 'name': name});
expect(User.fromJson(user.toJson()).toJson(), {'id': id, 'name': name});
const nameOne = 'Name One';
var newUser = user.copyWith(
extraData: {'name': nameOne},
);
expect(newUser.extraData['name'], nameOne);
expect(newUser.name, nameOne);
const nameTwo = 'Name Two';
newUser = user.copyWith(
name: nameTwo,
);
expect(newUser.extraData['name'], nameTwo);
expect(newUser.name, nameTwo);
const nameThree = 'Name Three';
newUser = user.copyWith(
name: nameThree,
extraData: {'name': nameThree},
);
expect(newUser.extraData['name'], nameThree);
expect(newUser.name, nameThree);
});
test('image property and extraData manipulation', () { test('image property and extraData manipulation', () {
final user = User(id: id, image: image); final user = User(id: id, image: image);