Skip to content

Commit

Permalink
Added an option to switch to the official map
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Apr 28, 2021
1 parent 94c3cbb commit 788577f
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 11 deletions.
16 changes: 15 additions & 1 deletion lib/application/settings/settings_bloc.dart
Expand Up @@ -3,6 +3,7 @@ import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:flutter/foundation.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:genshindb/application/url_page/url_page_bloc.dart';
import 'package:genshindb/domain/enums/enums.dart';
import 'package:genshindb/domain/services/device_info_service.dart';
import 'package:genshindb/domain/services/settings_service.dart';
Expand All @@ -18,8 +19,15 @@ class SettingsBloc extends Bloc<SettingsEvent, SettingsState> {
final DeviceInfoService _deviceInfoService;
final MainBloc _mainBloc;
final HomeBloc _homeBloc;
final UrlPageBloc _urlPageBloc;

SettingsBloc(this._settingsService, this._deviceInfoService, this._mainBloc, this._homeBloc) : super(const SettingsState.loading());
SettingsBloc(
this._settingsService,
this._deviceInfoService,
this._mainBloc,
this._homeBloc,
this._urlPageBloc,
) : super(const SettingsState.loading());

_LoadedState get currentState => state as _LoadedState;

Expand All @@ -39,6 +47,7 @@ class SettingsBloc extends Bloc<SettingsEvent, SettingsState> {
showWeaponDetails: settings.showWeaponDetails,
serverResetTime: settings.serverResetTime,
doubleBackToClose: settings.doubleBackToClose,
useOfficialMap: settings.useOfficialMap,
);
},
themeChanged: (event) async {
Expand Down Expand Up @@ -73,6 +82,11 @@ class SettingsBloc extends Bloc<SettingsEvent, SettingsState> {
_settingsService.doubleBackToClose = event.newValue;
return currentState.copyWith.call(doubleBackToClose: event.newValue);
},
useOfficialMapChanged: (event) async {
_settingsService.useOfficialMap = event.newValue;
_urlPageBloc.add(const UrlPageEvent.init(loadMap: false, loadWishSimulator: false));
return currentState.copyWith.call(useOfficialMap: event.newValue);
},
);

yield s;
Expand Down
4 changes: 4 additions & 0 deletions lib/application/settings/settings_event.dart
Expand Up @@ -31,4 +31,8 @@ abstract class SettingsEvent with _$SettingsEvent {
const factory SettingsEvent.doubleBackToCloseChanged({
@required bool newValue,
}) = _DoubleBackToCloseChanged;

const factory SettingsEvent.useOfficialMapChanged({
@required bool newValue,
}) = _UseOfficialMapChanged;
}
1 change: 1 addition & 0 deletions lib/application/settings/settings_state.dart
Expand Up @@ -12,5 +12,6 @@ abstract class SettingsState with _$SettingsState {
@required bool showWeaponDetails,
@required AppServerResetTimeType serverResetTime,
@required bool doubleBackToClose,
@required bool useOfficialMap,
}) = _LoadedState;
}
15 changes: 12 additions & 3 deletions lib/application/url_page/url_page_bloc.dart
Expand Up @@ -4,6 +4,7 @@ import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:genshindb/domain/services/device_info_service.dart';
import 'package:genshindb/domain/services/network_service.dart';
import 'package:genshindb/domain/services/settings_service.dart';
import 'package:genshindb/domain/services/telemetry_service.dart';

part 'url_page_bloc.freezed.dart';
Expand All @@ -12,25 +13,33 @@ part 'url_page_state.dart';

class UrlPageBloc extends Bloc<UrlPageEvent, UrlPageState> {
final wishSimulatorUrl = 'https://gi-wish-simulator.uzairashraf.dev';
final mapUrl = 'https://genshin-impact-map.appsample.com';
final officialMapUrl = 'https://webstatic-sea.mihoyo.com/app/ys-map-sea/index.html';
final unofficialMapUrl = 'https://genshin-impact-map.appsample.com';

final NetworkService _networkService;
final TelemetryService _telemetryService;
final DeviceInfoService _deviceInfoService;
final SettingsService _settingsService;

UrlPageBloc(this._networkService, this._telemetryService, this._deviceInfoService) : super(const UrlPageState.loading());
UrlPageBloc(
this._networkService,
this._telemetryService,
this._deviceInfoService,
this._settingsService,
) : super(const UrlPageState.loading());

@override
Stream<UrlPageState> mapEventToState(
UrlPageEvent event,
) async* {
final s = await event.map(
init: (e) async {
final finalMapUrl = _settingsService.useOfficialMap ? officialMapUrl : unofficialMapUrl;
final isInternetAvailable = await _networkService.isInternetAvailable();
await _telemetryService.trackUrlOpened(e.loadMap, e.loadWishSimulator, isInternetAvailable);
return UrlPageState.loaded(
hasInternetConnection: isInternetAvailable,
mapUrl: mapUrl,
mapUrl: finalMapUrl,
wishSimulatorUrl: wishSimulatorUrl,
userAgent: _deviceInfoService.userAgent,
);
Expand Down
1 change: 1 addition & 0 deletions lib/domain/models/settings/app_settings.dart
Expand Up @@ -19,6 +19,7 @@ abstract class AppSettings implements _$AppSettings {
@required bool isFirstInstall,
@required AppServerResetTimeType serverResetTime,
@required bool doubleBackToClose,
@required bool useOfficialMap,
}) = _AppSettings;
const AppSettings._();

Expand Down
3 changes: 3 additions & 0 deletions lib/domain/services/settings_service.dart
Expand Up @@ -28,5 +28,8 @@ abstract class SettingsService {
bool get doubleBackToClose;
set doubleBackToClose(bool value);

bool get useOfficialMap;
set useOfficialMap(bool value);

Future<void> init();
}
13 changes: 13 additions & 0 deletions lib/infrastructure/settings_service.dart
Expand Up @@ -15,6 +15,7 @@ class SettingsServiceImpl extends SettingsService {
final _showWeaponDetailsKey = 'ShowWeaponDetailsKey';
final _serverResetTimeKey = 'ServerResetTimeKey';
final _doubleBackToCloseKey = 'DoubleBackToCloseKey';
final _useOfficialMapKey = 'UseOfficialMapKey';

bool _initialized = false;

Expand Down Expand Up @@ -69,6 +70,12 @@ class SettingsServiceImpl extends SettingsService {
@override
set doubleBackToClose(bool value) => _prefs.setBool(_doubleBackToCloseKey, value);

@override
bool get useOfficialMap => _prefs.getBool(_useOfficialMapKey);

@override
set useOfficialMap(bool value) => _prefs.setBool(_useOfficialMapKey, value);

@override
AppSettings get appSettings => AppSettings(
appTheme: appTheme,
Expand All @@ -80,6 +87,7 @@ class SettingsServiceImpl extends SettingsService {
isFirstInstall: isFirstInstall,
serverResetTime: serverResetTime,
doubleBackToClose: doubleBackToClose,
useOfficialMap: useOfficialMap,
);

SettingsServiceImpl(this._logger);
Expand Down Expand Up @@ -134,6 +142,11 @@ class SettingsServiceImpl extends SettingsService {
doubleBackToClose = false;
}

if (_prefs.get(_useOfficialMapKey) == null) {
_logger.info(runtimeType, 'Use the official map will be set to its default (false)');
useOfficialMap = false;
}

_initialized = true;
_logger.info(runtimeType, 'Settings were initialized successfully');
}
Expand Down
1 change: 1 addition & 0 deletions lib/infrastructure/telemetry/telemetry_service.dart
Expand Up @@ -98,6 +98,7 @@ class TelemetryServiceImpl implements TelemetryService {
'IsFirstInstall': settings.isFirstInstall.toString(),
'ServerResetTime': EnumToString.convertToString(settings.serverResetTime),
'DoubleBackToClose': settings.doubleBackToClose.toString(),
'UseOfficialMap': settings.useOfficialMap.toString(),
});
}

Expand Down
3 changes: 2 additions & 1 deletion lib/l10n/intl_en.arb
Expand Up @@ -292,5 +292,6 @@
"pressOnceAgainToExit": "Press once again to exit",
"discordServer": "Discord Server",
"usedItem": "Used item",
"itemIsBeingUsedOnACalculation": "This item is being used in one or more calculator sessions.\nThe used quantity is {quantity}"
"itemIsBeingUsedOnACalculation": "This item is being used in one or more calculator sessions.\nThe used quantity is {quantity}",
"useOfficialMap": "Use the official map"
}
3 changes: 2 additions & 1 deletion lib/l10n/intl_es_ES.arb
Expand Up @@ -292,5 +292,6 @@
"pressOnceAgainToExit": "Presiona una vez más para salir",
"discordServer": "Servidor de Discord",
"usedItem": "Item usado",
"itemIsBeingUsedOnACalculation": "Este item está siendo usado en una o más sesiones de la calculadora.\nLa cantidad usada es {quantity}"
"itemIsBeingUsedOnACalculation": "Este item está siendo usado en una o más sesiones de la calculadora.\nLa cantidad usada es {quantity}",
"useOfficialMap": "Usar el mapa oficial"
}
3 changes: 2 additions & 1 deletion lib/l10n/intl_pt.arb
Expand Up @@ -292,5 +292,6 @@
"pressOnceAgainToExit": "Press once again to exit",
"discordServer": "Discord Server",
"usedItem": "Used item",
"itemIsBeingUsedOnACalculation": "This item is being used in one or more calculator sessions.\nThe used quantity is {quantity}"
"itemIsBeingUsedOnACalculation": "This item is being used in one or more calculator sessions.\nThe used quantity is {quantity}",
"useOfficialMap": "Use the official map"
}
3 changes: 2 additions & 1 deletion lib/l10n/intl_ru.arb
Expand Up @@ -292,5 +292,6 @@
"pressOnceAgainToExit": "Нажмите еще раз, чтобы выйти",
"discordServer": "Discord Server",
"usedItem": "Used item",
"itemIsBeingUsedOnACalculation": "This item is being used in one or more calculator sessions.\nThe used quantity is {quantity}"
"itemIsBeingUsedOnACalculation": "This item is being used in one or more calculator sessions.\nThe used quantity is {quantity}",
"useOfficialMap": "Use the official map"
}
3 changes: 2 additions & 1 deletion lib/l10n/intl_zh_CN.arb
Expand Up @@ -292,5 +292,6 @@
"pressOnceAgainToExit": "再点击一次退出",
"discordServer": "Discord 服务器",
"usedItem": "Used item",
"itemIsBeingUsedOnACalculation": "This item is being used in one or more calculator sessions.\nThe used quantity is {quantity}"
"itemIsBeingUsedOnACalculation": "This item is being used in one or more calculator sessions.\nThe used quantity is {quantity}",
"useOfficialMap": "Use the official map"
}
5 changes: 3 additions & 2 deletions lib/main.dart
Expand Up @@ -89,7 +89,8 @@ class MyApp extends StatelessWidget {
final networkService = getIt<NetworkService>();
final telemetryService = getIt<TelemetryService>();
final deviceInfoService = getIt<DeviceInfoService>();
return UrlPageBloc(networkService, telemetryService, deviceInfoService);
final settingsService = getIt<SettingsService>();
return UrlPageBloc(networkService, telemetryService, deviceInfoService, settingsService);
},
),
BlocProvider(
Expand Down Expand Up @@ -132,7 +133,7 @@ class MyApp extends StatelessWidget {
create: (ctx) {
final settingsService = getIt<SettingsService>();
final deviceInfoService = getIt<DeviceInfoService>();
return SettingsBloc(settingsService, deviceInfoService, ctx.read<MainBloc>(), ctx.read<HomeBloc>());
return SettingsBloc(settingsService, deviceInfoService, ctx.read<MainBloc>(), ctx.read<HomeBloc>(), ctx.read<UrlPageBloc>());
},
),
BlocProvider(
Expand Down
6 changes: 6 additions & 0 deletions lib/presentation/settings/widgets/other_settings.dart
Expand Up @@ -59,6 +59,12 @@ class OtherSettings extends StatelessWidget {
value: settingsState.doubleBackToClose,
onChanged: (newVal) => context.read<SettingsBloc>().add(SettingsEvent.doubleBackToCloseChanged(newValue: newVal)),
),
SwitchListTile(
activeColor: theme.accentColor,
title: Text(s.useOfficialMap),
value: settingsState.useOfficialMap,
onChanged: (newVal) => context.read<SettingsBloc>().add(SettingsEvent.useOfficialMapChanged(newValue: newVal)),
),
ListTile(
dense: true,
contentPadding: EdgeInsets.zero,
Expand Down

0 comments on commit 788577f

Please sign in to comment.