Files
oott/frontend/lib/main.dart
T
rzuastiandClaude Opus 4.8 0075072196 Re-register push token at launch; refactor push_service
After a cold restart the app considered push enabled (from prefs) but never
re-registered its FCM token, so a rotated token (or a backend that lost its token
store) left the backend delivering to nothing while the test button still
reported success. Re-register the current token at launch so the backend
converges to the live token on every start, and re-attach the foreground display
handler then too (it was previously only wired during enable()).

Also tidy push_service.dart: a single initPushOnLaunch() startup entry point so
main.dart needs no push internals, one owner for the persisted push-enabled flag
(pushEnabledOnThisDevice / setPushEnabledOnThisDevice) instead of a hard-coded key
in four places, private internal helpers, deduped platform checks, and section
grouping. enable() now reuses the shared token-registration path.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-09 13:56:13 -04:00

96 lines
2.7 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:frontend/theme/catppuccin_mocha_theme.dart';
import 'package:frontend/utils/local_network_permission.dart';
import 'package:frontend/utils/pref_utils.dart';
import 'package:frontend/utils/push_service.dart';
import 'package:provider/provider.dart';
import 'navigation.dart';
import 'theme/gruvbox_theme.dart';
void main() {
runZonedGuarded(
() async {
WidgetsFlutterBinding.ensureInitialized();
FlutterError.onError = (details) {
FlutterError.presentError(details);
debugPrint('FlutterError: ${details.exceptionAsString()}');
};
PlatformDispatcher.instance.onError = (error, stack) {
debugPrint('Uncaught platform error: $error\n$stack');
return true;
};
try {
await PrefUtil.init();
} catch (e, stack) {
debugPrint(
'PrefUtil.init failed, continuing with defaults: $e\n$stack',
);
}
// Resume push at launch (Firebase init + foreground handler + token
// re-registration when already enabled); see initPushOnLaunch.
try {
await initPushOnLaunch();
} catch (e, stack) {
debugPrint('Push launch init failed, continuing: $e\n$stack');
}
// Settle iOS's local-network permission now, at launch, so it isn't
// still being decided when the user first taps "Test" in settings.
unawaited(requestLocalNetworkPermission());
runApp(const MainApp());
},
(error, stack) {
debugPrint('Uncaught zone error: $error\n$stack');
},
);
}
final class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => AppState(),
child: Consumer<AppState>(
builder: (context, appState, _) => MaterialApp.router(
title: 'OOTT',
theme: appState.theme,
routerConfig: router,
),
),
);
}
}
final _themes = {
'catppuccin_mocha': catppuccinMochaDarkTheme,
'gruvbox_dark': gruvboxDarkTheme,
};
class AppState extends ChangeNotifier {
AppState() {
final saved = PrefUtil.getValue('theme', 'gruvbox_dark') as String;
_themeKey = _themes.containsKey(saved) ? saved : 'gruvbox_dark';
}
late String _themeKey;
String get themeKey => _themeKey;
ThemeData get theme => _themes[_themeKey]!;
Future<bool> setTheme(String key) async {
if (!_themes.containsKey(key)) return false;
if (key == _themeKey) return true;
_themeKey = key;
notifyListeners();
return PrefUtil.setValue('theme', key);
}
}