276 lines
8.1 KiB
Plaintext
276 lines
8.1 KiB
Plaintext
import 'package:flutter/material.dart';
|
|
import 'package:flutter/widgets.dart';
|
|
import 'package:redux/redux.dart';
|
|
import 'package:invoiceninja_flutter/utils/platforms.dart';
|
|
import 'package:invoiceninja_flutter/redux/app/app_middleware.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<Middleware<AppState>> 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<AppState, ViewStubList>(viewStubList),
|
|
TypedMiddleware<AppState, ViewStub>(viewStub),
|
|
TypedMiddleware<AppState, EditStub>(editStub),
|
|
TypedMiddleware<AppState, LoadStubs>(loadStubs),
|
|
TypedMiddleware<AppState, LoadStub>(loadStub),
|
|
TypedMiddleware<AppState, SaveStubRequest>(saveStub),
|
|
TypedMiddleware<AppState, ArchiveStubRequest>(archiveStub),
|
|
TypedMiddleware<AppState, DeleteStubRequest>(deleteStub),
|
|
TypedMiddleware<AppState, RestoreStubRequest>(restoreStub),
|
|
];
|
|
}
|
|
|
|
Middleware<AppState> _editStub() {
|
|
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) async {
|
|
|
|
final action = dynamicAction as EditStub;
|
|
|
|
if (hasChanges(
|
|
store: store, context: action.context, force: action.force)) {
|
|
return;
|
|
}
|
|
|
|
next(action);
|
|
|
|
store.dispatch(UpdateCurrentRoute(StubEditScreen.route));
|
|
|
|
if (isMobile(action.context)) {
|
|
final stub =
|
|
await Navigator.of(action.context).pushNamed(StubEditScreen.route);
|
|
|
|
if (action.completer != null && stub != null) {
|
|
action.completer.complete(stub);
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
Middleware<AppState> _viewStub() {
|
|
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) async {
|
|
|
|
final action = dynamicAction as ViewStub;
|
|
|
|
if (hasChanges(
|
|
store: store, context: action.context, force: action.force)) {
|
|
return;
|
|
}
|
|
|
|
|
|
next(action);
|
|
|
|
store.dispatch(UpdateCurrentRoute(StubViewScreen.route));
|
|
Navigator.of(action.context).pushNamed(StubViewScreen.route);
|
|
};
|
|
}
|
|
|
|
Middleware<AppState> _viewStubList() {
|
|
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
|
|
final action = dynamicAction as ViewStubList;
|
|
|
|
if (hasChanges(
|
|
store: store, context: action.context, force: action.force)) {
|
|
return;
|
|
}
|
|
|
|
next(action);
|
|
|
|
if (store.state.stubState.isStale) {
|
|
store.dispatch(LoadStubs());
|
|
}
|
|
|
|
store.dispatch(UpdateCurrentRoute(StubScreen.route));
|
|
|
|
if (isMobile(action.context)) {
|
|
Navigator.of(action.context).pushNamedAndRemoveUntil(
|
|
StubScreen.route, (Route<dynamic> route) => false);
|
|
}
|
|
};
|
|
}
|
|
|
|
Middleware<AppState> _archiveStub(StubRepository repository) {
|
|
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
|
|
final action = dynamicAction as ArchiveStubRequest;
|
|
final origStub = store.state.stubState.map[action.stubId];
|
|
repository
|
|
.saveData(store.state.credentials,
|
|
origStub, EntityAction.archive)
|
|
.then((StubEntity stub) {
|
|
store.dispatch(ArchiveStubSuccess(stub));
|
|
if (action.completer != null) {
|
|
action.completer.complete(null);
|
|
}
|
|
}).catchError((Object error) {
|
|
print(error);
|
|
store.dispatch(ArchiveStubFailure(origStub));
|
|
if (action.completer != null) {
|
|
action.completer.completeError(error);
|
|
}
|
|
});
|
|
|
|
next(action);
|
|
};
|
|
}
|
|
|
|
Middleware<AppState> _deleteStub(StubRepository repository) {
|
|
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
|
|
final action = dynamicAction as DeleteStubRequest;
|
|
final origStub = store.state.stubState.map[action.stubId];
|
|
repository
|
|
.saveData(store.state.credentials,
|
|
origStub, EntityAction.delete)
|
|
.then((StubEntity stub) {
|
|
store.dispatch(DeleteStubSuccess(stub));
|
|
if (action.completer != null) {
|
|
action.completer.complete(null);
|
|
}
|
|
}).catchError((Object error) {
|
|
print(error);
|
|
store.dispatch(DeleteStubFailure(origStub));
|
|
if (action.completer != null) {
|
|
action.completer.completeError(error);
|
|
}
|
|
});
|
|
|
|
next(action);
|
|
};
|
|
}
|
|
|
|
Middleware<AppState> _restoreStub(StubRepository repository) {
|
|
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
|
|
final action = dynamicAction as RestoreStubRequest;
|
|
final origStub = store.state.stubState.map[action.stubId];
|
|
repository
|
|
.saveData(store.state.credentials,
|
|
origStub, EntityAction.restore)
|
|
.then((StubEntity stub) {
|
|
store.dispatch(RestoreStubSuccess(stub));
|
|
if (action.completer != null) {
|
|
action.completer.complete(null);
|
|
}
|
|
}).catchError((Object error) {
|
|
print(error);
|
|
store.dispatch(RestoreStubFailure(origStub));
|
|
if (action.completer != null) {
|
|
action.completer.completeError(error);
|
|
}
|
|
});
|
|
|
|
next(action);
|
|
};
|
|
}
|
|
|
|
Middleware<AppState> _saveStub(StubRepository repository) {
|
|
return (Store<AppState> 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<AppState> _loadStub(StubRepository repository) {
|
|
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
|
|
final action = dynamicAction as LoadStub;
|
|
final AppState state = store.state;
|
|
|
|
if (state.isLoading) {
|
|
next(action);
|
|
return;
|
|
}
|
|
|
|
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<AppState> _loadStubs(StubRepository repository) {
|
|
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
|
|
final action = dynamicAction as LoadStubs;
|
|
final AppState state = store.state;
|
|
|
|
if (!state.stubState.isStale && !action.force) {
|
|
next(action);
|
|
return;
|
|
}
|
|
|
|
if (state.isLoading) {
|
|
next(action);
|
|
return;
|
|
}
|
|
|
|
final int updatedAt = (state.stubState.lastUpdated / 1000).round();
|
|
|
|
store.dispatch(LoadStubsRequest());
|
|
repository
|
|
.loadList(state.credentials, updatedAt)
|
|
.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);
|
|
};
|
|
}
|