Skip to content

Commit

Permalink
[Application] Added a refresh event to load the game codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Jun 3, 2021
1 parent 2265780 commit 6e1878f
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 7 deletions.
24 changes: 22 additions & 2 deletions lib/application/game_codes/game_codes_bloc.dart
Expand Up @@ -4,6 +4,8 @@ import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:genshindb/domain/models/models.dart';
import 'package:genshindb/domain/services/data_service.dart';
import 'package:genshindb/domain/services/game_code_service.dart';
import 'package:genshindb/domain/services/network_service.dart';
import 'package:genshindb/domain/services/telemetry_service.dart';
import 'package:meta/meta.dart';

Expand All @@ -16,12 +18,29 @@ const _initialState = GameCodesState.loaded(workingGameCodes: [], expiredGameCod
class GameCodesBloc extends Bloc<GameCodesEvent, GameCodesState> {
final DataService _dataService;
final TelemetryService _telemetryService;
final GameCodeService _gameCodeService;
final NetworkService _networkService;

GameCodesBloc(this._dataService, this._telemetryService) : super(_initialState);
GameCodesBloc(this._dataService, this._telemetryService, this._gameCodeService, this._networkService) : super(_initialState);

@override
Stream<GameCodesState> mapEventToState(GameCodesEvent event) async* {
final s = await event.when(
if (event is _Refresh) {
final isInternetAvailable = await _networkService.isInternetAvailable();
if (!isInternetAvailable) {
yield state.copyWith.call(isInternetAvailable: false);
yield state.copyWith.call(isInternetAvailable: null);
return;
}
yield _initialState.copyWith.call(isBusy: true, workingGameCodes: [], expiredGameCodes: []);
final gameCodes = await _gameCodeService.getAllGameCodes();
await _dataService.saveGameCodes(gameCodes);

add(const GameCodesEvent.init());
return;
}

final s = await event.maybeWhen(
init: () async {
await _telemetryService.trackGameCodesOpened();
return _buildInitialState();
Expand All @@ -31,6 +50,7 @@ class GameCodesBloc extends Bloc<GameCodesEvent, GameCodesState> {
return _buildInitialState();
},
close: () async => _initialState,
orElse: () async => _initialState,
);

yield s;
Expand Down
2 changes: 2 additions & 0 deletions lib/application/game_codes/game_codes_event.dart
Expand Up @@ -9,5 +9,7 @@ abstract class GameCodesEvent with _$GameCodesEvent {
@required bool wasUsed,
}) = _MarkAsUsed;

const factory GameCodesEvent.refresh() = _Refresh;

const factory GameCodesEvent.close() = _Close;
}
2 changes: 2 additions & 0 deletions lib/application/game_codes/game_codes_state.dart
Expand Up @@ -5,5 +5,7 @@ abstract class GameCodesState with _$GameCodesState {
const factory GameCodesState.loaded({
@required List<GameCodeModel> workingGameCodes,
@required List<GameCodeModel> expiredGameCodes,
@Default(false) bool isBusy,
bool isInternetAvailable,
}) = _Loaded;
}
2 changes: 1 addition & 1 deletion lib/domain/assets.dart
Expand Up @@ -8,7 +8,6 @@ class Assets {
static String artifactsDbPath = '$dbPath/artifacts.json';
static String materialsDbPath = '$dbPath/materials.json';
static String elementsDbPath = '$dbPath/elements.json';
static String gameCodesDbPath = '$dbPath/game_codes.json';
static String monstersDbPath = '$dbPath/monsters.json';
static String translationsBasePath = 'assets/i18n';

Expand Down Expand Up @@ -44,6 +43,7 @@ class Assets {

//Others
static String otherImgsBasePath = 'assets/others';
static String noImageAvailablePath = '$othersBasePath/$noImageAvailableName';

//Monsters
static String monstersImgsBasePath = 'assets/monsters';
Expand Down
6 changes: 5 additions & 1 deletion lib/domain/services/data_service.dart
Expand Up @@ -68,7 +68,11 @@ abstract class DataService {

List<GameCodeModel> getAllGameCodes();

List<String> getAllUsedGameCodes();
Future<void> saveGameCodes(List<GameCodeModel> items);

Future<void> saveGameCodeRewards(int gameCodeKey, List<ItemAscensionMaterialModel> rewards);

Future<void> deleteAllGameCodeRewards(int gameCodeKey);

Future<void> markCodeAsUsed(String code, {bool wasUsed = true});

Expand Down
5 changes: 5 additions & 0 deletions lib/domain/services/game_code_service.dart
@@ -0,0 +1,5 @@
import 'package:genshindb/domain/models/models.dart';

abstract class GameCodeService {
Future<List<GameCodeModel>> getAllGameCodes();
}
3 changes: 0 additions & 3 deletions lib/domain/services/genshin_service.dart
Expand Up @@ -8,7 +8,6 @@ abstract class GenshinService {
Future<void> initArtifacts();
Future<void> initMaterials();
Future<void> initElements();
Future<void> initGameCodes();
Future<void> initMonsters();
Future<void> initTranslations(AppLanguageType languageType);

Expand Down Expand Up @@ -60,8 +59,6 @@ abstract class GenshinService {
int getServerDay(AppServerResetTimeType type);
DateTime getServerDate(AppServerResetTimeType type);

List<GameCodeFileModel> getAllGameCodes();

List<String> getUpcomingKeys();

MonsterFileModel getMonster(String key);
Expand Down
25 changes: 25 additions & 0 deletions lib/domain/utils/date_utils.dart
@@ -0,0 +1,25 @@
import 'package:intl/intl.dart';

class DateUtils {
static const String defaultFormat = 'dd/MM/yyyy hh:mm:ss a';
static const String twentyFourHoursFormat = 'dd/MM/yyyy HH:mm:ss';
static const String dayMonthYearFormat = 'dd/MM/yyyy';

static String formatDate(
DateTime date, {
String locale,
String format = defaultFormat,
}) {
if (date == null) {
return 'N/A';
}
final formatter = DateFormat(format, locale);
final formatted = formatter.format(date);
return formatted;
}

static String formatDateMilitaryTime(DateTime date, {bool useTwentyFourHoursFormat = false}) {
final format = useTwentyFourHoursFormat ? twentyFourHoursFormat : defaultFormat;
return formatDate(date, format: format);
}
}

0 comments on commit 6e1878f

Please sign in to comment.