144 lines
4.6 KiB
Dart
144 lines
4.6 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:aitrainer_app/util/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:badges/badges.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:sentry/sentry.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;
|
|
}
|
|
|
|
static String emailValidation(String email) {
|
|
bool emailValid = RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+").hasMatch(email);
|
|
return emailValid ? null : "Please type an email address";
|
|
}
|
|
|
|
static String passwordValidation(String value) {
|
|
if (value == null || value.length == 0) {
|
|
return null;
|
|
}
|
|
bool valid = 8 < value.length;
|
|
return valid ? null : "Password too short";
|
|
}
|
|
|
|
static Widget badgedIcon(Color color, IconData icon, String badgeKey) {
|
|
bool show = Cache().getBadges()[badgeKey] != null;
|
|
int counter = Cache().getBadges()[badgeKey] != null ? Cache().getBadges()[badgeKey] : 0;
|
|
return Badge(
|
|
position: BadgePosition.topEnd(top: -10, end: -10),
|
|
animationDuration: Duration(milliseconds: 500),
|
|
animationType: BadgeAnimationType.slide,
|
|
badgeColor: Colors.red,
|
|
showBadge: show,
|
|
badgeContent: Text(
|
|
counter.toString(),
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
child: Icon(
|
|
icon,
|
|
color: color,
|
|
),
|
|
);
|
|
}
|
|
|
|
static Future<void> sendMessage(String message) async {
|
|
// Sends a full Sentry event payload to show the different parts of the UI.
|
|
await Sentry.captureMessage(
|
|
message,
|
|
level: SentryLevel.info,
|
|
template: 'Message: %s, customerId: ' + Cache().userLoggedIn.customerId.toString(),
|
|
);
|
|
}
|
|
}
|