100 lines
3.2 KiB
Dart
100 lines
3.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:aitrainer_app/localization/app_language.dart';
|
|
import 'package:aitrainer_app/model/cache.dart';
|
|
import 'package:aitrainer_app/model/exercise_type.dart';
|
|
import 'package:aitrainer_app/repository/user_repository.dart';
|
|
import 'package:aitrainer_app/util/env.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:intl/intl.dart';
|
|
|
|
class DateRate {
|
|
static String daily = "daily";
|
|
static String weekly = "weekly";
|
|
static String monthly = "monthly";
|
|
static String yearly = "yearly";
|
|
}
|
|
|
|
mixin Common {
|
|
final EMAIL_ERROR = "Please type a right email address here.";
|
|
final PASSWORD_ERROR = "The password must have at least 8 characters.";
|
|
|
|
String toJson(Map<String, String> map) {
|
|
String rc = "{";
|
|
map.forEach((key, value) {
|
|
rc += "'$key':'$value'";
|
|
});
|
|
rc += "}";
|
|
return rc;
|
|
}
|
|
|
|
ExerciseType getExerciseType(int exerciseTypeId) {
|
|
ExerciseType returnElement;
|
|
List<ExerciseType> listExerciseType = Cache().getExerciseTypes();
|
|
if (listExerciseType != null) {
|
|
for (var element in listExerciseType) {
|
|
if (exerciseTypeId == element.exerciseTypeId) {
|
|
returnElement = element;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return returnElement;
|
|
}
|
|
|
|
String getDateLocale(DateTime datetime, bool timeDisplay) {
|
|
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;
|
|
}
|
|
|
|
String utf8convert(String text) {
|
|
List<int> bytes = text.toString().codeUnits;
|
|
return utf8.decode(bytes);
|
|
}
|
|
|
|
double mediaSizeWidth(BuildContext context) {
|
|
return MediaQuery.of(context).size.width;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
/// Calculates week number from a date as per https://en.wikipedia.org/wiki/ISO_week_date#Calculation
|
|
int weekNumber(DateTime date) {
|
|
int dayOfYear = int.parse(DateFormat("D").format(date));
|
|
return ((dayOfYear - date.weekday + 10) / 7).floor();
|
|
}
|
|
|
|
String getDatePart(DateTime date, String dateRate) {
|
|
String datePart = DateFormat('MM.dd', AppLanguage().appLocal.toString()).format(date);
|
|
if (dateRate == DateRate.weekly) {
|
|
datePart = weekNumber(date).toString();
|
|
} else if (dateRate == DateRate.monthly) {
|
|
datePart = DateFormat('MMM', AppLanguage().appLocal.toString()).format(date);
|
|
} else if (dateRate == DateRate.yearly) {
|
|
datePart = DateFormat('y', AppLanguage().appLocal.toString()).format(date);
|
|
} else if (dateRate == DateRate.daily) {
|
|
datePart = DateFormat('MM.dd', AppLanguage().appLocal.toString()).format(date);
|
|
}
|
|
return datePart;
|
|
}
|
|
}
|