Skip to content

Commit

Permalink
[Infrastructure] Added a better impl. of the getCharacterBirthdays me…
Browse files Browse the repository at this point in the history
…thod
  • Loading branch information
Wolfteam committed May 6, 2022
1 parent 8d1619b commit c965aff
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
62 changes: 62 additions & 0 deletions lib/infrastructure/genshin_service.dart
Expand Up @@ -10,6 +10,7 @@ import 'package:shiori/domain/extensions/string_extensions.dart';
import 'package:shiori/domain/models/models.dart';
import 'package:shiori/domain/services/genshin_service.dart';
import 'package:shiori/domain/services/locale_service.dart';
import 'package:shiori/domain/utils/date_utils.dart';

class GenshinServiceImpl implements GenshinService {
final LocaleService _localeService;
Expand Down Expand Up @@ -1062,6 +1063,67 @@ class GenshinServiceImpl implements GenshinService {
return charts;
}

//TODO: CALL THIS METHOD IN THE MAIN PAGE
@override
List<CharacterBirthdayModel> getCharacterBirthdays({int? month, int? day}) {
if (month == null && day == null) {
throw Exception('You must provide a month, day or both');
}

if (month != null && (month < DateTime.january || month > DateTime.december)) {
throw Exception('The provided month = $month is not valid');
}

if (day != null && day <= 0) {
throw Exception('The provided day = $day is not valid');
}

if (day != null && month != null) {
final lastDay = DateUtils.getLastDayOfMonth(month);
if (day > lastDay) {
throw Exception('The provided day = $day is not valid for month = $month');
}
}

return _charactersFile.characters.where((char) {
if (char.isComingSoon) {
return false;
}

if (char.birthday.isNullEmptyOrWhitespace) {
return false;
}

final charBirthday = _localeService.getCharBirthDate(char.birthday);
if (month != null && day != null) {
return charBirthday.month == month && charBirthday.day == day;
}
if (month != null) {
return charBirthday.month == month;
}
if (day != null) {
return charBirthday.day == day;
}

return true;
}).map((e) {
//TODO: CHECK BENNET
final char = getCharacterForCard(e.key);
final birthday = _localeService.getCharBirthDate(e.birthday, useCurrentYear: true);
final now = DateTime.now();
final nowFromZero = DateTime(now.year, now.month, now.day);
return CharacterBirthdayModel(
key: e.key,
name: char.name,
image: char.image,
birthday: birthday,
birthdayString: e.birthday!,
daysUntilBirthday: nowFromZero.difference(birthday).inDays.abs(),
);
}).toList()
..sort((x, y) => x.daysUntilBirthday.compareTo(y.daysUntilBirthday));
}

CharacterCardModel _toCharacterForCard(CharacterFileModel character) {
final translation = getCharacterTranslation(character.key);

Expand Down
15 changes: 13 additions & 2 deletions lib/infrastructure/locale_service.dart
Expand Up @@ -12,7 +12,7 @@ class LocaleServiceImpl implements LocaleService {
LocaleServiceImpl(this._settingsService);

@override
DateTime getCharBirthDate(String? birthday) {
DateTime getCharBirthDate(String? birthday, {bool useCurrentYear = false}) {
if (birthday.isNullEmptyOrWhitespace) {
throw Exception('Character birthday must not be null');
}
Expand All @@ -23,7 +23,18 @@ class LocaleServiceImpl implements LocaleService {
final locale = getFormattedLocale(_settingsService.language);
final format = DateFormat('MM/dd/yyyy', locale);
//The format is in MM/dd, I use 2024 since that is a leap-year
return format.parse('$birthday/2024');
final now = DateTime.now();
//TODO: TEST THIS WITH BENNET SINCE THE NOW DATE MAY NOT BE A LEAP YEAR
final year = useCurrentYear ? now.year : 2024;
final charBirthday = format.parse('$birthday/$year');
if (!useCurrentYear) {
return charBirthday;
}
//TODO: TEST THIS WITH BENNET
if (charBirthday.isBefore(now)) {
return charBirthday.add(const Duration(days: 365));
}
return charBirthday;
}

@override
Expand Down
3 changes: 3 additions & 0 deletions lib/infrastructure/telemetry/telemetry_service.dart
Expand Up @@ -171,4 +171,7 @@ class TelemetryServiceImpl implements TelemetryService {

@override
Future<void> trackChartsOpened() => trackEventAsync('Charts-Opened');

@override
Future<void> trackBirthdaysPerMonthOpened(int month) => trackEventAsync('BirthdaysPerMonth-Opened', {'Month': '$month'});
}

0 comments on commit c965aff

Please sign in to comment.