Refresh data

This commit is contained in:
unknown 2018-07-31 09:58:47 +03:00
parent 96d025ef87
commit f6e7e46728
8 changed files with 70 additions and 15 deletions

View File

@ -22,9 +22,23 @@ class AuthRepository {
'password': password,
};
return sendRequest(url, credentials);
}
Future<LoginResponseData> refresh(String url, String token, String platform) async {
final credentials = {
'token_name': 'invoice-ninja-$platform-app',
};
return sendRequest(url, credentials, token);
}
Future<LoginResponseData> sendRequest(String url, dynamic data, [String token]) async {
url = formatApiUrlMachine(url);
final dynamic response = await webClient.post(url + '/login?include=tax_rates&include_static=true', '', json.encode(credentials));
final dynamic response = await webClient.post(url + '/login?include=tax_rates&include_static=true', token ?? '', json.encode(data));
final LoginResponse loginResponse = serializers.deserializeWith(
LoginResponse.serializer, response);
@ -34,5 +48,6 @@ class AuthRepository {
}
return loginResponse.data;
}
}
}

View File

@ -27,4 +27,7 @@ class LoadDataSuccess {
LoadDataSuccess({this.loginResponse, this.completer});
}
class RefreshData {}
class RefreshData {
final String platform;
RefreshData(this.platform);
}

View File

@ -74,9 +74,9 @@ List<Middleware<AppState>> createStorePersistenceMiddleware([
company4Repository,
company5Repository);
final loadData = _createLoadData();
final dataLoaded = _createDataLoaded();
final dataLoaded = _createDataLoaded(company1Repository, company2Repository,
final persistData = _createPersistData(company1Repository, company2Repository,
company3Repository, company4Repository, company5Repository);
final userLoggedIn = _createUserLoggedIn(
@ -89,7 +89,7 @@ List<Middleware<AppState>> createStorePersistenceMiddleware([
company4Repository,
company5Repository);
final uiChange = _createUIChange(uiRepository);
final persistUI = _createPersistUI(uiRepository);
final deleteState = _createDeleteState(
authRepository,
@ -105,9 +105,9 @@ List<Middleware<AppState>> createStorePersistenceMiddleware([
TypedMiddleware<AppState, UserLogout>(deleteState),
TypedMiddleware<AppState, LoadStateRequest>(loadState),
TypedMiddleware<AppState, UserLoginSuccess>(userLoggedIn),
TypedMiddleware<AppState, LoadDataSuccess>(loadData),
TypedMiddleware<AppState, PersistData>(dataLoaded),
TypedMiddleware<AppState, PersistUI>(uiChange),
TypedMiddleware<AppState, LoadDataSuccess>(dataLoaded),
TypedMiddleware<AppState, PersistData>(persistData),
TypedMiddleware<AppState, PersistUI>(persistUI),
];
}
@ -253,7 +253,7 @@ Middleware<AppState> _createUserLoggedIn(
};
}
Middleware<AppState> _createUIChange(PersistenceRepository uiRepository) {
Middleware<AppState> _createPersistUI(PersistenceRepository uiRepository) {
return (Store<AppState> store, dynamic action, NextDispatcher next) {
next(action);
@ -261,7 +261,7 @@ Middleware<AppState> _createUIChange(PersistenceRepository uiRepository) {
};
}
Middleware<AppState> _createLoadData() {
Middleware<AppState> _createDataLoaded() {
return (Store<AppState> store, dynamic action, NextDispatcher next) {
final dynamic data = action.loginResponse;
store.dispatch(LoadStaticSuccess(data.static));
@ -278,7 +278,7 @@ Middleware<AppState> _createLoadData() {
};
}
Middleware<AppState> _createDataLoaded(
Middleware<AppState> _createPersistData(
PersistenceRepository company1Repository,
PersistenceRepository company2Repository,
PersistenceRepository company3Repository,

View File

@ -32,6 +32,6 @@ AppState appReducer(AppState state, dynamic action) {
? companyReducer(state.companyState4, action) : state.companyState4)
..companyState5.replace(state.uiState.selectedCompanyIndex == 5
? companyReducer(state.companyState5, action) : state.companyState5)
..uiState.replace(uiReducer(state.uiState, action))
..uiState.replace(uiReducer(state.uiState, action))
);
}

View File

@ -14,10 +14,12 @@ List<Middleware<AppState>> createStoreAuthMiddleware([
]) {
final loginInit = _createLoginInit();
final loginRequest = _createLoginRequest(repository);
final refreshRequest = _createRefreshRequest(repository);
return [
TypedMiddleware<AppState, LoadUserLogin>(loginInit),
TypedMiddleware<AppState, UserLoginRequest>(loginRequest),
TypedMiddleware<AppState, RefreshData>(refreshRequest),
];
}
@ -79,6 +81,30 @@ Middleware<AppState> _createLoginRequest(AuthRepository repository) {
};
}
Middleware<AppState> _createRefreshRequest(AuthRepository repository) {
return (Store<AppState> store, dynamic action, NextDispatcher next) {
final authState = store.state.authState;
final companyState = store.state.selectedCompany;
repository
.refresh(authState.url, companyState.token, action.platform)
.then((data) {
_saveAuthLocal(action);
if (_isVersionSupported(data.version)) {
store.dispatch(LoadDataSuccess(completer: action.completer, loginResponse: data));
} else {
store.dispatch(UserLoginFailure(
'The minimum version is v$kMinMajorAppVersion.$kMinMinorAppVersion'));
}
}).catchError((Object error) {
print(error);
store.dispatch(UserLoginFailure(error.toString()));
});
next(action);
};
}
bool _isVersionSupported(String version) {
final parts = version.split('.');

View File

@ -1,3 +1,4 @@
import 'package:invoiceninja_flutter/redux/app/app_actions.dart';
import 'package:redux/redux.dart';
import 'package:invoiceninja_flutter/data/models/models.dart';
import 'package:invoiceninja_flutter/redux/company/company_state.dart';
@ -8,10 +9,13 @@ import 'package:invoiceninja_flutter/redux/dashboard/dashboard_reducer.dart';
import 'package:invoiceninja_flutter/redux/company/company_actions.dart';
CompanyState companyReducer(CompanyState state, dynamic action) {
if (action == RefreshData) {
return CompanyState();
}
return state.rebuild((b) => b
..clientState.replace(clientsReducer(state.clientState, action))
..company.replace(companyEntityReducer(state.company, action))
..clientState.replace(clientsReducer(state.clientState, action))
..dashboardState.replace(dashboardReducer(state.dashboardState, action))
..productState.replace(productsReducer(state.productState, action))
..invoiceState.replace(invoicesReducer(state.invoiceState, action))

View File

@ -4,8 +4,13 @@ import 'package:redux/redux.dart';
Reducer<StaticState> staticReducer = combineReducers([
TypedReducer<StaticState, LoadStaticSuccess>(staticLoadedReducer),
TypedReducer<StaticState, RefreshData>(refreshData),
]);
StaticState refreshData(StaticState staticState, RefreshData action) {
return StaticState();
}
StaticState staticLoadedReducer(StaticState staticState, LoadStaticSuccess action) {
return StaticState().rebuild((b) => b
..currencyMap.addAll(Map.fromIterable(

View File

@ -61,7 +61,9 @@ class SettingsListVM {
store.dispatch(UserLogout());
},
onRefreshTap: (BuildContext context) {
store.dispatch(RefreshData(
Theme.of(context).platform == TargetPlatform.iOS ? 'ios' : 'android',
));
},
onDarkModeChanged: (BuildContext context, bool value) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();