2020-12-03 17:52:53 +01:00
|
|
|
part of 'users_cubit.dart';
|
|
|
|
|
2022-05-17 16:31:34 +03:00
|
|
|
class UsersState extends ServerInstallationDependendState {
|
2022-09-05 14:51:01 +04:00
|
|
|
const UsersState(this.users, this.isLoading);
|
2020-12-03 17:52:53 +01:00
|
|
|
|
|
|
|
final List<User> users;
|
2022-09-05 14:51:01 +04:00
|
|
|
final bool isLoading;
|
2022-09-05 07:01:36 +03:00
|
|
|
|
2022-09-05 07:34:47 +03:00
|
|
|
User get rootUser =>
|
|
|
|
users.firstWhere((final user) => user.type == UserType.root);
|
2022-09-05 07:01:36 +03:00
|
|
|
|
2022-09-05 07:34:47 +03:00
|
|
|
User get primaryUser =>
|
|
|
|
users.firstWhere((final user) => user.type == UserType.primary);
|
2022-09-05 07:01:36 +03:00
|
|
|
|
2022-09-05 07:34:47 +03:00
|
|
|
List<User> get normalUsers =>
|
|
|
|
users.where((final user) => user.type == UserType.normal).toList();
|
2020-12-03 17:52:53 +01:00
|
|
|
|
|
|
|
@override
|
2022-09-05 14:51:01 +04:00
|
|
|
List<Object> get props => [users, isLoading];
|
2022-03-03 20:38:30 +03:00
|
|
|
|
2023-09-16 01:18:37 -03:00
|
|
|
/// Makes a copy of existing users list, but places 'primary'
|
|
|
|
/// to the beginning and sorts the rest alphabetically
|
|
|
|
///
|
|
|
|
/// If found a 'root' user, it doesn't get copied into the result
|
|
|
|
List<User> get orderedUsers {
|
|
|
|
User? primaryUser;
|
|
|
|
final List<User> normalUsers = [];
|
|
|
|
for (final User user in users) {
|
|
|
|
if (user.type == UserType.primary) {
|
|
|
|
primaryUser = user;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (user.type == UserType.root) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
normalUsers.add(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
normalUsers.sort(
|
|
|
|
(final User a, final User b) =>
|
|
|
|
a.login.toLowerCase().compareTo(b.login.toLowerCase()),
|
|
|
|
);
|
|
|
|
|
|
|
|
return primaryUser == null ? normalUsers : [primaryUser] + normalUsers;
|
|
|
|
}
|
|
|
|
|
2022-03-03 20:38:30 +03:00
|
|
|
UsersState copyWith({
|
2022-06-05 22:36:32 +03:00
|
|
|
final List<User>? users,
|
2022-09-05 14:51:01 +04:00
|
|
|
final bool? isLoading,
|
2022-06-06 01:40:34 +03:00
|
|
|
}) =>
|
|
|
|
UsersState(
|
|
|
|
users ?? this.users,
|
2022-09-05 14:51:01 +04:00
|
|
|
isLoading ?? this.isLoading,
|
2022-06-06 01:40:34 +03:00
|
|
|
);
|
2021-01-06 18:35:57 +01:00
|
|
|
|
2022-09-05 07:34:47 +03:00
|
|
|
bool isLoginRegistered(final String login) =>
|
|
|
|
users.any((final User user) => user.login == login);
|
2022-05-04 19:58:47 +03:00
|
|
|
|
2021-01-06 18:35:57 +01:00
|
|
|
bool get isEmpty => users.isEmpty;
|
2020-12-03 17:52:53 +01:00
|
|
|
}
|