wt1.1c auth->cache, control page, animation base exercise percent
This commit is contained in:
+13
-16
@@ -1,18 +1,17 @@
|
||||
import 'dart:convert';
|
||||
import 'package:aitrainer_app/util/common.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:aitrainer_app/model/auth.dart';
|
||||
import 'package:aitrainer_app/model/cache.dart';
|
||||
|
||||
class APIClient extends ChangeNotifier {
|
||||
class APIClient with Common {
|
||||
|
||||
Future<String> get(String endPoint, String param) async {
|
||||
final url = Auth.getBaseUrl() + endPoint + param;
|
||||
String authToken = Auth().getAuthToken();
|
||||
final url = Cache.getBaseUrl() + endPoint + param;
|
||||
String authToken = Cache().getAuthToken();
|
||||
if ( authToken.length == 0 ) {
|
||||
var responseJson = await APIClient.authenticateUser(
|
||||
Auth.username,
|
||||
Auth.password
|
||||
Cache.username,
|
||||
Cache.password
|
||||
);
|
||||
authToken = responseJson['token'];
|
||||
}
|
||||
@@ -21,7 +20,6 @@ class APIClient extends ChangeNotifier {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization' : "Bearer " + authToken }
|
||||
);
|
||||
notifyListeners();
|
||||
if(response.statusCode == 200) {
|
||||
return utf8.decode(response.bodyBytes);
|
||||
} else {
|
||||
@@ -30,13 +28,13 @@ class APIClient extends ChangeNotifier {
|
||||
}
|
||||
|
||||
Future<String> post(String endPoint, String body) async {
|
||||
final url = Auth.getBaseUrl() + endPoint;
|
||||
final url = Cache.getBaseUrl() + endPoint;
|
||||
print(" ------------ http/post endpoint $endPoint body $body - url: $url ");
|
||||
String authToken = Auth().getAuthToken();
|
||||
String authToken = Cache().getAuthToken();
|
||||
if ( authToken.length == 0 ) {
|
||||
var responseJson = await APIClient.authenticateUser(
|
||||
Auth.username,
|
||||
Auth.password
|
||||
Cache.username,
|
||||
Cache.password
|
||||
);
|
||||
authToken = responseJson['token'];
|
||||
}
|
||||
@@ -48,14 +46,13 @@ class APIClient extends ChangeNotifier {
|
||||
},
|
||||
body: body,
|
||||
);
|
||||
String decodedResponse = Common.utf8convert(response.body);
|
||||
String decodedResponse = utf8convert(response.body);
|
||||
print(" ------------ response: " + decodedResponse);
|
||||
notifyListeners();
|
||||
return decodedResponse;
|
||||
}
|
||||
|
||||
static dynamic authenticateUser(String email, String password) async {
|
||||
var uri = Auth.getBaseUrl() + "authenticate";
|
||||
var uri = Cache.getBaseUrl() + "authenticate";
|
||||
|
||||
try {
|
||||
final body = '{"username":"$email", "password":"$password"}';
|
||||
@@ -82,7 +79,7 @@ class APIClient extends ChangeNotifier {
|
||||
}
|
||||
|
||||
static fetch(var authToken, var endPoint) async {
|
||||
var uri = Auth.getBaseUrl() + endPoint;
|
||||
var uri = Cache.getBaseUrl() + endPoint;
|
||||
|
||||
try {
|
||||
final response = await http.get(
|
||||
|
||||
@@ -2,7 +2,7 @@ import 'dart:convert';
|
||||
import 'package:aitrainer_app/model/customer.dart';
|
||||
import 'package:aitrainer_app/model/user.dart';
|
||||
import 'package:aitrainer_app/service/api.dart';
|
||||
import 'package:aitrainer_app/model/auth.dart';
|
||||
import 'package:aitrainer_app/model/cache.dart';
|
||||
|
||||
class CustomerApi {
|
||||
final APIClient _client=new APIClient();
|
||||
@@ -44,7 +44,7 @@ class CustomerApi {
|
||||
throw new Exception(jsonDecode(responseBody)['error']);
|
||||
} else {
|
||||
customer = Customer.fromJson(jsonDecode(responseBody));
|
||||
Auth().afterRegistration(customer);
|
||||
Cache().afterRegistration(customer);
|
||||
}
|
||||
} on FormatException catch(exception) {
|
||||
throw new Exception(responseBody);
|
||||
@@ -61,7 +61,7 @@ class CustomerApi {
|
||||
Customer customer;
|
||||
try {
|
||||
customer = Customer.fromJson(jsonDecode(responseBody));
|
||||
await Auth().afterLogin(customer);
|
||||
await Cache().afterLogin(customer);
|
||||
} on FormatException catch(exception) {
|
||||
throw new Exception(responseBody);
|
||||
}
|
||||
@@ -76,12 +76,12 @@ class CustomerApi {
|
||||
"customers/"+customerId.toString(),
|
||||
body);
|
||||
Customer customer = Customer.fromJson(jsonDecode(responseBody));
|
||||
Auth().afterRegistration(customer);
|
||||
Cache().afterRegistration(customer);
|
||||
} catch (exception) {
|
||||
print ("Exception: " + exception.toString());
|
||||
print (" === go to registration ");
|
||||
Auth().logout();
|
||||
Auth().startPage = "registration";
|
||||
Cache().logout();
|
||||
Cache().startPage = "registration";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:aitrainer_app/model/auth.dart';
|
||||
import 'package:aitrainer_app/model/cache.dart';
|
||||
import 'package:aitrainer_app/model/exercise_tree.dart';
|
||||
import 'api.dart';
|
||||
|
||||
@@ -12,7 +12,7 @@ class ExerciseTreeApi {
|
||||
final Iterable json = jsonDecode(body);
|
||||
final List<ExerciseTree> exerciseTree = json.map((exerciseTree) =>
|
||||
ExerciseTree.fromJson(exerciseTree)).toList();
|
||||
Auth().setExerciseTree(exerciseTree);
|
||||
Cache().setExerciseTree(exerciseTree);
|
||||
return exerciseTree;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import 'dart:convert';
|
||||
import 'package:aitrainer_app/model/auth.dart';
|
||||
import 'package:aitrainer_app/model/cache.dart';
|
||||
import 'package:aitrainer_app/model/exercise_type.dart';
|
||||
import 'package:aitrainer_app/service/api.dart';
|
||||
|
||||
@@ -11,7 +11,7 @@ class ExerciseTypeApi {
|
||||
final body = await _client.get("exercise_type/active", "");
|
||||
final Iterable json = jsonDecode(body);
|
||||
final List<ExerciseType> exerciseTypes = json.map( (exerciseType) => ExerciseType.fromJson(exerciseType) ).toList();
|
||||
Auth().setExerciseTypes(exerciseTypes);
|
||||
Cache().setExerciseTypes(exerciseTypes);
|
||||
return exerciseTypes;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user