Persistence
This commit is contained in:
parent
194d717115
commit
ce4ef6e345
|
|
@ -10,7 +10,7 @@ class FileStorage {
|
|||
this.getDirectory,
|
||||
);
|
||||
|
||||
Future<String> loadData() async {
|
||||
Future<dynamic> loadData() async {
|
||||
final file = await _getLocalFile();
|
||||
final contents = await file.readAsString();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:core';
|
||||
import 'dart:io';
|
||||
import 'package:invoiceninja/redux/app/app_state.dart';
|
||||
|
|
@ -14,9 +15,14 @@ class PersistenceRepository {
|
|||
});
|
||||
|
||||
Future<File> saveData(AppState state) async {
|
||||
var data = serializers.serializeWith(AppState.serializer, state);
|
||||
|
||||
var data = serializers.serializeWith(AppState.serializer, state).toString();
|
||||
return await fileStorage.saveData(json.encode(data));
|
||||
}
|
||||
|
||||
fileStorage.saveData(data);
|
||||
Future<AppState> loadData() async {
|
||||
var data = await fileStorage.loadData();
|
||||
|
||||
return serializers.deserializeWith(AppState.serializer, json.decode(data));
|
||||
}
|
||||
}
|
||||
|
|
@ -67,7 +67,7 @@ class _InvoiceNinjaAppState extends State<InvoiceNinjaApp> {
|
|||
title: 'Invoice Ninja',
|
||||
routes: {
|
||||
AppRoutes.login: (context) {
|
||||
StoreProvider.of<AppState>(context).dispatch(LoadUserLogin());
|
||||
StoreProvider.of<AppState>(context).dispatch(LoadStateRequest(context));
|
||||
return LoginVM();
|
||||
},
|
||||
AppRoutes.dashboard: (context) {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,10 @@
|
|||
import 'package:flutter/widgets.dart';
|
||||
import 'package:invoiceninja/data/file_storage.dart';
|
||||
import 'package:invoiceninja/data/repositories/persistence_repository.dart';
|
||||
import 'package:invoiceninja/redux/app/app_state.dart';
|
||||
import 'package:invoiceninja/redux/auth/auth_actions.dart';
|
||||
import 'package:invoiceninja/redux/dashboard/dashboard_actions.dart';
|
||||
import 'package:invoiceninja/routes.dart';
|
||||
import 'package:redux/redux.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
|
|
@ -13,13 +16,31 @@ List<Middleware<AppState>> createStorePersistenceMiddleware([
|
|||
),
|
||||
),
|
||||
]) {
|
||||
final loadState = _createLoadState(repository);
|
||||
final dataLoaded = _createDataLoaded(repository);
|
||||
|
||||
return [
|
||||
TypedMiddleware<AppState, LoadStateRequest>(loadState),
|
||||
TypedMiddleware<AppState, LoadDashboardSuccess>(dataLoaded),
|
||||
];
|
||||
}
|
||||
|
||||
Middleware<AppState> _createLoadState(PersistenceRepository repository) {
|
||||
return (Store<AppState> store, action, NextDispatcher next) {
|
||||
|
||||
repository.loadData().then((state) {
|
||||
store.dispatch(LoadStateSuccess(state));
|
||||
Navigator.of(action.context).pushReplacementNamed(AppRoutes.dashboard);
|
||||
}).catchError((error) {
|
||||
print(error);
|
||||
store.dispatch(LoadUserLogin());
|
||||
});
|
||||
|
||||
next(action);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Middleware<AppState> _createDataLoaded(PersistenceRepository repository) {
|
||||
return (Store<AppState> store, action, NextDispatcher next) {
|
||||
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@ import 'package:invoiceninja/redux/company/company_actions.dart';
|
|||
AppState appReducer(AppState state, action) {
|
||||
if (action is UserLogout) {
|
||||
return AppState();
|
||||
} else if (action is LoadStateSuccess) {
|
||||
return action.state.rebuild((b) => b.isLoading = false);
|
||||
}
|
||||
|
||||
return state.rebuild((b) => b
|
||||
|
|
|
|||
|
|
@ -1,4 +1,15 @@
|
|||
import 'dart:async';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:invoiceninja/redux/app/app_state.dart';
|
||||
|
||||
class LoadStateRequest {
|
||||
final BuildContext context;
|
||||
LoadStateRequest(this.context);
|
||||
}
|
||||
class LoadStateSuccess {
|
||||
final AppState state;
|
||||
LoadStateSuccess(this.state);
|
||||
}
|
||||
|
||||
class LoadUserLogin {}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ import 'package:flutter/foundation.dart';
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:invoiceninja/ui/app/app_loading.dart';
|
||||
import 'package:invoiceninja/ui/app/loading_indicator.dart';
|
||||
import 'package:invoiceninja/redux/dashboard/dashboard_state.dart';
|
||||
import 'package:invoiceninja/ui/dashboard/dashboard_vm.dart';
|
||||
import 'package:invoiceninja/utils/localization.dart';
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue