diff --git a/lib/data/file_storage.dart b/lib/data/file_storage.dart index f2d6f89de..463d868ec 100644 --- a/lib/data/file_storage.dart +++ b/lib/data/file_storage.dart @@ -10,7 +10,7 @@ class FileStorage { this.getDirectory, ); - Future loadData() async { + Future loadData() async { final file = await _getLocalFile(); final contents = await file.readAsString(); diff --git a/lib/data/repositories/persistence_repository.dart b/lib/data/repositories/persistence_repository.dart index fb2d11a3b..b82343c0a 100644 --- a/lib/data/repositories/persistence_repository.dart +++ b/lib/data/repositories/persistence_repository.dart @@ -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 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 loadData() async { + var data = await fileStorage.loadData(); + + return serializers.deserializeWith(AppState.serializer, json.decode(data)); } } \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 895fdc7c7..d8c89551b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -67,7 +67,7 @@ class _InvoiceNinjaAppState extends State { title: 'Invoice Ninja', routes: { AppRoutes.login: (context) { - StoreProvider.of(context).dispatch(LoadUserLogin()); + StoreProvider.of(context).dispatch(LoadStateRequest(context)); return LoginVM(); }, AppRoutes.dashboard: (context) { diff --git a/lib/redux/app/app_middleware.dart b/lib/redux/app/app_middleware.dart index 131ba964a..0a787b4cd 100644 --- a/lib/redux/app/app_middleware.dart +++ b/lib/redux/app/app_middleware.dart @@ -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> createStorePersistenceMiddleware([ ), ), ]) { + final loadState = _createLoadState(repository); final dataLoaded = _createDataLoaded(repository); return [ + TypedMiddleware(loadState), TypedMiddleware(dataLoaded), ]; } +Middleware _createLoadState(PersistenceRepository repository) { + return (Store 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 _createDataLoaded(PersistenceRepository repository) { return (Store store, action, NextDispatcher next) { diff --git a/lib/redux/app/app_reducer.dart b/lib/redux/app/app_reducer.dart index 2ffd9187f..1b71889e0 100644 --- a/lib/redux/app/app_reducer.dart +++ b/lib/redux/app/app_reducer.dart @@ -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 diff --git a/lib/redux/auth/auth_actions.dart b/lib/redux/auth/auth_actions.dart index 73f84df38..aeec07d2c 100644 --- a/lib/redux/auth/auth_actions.dart +++ b/lib/redux/auth/auth_actions.dart @@ -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 {} diff --git a/lib/ui/dashboard/dashboard_panels.dart b/lib/ui/dashboard/dashboard_panels.dart index 78dd88569..186b7cc7a 100644 --- a/lib/ui/dashboard/dashboard_panels.dart +++ b/lib/ui/dashboard/dashboard_panels.dart @@ -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';