135 lines
4.5 KiB
Dart
135 lines
4.5 KiB
Dart
import 'dart:async';
|
|
import 'dart:developer';
|
|
|
|
import 'package:easy_debounce/easy_throttle.dart';
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:persistent_bottom_nav_bar_v2/persistent_bottom_nav_bar_v2.dart';
|
|
|
|
import 'api/mhsl/breaker/getBreakers/getBreakersResponse.dart';
|
|
import 'api/mhsl/server/userIndex/update/updateUserindex.dart';
|
|
import 'main.dart';
|
|
import 'widget/breaker/breaker.dart';
|
|
import 'model/dataCleaner.dart';
|
|
import 'notification/notificationController.dart';
|
|
import 'notification/notificationTasks.dart';
|
|
import 'notification/notifyUpdater.dart';
|
|
import 'state/app/modules/app_modules.dart';
|
|
import 'state/app/modules/breaker/bloc/breaker_bloc.dart';
|
|
import 'state/app/modules/chatList/bloc/chat_list_bloc.dart';
|
|
import 'state/app/modules/settings/bloc/settings_cubit.dart';
|
|
import 'view/pages/overhang.dart';
|
|
|
|
class App extends StatefulWidget {
|
|
const App({super.key});
|
|
|
|
@override
|
|
State<App> createState() => _AppState();
|
|
}
|
|
|
|
class _AppState extends State<App> with WidgetsBindingObserver {
|
|
late Timer _refetchChats;
|
|
late Timer _updateTimings;
|
|
|
|
@override
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
log('AppLifecycle: $state');
|
|
if (state == AppLifecycleState.resumed) {
|
|
EasyThrottle.throttle('appLifecycleState', const Duration(seconds: 10), () {
|
|
if (!mounted) return;
|
|
log('Refreshing due to LifecycleChange');
|
|
NotificationTasks.updateProviders(context);
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
Main.bottomNavigator = PersistentTabController(initialIndex: 0);
|
|
WidgetsBinding.instance.addObserver(this);
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (!mounted) return;
|
|
context.read<BreakerBloc>().refresh();
|
|
context.read<ChatListBloc>().refresh();
|
|
});
|
|
|
|
_updateTimings = Timer.periodic(const Duration(seconds: 30), (_) {
|
|
if (mounted) setState(() {});
|
|
});
|
|
|
|
_refetchChats = Timer.periodic(const Duration(seconds: 60), (_) {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (!mounted) return;
|
|
context.read<ChatListBloc>().refresh();
|
|
});
|
|
});
|
|
|
|
UpdateUserIndex.index();
|
|
|
|
if (context.read<SettingsCubit>().val().notificationSettings.enabled) {
|
|
void update() => NotifyUpdater.registerToServer();
|
|
FirebaseMessaging.instance.onTokenRefresh.listen((_) => update());
|
|
update();
|
|
}
|
|
|
|
FirebaseMessaging.onMessage.listen((message) {
|
|
if (!mounted) return;
|
|
NotificationController.onForegroundMessageHandler(message, context);
|
|
});
|
|
FirebaseMessaging.onBackgroundMessage(NotificationController.onBackgroundMessageHandler);
|
|
|
|
FirebaseMessaging.onMessageOpenedApp.listen((message) {
|
|
if (!mounted) return;
|
|
NotificationController.onAppOpenedByNotification(message, context);
|
|
});
|
|
FirebaseMessaging.instance.getInitialMessage().then((message) {
|
|
if (message == null || !mounted) return;
|
|
NotificationController.onAppOpenedByNotification(message, context);
|
|
});
|
|
|
|
DataCleaner.cleanOldCache();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_refetchChats.cancel();
|
|
_updateTimings.cancel();
|
|
WidgetsBinding.instance.removeObserver(this);
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => PersistentTabView(
|
|
controller: Main.bottomNavigator,
|
|
navBarOverlap: const NavBarOverlap.none(),
|
|
backgroundColor: Theme.of(context).colorScheme.primary,
|
|
handleAndroidBackButtonPress: false,
|
|
screenTransitionAnimation: const ScreenTransitionAnimation(
|
|
curve: Curves.easeOutQuad,
|
|
duration: Duration(milliseconds: 200),
|
|
),
|
|
tabs: [
|
|
...AppModule.getBottomBarModules(context).map((e) => e.toBottomTab(context)),
|
|
PersistentTabConfig(
|
|
screen: const Breaker(breaker: BreakerArea.more, child: Overhang()),
|
|
item: ItemConfig(
|
|
activeForegroundColor: Theme.of(context).primaryColor,
|
|
inactiveForegroundColor: Theme.of(context).colorScheme.secondary,
|
|
icon: const Icon(Icons.apps),
|
|
title: 'Mehr',
|
|
),
|
|
),
|
|
],
|
|
navBarBuilder: (config) => Style6BottomNavBar(
|
|
navBarConfig: config,
|
|
navBarDecoration: NavBarDecoration(
|
|
border: const Border(top: BorderSide(width: 1, color: Colors.grey)),
|
|
color: Theme.of(context).colorScheme.surface,
|
|
),
|
|
),
|
|
);
|
|
}
|