Skip to content

Commit

Permalink
[Application] Related push notification logic will be initialized in …
Browse files Browse the repository at this point in the history
…the main_bloc.dart.dart
  • Loading branch information
Wolfteam committed Oct 9, 2024
1 parent 375f33e commit 1462e41
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
47 changes: 45 additions & 2 deletions lib/application/main/main_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:shiori/application/bloc.dart';
import 'package:shiori/domain/enums/enums.dart';
import 'package:shiori/domain/extensions/string_extensions.dart';
import 'package:shiori/domain/models/models.dart';
import 'package:shiori/domain/services/api_service.dart';
import 'package:shiori/domain/services/data_service.dart';
import 'package:shiori/domain/services/device_info_service.dart';
import 'package:shiori/domain/services/genshin_service.dart';
Expand All @@ -29,12 +31,15 @@ class MainBloc extends Bloc<MainEvent, MainState> {
final PurchaseService _purchaseService;
final DataService _dataService;
final NotificationService _notificationService;
final ApiService _apiService;

final CharactersBloc _charactersBloc;
final WeaponsBloc _weaponsBloc;
final HomeBloc _homeBloc;
final ArtifactsBloc _artifactsBloc;

final List<StreamSubscription> _subscriptions = [];

MainBloc(
this._logger,
this._genshinService,
Expand All @@ -45,6 +50,7 @@ class MainBloc extends Bloc<MainEvent, MainState> {
this._purchaseService,
this._dataService,
this._notificationService,
this._apiService,
this._charactersBloc,
this._weaponsBloc,
this._homeBloc,
Expand All @@ -56,7 +62,11 @@ class MainBloc extends Bloc<MainEvent, MainState> {
@override
Stream<MainState> mapEventToState(MainEvent event) async* {
final s = await event.when(
init: (updateResult) async => _init(init: true, updateResult: updateResult),
init: (updateResult, pushNotificationTranslations) async => _init(
init: true,
updateResult: updateResult,
pushNotificationTranslations: pushNotificationTranslations,
),
themeChanged: (theme) async => _loadThemeData(theme, _settingsService.accentColor),
accentColorChanged: (accentColor) async => _loadThemeData(_settingsService.appTheme, accentColor),
languageChanged: (language) async => _init(languageChanged: true),
Expand All @@ -67,7 +77,18 @@ class MainBloc extends Bloc<MainEvent, MainState> {
yield s;
}

Future<MainState> _init({bool languageChanged = false, bool init = false, AppResourceUpdateResultType? updateResult}) async {
@override
Future<void> close() async {
await _cancelSubscriptions();
return super.close();
}

Future<MainState> _init({
bool languageChanged = false,
bool init = false,
AppResourceUpdateResultType? updateResult,
PushNotificationTranslations? pushNotificationTranslations,
}) async {
_logger.info(runtimeType, '_init: Initializing all..');
await _genshinService.init(_settingsService.language, noResourcesHaveBeenDownloaded: _settingsService.noResourcesHasBeenDownloaded);

Expand All @@ -82,6 +103,24 @@ class MainBloc extends Bloc<MainEvent, MainState> {
final settings = _settingsService.appSettings;
await _telemetryService.trackInit(settings);

if (pushNotificationTranslations != null) {
//TODO: if the language changes, the push notification will still be shown in the previous lang
await _cancelSubscriptions();
_subscriptions.addAll(await _notificationService.initPushNotifications(pushNotificationTranslations));

if (_settingsService.mustRegisterPushNotificationsToken && _settingsService.pushNotificationsToken.isNotNullEmptyOrWhitespace) {
final registerResponse = await _apiService.registerDeviceToken(
_deviceInfoService.version,
_settingsService.resourceVersion,
_settingsService.pushNotificationsToken,
);
if (registerResponse.succeed) {
_settingsService.mustRegisterPushNotificationsToken = false;
await _telemetryService.trackDeviceRegisteredForPushNotifications(_settingsService.pushNotificationsToken);
}
}
}

final state = _loadThemeData(settings.appTheme, settings.accentColor, updateResult: updateResult);

if (init) {
Expand Down Expand Up @@ -123,4 +162,8 @@ class MainBloc extends Bloc<MainEvent, MainState> {

return MainState.loading(language: _localeService.getLocaleWithoutLang(), restarted: true);
}

Future<void> _cancelSubscriptions() {
return Future.wait(_subscriptions.map((s) => s.cancel()));
}
}
1 change: 1 addition & 0 deletions lib/application/main/main_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ part of 'main_bloc.dart';
class MainEvent with _$MainEvent {
const factory MainEvent.init({
required AppResourceUpdateResultType? updateResultType,
required PushNotificationTranslations? pushNotificationTranslations,
}) = _Init;

const factory MainEvent.themeChanged({
Expand Down
2 changes: 1 addition & 1 deletion lib/injection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ class Injection {

getIt.registerLazySingleton<TelemetryService>(() => TelemetryServiceImpl(getIt<DeviceInfoService>()));

getIt.registerLazySingleton<NotificationService>(() => NotificationServiceImpl(getIt<LoggingService>()));
getIt.registerLazySingleton<NotificationService>(() => NotificationServiceImpl(getIt<LoggingService>(), getIt<SettingsService>()));

getIt.registerLazySingleton<ChangelogProvider>(
() => ChangelogProviderImpl(getIt<LoggingService>(), getIt<NetworkService>(), getIt<ApiService>()),
Expand Down
7 changes: 6 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'dart:io';

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:shiori/application/bloc.dart';
import 'package:shiori/domain/services/api_service.dart';
import 'package:shiori/domain/services/calculator_asc_materials_service.dart';
import 'package:shiori/domain/services/data_service.dart';
import 'package:shiori/domain/services/device_info_service.dart';
Expand All @@ -14,6 +16,7 @@ import 'package:shiori/domain/services/purchase_service.dart';
import 'package:shiori/domain/services/resources_service.dart';
import 'package:shiori/domain/services/settings_service.dart';
import 'package:shiori/domain/services/telemetry_service.dart';
import 'package:shiori/firebase_options.dart';
import 'package:shiori/injection.dart';
import 'package:shiori/presentation/app_widget.dart';
import 'package:shiori/presentation/shared/utils/size_utils.dart';
Expand All @@ -22,13 +25,13 @@ import 'package:window_size/window_size.dart';
Future<void> main() async {
//This is required by app center
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
await Injection.init();
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
setWindowMinSize(SizeUtils.minSizeOnDesktop);
setWindowMaxSize(Size.infinite);
}
//TODO: CHECK THE NOTIFICATION LOGIC
//TODO: WEBVIEW SUPPORT IN MACOS
Bloc.observer = AppBlocObserver(getIt<LoggingService>());
runApp(MyApp());
}
Expand Down Expand Up @@ -85,6 +88,7 @@ class MyApp extends StatelessWidget {
final purchaseService = getIt<PurchaseService>();
final dataService = getIt<DataService>();
final notificationService = getIt<NotificationService>();
final apiService = getIt<ApiService>();
return MainBloc(
loggingService,
genshinService,
Expand All @@ -95,6 +99,7 @@ class MyApp extends StatelessWidget {
purchaseService,
dataService,
notificationService,
apiService,
ctx.read<CharactersBloc>(),
ctx.read<WeaponsBloc>(),
ctx.read<HomeBloc>(),
Expand Down
12 changes: 10 additions & 2 deletions lib/presentation/splash/splash_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ class SplashPage extends StatelessWidget {

void _initMain(AppResourceUpdateResultType result, BuildContext context) {
WakelockPlus.disable();
context.read<MainBloc>().add(MainEvent.init(updateResultType: result));
final s = S.of(context);
final pushNotificationTranslations = PushNotificationTranslations.fromS(s: s);
context.read<MainBloc>().add(MainEvent.init(updateResultType: result, pushNotificationTranslations: pushNotificationTranslations));
}

void _applyUpdate(CheckForUpdatesResult result, BuildContext context) {
Expand Down Expand Up @@ -212,7 +214,13 @@ class _Buttons extends StatelessWidget {
),
if (canSkipUpdate)
TextButton.icon(
onPressed: () => context.read<MainBloc>().add(MainEvent.init(updateResultType: updateResultType)),
onPressed: () {
final s = S.of(context);
final pushNotificationTranslations = PushNotificationTranslations.fromS(s: s);
context
.read<MainBloc>()
.add(MainEvent.init(updateResultType: updateResultType, pushNotificationTranslations: pushNotificationTranslations));
},
icon: const Icon(Icons.arrow_right_alt, color: Colors.white),
label: Text(s.continueLabel, style: const TextStyle(color: Colors.white)),
style: buttonStyle,
Expand Down

0 comments on commit 1462e41

Please sign in to comment.