selfprivacy.org.app/lib/logic/bloc/users/users_state.dart

74 lines
1.8 KiB
Dart
Raw Normal View History

part of 'users_bloc.dart';
2020-12-03 16:52:53 +00:00
sealed class UsersState extends Equatable {
UsersState({
required final List<User> users,
}) : _hashCode = Object.hashAll(users);
2020-12-03 16:52:53 +00:00
final int _hashCode;
List<User> get users =>
getIt<ApiConnectionRepository>().apiData.users.data ?? const [];
2022-09-05 04:34:47 +00:00
User get rootUser =>
users.firstWhere((final user) => user.type == UserType.root);
2022-09-05 04:34:47 +00:00
User get primaryUser =>
users.firstWhere((final user) => user.type == UserType.primary);
2022-09-05 04:34:47 +00:00
List<User> get normalUsers =>
users.where((final user) => user.type == UserType.normal).toList();
2020-12-03 16:52:53 +00: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-09-05 04:34:47 +00:00
bool isLoginRegistered(final String login) =>
users.any((final User user) => user.login == login);
2021-01-06 17:35:57 +00:00
bool get isEmpty => users.isEmpty;
2020-12-03 16:52:53 +00:00
}
class UsersInitial extends UsersState {
UsersInitial() : super(users: const []);
@override
List<Object> get props => [_hashCode];
}
class UsersRefreshing extends UsersState {
UsersRefreshing({required super.users});
@override
List<Object> get props => [_hashCode];
}
class UsersLoaded extends UsersState {
UsersLoaded({required super.users});
@override
List<Object> get props => [_hashCode];
}