379 lines
13 KiB
Dart
379 lines
13 KiB
Dart
import 'dart:collection';
|
|
import 'package:aitrainer_app/model/customer.dart';
|
|
import 'package:aitrainer_app/model/exercise_plan.dart';
|
|
import 'package:aitrainer_app/model/exercise_plan_detail.dart';
|
|
import 'package:aitrainer_app/model/exercise_tree.dart';
|
|
import 'package:aitrainer_app/model/exercise.dart';
|
|
import 'package:aitrainer_app/model/model_change.dart';
|
|
import 'package:aitrainer_app/model/property.dart';
|
|
import 'package:aitrainer_app/model/workout_menu_tree.dart';
|
|
import 'package:aitrainer_app/repository/customer_repository.dart';
|
|
import 'package:aitrainer_app/repository/exercise_repository.dart';
|
|
import 'package:aitrainer_app/service/customer_exercise_device_service.dart';
|
|
import 'package:aitrainer_app/service/exercise_device_service.dart';
|
|
import 'package:aitrainer_app/service/exercise_tree_service.dart';
|
|
import 'package:aitrainer_app/service/exercisetype_service.dart';
|
|
import 'package:aitrainer_app/util/env.dart';
|
|
import 'package:flurry/flurry.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:aitrainer_app/model/exercise_type.dart';
|
|
|
|
import 'customer_exercise_device.dart';
|
|
import 'exercise_device.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 firebaseUidKey = 'firebase_uid';
|
|
static final String lastStoreDateKey = 'last_date';
|
|
static final String isRegisteredKey = 'is_registered';
|
|
static final String isLoggedInKey = 'is_logged_in';
|
|
static final String langKey = 'lang';
|
|
static final String serverKey = 'live';
|
|
|
|
static 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;
|
|
String firebaseUid;
|
|
|
|
bool firstLoad = true;
|
|
|
|
List<ExerciseType> _exerciseTypes;
|
|
List<ExerciseTree> _exerciseTree;
|
|
|
|
List<Exercise> _exercises;
|
|
ExercisePlan _myExercisePlan;
|
|
List<Property> _properties;
|
|
List<ExerciseDevice> _devices;
|
|
List<CustomerExerciseDevice> _customerDevices;
|
|
|
|
LinkedHashMap<int, ExercisePlanDetail> _myExercisesPlanDetails = LinkedHashMap<int, ExercisePlanDetail>();
|
|
|
|
LinkedHashMap _tree = LinkedHashMap<String, WorkoutMenuTree>();
|
|
double _percentExercises = -1;
|
|
|
|
Customer _trainee;
|
|
List<Exercise> _exercisesTrainee;
|
|
ExercisePlan _traineeExercisePlan;
|
|
List<ExercisePlanDetail> _traineeExercisesPlanDetail;
|
|
|
|
LinkedHashMap<String, int> _badges = LinkedHashMap();
|
|
|
|
List deviceLanguages;
|
|
String startPage;
|
|
String testEnvironment;
|
|
bool liveServer = true;
|
|
|
|
factory Cache() {
|
|
return _singleton;
|
|
}
|
|
|
|
Cache._internal() {
|
|
String testEnv = EnvironmentConfig.test_env;
|
|
this.testEnvironment = testEnv;
|
|
if (testEnv == "1") {
|
|
baseUrl = 'http://aitrainer.app:8899/api/';
|
|
liveServer = false;
|
|
}
|
|
}
|
|
|
|
void setTestBaseUrl() {
|
|
baseUrl = 'http://aitrainer.app:8899/api/';
|
|
}
|
|
|
|
String getAuthToken() {
|
|
return this.authToken;
|
|
}
|
|
|
|
void setServer(bool live) async {
|
|
if (this.testEnvironment == "1") {
|
|
liveServer = false;
|
|
live = false;
|
|
}
|
|
Future<SharedPreferences> prefs = SharedPreferences.getInstance();
|
|
SharedPreferences sharedPreferences;
|
|
sharedPreferences = await prefs;
|
|
liveServer = live;
|
|
print("Set Live servier. live? " + live.toString() + " env? " + this.testEnvironment);
|
|
sharedPreferences.setBool(Cache.serverKey, live);
|
|
}
|
|
|
|
void setServerAddress(SharedPreferences prefs) {
|
|
if (this.testEnvironment == "1") {
|
|
return;
|
|
}
|
|
final bool live = prefs.getBool(Cache.serverKey);
|
|
if (live == null) {
|
|
return;
|
|
}
|
|
liveServer = live;
|
|
if (live) {
|
|
baseUrl = 'http://aitrainer.info:8888/api/';
|
|
}
|
|
}
|
|
|
|
static String getToken(SharedPreferences prefs) {
|
|
return prefs.getString(authTokenKey);
|
|
}
|
|
|
|
static String getBaseUrl() {
|
|
return baseUrl;
|
|
}
|
|
|
|
static String getMediaUrl() {
|
|
return mediaUrl;
|
|
}
|
|
|
|
afterRegistration(Customer customer) {
|
|
Future<SharedPreferences> prefs = SharedPreferences.getInstance();
|
|
|
|
userLoggedIn = customer;
|
|
setPreferences(prefs, SharePrefsChange.registration, customer.customerId, Cache().firebaseUid);
|
|
}
|
|
|
|
afterLogin(Customer customer) async {
|
|
Future<SharedPreferences> prefs = SharedPreferences.getInstance();
|
|
|
|
userLoggedIn = customer;
|
|
await setPreferences(prefs, SharePrefsChange.login, customer.customerId, Cache().firebaseUid);
|
|
}
|
|
|
|
afterFirebaseLogin() async {
|
|
Future<SharedPreferences> prefs = SharedPreferences.getInstance();
|
|
await setPreferences(prefs, SharePrefsChange.login, userLoggedIn.customerId, Cache().firebaseUid);
|
|
}
|
|
|
|
logout() async {
|
|
userLoggedIn = null;
|
|
firebaseUid = null;
|
|
authToken = "";
|
|
_trainee = null;
|
|
_percentExercises = -1;
|
|
_exercisesTrainee = null;
|
|
_traineeExercisePlan = null;
|
|
_exercises = List();
|
|
_myExercisesPlanDetails = LinkedHashMap();
|
|
print("Trainees is null? " + (_trainee == null).toString());
|
|
Future<SharedPreferences> prefs = SharedPreferences.getInstance();
|
|
await setPreferences(prefs, SharePrefsChange.logout, 0, "");
|
|
}
|
|
|
|
setPreferences(Future<SharedPreferences> prefs, SharePrefsChange type, int customerId, String firebaseUid) 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";
|
|
Flurry.setUserId(customerId.toString());
|
|
sharedPreferences.setInt(Cache.customerIdKey, customerId);
|
|
sharedPreferences.setBool(Cache.isRegisteredKey, true);
|
|
sharedPreferences.setBool(Cache.isLoggedInKey, true);
|
|
sharedPreferences.setString(Cache.firebaseUidKey, firebaseUid);
|
|
await ExerciseTypeApi().getExerciseTypes();
|
|
await ExerciseTreeApi().getExerciseTree();
|
|
await ExerciseDeviceApi().getDevices();
|
|
final customerDevices = await CustomerExerciseDeviceApi().getDevices(customerId);
|
|
Cache().setCustomerDevices(customerDevices);
|
|
await exerciseRepository.getExercisesByCustomer(customerId);
|
|
} else if (type == SharePrefsChange.login) {
|
|
Flurry.setUserId(customerId.toString());
|
|
Cache().startPage = "home";
|
|
sharedPreferences.setInt(Cache.customerIdKey, customerId);
|
|
sharedPreferences.setString(Cache.firebaseUidKey, firebaseUid);
|
|
sharedPreferences.setBool(Cache.isLoggedInKey, true);
|
|
await ExerciseTypeApi().getExerciseTypes();
|
|
await ExerciseTreeApi().getExerciseTree();
|
|
await ExerciseDeviceApi().getDevices();
|
|
final customerDevices = await CustomerExerciseDeviceApi().getDevices(customerId);
|
|
Cache().setCustomerDevices(customerDevices);
|
|
await exerciseRepository.getExercisesByCustomer(customerId);
|
|
} else if (type == SharePrefsChange.logout) {
|
|
sharedPreferences.setBool(Cache.isLoggedInKey, false);
|
|
sharedPreferences.setInt(Cache.customerIdKey, 0);
|
|
sharedPreferences.setString(Cache.firebaseUidKey, null);
|
|
sharedPreferences.setString(authTokenKey, "");
|
|
}
|
|
initBadges();
|
|
}
|
|
|
|
void setExerciseTypes(List<ExerciseType> exerciseTypes) {
|
|
this._exerciseTypes = exerciseTypes;
|
|
}
|
|
|
|
void setExerciseTree(List<ExerciseTree> exerciseTree) {
|
|
this._exerciseTree = exerciseTree;
|
|
}
|
|
|
|
void setExercises(List<Exercise> exercises) {
|
|
this._exercises = exercises;
|
|
}
|
|
|
|
void setExercisesTrainee(List<Exercise> exercises) {
|
|
this._exercisesTrainee = exercises;
|
|
}
|
|
|
|
void setWorkoutMenuTree(LinkedHashMap<String, WorkoutMenuTree> tree) {
|
|
this._tree = tree;
|
|
}
|
|
|
|
List<ExerciseType> getExerciseTypes() => this._exerciseTypes;
|
|
|
|
List<ExerciseTree> getExerciseTree() => this._exerciseTree;
|
|
|
|
List<Exercise> getExercises() => this._exercises;
|
|
|
|
List<Exercise> getExercisesTrainee() => this._exercisesTrainee;
|
|
|
|
LinkedHashMap<String, WorkoutMenuTree> getWorkoutMenuTree() => this._tree;
|
|
|
|
void setPercentExercises(double percent) => this._percentExercises = percent;
|
|
|
|
double getPercentExercises() => this._percentExercises;
|
|
|
|
void addExercise(Exercise exercise) => _exercises.add(exercise);
|
|
|
|
void addExerciseTrainee(Exercise exercise) => _exercisesTrainee.add(exercise);
|
|
|
|
Customer getTrainee() => this._trainee;
|
|
|
|
void setTrainee(Customer trainee) => _trainee = trainee;
|
|
|
|
void setTraineeExercisePlan(ExercisePlan exercisePlan) => this._traineeExercisePlan = exercisePlan;
|
|
|
|
ExercisePlan getTraineesExercisePlan() => this._traineeExercisePlan;
|
|
|
|
void setMyExercisePlan(ExercisePlan exercisePlan) => _myExercisePlan = exercisePlan;
|
|
|
|
ExercisePlan getMyExercisePlan() => _myExercisePlan;
|
|
|
|
void setMyExercisePlanDetails(LinkedHashMap<int, ExercisePlanDetail> listExercisePlanDetail) =>
|
|
_myExercisesPlanDetails = listExercisePlanDetail;
|
|
|
|
void addToMyExercisePlanDetails(ExercisePlanDetail detail) => _myExercisesPlanDetails[detail.exerciseTypeId] = detail;
|
|
|
|
LinkedHashMap<int, ExercisePlanDetail> getMyExercisePlanDetails() => _myExercisesPlanDetails;
|
|
|
|
void resetMyExercisePlanDetails() => _myExercisesPlanDetails = LinkedHashMap<int, ExercisePlanDetail>();
|
|
|
|
void updateMyExercisePlanDetail(ExercisePlanDetail detail) {
|
|
this.addToMyExercisePlanDetails(detail);
|
|
}
|
|
|
|
void deleteMyExercisePlanDetail(ExercisePlanDetail detail) => this.deleteMyExercisePlanDetailByExerciseTypeId(detail.exerciseTypeId);
|
|
|
|
void deletedMyExercisePlanDetail(ExercisePlanDetail detail) =>
|
|
this._myExercisesPlanDetails[detail.exerciseTypeId].change = ModelChange.deleted;
|
|
|
|
void deleteMyExercisePlanDetailByExerciseTypeId(int exerciseTypeId) {
|
|
this._myExercisesPlanDetails[exerciseTypeId].change = ModelChange.delete;
|
|
}
|
|
|
|
void setProperties(List<Property> properties) => this._properties = properties;
|
|
|
|
List<Property> getProperties() => _properties;
|
|
|
|
void setDevices(List<ExerciseDevice> devices) => this._devices = devices;
|
|
|
|
List<ExerciseDevice> getDevices() => this._devices;
|
|
|
|
void setCustomerDevices(List<CustomerExerciseDevice> devices) => this._customerDevices = devices;
|
|
|
|
List<CustomerExerciseDevice> getCustomerDevices() => this._customerDevices;
|
|
|
|
LinkedHashMap getBadges() => _badges;
|
|
|
|
void setBadge(String key, bool inc) {
|
|
if (inc) {
|
|
if (_badges[key] != null) {
|
|
_badges[key]++;
|
|
} else {
|
|
_badges[key] = 1;
|
|
}
|
|
} else {
|
|
if (_badges[key] != null) {
|
|
if (_badges[key] == 1) {
|
|
_badges.remove(key);
|
|
} else {
|
|
_badges[key]--;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void setBadgeNr(String key, int counter) {
|
|
if (_badges[key] != null) {
|
|
_badges[key] += counter;
|
|
} else {
|
|
_badges[key] = counter;
|
|
}
|
|
}
|
|
|
|
void initBadges() {
|
|
CustomerRepository customerRepository = CustomerRepository();
|
|
_badges = LinkedHashMap();
|
|
customerRepository.setCustomer(userLoggedIn);
|
|
if (this.userLoggedIn != null) {
|
|
if (this.userLoggedIn.firstname == null || userLoggedIn.firstname.length == 0) {
|
|
setBadge("personalData", true);
|
|
setBadge("account", true);
|
|
}
|
|
if (this._customerDevices == null || this._customerDevices.isEmpty) {
|
|
setBadge("customerDevice", true);
|
|
setBadge("account", true);
|
|
}
|
|
if (userLoggedIn.properties == null || userLoggedIn.properties.isEmpty) {
|
|
setBadge("personalData", true);
|
|
setBadge("Sizes", true);
|
|
setBadge("BMI", true);
|
|
setBadge("BMR", true);
|
|
setBadgeNr("Body Compositions", 3);
|
|
setBadgeNr("home", 3);
|
|
} else if (customerRepository.getWeight() == 0) {
|
|
setBadge("BMI", true);
|
|
setBadge("BMR", true);
|
|
setBadge("Body Compositions", true);
|
|
setBadgeNr("home", 1);
|
|
}
|
|
if (customerRepository.getHeight() == 0) {
|
|
setBadge("BMI", true);
|
|
setBadge("BMR", true);
|
|
setBadge("Body Compositions", true);
|
|
setBadgeNr("home", 1);
|
|
}
|
|
}
|
|
print("Badges: " + _badges.toString());
|
|
}
|
|
}
|