Commit 1927d094 authored by Leon Tappe's avatar Leon Tappe 🔥
Browse files

refactor for bloc package upgrade

parent a5b2995f
......@@ -3,7 +3,7 @@ include: package:lints/recommended.yaml
analyzer:
exclude: [build/**]
language:
strict-casts: true
strict-inference: true
strict-raw-types: true
linter:
......
......@@ -17,65 +17,72 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
AdminsBloc(this._client) : super(AdminsState.init()) {
_log.info('$this started');
}
@override
Future<void> close() {
_log.info('closing $this');
return super.close();
}
@override
Stream<AdminsState> mapEventToState(AdminsEvent event) async* {
_log.fine('Event: $event');
if (event is InitAdmins) {
on<InitAdmins>((InitAdmins event, Emitter<AdminsState> emit) async {
_token = event.token;
await _getAdmins();
yield AdminsState.result(_admins);
} else if (event is RefreshAdmins) {
emit(AdminsState.result(_admins));
});
on<RefreshAdmins>((RefreshAdmins event, Emitter<AdminsState> emit) async {
try {
await _getAdmins();
yield AdminsState.result(_admins);
emit(AdminsState.result(_admins));
} on NetworkException catch (e) {
_log.severe(e);
}
} else if (event is AddAdmin) {
});
on<AddAdmin>((AddAdmin event, Emitter<AdminsState> emit) async {
try {
await _postAdmin(event.firstName, event.lastName, event.password, login: event.login);
await _getAdmins();
yield AdminsState.result(_admins);
emit(AdminsState.result(_admins));
} on NetworkException catch (e) {
_log.severe(e);
}
} else if (event is SetAdminPassword) {
});
on<SetAdminPassword>((SetAdminPassword event, Emitter<AdminsState> emit) async {
try {
await _setAdminPassword(event.id, event.password);
await _getAdmins();
yield AdminsState.result(_admins);
emit(AdminsState.result(_admins));
} on NetworkException catch (e) {
_log.severe(e);
}
} else if (event is UpdateAdmin) {
});
on<UpdateAdmin>((UpdateAdmin event, Emitter<AdminsState> emit) async {
try {
await _updateAdmin(event.id, event.login, event.firstName, event.lastName);
await _getAdmins();
yield AdminsState.result(_admins);
emit(AdminsState.result(_admins));
} on NetworkException catch (e) {
_log.severe(e);
}
} else if (event is DeleteAdmin) {
});
on<DeleteAdmin>((DeleteAdmin event, Emitter<AdminsState> emit) async {
try {
await _deleteAdmin(event.id);
await _getAdmins();
yield AdminsState.result(_admins);
emit(AdminsState.result(_admins));
} on NetworkException catch (e) {
_log.severe(e);
}
} else if (event is SortAdmins) {
});
on<SortAdmins>((SortAdmins event, Emitter<AdminsState> emit) async {
_sortAdmins(event.param, event.direction);
yield AdminsState.result(_admins);
}
emit(AdminsState.result(_admins));
});
}
@override
Future<void> close() {
_log.info('closing $this');
return super.close();
}
void onCreateAdmin(String firstName, String lastName, String password, {String login}) =>
......
......@@ -16,6 +16,25 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
AuthBloc(this.client) : super(AuthState.unauthorized()) {
log.fine('$this started');
on<Login>((Login event, Emitter<AuthState> emit) async {
try {
await _postLogin(event.username, event.password);
if (_token != null) emit(AuthState.authorized(_token));
} on NetworkException catch (e) {
emit(AuthState.exception(e));
}
});
on<Logout>((Logout event, Emitter<AuthState> emit) async {
_token = null;
emit(AuthState.unauthorized());
});
on<TokenLogin>((TokenLogin event, Emitter<AuthState> emit) async {
_token = event.token;
emit(AuthState.authorized(_token));
});
}
@override
......@@ -28,30 +47,6 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
void logout() => add(Logout());
@override
Stream<AuthState> mapEventToState(AuthEvent event) async* {
log.fine('Event: $event');
if (event is Login) {
try {
await _postLogin(event.username, event.password);
if (_token != null) yield AuthState.authorized(_token);
} on NetworkException catch (e) {
yield AuthState.exception(e);
}
}
if (event is Logout) {
_token = null;
yield AuthState.unauthorized();
}
if (event is TokenLogin) {
_token = event.token;
yield AuthState.authorized(_token);
}
}
@override
void onTransition(Transition<AuthEvent, AuthState> transition) {
log.fine('State: ${transition.nextState}');
......
......@@ -18,6 +18,21 @@ class DispatcherBloc extends Bloc<DispatcherEvent, DispatcherState> {
DispatcherBloc(this._client) : super(DispatcherState.init()) {
_l.fine('$this started');
on<GetTasks>((GetTasks event, Emitter<DispatcherState> emit) async {
try {
emit(DispatcherState.busy());
emit(DispatcherState.result(await _getQueue()));
} on NetworkException catch (e) {
_l.severe(e.toString());
emit(DispatcherState.exception(e));
}
});
on<InitDispatcher>((InitDispatcher event, Emitter<DispatcherState> emit) async {
_token = event.token;
_l.fine('token initialized');
});
}
@override
......@@ -26,36 +41,16 @@ class DispatcherBloc extends Bloc<DispatcherEvent, DispatcherState> {
return super.close();
}
void onRefresh() => add(GetTasks());
void onStart(String token) => add(InitDispatcher(token));
@override
void onTransition(Transition<DispatcherEvent, DispatcherState> transition) {
_l.fine('Transition from ${transition.currentState} to ${transition.nextState}');
super.onTransition(transition);
}
@override
Stream<DispatcherState> mapEventToState(DispatcherEvent event) async* {
_l.fine('Event: $event');
if (event is InitDispatcher) {
_token = event.token;
_l.fine('token initialized');
}
if (event is GetTasks) {
try {
yield DispatcherState.busy();
yield DispatcherState.result(await _getQueue());
} on NetworkException catch (e) {
_l.severe(e.toString());
yield DispatcherState.exception(e);
}
}
}
void onStart(String token) => add(InitDispatcher(token));
void onRefresh() => add(GetTasks());
Future<List<DispatcherTask>> _getQueue() async {
_l.finer('_getQueue');
return await _client.get(
......
......@@ -4,9 +4,8 @@ import 'package:bloc/bloc.dart';
import 'package:http/http.dart';
import 'package:logging/logging.dart';
import '../models/transaction.dart';
import '../models/journal_sort.dart';
import '../models/transaction.dart';
import '../network_exception.dart';
import 'journal_events.dart';
import 'journal_state.dart';
......@@ -24,34 +23,27 @@ class JournalBloc extends Bloc<JournalEvent, JournalState> {
JournalBloc(this.client) : super(JournalState.init()) {
log.fine('$this started');
}
@override
Future<void> close() {
log.fine('closing $this');
return super.close();
}
@override
Stream<JournalState> mapEventToState(JournalEvent event) async* {
log.fine('Event: $event');
if (event is InitJournal) {
on<InitJournal>((InitJournal event, Emitter<JournalState> emit) async {
_token = event.token;
try {
await _getJournal(limit: chunkSize, offset: chunkOffset);
yield JournalState.result(_journal);
emit(JournalState.result(_journal));
} on NetworkException catch (e) {
yield JournalState.exception(e);
emit(JournalState.exception(e));
}
} else if (event is RefreshJournal) {
});
on<RefreshJournal>((RefreshJournal event, Emitter<JournalState> emit) async {
try {
await _getJournal(limit: chunkSize, offset: chunkOffset);
yield JournalState.result(_journal);
emit(JournalState.result(_journal));
} on NetworkException catch (e) {
yield JournalState.exception(e);
emit(JournalState.exception(e));
}
} else if (event is AddTransaction) {
});
on<AddTransaction>((AddTransaction event, Emitter<JournalState> emit) async {
try {
await _postJournal(
event.userId,
......@@ -67,40 +59,52 @@ class JournalBloc extends Bloc<JournalEvent, JournalState> {
null,
DateTime.now(),
));
yield JournalState.result(_journal);
emit(JournalState.result(_journal));
} on NetworkException catch (e) {
yield JournalState.exception(e);
emit(JournalState.exception(e));
}
} else if (event is FilterJournal) {
});
on<FilterJournal>((FilterJournal event, Emitter<JournalState> emit) async {
final filtered = _filterJournal(event.filter);
yield JournalState.result(filtered ? _journalFiltered : _journal);
} else if (event is FuzzyFilterJournal) {
emit(JournalState.result(filtered ? _journalFiltered : _journal));
});
on<FuzzyFilterJournal>((FuzzyFilterJournal event, Emitter<JournalState> emit) async {
_fuzzyFilterJournal(event.filter.toLowerCase());
yield JournalState.result(_journalFiltered);
} else if (event is SortJournal) {
emit(JournalState.result(_journalFiltered));
});
on<SortJournal>((SortJournal event, Emitter<JournalState> emit) async {
_sortJournal(event.param, event.direction);
yield JournalState.result(_journalFiltered.isNotEmpty ? _journalFiltered : _journal);
}
emit(JournalState.result(_journalFiltered.isNotEmpty ? _journalFiltered : _journal));
});
}
void onRefresh() => add(RefreshJournal());
void onStart(String token) => add(InitJournal(token));
@override
Future<void> close() {
log.fine('closing $this');
return super.close();
}
void onAddTransaction(int userId, int adminId, int value, String description,
{bool withoutReceipt = false}) =>
add(AddTransaction(userId, adminId, value, description, withoutReceipt));
void onFilterJournal(Transaction filter) => add(FilterJournal(filter));
void onFuzzyFilterJournal(String filter) => add(FuzzyFilterJournal(filter));
void onRefresh() => add(RefreshJournal());
void onStart(String token) => add(InitJournal(token));
@override
void onTransition(Transition<JournalEvent, JournalState> transition) {
log.fine('State: ${transition.nextState}');
super.onTransition(transition);
}
void onFilterJournal(Transaction filter) => add(FilterJournal(filter));
void onFuzzyFilterJournal(String filter) => add(FuzzyFilterJournal(filter));
bool _filterJournal(Transaction filter) {
_journalFiltered = [];
var filtered = false;
......@@ -156,6 +160,60 @@ class JournalBloc extends Bloc<JournalEvent, JournalState> {
.toList();
}
Future<void> _getJournal({int limit = 50, int offset = 0}) async {
log.finer('_getJournal:');
return await client.get(
Uri.parse(
'https://astaprint.upb.de/api/v1/admin/journal?limit=${limit?.toString()}&offset=${offset?.toString()}'),
headers: {
'X-Api-Key': _token,
'Accept': 'application/json',
},
).then(
(Response response) {
if (response.statusCode == 200) {
log.finer('_getJournal: ${response.body}');
_journal = List<Transaction>.from((json.decode(response.body) as Iterable)
.map<Transaction>((dynamic job) => Transaction.fromMap(job as Map)));
return;
} else {
log.severe('_getJournal: ${response.body}');
throw NetworkException('status code other than 200 received', response.statusCode);
}
},
);
}
Future<void> _postJournal(int userId, int value, String annotation, bool withoutReceipt) async {
log.finer('_postJournal:');
return await client
.post(
Uri.parse('https://astaprint.upb.de/api/v1/admin/journal'),
headers: {
'X-Api-Key': _token,
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: json.encode({
'user_id': userId,
'value': value,
'description': annotation,
'without_receipt': withoutReceipt,
}),
)
.then(
(Response response) {
if (response.statusCode == 204) {
log.finer('_getJournal: ${response.body}');
return;
} else {
log.severe('_getJournal: ${response.body}');
throw NetworkException('status code other than 204 received', response.statusCode);
}
},
);
}
void _sortJournal(JournalSortParam param, JournalSortDirection direction) {
switch (param) {
case JournalSortParam.value:
......@@ -219,58 +277,4 @@ class JournalBloc extends Bloc<JournalEvent, JournalState> {
break;
}
}
Future<void> _getJournal({int limit = 50, int offset = 0}) async {
log.finer('_getJournal:');
return await client.get(
Uri.parse(
'https://astaprint.upb.de/api/v1/admin/journal?limit=${limit?.toString()}&offset=${offset?.toString()}'),
headers: {
'X-Api-Key': _token,
'Accept': 'application/json',
},
).then(
(Response response) {
if (response.statusCode == 200) {
log.finer('_getJournal: ${response.body}');
_journal = List<Transaction>.from((json.decode(response.body) as Iterable)
.map<Transaction>((dynamic job) => Transaction.fromMap(job as Map)));
return;
} else {
log.severe('_getJournal: ${response.body}');
throw NetworkException('status code other than 200 received', response.statusCode);
}
},
);
}
Future<void> _postJournal(int userId, int value, String annotation, bool withoutReceipt) async {
log.finer('_postJournal:');
return await client
.post(
Uri.parse('https://astaprint.upb.de/api/v1/admin/journal'),
headers: {
'X-Api-Key': _token,
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: json.encode({
'user_id': userId,
'value': value,
'description': annotation,
'without_receipt': withoutReceipt,
}),
)
.then(
(Response response) {
if (response.statusCode == 204) {
log.finer('_getJournal: ${response.body}');
return;
} else {
log.severe('_getJournal: ${response.body}');
throw NetworkException('status code other than 204 received', response.statusCode);
}
},
);
}
}
......@@ -21,48 +21,47 @@ class JournalTokenBloc extends Bloc<JournalTokenEvent, JournalTokenState> {
JournalTokenBloc(this.client) : super(JournalTokenState.init()) {
log.fine('$this started');
}
@override
Future<void> close() {
log.fine('closing $this');
return super.close();
}
@override
Stream<JournalTokenState> mapEventToState(JournalTokenEvent event) async* {
log.fine('Event: $event');
if (event is InitJournalToken) {
on<InitJournalToken>((InitJournalToken event, Emitter<JournalTokenState> emit) async {
_token = event.token;
try {
yield JournalTokenState.result(_journalTokens);
emit(JournalTokenState.result(_journalTokens));
} on NetworkException catch (e) {
yield JournalTokenState.exception(e);
emit(JournalTokenState.exception(e));
}
} else if (event is RefreshJournalToken) {
});
on<RefreshJournalToken>((RefreshJournalToken event, Emitter<JournalTokenState> emit) async {
try {
await _getJournalTokens();
yield JournalTokenState.result(_journalTokens);
emit(JournalTokenState.result(_journalTokens));
} on NetworkException catch (e) {
yield JournalTokenState.exception(e);
emit(JournalTokenState.exception(e));
}
} else if (event is CreateJournalToken) {
});
on<CreateJournalToken>((CreateJournalToken event, Emitter<JournalTokenState> emit) async {
try {
await _postJournalToken(event.value);
yield JournalTokenState.result(_journalTokens);
emit(JournalTokenState.result(_journalTokens));
} on NetworkException catch (e) {
yield JournalTokenState.exception(e);
emit(JournalTokenState.exception(e));
}
}
});
}
@override
Future<void> close() {
log.fine('closing $this');
return super.close();
}
void onCreateToken(int value) => add(CreateJournalToken(value));
void onRefresh() => add(RefreshJournalToken());
void onStart(String token) => add(InitJournalToken(token));
void onCreateToken(int value) => add(CreateJournalToken(value));
@override
void onTransition(Transition<JournalTokenEvent, JournalTokenState> transition) {
log.fine('State: ${transition.nextState}');
......@@ -96,7 +95,7 @@ class JournalTokenBloc extends Bloc<JournalTokenEvent, JournalTokenState> {
Future<void> _postJournalToken(int value) async {
log.finer('_postJournalToken:');
return await client.post(
Uri.parse('https://astaprint.upb.de/api/v1/admin/journal/tokens?value=${value}'),
Uri.parse('https://astaprint.upb.de/api/v1/admin/journal/tokens?value=$value'),
headers: {
'X-Api-Key': _token,
'Accept': 'application/json',
......
......@@ -60,4 +60,4 @@ class JobOptions {
String toString() => toMap().toString();
}
enum NupPageOrder { RIGHTTHENDOWN, DOWNTHENRIGHT, LEFTTHENDOWN, DOWNTHENLEFT }
enum NupPageOrder { rightThenDown, downThenRight, leftThenDown, downThenLeft }
class PrinterStatus {
int scan;
int copy;
int toner_c;
int toner_m;
int toner_y;
int toner_k;
int tonerC;
int tonerM;
int tonerY;
int tonerK;
int tray_1;
int tray_2;
int tray_3;
......@@ -13,10 +13,10 @@ class PrinterStatus {
PrinterStatus({
this.scan,
this.copy,
this.toner_c,
this.toner_m,
this.toner_y,
this.toner_k,
this.tonerC,
this.tonerM,
this.tonerY,
this.tonerK,
this.tray_1,
this.tray_2,
this.tray_3,
......@@ -25,10 +25,10 @@ class PrinterStatus {
factory PrinterStatus.fromMap(Map<String, dynamic> map) => PrinterStatus(
scan: map['scan'] as int,
copy: map['copy'] as int,
toner_c: map['toner_c'] as int,
toner_m: map['toner_m'] as int,
toner_y: map['toner_y'] as int,
toner_k: map['toner_k'] as int,
tonerC: map['toner_c'] as int,
tonerM: map['toner_m'] as int,