Skip to content

Commit

Permalink
Merge branch 'develop' into feature/notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Jun 3, 2021
2 parents 68eb851 + 4604b46 commit 6dca08d
Show file tree
Hide file tree
Showing 36 changed files with 657 additions and 734 deletions.
547 changes: 0 additions & 547 deletions assets/db/game_codes.json

This file was deleted.

2 changes: 1 addition & 1 deletion assets/i18n/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -4436,7 +4436,7 @@
},
{
"key": "Mappa Mare",
"name": "Морская карта",
"name": "Морской атлас",
"description": "Привезённая иностранными торговцами в Ли Юэ карта морских течений и ветров.",
"refinement": "В течение 10 сек. после вызова элементальной реакции увелчивает бонус элементального урона на {{0}}%. Может складываться до 2 раз."
},
Expand Down
24 changes: 22 additions & 2 deletions lib/application/game_codes/game_codes_bloc.dart
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 gadgetsDbPath = '$dbPath/gadgets.json';
static String furnitureDbPath = '$dbPath/furniture.json';
Expand Down Expand Up @@ -46,6 +45,7 @@ class Assets {

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

//Monsters
static String monstersImgsBasePath = 'assets/monsters';
Expand Down
31 changes: 0 additions & 31 deletions lib/domain/models/db/game_codes/game_codes_file.dart

This file was deleted.

3 changes: 2 additions & 1 deletion lib/domain/models/entities.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export 'entities/calculator/calculator_character_skill.dart';
export 'entities/calculator/calculator_item.dart';
export 'entities/calculator/calculator_session.dart';
export 'entities/game_code/used_game_code.dart';
export 'entities/game_code/game_code.dart';
export 'entities/game_code/game_code_reward.dart';
export 'entities/inventory/inventory_item.dart';
export 'entities/inventory/inventory_used_item.dart';
export 'entities/notifications/notification_base.dart';
Expand Down
26 changes: 26 additions & 0 deletions lib/domain/models/entities/game_code/game_code.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:hive/hive.dart';

part 'game_code.g.dart';

@HiveType(typeId: 6)
class GameCode extends HiveObject {
@HiveField(0)
final String code;

@HiveField(1)
DateTime usedOn;

@HiveField(2)
DateTime discoveredOn;

@HiveField(3)
DateTime expiredOn;

@HiveField(4)
bool isExpired;

@HiveField(5)
int region;

GameCode(this.code, this.usedOn, this.discoveredOn, this.expiredOn, this.isExpired, this.region);
}
17 changes: 17 additions & 0 deletions lib/domain/models/entities/game_code/game_code_reward.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:hive/hive.dart';

part 'game_code_reward.g.dart';

@HiveType(typeId: 8)
class GameCodeReward extends HiveObject {
@HiveField(0)
final int gameCodeKey;

@HiveField(1)
final String itemKey;

@HiveField(2)
final int quantity;

GameCodeReward(this.gameCodeKey, this.itemKey, this.quantity);
}
14 changes: 0 additions & 14 deletions lib/domain/models/entities/game_code/used_game_code.dart

This file was deleted.

11 changes: 10 additions & 1 deletion lib/domain/models/game_codes/game_code_model.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import 'package:flutter/cupertino.dart';
import 'package:genshindb/domain/enums/enums.dart';
import 'package:genshindb/domain/models/models.dart';

class GameCodeModel {
final String code;
final AppServerResetTimeType region;

final DateTime discoveredOn;
final DateTime expiredOn;
final bool isExpired;

final bool isUsed;
final List<ItemAscensionMaterialModel> rewards;

GameCodeModel({
@required this.code,
@required this.isExpired,
@required this.isUsed,
@required this.rewards,
@required this.isExpired,
@required this.discoveredOn,
this.expiredOn,
this.region,
});
}
1 change: 0 additions & 1 deletion lib/domain/models/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export 'db/furniture/furniture_file.dart';
export 'db/furniture/furniture_file_model.dart';
export 'db/gadgets/gadget_file_model.dart';
export 'db/gadgets/gadgets_file.dart';
export 'db/game_codes/game_codes_file.dart';
export 'db/materials/material_file_model.dart';
export 'db/materials/materials_file.dart';
export 'db/monsters/monster_file_model.dart';
Expand Down
6 changes: 5 additions & 1 deletion lib/domain/services/data_service.dart
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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> initGadgets();
Future<void> initFurniture();
Expand Down Expand Up @@ -65,8 +64,6 @@ abstract class GenshinService {
DateTime getServerDate(AppServerResetTimeType type);
Duration getDurationUntilServerResetDate(AppServerResetTimeType type);

List<GameCodeFileModel> getAllGameCodes();

List<String> getUpcomingKeys();

MonsterFileModel getMonster(String key);
Expand Down
1 change: 1 addition & 0 deletions lib/domain/utils/date_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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, {
Expand Down
77 changes: 60 additions & 17 deletions lib/infrastructure/data_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class DataServiceImpl implements DataService {
Box<CalculatorCharacterSkill> _calcItemSkillBox;
Box<InventoryItem> _inventoryBox;
Box<InventoryUsedItem> _inventoryUsedItemsBox;
Box<UsedGameCode> _usedGameCodesBox;
Box<GameCode> _gameCodesBox;
Box<GameCodeReward> _gameCodeRewardsBox;
Box<TierListItem> _tierListBox;

Box<NotificationCustom> _notificationsCustomBox;
Expand All @@ -45,7 +46,8 @@ class DataServiceImpl implements DataService {
_calcItemSkillBox = await Hive.openBox<CalculatorCharacterSkill>('calculatorSessionsItemsSkills');
_inventoryBox = await Hive.openBox<InventoryItem>('inventory');
_inventoryUsedItemsBox = await Hive.openBox<InventoryUsedItem>('inventoryUsedItems');
_usedGameCodesBox = await Hive.openBox<UsedGameCode>('usedGameCodes');
_gameCodesBox = await Hive.openBox<GameCode>('gameCodes');
_gameCodeRewardsBox = await Hive.openBox<GameCodeReward>('gameCodeRewards');
_tierListBox = await Hive.openBox<TierListItem>('tierList');

_notificationsCustomBox = await Hive.openBox('notificationsCustom');
Expand Down Expand Up @@ -372,28 +374,68 @@ class DataServiceImpl implements DataService {

@override
List<GameCodeModel> getAllGameCodes() {
final usedCodes = getAllUsedGameCodes();
return _genshinService.getAllGameCodes().map((e) {
final isUsed = usedCodes.contains(e.code);
return GameCodeModel(code: e.code, isExpired: e.isExpired, isUsed: isUsed, rewards: e.rewards);
return _gameCodesBox.values.map((e) {
final rewards = _gameCodeRewardsBox.values.where((el) => el.gameCodeKey == e.key).map((reward) {
final material = _genshinService.getMaterial(reward.itemKey);
return ItemAscensionMaterialModel(quantity: reward.quantity, image: material.image, materialType: material.type);
}).toList();
return GameCodeModel(
code: e.code,
isExpired: e.isExpired,
expiredOn: e.expiredOn,
discoveredOn: e.discoveredOn,
isUsed: e.usedOn != null,
rewards: rewards,
region: e.region != null ? AppServerResetTimeType.values[e.region] : null,
);
}).toList();
}

@override
List<String> getAllUsedGameCodes() {
return _usedGameCodesBox.values.map((e) => e.code).toList();
Future<void> saveGameCodes(List<GameCodeModel> itemsFromApi) async {
final itemsOnDb = _gameCodesBox.values.toList();

for (final item in itemsFromApi) {
final gcOnDb = itemsOnDb.firstWhere((el) => el.code == item.code, orElse: () => null);
if (gcOnDb != null) {
gcOnDb.isExpired = item.isExpired;
gcOnDb.expiredOn = item.expiredOn;
gcOnDb.discoveredOn = item.discoveredOn;
gcOnDb.region = item.region?.index;
await gcOnDb.save();
await deleteAllGameCodeRewards(gcOnDb.key as int);
await saveGameCodeRewards(gcOnDb.key as int, item.rewards);
} else {
final gc = GameCode(item.code, null, item.discoveredOn, item.expiredOn, item.isExpired, item.region?.index);
await _gameCodesBox.add(gc);
//This line shouldn't be necessary, though for testing purposes I'll leave it here
await deleteAllGameCodeRewards(gc.key as int);
await saveGameCodeRewards(gc.key as int, item.rewards);
}
}
}

@override
Future<void> markCodeAsUsed(String code, {bool wasUsed = true}) async {
final usedGameCode = _usedGameCodesBox.values.firstWhere((el) => el.code == code, orElse: () => null);
if (usedGameCode != null) {
await _usedGameCodesBox.delete(usedGameCode.key);
}
Future<void> saveGameCodeRewards(int gameCodeKey, List<ItemAscensionMaterialModel> rewards) {
final rewardsToSave = rewards
.map(
(e) => GameCodeReward(gameCodeKey, _genshinService.getMaterialByImage(e.fullImagePath).key, e.quantity),
)
.toList();
return _gameCodeRewardsBox.addAll(rewardsToSave);
}

if (wasUsed) {
await _usedGameCodesBox.add(UsedGameCode(code, DateTime.now()));
}
@override
Future<void> deleteAllGameCodeRewards(int gameCodeKey) {
final keys = _gameCodeRewardsBox.values.where((el) => el.gameCodeKey == gameCodeKey).map((e) => e.key).toList();
return _gameCodeRewardsBox.deleteAll(keys);
}

@override
Future<void> markCodeAsUsed(String code, {bool wasUsed = true}) async {
final usedGameCode = _gameCodesBox.values.firstWhere((el) => el.code == code, orElse: () => null);
usedGameCode.usedOn = wasUsed ? DateTime.now() : null;
await usedGameCode.save();
}

@override
Expand Down Expand Up @@ -959,7 +1001,8 @@ class DataServiceImpl implements DataService {
Hive.registerAdapter(CalculatorSessionAdapter());
Hive.registerAdapter(InventoryItemAdapter());
Hive.registerAdapter(InventoryUsedItemAdapter());
Hive.registerAdapter(UsedGameCodeAdapter());
Hive.registerAdapter(GameCodeAdapter());
Hive.registerAdapter(GameCodeRewardAdapter());
Hive.registerAdapter(TierListItemAdapter());
Hive.registerAdapter(NotificationCustomAdapter());
Hive.registerAdapter(NotificationExpeditionAdapter());
Expand Down

0 comments on commit 6dca08d

Please sign in to comment.