From ed8760f8e8ccf10b3330ca18dea25f7b52d9b8b1 Mon Sep 17 00:00:00 2001 From: bossanyit Date: Fri, 20 Nov 2020 15:32:06 +0100 Subject: [PATCH] WT1.1.2 build 32 login error messages fixes --- ios/Runner.xcodeproj/project.pbxproj | 10 +-- ios/Runner/Info.plist | 4 +- .../customer_change/customer_change_bloc.dart | 3 + lib/bloc/exercise_new/exercise_new_bloc.dart | 4 ++ lib/bloc/login_form_bloc.dart | 3 + lib/localization/app_localization.dart | 16 ++--- lib/repository/user_repository.dart | 21 ++++-- lib/service/api.dart | 64 ++++++------------- lib/service/customer_service.dart | 34 ++++++---- lib/service/firebase_api.dart | 5 +- lib/view/login.dart | 8 +-- lib/view/registration.dart | 14 ++-- lib/widgets/bmr_widget.dart | 1 - 13 files changed, 90 insertions(+), 97 deletions(-) diff --git a/ios/Runner.xcodeproj/project.pbxproj b/ios/Runner.xcodeproj/project.pbxproj index 93b447e..738d8ad 100644 --- a/ios/Runner.xcodeproj/project.pbxproj +++ b/ios/Runner.xcodeproj/project.pbxproj @@ -177,7 +177,7 @@ }; }; }; - buildConfigurationList = 97C146E91CF9000F007C117D /* Build configuration list for PBXProject " " */; + buildConfigurationList = 97C146E91CF9000F007C117D /* Build configuration list for PBXProject "Runner" */; compatibilityVersion = "Xcode 10.0"; developmentRegion = en; hasScannedForEncodings = 0; @@ -362,7 +362,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements; - CURRENT_PROJECT_VERSION = 31; + CURRENT_PROJECT_VERSION = 32; DEVELOPMENT_TEAM = SFJJBDCU6Z; ENABLE_BITCODE = NO; FRAMEWORK_SEARCH_PATHS = ( @@ -505,7 +505,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements; - CURRENT_PROJECT_VERSION = 31; + CURRENT_PROJECT_VERSION = 32; DEVELOPMENT_TEAM = SFJJBDCU6Z; ENABLE_BITCODE = NO; FRAMEWORK_SEARCH_PATHS = ( @@ -540,7 +540,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements; - CURRENT_PROJECT_VERSION = 31; + CURRENT_PROJECT_VERSION = 32; DEVELOPMENT_TEAM = SFJJBDCU6Z; ENABLE_BITCODE = NO; FRAMEWORK_SEARCH_PATHS = ( @@ -570,7 +570,7 @@ /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ - 97C146E91CF9000F007C117D /* Build configuration list for PBXProject " " */ = { + 97C146E91CF9000F007C117D /* Build configuration list for PBXProject "Runner" */ = { isa = XCConfigurationList; buildConfigurations = ( 97C147031CF9000F007C117D /* Debug */, diff --git a/ios/Runner/Info.plist b/ios/Runner/Info.plist index beb9209..80c67d1 100644 --- a/ios/Runner/Info.plist +++ b/ios/Runner/Info.plist @@ -33,6 +33,8 @@ 584181112271127 FacebookDisplayName Mobile Login + FirebaseAppDelegateProxyEnabled + NO LSApplicationQueriesSchemes fbapi @@ -66,7 +68,5 @@ UIViewControllerBasedStatusBarAppearance - FirebaseAppDelegateProxyEnabled - NO diff --git a/lib/bloc/customer_change/customer_change_bloc.dart b/lib/bloc/customer_change/customer_change_bloc.dart index 1320060..3777b72 100644 --- a/lib/bloc/customer_change/customer_change_bloc.dart +++ b/lib/bloc/customer_change/customer_change_bloc.dart @@ -16,6 +16,9 @@ class CustomerChangeBloc extends Bloc double height = 170; CustomerChangeBloc({this.customerRepository}) : super(CustomerChangeInitial()) { year = this.customerRepository.customer.birthYear; + if (year == 0) { + year = 1990; + } weight = this.customerRepository.getWeight(); height = this.customerRepository.getHeight(); } diff --git a/lib/bloc/exercise_new/exercise_new_bloc.dart b/lib/bloc/exercise_new/exercise_new_bloc.dart index 9ccb483..fcd5df2 100644 --- a/lib/bloc/exercise_new/exercise_new_bloc.dart +++ b/lib/bloc/exercise_new/exercise_new_bloc.dart @@ -288,6 +288,10 @@ class ExerciseNewBloc extends Bloc { } double getBMI() { + if (height == 0) { + this.bmi = 0; + return 0; + } this.bmi = weight / (height * height / 10000); this.getGoalBMI(); return this.bmi; diff --git a/lib/bloc/login_form_bloc.dart b/lib/bloc/login_form_bloc.dart index 78254ec..2180212 100644 --- a/lib/bloc/login_form_bloc.dart +++ b/lib/bloc/login_form_bloc.dart @@ -47,8 +47,11 @@ class LoginFormBloc extends FormBloc with Common { } else { // Emit either Loaded or Error await PropertyApi().getProperties(); + print("properties OK"); await userRepository.getUser(); + print("get User OK"); await ExerciseTypeApi().getExerciseTypes(); + print("exerciesTypes OK"); await ExerciseTreeApi().getExerciseTree(); if (Cache().userLoggedIn != null && Cache().userLoggedIn.customerId > 0) { ExerciseRepository exerciseRepository = ExerciseRepository(); diff --git a/lib/localization/app_localization.dart b/lib/localization/app_localization.dart index ab032e3..308a3fc 100644 --- a/lib/localization/app_localization.dart +++ b/lib/localization/app_localization.dart @@ -17,8 +17,7 @@ class AppLocalizations { } // Static member to have a simple access to the delegate from the MaterialApp - static const LocalizationsDelegate delegate = - AppLocalizationsDelegate(); + static const LocalizationsDelegate delegate = AppLocalizationsDelegate(); Map _localizedStrings; @@ -29,8 +28,7 @@ class AppLocalizations { Future load() async { // Load the language JSON file from the "lang" folder print(" -- load language pieces " + locale.languageCode); - String jsonString = - await rootBundle.loadString('i18n/${locale.languageCode}.json'); + String jsonString = await rootBundle.loadString('i18n/${locale.languageCode}.json'); Map jsonMap = json.decode(jsonString); _localizedStrings = jsonMap.map((key, value) { @@ -47,13 +45,12 @@ class AppLocalizations { // This method will be called from every widget which needs a localized text String translate(String key) { if (isTest) return key; - return _localizedStrings[key]; + String translated = _localizedStrings[key]; + return translated != null ? translated : key; } - } -class AppLocalizationsDelegate - extends LocalizationsDelegate { +class AppLocalizationsDelegate extends LocalizationsDelegate { final bool isTest; // This delegate instance will never change (it doesn't even have fields!) // It can provide a constant constructor. @@ -77,7 +74,6 @@ class AppLocalizationsDelegate return localizations; } - @override bool shouldReload(AppLocalizationsDelegate old) => false; -} \ No newline at end of file +} diff --git a/lib/repository/user_repository.dart b/lib/repository/user_repository.dart index 266b3a9..4888683 100644 --- a/lib/repository/user_repository.dart +++ b/lib/repository/user_repository.dart @@ -7,7 +7,7 @@ class UserRepository { User user; UserRepository() { - this.createNewUser(); + this.createNewUser(); } setEmail(String email) { @@ -24,10 +24,15 @@ class UserRepository { Future addUser() async { final User modelUser = this.user; - String rc = await FirebaseApi().registerEmail(modelUser.email, modelUser.password); - if ( rc == FirebaseApi.SIGN_IN_OK ) { - modelUser.firebaseUid = Cache().firebaseUid; - await CustomerApi().addUser(modelUser); + try { + String rc = await FirebaseApi().registerEmail(modelUser.email, modelUser.password); + if (rc == FirebaseApi.SIGN_IN_OK) { + modelUser.firebaseUid = Cache().firebaseUid; + await CustomerApi().addUser(modelUser); + } + } catch (e) { + print(e.toString()); + throw new Exception(e); } } @@ -35,12 +40,14 @@ class UserRepository { final User modelUser = this.user; String rc = await FirebaseApi().signInEmail(modelUser.email, modelUser.password); - if ( rc == FirebaseApi.SIGN_IN_OK ) { + if (rc == FirebaseApi.SIGN_IN_OK) { print("Firebase login ok"); await CustomerApi().getUserByEmail(modelUser.email); + print("GetUserBy Email OK"); Cache().afterFirebaseLogin(); } else { - throw Exception("Exception: Customer does not exist or the password is wrong"); + print("Exception: user not found or password is wrong"); + throw Exception("Customer does not exist or the password is wrong"); } } diff --git a/lib/service/api.dart b/lib/service/api.dart index ed4abac..665cda9 100644 --- a/lib/service/api.dart +++ b/lib/service/api.dart @@ -5,26 +5,18 @@ import 'package:http/http.dart' as http; import 'package:aitrainer_app/model/cache.dart'; class APIClient with Common { - Future get(String endPoint, String param) async { final url = Cache.getBaseUrl() + endPoint + param; String authToken = Cache().getAuthToken(); - if ( authToken.length == 0 ) { - var responseJson = await APIClient.authenticateUser( - Cache.username, - Cache.password - ); + if (authToken.length == 0) { + var responseJson = await APIClient.authenticateUser(Cache.username, Cache.password); authToken = responseJson['token']; } - final response = await http.get(url, - headers: { - 'Content-Type': 'application/json', - 'Authorization' : "Bearer " + authToken } - ); + final response = await http.get(url, headers: {'Content-Type': 'application/json', 'Authorization': "Bearer " + authToken}); print(" ------------get response code: " + response.statusCode.toString()); - if(response.statusCode == 200) { + if (response.statusCode == 200) { return utf8.decode(response.bodyBytes); - } else if(response.statusCode == 404 ) { + } else if (response.statusCode == 404) { throw NotFoundException(message: "Not Found"); } else { throw Exception("Network Error, please try again later"); @@ -35,20 +27,15 @@ class APIClient with Common { final url = Cache.getBaseUrl() + endPoint; print(" ------------ http/post endpoint $endPoint body $body - url: $url "); String authToken = Cache().getAuthToken(); - if ( authToken.length == 0 ) { - var responseJson = await APIClient.authenticateUser( - Cache.username, - Cache.password - ); + if (authToken.length == 0) { + var responseJson = await APIClient.authenticateUser(Cache.username, Cache.password); authToken = responseJson['token']; } - final response = await http.post(url, - headers: { - 'Content-Type': 'application/json; charset=UTF-8', - 'Authorization' : "Bearer " + authToken - }, - body: body, + final response = await http.post( + url, + headers: {'Content-Type': 'application/json; charset=UTF-8', 'Authorization': "Bearer " + authToken}, + body: body, ); print(" ------------post response code: " + response.statusCode.toString()); final String decodedResponse = utf8convert(response.body); @@ -62,25 +49,18 @@ class APIClient with Common { try { final body = '{"username":"$email", "password":"$password"}'; print("authentication with $email"); - final response = await http.post( - uri, - headers: { - 'Authorization': '1', - 'Content-Type': 'application/json' - }, - body: body - ); + final response = await http.post(uri, headers: {'Authorization': '1', 'Content-Type': 'application/json'}, body: body); final responseCode = response.statusCode; - if ( responseCode != 200) { - return { "error" : "Authentication error, total failure", }; + if (responseCode != 200) { + return { + "error": "Authentication error, total failure", + }; } final responseJson = json.decode(response.body); return responseJson; - } catch (exception) { - return { "error" : "Network error, try again later " - + exception.toString()}; + return {"error": "Network error, try again later " + exception.toString()}; } } @@ -90,22 +70,18 @@ class APIClient with Common { try { final response = await http.get( uri, - headers: { - 'Authorization': authToken - }, + headers: {'Authorization': authToken}, ); final responseJson = json.decode(response.body); return responseJson; - } catch (exception) { print(exception); - if(exception.toString().contains('SocketException')) { + if (exception.toString().contains('SocketException')) { return 'NetworkError'; } else { return null; } } } - -} \ No newline at end of file +} diff --git a/lib/service/customer_service.dart b/lib/service/customer_service.dart index 11a1caf..5cd229d 100644 --- a/lib/service/customer_service.dart +++ b/lib/service/customer_service.dart @@ -6,6 +6,7 @@ import 'package:aitrainer_app/model/property.dart'; import 'package:aitrainer_app/model/user.dart'; import 'package:aitrainer_app/service/api.dart'; import 'package:aitrainer_app/model/cache.dart'; +import 'package:aitrainer_app/util/not_found_exception.dart'; class CustomerApi { final APIClient _client = new APIClient(); @@ -72,12 +73,14 @@ class CustomerApi { Customer customer; try { customer = Customer.fromJson(jsonDecode(responseBody)); - /* if (customer.firebaseUid == null) { + if (customer.firebaseUid == null) { await this.updateFirebaseUid(customer.customerId, Cache().firebaseUid); - } */ + } Cache().userLoggedIn = customer; final List properties = await this.getActualProperties(customer.customerId); - this._initProperties(properties); + if (properties != null) { + this._initProperties(properties); + } } on FormatException { throw new Exception(responseBody); } @@ -93,8 +96,10 @@ class CustomerApi { final List properties = await this.getActualProperties(customerId); print(" ---- Props: " + properties.toString()); Cache().afterRegistration(customer); - this._initProperties(properties); - } catch (exception) { + if (properties != null) { + this._initProperties(properties); + } + } on Exception catch (exception) { print("Exception: " + exception.toString()); print(" === go to registration "); Cache().logout(); @@ -162,13 +167,20 @@ class CustomerApi { } Future> getActualProperties(int customerId) async { - final body = await _client.get("customer_property/last/", customerId.toString()); - final Iterable json = jsonDecode(body); - final List properties = json.map((property) => CustomerProperty.fromJson(property)).toList(); + List properties; + try { + final body = await _client.get("customer_property/last/", customerId.toString()); + final Iterable json = jsonDecode(body); + properties = json.map((property) => CustomerProperty.fromJson(property)).toList(); - properties.forEach((element) { - print("Property " + element.toString()); - }); + if (properties != null) { + properties.forEach((element) { + print("Property " + element.toString()); + }); + } + } on Exception catch (ex) { + print(ex.toString()); + } return properties; } diff --git a/lib/service/firebase_api.dart b/lib/service/firebase_api.dart index ec72675..58b89c2 100644 --- a/lib/service/firebase_api.dart +++ b/lib/service/firebase_api.dart @@ -65,10 +65,11 @@ class FirebaseApi { } else if (e.code == 'email-already-in-use') { print('The account already exists for that email.'); rc = REGISTER_EMAIL_IN_USE; - userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(email: email, password: password); + throw Exception("The email address has been registered already"); + /* userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(email: email, password: password); if (rc != null) { rc = SIGN_IN_OK; - } + } */ } } catch (e) { print(e); diff --git a/lib/view/login.dart b/lib/view/login.dart index 3b04e5a..0aa0763 100644 --- a/lib/view/login.dart +++ b/lib/view/login.dart @@ -51,7 +51,8 @@ class _LoginWidget extends State with Common, Trans { }, onFailure: (context, state) { LoadingDialog.hide(context); - showInSnackBar(t(state.failureResponse)); + Scaffold.of(context).showSnackBar(SnackBar( + backgroundColor: Colors.orange, content: Text(t(state.failureResponse), style: TextStyle(color: Colors.white)))); }, onLoaded: (context, state) {}, child: loadForm(loginBloc, accountBloc)), @@ -159,9 +160,4 @@ class _LoginWidget extends State with Common, Trans { ])), ); } - - void showInSnackBar(String error) { - _scaffoldKey.currentState - .showSnackBar(SnackBar(backgroundColor: Colors.orange, content: Text(error, style: TextStyle(color: Colors.white)))); - } } diff --git a/lib/view/registration.dart b/lib/view/registration.dart index 12c8c71..cb89508 100644 --- a/lib/view/registration.dart +++ b/lib/view/registration.dart @@ -4,6 +4,7 @@ import 'package:aitrainer_app/localization/app_localization.dart'; import 'package:aitrainer_app/util/common.dart'; import 'package:aitrainer_app/repository/customer_repository.dart'; import 'package:aitrainer_app/repository/user_repository.dart'; +import 'package:aitrainer_app/util/trans.dart'; import 'package:aitrainer_app/widgets/app_bar_min.dart'; import 'package:aitrainer_app/widgets/splash.dart'; import 'package:flutter/material.dart'; @@ -34,11 +35,12 @@ class RegistrationWidget extends StatefulWidget { State createState() => _RegistrationWidget(); } -class _RegistrationWidget extends State with Common { +class _RegistrationWidget extends State with Common, Trans { final GlobalKey _scaffoldKey = new GlobalKey(); @override Widget build(BuildContext context) { + setContext(context); // ignore: close_sinks final accountBloc = BlocProvider.of(context); return BlocProvider( @@ -60,7 +62,8 @@ class _RegistrationWidget extends State with Common { }, onFailure: (context, state) { LoadingDialog.hide(context); - showInSnackBar(state.failureResponse); + Scaffold.of(context).showSnackBar(SnackBar( + backgroundColor: Colors.orange, content: Text(t(state.failureResponse), style: TextStyle(color: Colors.white)))); }, child: SafeArea( bottom: false, @@ -158,11 +161,4 @@ class _RegistrationWidget extends State with Common { ); })); } - - void showInSnackBar(String error) { - _scaffoldKey.currentState.showSnackBar(SnackBar( - backgroundColor: Colors.orange, - content: Text(AppLocalizations.of(context).translate("There is an error: during registration:") + " " + error, - style: TextStyle(color: Colors.white)))); - } } diff --git a/lib/widgets/bmr_widget.dart b/lib/widgets/bmr_widget.dart index 0bfef08..e6eb547 100644 --- a/lib/widgets/bmr_widget.dart +++ b/lib/widgets/bmr_widget.dart @@ -168,7 +168,6 @@ class _BMRState extends State with Trans { Widget getFitnessLevel() { String fitnessLevel = widget.exerciseBloc.fitnessLevel; - print("sel " + fitnessLevel + FitnessItem().getItem(fitnessLevel).stateText); return Container( padding: EdgeInsets.only(left: 65, right: 65), child: DropdownSearch(