Commit 53e753d7 authored by Leon Tappe's avatar Leon Tappe 🔥
Browse files

refactor states and events

parent 167b14cd
......@@ -8,7 +8,7 @@ class AddAdmin extends AdminsEvent {
AddAdmin(this.firstName, this.lastName, this.login, this.password);
Map<String, dynamic> toMap() => <String, dynamic>{
Map<String, dynamic> get toMap => {
'first_name': firstName,
'last_name': lastName,
'login': login,
......@@ -16,7 +16,7 @@ class AddAdmin extends AdminsEvent {
};
@override
String toString() => toMap().toString();
String toString() => '[AddAdmin $toMap]';
}
abstract class AdminsEvent {}
......@@ -26,10 +26,10 @@ class DeleteAdmin extends AdminsEvent {
DeleteAdmin(this.id);
Map<String, dynamic> toMap() => <String, dynamic>{'id': id};
Map<String, dynamic> get toMap => {'id': id};
@override
String toString() => toMap().toString();
String toString() => '[DeleteAdmin $toMap]';
}
class InitAdmins extends AdminsEvent {
......@@ -37,10 +37,10 @@ class InitAdmins extends AdminsEvent {
InitAdmins(this.token);
Map<String, dynamic> toMap() => <String, dynamic>{'token': token};
Map<String, dynamic> get toMap => {'token': token};
@override
String toString() => toMap().toString();
String toString() => '[initAdmins $toMap]';
}
class RefreshAdmins extends AdminsEvent {}
......@@ -51,13 +51,13 @@ class SetAdminPassword extends AdminsEvent {
SetAdminPassword(this.id, this.password);
Map<String, dynamic> toMap() => <String, dynamic>{
Map<String, dynamic> get toMap => {
'id': id,
'password': password,
};
@override
String toString() => toMap().toString();
String toString() => '[SetAdminPassword $toMap]';
}
class SortAdmins extends AdminsEvent {
......@@ -66,10 +66,10 @@ class SortAdmins extends AdminsEvent {
SortAdmins(this.param, this.direction);
Map<String, dynamic> toMap() => <String, dynamic>{'param': param, 'direction': direction};
Map<String, dynamic> get toMap => {'param': param, 'direction': direction};
@override
String toString() => toMap().toString();
String toString() => '[SortAdmins $toMap]';
}
class UpdateAdmin extends AdminsEvent {
......@@ -80,7 +80,7 @@ class UpdateAdmin extends AdminsEvent {
UpdateAdmin(this.id, this.login, this.firstName, this.lastName);
Map<String, dynamic> toMap() => <String, dynamic>{
Map<String, dynamic> get toMap => {
'id': id,
'login': login,
'first_name': firstName,
......@@ -88,5 +88,5 @@ class UpdateAdmin extends AdminsEvent {
};
@override
String toString() => toMap().toString();
String toString() => '[UpdateAdmin $toMap]';
}
......@@ -26,4 +26,7 @@ class AdminsState extends ResultState<List<Admin>> {
factory AdminsState.init() => AdminsState(isInit: true);
factory AdminsState.result(List<Admin> admins) => AdminsState(admins: admins, isResult: true);
@override
String toString() => '[AdminsState $toMap]';
}
......@@ -6,13 +6,13 @@ class Login extends AuthEvent {
Login(this.username, this.password);
Map<String, dynamic> toMap() => <String, dynamic>{
Map<String, dynamic> get toMap => {
'username': username,
'password': password,
};
@override
String toString() => toMap().toString();
String toString() => '[Login ${toMap.remove('password')}]';
}
class Logout extends AuthEvent {}
......@@ -22,8 +22,8 @@ class TokenLogin extends AuthEvent {
TokenLogin(this.token);
Map<String, dynamic> toMap() => <String, dynamic>{'token': token};
Map<String, dynamic> get toMap => {'token': token};
@override
String toString() => toMap().toString();
String toString() => '[TokenLogin $toMap]';
}
......@@ -13,13 +13,13 @@ class AuthState {
this.isException = false,
});
factory AuthState.unauthorized() => AuthState(isUnauthorized: true);
factory AuthState.authorized(String token) => AuthState(isAuthorized: true, token: token);
factory AuthState.exception(Exception e) => AuthState(isException: true, error: e);
Map<String, dynamic> toMap() => <String, dynamic>{
factory AuthState.unauthorized() => AuthState(isUnauthorized: true);
Map<String, dynamic> get toMap => {
'token': token,
'error': error,
'isUnauthorized': isUnauthorized,
......@@ -28,5 +28,5 @@ class AuthState {
};
@override
String toString() => toMap().toString();
String toString() => '[AuthState $toMap]';
}
......@@ -12,7 +12,7 @@ abstract class CommonState {
this.isException,
});
Map<String, dynamic> toMap() => <String, dynamic>{
Map<String, dynamic> get toMap => {
'error': error,
'isInit': isInit,
'isBusy': isBusy,
......@@ -42,10 +42,11 @@ abstract class ResultState<T> extends CommonState {
);
@override
Map<String, dynamic> toMap() => <String, dynamic>{
Map<String, dynamic> get toMap => {
'value': value,
}..addAll(super.toMap());
...super.toMap,
};
@override
String toString() => toMap().toString();
String toString() => '[ResultState<$T> $toMap]';
}
......@@ -7,8 +7,8 @@ class InitDispatcher extends DispatcherEvent {
InitDispatcher(this.token);
Map<String, dynamic> toMap() => <String, dynamic>{'token': token};
Map<String, dynamic> get toMap => {'token': token};
@override
String toString() => toMap().toString();
String toString() => '[InitDispatcher $toMap]';
}
......@@ -28,4 +28,7 @@ class DispatcherState extends ResultState<List<DispatcherTask>> {
factory DispatcherState.result(List<DispatcherTask> queue) =>
DispatcherState(queue: queue, isResult: true);
@override
String toString() => '[DispatcherState $toMap]';
}
......@@ -16,14 +16,14 @@ class AddTransaction extends JournalEvent {
this.withoutReceipt,
);
Map<String, dynamic> toMap() => <String, dynamic>{
Map<String, dynamic> get toMap => {
'value': value,
'annotation': annotation,
'withoutReceipt': withoutReceipt,
};
@override
String toString() => toMap().toString();
String toString() => '[AddTransaction $toMap]';
}
class FilterJournal extends JournalEvent {
......@@ -49,10 +49,10 @@ class InitJournal extends JournalEvent {
InitJournal(this.token);
Map<String, dynamic> toMap() => <String, dynamic>{'token': token};
Map<String, dynamic> get toMap => {'token': token};
@override
String toString() => toMap().toString();
String toString() => '[InitJournal $toMap]';
}
abstract class JournalEvent {}
......@@ -65,6 +65,8 @@ class SortJournal extends JournalEvent {
SortJournal(this.param, this.direction);
Map<String, dynamic> get toMap => {'param': param};
@override
String toString() => '[SortJournal param: $param, direction: $direction]';
String toString() => '[SortJournal $toMap]';
}
import '../common.dart';
import '../models/transaction.dart';
class JournalState {
final List<Transaction> journal;
final Exception error;
final bool isInit;
final bool isResult;
final bool isException;
class JournalState extends ResultState<List<Transaction>> {
JournalState({
this.journal,
this.error,
this.isInit = false,
this.isResult = false,
this.isException = false,
});
List<Transaction> journal,
Exception error,
bool isInit = false,
bool isResult = false,
bool isException = false,
bool isBusy = false,
}) : super(
value: journal,
error: error,
isException: isException,
isInit: isInit,
isResult: isResult,
isBusy: isBusy,
);
factory JournalState.busy() => JournalState(isBusy: true);
factory JournalState.exception(Exception e) => JournalState(isException: true, error: e);
......@@ -22,14 +27,6 @@ class JournalState {
factory JournalState.result(List<Transaction> journal) =>
JournalState(isResult: true, journal: journal);
Map<String, dynamic> toMap() => <String, dynamic>{
'journal': journal,
'error': error,
'isInit': isInit,
'isResult': isResult,
'isException': isException,
};
@override
String toString() => toMap().toString();
String toString() => '[JournalState $toMap]';
}
class InitJournalToken extends JournalTokenEvent {
final String token;
InitJournalToken(this.token);
Map<String, dynamic> toMap() => <String, dynamic>{'token': token};
@override
String toString() => toMap().toString();
}
abstract class JournalTokenEvent {}
class RefreshJournalToken extends JournalTokenEvent {}
class CreateJournalToken extends JournalTokenEvent {
final int value;
CreateJournalToken(this.value);
Map<String, dynamic> toMap() => <String, dynamic>{'value': value};
Map<String, dynamic> get toMap => {'value': value};
@override
String toString() => toMap().toString();
String toString() => '[CreateJournalToken $toMap]';
}
class GetJournalTokenQR extends JournalTokenEvent {
......@@ -29,8 +14,23 @@ class GetJournalTokenQR extends JournalTokenEvent {
GetJournalTokenQR(this.id);
Map<String, dynamic> toMap() => <String, dynamic>{'id': id};
Map<String, dynamic> get toMap => {'id': id};
@override
String toString() => toMap().toString();
String toString() => '[GetJournalTokenQR $toMap]';
}
class InitJournalToken extends JournalTokenEvent {
final String token;
InitJournalToken(this.token);
Map<String, dynamic> get toMap => {'token': token};
@override
String toString() => '[InitJournalToken $toMap]';
}
abstract class JournalTokenEvent {}
class RefreshJournalToken extends JournalTokenEvent {}
import '../common.dart';
import '../models/journal_token.dart';
class JournalTokenState {
final List<JournalToken> journalTokens;
final Exception error;
final bool isInit;
final bool isResult;
final bool isException;
class JournalTokenState extends ResultState<List<JournalToken>> {
JournalTokenState({
this.journalTokens,
this.error,
this.isInit = false,
this.isResult = false,
this.isException = false,
});
List<JournalToken> journalTokens,
Exception error,
bool isInit = false,
bool isResult = false,
bool isException = false,
bool isBusy = false,
}) : super(
value: journalTokens,
error: error,
isBusy: isBusy,
isException: isException,
isInit: isInit,
isResult: isResult,
);
factory JournalTokenState.busy() => JournalTokenState(isBusy: true);
factory JournalTokenState.exception(Exception e) =>
JournalTokenState(isException: true, error: e);
......@@ -23,14 +28,6 @@ class JournalTokenState {
factory JournalTokenState.result(List<JournalToken> journalToken) =>
JournalTokenState(isResult: true, journalTokens: journalToken);
Map<String, dynamic> toMap() => <String, dynamic>{
'journal': journalTokens,
'error': error,
'isInit': isInit,
'isResult': isResult,
'isException': isException,
};
@override
String toString() => toMap().toString();
String toString() => '[JournalTokenState $toMap]';
}
......@@ -37,7 +37,7 @@ class Transaction {
);
}
Map<String, dynamic> toMap() => <String, dynamic>{
Map<String, dynamic> get toMap => {
'value': value,
'user_id': userId,
'admin_id': adminId,
......@@ -47,5 +47,5 @@ class Transaction {
};
@override
String toString() => toMap().toString();
String toString() => '[Transaction $toMap]';
}
import 'transaction.dart';
export 'transaction.dart';
class User {
......@@ -36,7 +37,7 @@ class User {
updated: DateTime.fromMillisecondsSinceEpoch((map['updated'] as int) * 1000),
);
Map<String, dynamic> toMap() => <String, dynamic>{
Map<String, dynamic> get toMap => {
'id': id,
'name': name,
'email': email,
......@@ -49,5 +50,5 @@ class User {
};
@override
String toString() => toMap().toString();
String toString() => '[User $toMap]';
}
class InitPrintQueue extends PrintQueueEvent {
String token;
class CancelJob extends PrintQueueEvent {
int deviceId;
InitPrintQueue(this.token);
CancelJob(this.deviceId);
Map<String, dynamic> toMap() => <String, dynamic>{'token': token};
Map<String, dynamic> get toMap => {'deviceId': deviceId};
@override
String toString() => toMap().toString();
String toString() => '[CancelJob $toMap]';
}
class CancelJob extends PrintQueueEvent {
class GetQueue extends PrintQueueEvent {
int deviceId;
CancelJob(this.deviceId);
GetQueue(this.deviceId);
Map<String, dynamic> toMap() => <String, dynamic>{'deviceId': deviceId};
Map<String, dynamic> get toMap => {'deviceId': deviceId};
@override
String toString() => toMap().toString();
String toString() => '[GetQueue $toMap]';
}
class GetQueue extends PrintQueueEvent {
int deviceId;
class InitPrintQueue extends PrintQueueEvent {
String token;
GetQueue(this.deviceId);
InitPrintQueue(this.token);
Map<String, dynamic> toMap() => <String, dynamic>{'deviceId': deviceId};
Map<String, dynamic> get toMap => {'token': token};
@override
String toString() => toMap().toString();
String toString() => '[initPrintQueue $toMap]';
}
abstract class PrintQueueEvent {}
import '../models/print_queue_task.dart';
import '../common.dart';
import '../../exceptions.dart';
import '../common.dart';
import '../models/print_queue_task.dart';
class PrintQueueState extends ResultState<List<PrintQueueTask>> {
bool isLocked;
......@@ -24,16 +24,18 @@ class PrintQueueState extends ResultState<List<PrintQueueTask>> {
isException: isException,
);
factory PrintQueueState.init() => PrintQueueState(isInit: true);
factory PrintQueueState.busy() => PrintQueueState(isBusy: true);
factory PrintQueueState.exception(NetworkException e) =>
PrintQueueState(isException: true, error: e);
factory PrintQueueState.init() => PrintQueueState(isInit: true);
factory PrintQueueState.locked(String uid) => PrintQueueState(isLocked: true, lockUid: uid);
factory PrintQueueState.result(List<PrintQueueTask> queue) =>
PrintQueueState(queue: queue, isResult: true);
factory PrintQueueState.locked(String uid) =>
PrintQueueState(isLocked: true, lockUid: uid);
@override
String toString() => '[PrintQueueState $toMap]';
}
......@@ -7,8 +7,10 @@ class AddPrinter extends PrintersEvent {
AddPrinter(this.deviceId, this.details);
Map<String, dynamic> get toMap => {'device_id': deviceId, 'details': details.toMap};
@override
String toString() => '[AddPrinter deviceId: $deviceId, $details]';
String toString() => '[AddPrinter $toMap]';
}
class DeletePrinter extends PrintersEvent {
......@@ -43,10 +45,10 @@ class GetPrinterDetails extends PrintersEvent {
GetPrinterDetails(this.deviceId);
Map<String, dynamic> toMap() => <String, dynamic>{'deviceId': deviceId};
Map<String, dynamic> get toMap => {'deviceId': deviceId};
@override
String toString() => toMap().toString();
String toString() => '[GetPrinterDetails $toMap]';
}
class InitPrinters extends PrintersEvent {
......@@ -54,10 +56,10 @@ class InitPrinters extends PrintersEvent {
InitPrinters(this.token);
Map<String, dynamic> toMap() => <String, dynamic>{'token': token};
Map<String, dynamic> get toMap => {'token': token};
@override
String toString() => toMap().toString();
String toString() => '[InitPrinters $toMap]';
}
abstract class PrintersEvent {}
......@@ -70,8 +72,10 @@ class SortPrinters extends PrintersEvent {
SortPrinters(this.param, this.direction);
Map<String, dynamic> get toMap => {'param': param, 'direction': direction};
@override
String toString() => '[SortPrinters param: $param, direction: $direction]';
String toString() => '[SortPrinters $toMap]';
}
class UpdatePrinter extends PrintersEvent {
......@@ -80,6 +84,8 @@ class UpdatePrinter extends PrintersEvent {
UpdatePrinter(this.deviceId, this.details);
Map<String, dynamic> get toMap => {'device_id': deviceId, 'details': details.toMap};
@override
String toString() => '[UpdatePrinter deviceId: $deviceId, $details]';
String toString() => '[UpdatePrinter $toMap]';
}
import '../common.dart';
import '../models/printer.dart';
class PrintersState {
final List<Printer> printers;
final Exception error;
final bool isInit;
final bool isResult;
final bool isException;
class PrintersState extends ResultState<List<Printer>> {
PrintersState({
this.printers,
this.error,
this.isInit = false,
this.isResult = false,
this.isException = false,
});
List<Printer> printers,
Exception error,
bool isBusy = false,
bool isInit = false,
bool isResult = false,
bool isException = false,
}) : super(
value: printers,
error: error,
isBusy: isBusy,