import 'dart:collection'; import 'package:aitrainer_app/model/customer.dart'; import 'package:aitrainer_app/model/exercise_tree.dart'; import 'package:aitrainer_app/model/exercise.dart'; import 'package:aitrainer_app/model/workout_tree.dart'; import 'package:aitrainer_app/repository/exercise_repository.dart'; import 'package:aitrainer_app/service/exercise_tree_service.dart'; import 'package:aitrainer_app/service/exercisetype_service.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:aitrainer_app/model/exercise_type.dart'; enum SharePrefsChange { login, registration, logout, } /* Auth flow of the app 1. During the login screen the authentication will be executed - if not successful: message: Network error, try again later - if successful - get the stored shared preferences and customer id - if customer_id not present -> registration page - if present, check if the expiration_date > 10 days -> login page - else get the API customer by the stored customer_id - After registration / login store the preferences: - AuthToken - customer_id - last_store_date - is_registered - is_logged_in */ class Cache { static final Cache _singleton = Cache._internal(); // Keys to store and fetch data from SharedPreferences static final String authTokenKey = 'auth_token'; static final String customerIdKey = 'customer_id'; static final String lastStoreDateKey = 'last_date'; static final String isRegisteredKey = 'is_registered'; static final String isLoggedInKey = 'is_logged_in'; static final String baseUrl = 'http://aitrainer.info:8888/api/'; static final String mediaUrl = 'https://aitrainer.info:4343/media/'; static final String username = 'bosi'; static final String password = 'andio2009'; String authToken = ""; Customer userLoggedIn; bool firstLoad = true; List _exerciseTypes; List _exerciseTree; List _exercises; LinkedHashMap _tree = LinkedHashMap(); List deviceLanguages; String startPage; factory Cache() { return _singleton; } Cache._internal(); String getAuthToken() { return this.authToken; } static String getToken(SharedPreferences prefs) { return prefs.getString(authTokenKey); } static String getBaseUrl() { return baseUrl; } static String getMediaUrl() { return mediaUrl; } afterRegistration(Customer customer) { Future prefs = SharedPreferences.getInstance(); userLoggedIn = customer; setPreferences(prefs, SharePrefsChange.registration, customer.customerId); } afterLogin(Customer customer) async { Future prefs = SharedPreferences.getInstance(); userLoggedIn = customer; await setPreferences(prefs, SharePrefsChange.login, customer.customerId); } logout() async { userLoggedIn = null; authToken = ""; //firstLoad = true; Future prefs = SharedPreferences.getInstance(); await setPreferences(prefs, SharePrefsChange.logout, 0); } setPreferences(Future prefs, SharePrefsChange type, int customerId) async { SharedPreferences sharedPreferences; sharedPreferences = await prefs; DateTime now = DateTime.now(); sharedPreferences.setString(Cache.lastStoreDateKey, now.toString()); ExerciseRepository exerciseRepository = ExerciseRepository(); if ( type == SharePrefsChange.registration ) { Cache().startPage = "home"; sharedPreferences.setInt(Cache.customerIdKey, customerId); sharedPreferences.setBool(Cache.isRegisteredKey, true); sharedPreferences.setBool(Cache.isLoggedInKey, true); await ExerciseTypeApi().getExerciseTypes(); await ExerciseTreeApi().getExerciseTree(); exerciseRepository.getExercisesByCustomer(customerId); } else if ( type == SharePrefsChange.login ) { Cache().startPage = "home"; sharedPreferences.setInt(Cache.customerIdKey, customerId); sharedPreferences.setBool(Cache.isLoggedInKey, true); await ExerciseTypeApi().getExerciseTypes(); await ExerciseTreeApi().getExerciseTree(); exerciseRepository.getExercisesByCustomer(customerId); } else if ( type == SharePrefsChange.logout ) { sharedPreferences.setBool(Cache.isLoggedInKey, false); sharedPreferences.setInt(Cache.customerIdKey, 0); sharedPreferences.setString(authTokenKey, ""); } } void setExerciseTypes( List exerciseTypes) { this._exerciseTypes = exerciseTypes; } void setExerciseTree( List exerciseTree) { this._exerciseTree = exerciseTree; } void setExercises( List exercises ) { this._exercises = exercises; } void setWorkoutTree( LinkedHashMap tree) { this._tree = tree; } List getExerciseTypes() { return this._exerciseTypes; } List getExerciseTree() { return this._exerciseTree; } List getExercises() { return this._exercises; } LinkedHashMap getWorkoutTree() { return this._tree; } }