import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import 'package:redux/redux.dart'; import 'package:invoiceninja_flutter/main_app.dart'; import 'package:invoiceninja_flutter/redux/app/app_actions.dart'; import 'package:invoiceninja_flutter/data/models/models.dart'; import 'package:invoiceninja_flutter/redux/ui/ui_actions.dart'; import 'package:invoiceninja_flutter/ui/stub/stub_screen.dart'; import 'package:invoiceninja_flutter/ui/stub/edit/stub_edit_vm.dart'; import 'package:invoiceninja_flutter/ui/stub/view/stub_view_vm.dart'; import 'package:invoiceninja_flutter/redux/stub/stub_actions.dart'; import 'package:invoiceninja_flutter/redux/app/app_state.dart'; import 'package:invoiceninja_flutter/data/repositories/stub_repository.dart'; List> createStoreStubsMiddleware([ StubRepository repository = const StubRepository(), ]) { final viewStubList = _viewStubList(); final viewStub = _viewStub(); final editStub = _editStub(); final loadStubs = _loadStubs(repository); final loadStub = _loadStub(repository); final saveStub = _saveStub(repository); final archiveStub = _archiveStub(repository); final deleteStub = _deleteStub(repository); final restoreStub = _restoreStub(repository); return [ TypedMiddleware(viewStubList), TypedMiddleware(viewStub), TypedMiddleware(editStub), TypedMiddleware(loadStubs), TypedMiddleware(loadStub), TypedMiddleware(saveStub), TypedMiddleware(archiveStub), TypedMiddleware(deleteStub), TypedMiddleware(restoreStub), ]; } Middleware _editStub() { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as EditStub; next(action); store.dispatch(UpdateCurrentRoute(StubEditScreen.route)); if (store.state.prefState.isMobile) { navigatorKey.currentState.pushNamed(StubEditScreen.route); } }; } Middleware _viewStub() { return (Store store, dynamic dynamicAction, NextDispatcher next) async { final action = dynamicAction as ViewStub; next(action); store.dispatch(UpdateCurrentRoute(StubViewScreen.route)); if (store.state.prefState.isMobile) { navigatorKey.currentState.pushNamed(StubViewScreen.route); } }; } Middleware _viewStubList() { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as ViewStubList; next(action); if (store.state.staticState.isStale) { store.dispatch(RefreshData()); } store.dispatch(UpdateCurrentRoute(StubScreen.route)); if (store.state.prefState.isMobile) { navigatorKey.currentState.pushNamedAndRemoveUntil( StubScreen.route, (Route route) => false); } }; } Middleware _archiveStub(StubRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as ArchiveStubsRequest; final prevStubs = action.stubIds.map((id) => store.state.stubState.map[id]).toList(); repository .bulkAction( store.state.credentials, action.stubIds, EntityAction.archive) .then((List stubs) { store.dispatch(ArchiveStubsSuccess(stubs)); if (action.completer != null) { action.completer.complete(null); } }).catchError((Object error) { print(error); store.dispatch(ArchiveStubsFailure(prevStubs)); if (action.completer != null) { action.completer.completeError(error); } }); next(action); }; } Middleware _deleteStub(StubRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as DeleteStubsRequest; final prevStubs = action.stubIds.map((id) => store.state.stubState.map[id]).toList(); repository .bulkAction( store.state.credentials, action.stubIds, EntityAction.delete) .then((List stubs) { store.dispatch(DeleteStubsSuccess(stubs)); if (action.completer != null) { action.completer.complete(null); } }).catchError((Object error) { print(error); store.dispatch(DeleteStubsFailure(prevStubs)); if (action.completer != null) { action.completer.completeError(error); } }); next(action); }; } Middleware _restoreStub(StubRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as RestoreStubsRequest; final prevStubs = action.stubIds.map((id) => store.state.stubState.map[id]).toList(); repository .bulkAction( store.state.credentials, action.stubIds, EntityAction.restore) .then((List stubs) { store.dispatch(RestoreStubsSuccess(stubs)); if (action.completer != null) { action.completer.complete(null); } }).catchError((Object error) { print(error); store.dispatch(RestoreStubsFailure(prevStubs)); if (action.completer != null) { action.completer.completeError(error); } }); next(action); }; } Middleware _saveStub(StubRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as SaveStubRequest; repository .saveData( store.state.credentials, action.stub) .then((StubEntity stub) { if (action.stub.isNew) { store.dispatch(AddStubSuccess(stub)); } else { store.dispatch(SaveStubSuccess(stub)); } action.completer.complete(stub); }).catchError((Object error) { print(error); store.dispatch(SaveStubFailure(error)); action.completer.completeError(error); }); next(action); }; } Middleware _loadStub(StubRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as LoadStub; final AppState state = store.state; store.dispatch(LoadStubRequest()); repository .loadItem(state.credentials, action.stubId) .then((stub) { store.dispatch(LoadStubSuccess(stub)); if (action.completer != null) { action.completer.complete(null); } }).catchError((Object error) { print(error); store.dispatch(LoadStubFailure(error)); if (action.completer != null) { action.completer.completeError(error); } }); next(action); }; } Middleware _loadStubs(StubRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as LoadStubs; final AppState state = store.state; store.dispatch(LoadStubsRequest()); repository .loadList(state.credentials) .then((data) { store.dispatch(LoadStubsSuccess(data)); if (action.completer != null) { action.completer.complete(null); } /* if (state.productState.isStale) { store.dispatch(LoadProducts()); } */ }).catchError((Object error) { print(error); store.dispatch(LoadStubsFailure(error)); if (action.completer != null) { action.completer.completeError(error); } }); next(action); }; }