Files
oott/frontend/lib/main.dart
T

97 lines
2.8 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:flutter/foundation.dart';
2026-02-11 14:07:27 -05:00
import 'package:flutter/material.dart';
2026-05-27 11:38:16 -04:00
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';
2026-02-11 14:07:27 -05:00
import 'package:provider/provider.dart';
import 'navigation.dart';
2026-05-27 11:38:16 -04:00
import 'theme/gruvbox_theme.dart';
2026-02-11 14:07:27 -05:00
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',
);
}
// Initialize Firebase at launch so the firebase_messaging plugin's iOS
// APNs swizzling has a configured app to forward the device token to;
// without this getAPNSToken() never resolves and enabling push fails.
try {
await initFirebaseForPush();
} catch (e, stack) {
debugPrint('Firebase init for push 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');
},
);
2026-02-11 14:07:27 -05:00
}
final class MainApp extends StatelessWidget {
2026-02-11 14:07:27 -05:00
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => AppState(),
2026-05-27 11:38:16 -04:00
child: Consumer<AppState>(
builder: (context, appState, _) => MaterialApp.router(
title: 'OOTT',
theme: appState.theme,
routerConfig: router,
2026-02-11 14:07:27 -05:00
),
),
);
}
}
2026-05-27 11:38:16 -04:00
final _themes = {
'catppuccin_mocha': catppuccinMochaDarkTheme,
'gruvbox_dark': gruvboxDarkTheme,
};
2026-02-11 14:07:27 -05:00
class AppState extends ChangeNotifier {
2026-05-27 11:38:16 -04:00
AppState() {
2026-06-04 09:38:23 -04:00
final saved = PrefUtil.getValue('theme', 'gruvbox_dark') as String;
_themeKey = _themes.containsKey(saved) ? saved : 'gruvbox_dark';
2026-05-27 11:38:16 -04:00
}
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;
2026-05-27 11:38:16 -04:00
_themeKey = key;
notifyListeners();
return PrefUtil.setValue('theme', key);
2026-05-27 11:38:16 -04:00
}
2026-02-11 14:07:27 -05:00
}