refactor(ui): Make users be ordered properly on users page #343

Merged
NaiJi merged 3 commits from users-order into master 2023-09-18 18:32:57 +03:00
3 changed files with 31 additions and 13 deletions

View File

@ -18,6 +18,32 @@ class UsersState extends ServerInstallationDependendState {
@override
List<Object> get props => [users, isLoading];
/// 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;
}
UsersState copyWith({
final List<User>? users,
final bool? isLoading,

View File

@ -3,11 +3,11 @@ part of 'users.dart';
class _User extends StatelessWidget {
const _User({
required this.user,
required this.isRootUser,
NaiJi marked this conversation as resolved Outdated

Because it's NOT ROOT USER! We filter UserType.root away in the first place!

Because it's NOT ROOT USER! We filter UserType.root away in the first place!
required this.isPrimaryUser,
NaiJi marked this conversation as resolved Outdated

isPrimaryUser

isPrimaryUser
});
final User user;
final bool isRootUser;
final bool isPrimaryUser;
@override
Widget build(final BuildContext context) => InkWell(
onTap: () {
@ -32,7 +32,7 @@ class _User extends StatelessWidget {
user.login,
style: Theme.of(context).textTheme.titleMedium?.copyWith(
color: Theme.of(context).colorScheme.onBackground,
decoration: isRootUser
decoration: isPrimaryUser
? TextDecoration.underline
: user.isFoundOnServer
? TextDecoration.none

View File

@ -45,15 +45,7 @@ class UsersPage extends StatelessWidget {
} else {
child = BlocBuilder<UsersCubit, UsersState>(
builder: (final BuildContext context, final UsersState state) {
final List<User> users = state.users
NaiJi marked this conversation as resolved Outdated

Ordering is rather business logic, not UI

Ordering is rather business logic, not UI
.where((final user) => user.type != UserType.root)
.toList();
// final List<User> users = [];
users.sort(
(final User a, final User b) =>
a.login.toLowerCase().compareTo(b.login.toLowerCase()),
);
final users = state.orderedUsers;
if (users.isEmpty) {
if (state.isLoading) {
return const Center(
@ -115,7 +107,7 @@ class UsersPage extends StatelessWidget {
itemBuilder:
(final BuildContext context, final int index) => _User(
user: users[index],
isRootUser: users[index].type == UserType.primary,
isPrimaryUser: users[index].type == UserType.primary,
),
),
),