Commit fd74f1ae authored by Michael Koch's avatar Michael Koch
Browse files

Add admin sorting, remove ambiguity in SortDirection/Param naming

parent 47b6894e
......@@ -3,3 +3,4 @@ export 'src/admins/admins_events.dart';
export 'src/admins/admins_state.dart';
export 'src/models/admin.dart';
export 'src/models/admin_sort.dart';
......@@ -6,6 +6,7 @@ import 'package:logging/logging.dart';
import '../../admins.dart';
import '../network_exception.dart';
import '../models/admin_sort.dart';
import 'admins_events.dart';
import 'admins_state.dart';
......@@ -77,7 +78,11 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
yield AdminsState.result(_admins);
} on NetworkException catch (e) {
_log.severe(e);
}}
}
} else if (event is SortAdmins) {
_sortAdmins(event.param, event.direction);
yield AdminsState.result(_admins);
}
}
void onCreateAdmin(String firstName, String lastName, String password,
......@@ -94,6 +99,9 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
void onSetAdminPassword(int id, String password) =>
add(SetAdminPassword(id, password));
void onSortAdmins(AdminSortParam param, AdminSortDirection direction) =>
add(SortAdmins(param, direction));
void onRefresh() => add(RefreshAdmins());
@override
......@@ -220,4 +228,39 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
}
});
}
void _sortAdmins(AdminSortParam param, AdminSortDirection direction) {
_log.finer('_sortAdmins: $param $direction');
switch (param) {
case AdminSortParam.login:
{
if (direction == AdminSortDirection.ascending) {
_admins.sort((a, b) => a.login.compareTo(b.login));
} else {
_admins.sort((a, b) => b.login.compareTo(a.login));
}
}
break;
case AdminSortParam.firstName:
{
if (direction == AdminSortDirection.ascending) {
_admins.sort((a, b) => a.firstName.compareTo(b.firstName));
} else {
_admins.sort((a, b) => b.firstName.compareTo(a.firstName));
}
}
break;
case AdminSortParam.lastName:
{
if (direction == AdminSortDirection.ascending) {
_admins.sort((a, b) => a.lastName.compareTo(b.lastName));
} else {
_admins.sort((a, b) => b.lastName.compareTo(a.lastName));
}
}
break;
}
}
}
import '../models/admin_sort.dart';
class AddAdmin extends AdminsEvent {
final String firstName;
final String lastName;
......@@ -75,4 +77,16 @@ class DeleteAdmin extends AdminsEvent {
String toString() => toMap().toString();
}
class SortAdmins extends AdminsEvent {
final AdminSortParam param;
final AdminSortDirection direction;
SortAdmins(this.param, this.direction);
Map<String, dynamic> toMap() => <String, dynamic>{'param': param, 'direction': direction};
@override
String toString() => toMap().toString();
}
class RefreshAdmins extends AdminsEvent {}
\ No newline at end of file
......@@ -159,11 +159,11 @@ class JournalBloc extends Bloc<JournalEvent, JournalState> {
.toList();
}
void _sortJournal(SortParam param, SortDirection direction) {
void _sortJournal(JournalSortParam param, JournalSortDirection direction) {
switch (param) {
case SortParam.value:
case JournalSortParam.value:
{
if (direction == SortDirection.ascending) {
if (direction == JournalSortDirection.ascending) {
_journal.sort((a, b) => a.value.compareTo(b.value));
_journalFiltered.sort((a, b) => a.value.compareTo(b.value));
} else {
......@@ -173,9 +173,9 @@ class JournalBloc extends Bloc<JournalEvent, JournalState> {
}
break;
case SortParam.userId:
case JournalSortParam.userId:
{
if (direction == SortDirection.ascending) {
if (direction == JournalSortDirection.ascending) {
_journal.sort((a, b) => a.userId.compareTo(b.userId));
_journalFiltered.sort((a, b) => a.userId.compareTo(b.userId));
} else {
......@@ -185,9 +185,9 @@ class JournalBloc extends Bloc<JournalEvent, JournalState> {
}
break;
case SortParam.adminId:
case JournalSortParam.adminId:
{
if (direction == SortDirection.ascending) {
if (direction == JournalSortDirection.ascending) {
_journal.sort((a, b) => a.adminId.compareTo(b.adminId));
_journalFiltered.sort((a, b) => a.adminId.compareTo(b.adminId));
} else {
......@@ -197,9 +197,9 @@ class JournalBloc extends Bloc<JournalEvent, JournalState> {
}
break;
case SortParam.description:
case JournalSortParam.description:
{
if (direction == SortDirection.ascending) {
if (direction == JournalSortDirection.ascending) {
_journal.sort((a, b) => a.description.compareTo(b.description));
_journalFiltered.sort((a, b) => a.description.compareTo(b.description));
} else {
......@@ -209,9 +209,9 @@ class JournalBloc extends Bloc<JournalEvent, JournalState> {
}
break;
case SortParam.timestamp:
case JournalSortParam.timestamp:
{
if (direction == SortDirection.ascending) {
if (direction == JournalSortDirection.ascending) {
_journal.sort((a, b) => a.timestamp.compareTo(b.timestamp));
_journalFiltered.sort((a, b) => a.timestamp.compareTo(b.timestamp));
} else {
......
......@@ -60,8 +60,8 @@ class FuzzyFilterJournal extends JournalEvent {
}
class SortJournal extends JournalEvent {
final SortParam param;
final SortDirection direction;
final JournalSortParam param;
final JournalSortDirection direction;
SortJournal(this.param, this.direction);
......
enum SortParam {
enum JournalSortParam {
value,
userId,
adminId,
......@@ -6,7 +6,7 @@ enum SortParam {
timestamp
}
enum SortDirection {
enum JournalSortDirection {
ascending,
descending
}
\ No newline at end of file
enum SortParam {
enum PrinterSortParam {
hostname,
ip,
community,
......@@ -10,7 +10,7 @@ enum SortParam {
coinOperated
}
enum SortDirection {
enum PrinterSortDirection {
ascending,
descending
}
\ No newline at end of file
......@@ -127,7 +127,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
void onRefreshPrinterById(int id) => add(GetPrinterDetails(id));
void onSortPrinters(SortParam param, SortDirection direction) =>
void onSortPrinters(PrinterSortParam param, PrinterSortDirection direction) =>
add(SortPrinters(param, direction));
void onStart(String token) => add(InitPrinters(token));
......@@ -359,11 +359,11 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
);
}
void _sortPrinters(SortParam param, SortDirection direction) {
void _sortPrinters(PrinterSortParam param, PrinterSortDirection direction) {
switch (param) {
case SortParam.coinOperated:
case PrinterSortParam.coinOperated:
{
if (direction == SortDirection.ascending) {
if (direction == PrinterSortDirection.ascending) {
_printers.sort((a, b) => (a.coinOperated ? 1 : 0).compareTo(b.coinOperated ? 1 : 0));
_printersFiltered
.sort((a, b) => (a.coinOperated ? 1 : 0).compareTo(b.coinOperated ? 1 : 0));
......@@ -375,9 +375,9 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
}
break;
case SortParam.community:
case PrinterSortParam.community:
{
if (direction == SortDirection.ascending) {
if (direction == PrinterSortDirection.ascending) {
_printers.sort((a, b) => a.community.compareTo(b.community));
_printersFiltered.sort((a, b) => a.community.compareTo(b.community));
} else {
......@@ -387,9 +387,9 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
}
break;
case SortParam.hostname:
case PrinterSortParam.hostname:
{
if (direction == SortDirection.ascending) {
if (direction == PrinterSortDirection.ascending) {
_printers.sort((a, b) => a.hostname.toLowerCase().compareTo(b.hostname.toLowerCase()));
_printersFiltered
.sort((a, b) => a.hostname.toLowerCase().compareTo(b.hostname.toLowerCase()));
......@@ -401,9 +401,9 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
}
break;
case SortParam.location:
case PrinterSortParam.location:
{
if (direction == SortDirection.ascending) {
if (direction == PrinterSortDirection.ascending) {
_printers.sort((a, b) => a.location.toLowerCase().compareTo(b.location.toLowerCase()));
_printersFiltered
.sort((a, b) => a.location.toLowerCase().compareTo(b.location.toLowerCase()));
......@@ -415,9 +415,9 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
}
break;
case SortParam.ip:
case PrinterSortParam.ip:
{
if (direction == SortDirection.ascending) {
if (direction == PrinterSortDirection.ascending) {
_printers.sort((a, b) => a.ip.toLowerCase().compareTo(b.ip.toLowerCase()));
_printersFiltered.sort((a, b) => a.ip.toLowerCase().compareTo(b.ip.toLowerCase()));
} else {
......@@ -427,9 +427,9 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
}
break;
case SortParam.hasA3:
case PrinterSortParam.hasA3:
{
if (direction == SortDirection.ascending) {
if (direction == PrinterSortDirection.ascending) {
_printers.sort((a, b) => (a.hasA3 ? 1 : 0).compareTo(b.hasA3 ? 1 : 0));
_printersFiltered.sort((a, b) => (a.hasA3 ? 1 : 0).compareTo(b.hasA3 ? 1 : 0));
} else {
......@@ -439,9 +439,9 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
}
break;
case SortParam.mac:
case PrinterSortParam.mac:
{
if (direction == SortDirection.ascending) {
if (direction == PrinterSortDirection.ascending) {
_printers.sort((a, b) => a.mac.toLowerCase().compareTo(b.mac.toLowerCase()));
_printersFiltered.sort((a, b) => a.mac.toLowerCase().compareTo(b.mac.toLowerCase()));
} else {
......@@ -451,9 +451,9 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
}
break;
case SortParam.deviceId:
case PrinterSortParam.deviceId:
{
if (direction == SortDirection.ascending) {
if (direction == PrinterSortDirection.ascending) {
_printers.sort((a, b) => a.deviceId.compareTo(b.deviceId));
_printersFiltered.sort((a, b) => a.deviceId.compareTo(b.deviceId));
} else {
......@@ -463,9 +463,9 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
}
break;
case SortParam.description:
case PrinterSortParam.description:
{
if (direction == SortDirection.ascending) {
if (direction == PrinterSortDirection.ascending) {
_printers
.sort((a, b) => a.description.toLowerCase().compareTo(b.description.toLowerCase()));
_printersFiltered
......
......@@ -75,8 +75,8 @@ class UpdatePrinter extends PrintersEvent {
}
class SortPrinters extends PrintersEvent {
final SortParam param;
final SortDirection direction;
final PrinterSortParam param;
final PrinterSortDirection direction;
SortPrinters(this.param, this.direction);
......
......@@ -70,7 +70,7 @@ void main() {
}
},
));
bloc.onSortPrinters(SortParam.location, SortDirection.ascending);
bloc.onSortPrinters(PrinterSortParam.location, PrinterSortDirection.ascending);
});
test('sort printers by hostname descending', () {
......@@ -86,7 +86,7 @@ void main() {
}
},
));
bloc.onSortPrinters(SortParam.hostname, SortDirection.descending);
bloc.onSortPrinters(PrinterSortParam.hostname, PrinterSortDirection.descending);
});
test('sort printers by coinOperated ascending', () {
......@@ -102,7 +102,7 @@ void main() {
}
},
));
bloc.onSortPrinters(SortParam.coinOperated, SortDirection.ascending);
bloc.onSortPrinters(PrinterSortParam.coinOperated, PrinterSortDirection.ascending);
});
test('sort printers by hostname descending after filtering by location', () {
......@@ -117,7 +117,7 @@ void main() {
}
}));
bloc.onFilterPrinters(PrinterDetails.empty(location: 'BI'));
bloc.onSortPrinters(SortParam.hostname, SortDirection.descending);
bloc.onSortPrinters(PrinterSortParam.hostname, PrinterSortDirection.descending);
});
});
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment