78 lines
2.2 KiB
Dart
78 lines
2.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:aitrainer_app/localization/app_language.dart';
|
|
import 'package:aitrainer_app/model/auth.dart';
|
|
import 'package:aitrainer_app/model/exercise_type.dart';
|
|
import 'package:aitrainer_app/repository/user_repository.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:intl/intl.dart';
|
|
|
|
class Common {
|
|
|
|
static const EMAIL_ERROR = "Please type a right email address here.";
|
|
static const PASSWORD_ERROR = "The password must have at least 8 characters.";
|
|
|
|
|
|
|
|
static String toJson( Map<String, String> map ) {
|
|
String rc = "{";
|
|
map.forEach((key, value) {
|
|
rc += "'$key':'$value'";
|
|
});
|
|
rc += "}";
|
|
return rc;
|
|
}
|
|
|
|
static ExerciseType getExerciseType( int exerciseTypeId ) {
|
|
ExerciseType returnElement = null;
|
|
List<ExerciseType> listExerciseType = Auth().getExerciseTypes();
|
|
if ( listExerciseType != null ) {
|
|
for ( var element in listExerciseType ) {
|
|
if (exerciseTypeId == element.exerciseTypeId) {
|
|
returnElement = element;
|
|
break;
|
|
}
|
|
};
|
|
}
|
|
return returnElement;
|
|
}
|
|
|
|
static String getDateLocale( DateTime datetime, bool timeDisplay ) {
|
|
AppLanguage appLanguage = AppLanguage();
|
|
var date = datetime;
|
|
|
|
String dateName = DateFormat(DateFormat.YEAR_MONTH_DAY, appLanguage.appLocal.toString()).format(date.toUtc());
|
|
if ( timeDisplay ) {
|
|
dateName += " " +DateFormat(DateFormat.HOUR_MINUTE, appLanguage.appLocal.toString()).format(date.toUtc());
|
|
}
|
|
|
|
return dateName;
|
|
}
|
|
|
|
static String utf8convert(String text) {
|
|
List<int> bytes = text.toString().codeUnits;
|
|
return utf8.decode(bytes);
|
|
}
|
|
|
|
static double mediaSizeWidth( BuildContext context ) {
|
|
return MediaQuery.of(context).size.width;
|
|
}
|
|
|
|
static bool validateEmail(UserRepository userRepository) {
|
|
final String email = userRepository.user.email;
|
|
final RegExp _emailRegExp = RegExp(
|
|
r'^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$',
|
|
);
|
|
return _emailRegExp.hasMatch(email);
|
|
}
|
|
|
|
static bool validatePassword(UserRepository userRepository) {
|
|
final password = userRepository.user.password;
|
|
final RegExp _passwordRegExp =
|
|
RegExp(r'^(?=.*[A-Za-z0-9])(?=.*\d)[A-Za-z\d]{7,}$');
|
|
|
|
return _passwordRegExp.hasMatch(password);
|
|
}
|
|
|
|
|
|
} |