Dr-Blank.Vaani/lib/main.dart

108 lines
3.5 KiB
Dart
Raw Normal View History

2024-05-17 11:04:20 -04:00
import 'package:audio_session/audio_session.dart';
2024-05-08 05:03:49 -04:00
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
2024-05-17 11:04:20 -04:00
import 'package:just_audio_background/just_audio_background.dart'
show JustAudioBackground;
import 'package:just_audio_media_kit/just_audio_media_kit.dart'
show JustAudioMediaKit;
2024-06-28 06:01:56 -04:00
import 'package:logging/logging.dart';
2024-05-08 05:03:49 -04:00
import 'package:whispering_pages/api/server_provider.dart';
import 'package:whispering_pages/db/storage.dart';
2024-06-15 23:43:08 -04:00
import 'package:whispering_pages/features/playback_reporting/providers/playback_reporter_provider.dart';
2024-06-06 15:35:30 -04:00
import 'package:whispering_pages/features/player/providers/audiobook_player.dart';
import 'package:whispering_pages/features/sleep_timer/providers/sleep_timer_provider.dart';
2024-05-09 00:41:19 -04:00
import 'package:whispering_pages/router/router.dart';
2024-05-08 05:03:49 -04:00
import 'package:whispering_pages/settings/api_settings_provider.dart';
import 'package:whispering_pages/settings/app_settings_provider.dart';
2024-06-28 06:01:56 -04:00
import 'package:whispering_pages/shared/extensions/duration_format.dart';
2024-05-08 05:03:49 -04:00
import 'package:whispering_pages/theme/theme.dart';
2024-06-28 06:01:56 -04:00
2024-05-08 05:03:49 -04:00
void main() async {
WidgetsFlutterBinding.ensureInitialized();
2024-06-28 06:01:56 -04:00
// Configure the root Logger
Logger.root.level = Level.ALL; // Capture all logs
Logger.root.onRecord.listen((record) {
// Print log records to the console
debugPrint(
'${record.loggerName}: ${record.level.name}: ${record.time.time}: ${record.message}',
);
});
2024-05-08 05:03:49 -04:00
2024-05-17 11:04:20 -04:00
// for playing audio on windows, linux
JustAudioMediaKit.ensureInitialized();
2024-05-08 05:03:49 -04:00
// initialize the storage
await initStorage();
2024-05-09 23:23:50 -04:00
2024-05-17 11:04:20 -04:00
// for configuring how this app will interact with other audio apps
final session = await AudioSession.instance;
await session.configure(const AudioSessionConfiguration.speech());
// for playing audio in the background
await JustAudioBackground.init(
androidNotificationChannelId: 'com.whispering_pages.bg_demo.channel.audio',
androidNotificationChannelName: 'Audio playback',
androidNotificationOngoing: true,
);
// run the app
2024-05-09 23:23:50 -04:00
runApp(
const ProviderScope(
child: MyApp(),
2024-05-08 05:03:49 -04:00
),
);
}
var routerConfig = const MyAppRouter().config;
2024-05-09 23:23:50 -04:00
2024-05-08 05:03:49 -04:00
class MyApp extends ConsumerWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final servers = ref.watch(audiobookShelfServerProvider);
final apiSettings = ref.watch(apiSettingsProvider);
final needOnboarding = apiSettings.activeUser == null || servers.isEmpty;
2024-05-10 04:11:39 -04:00
if (needOnboarding) {
routerConfig.goNamed(Routes.onboarding.name);
}
2024-05-10 04:11:39 -04:00
2024-06-06 15:35:30 -04:00
return _EagerInitialization(
child: MaterialApp.router(
// debugShowCheckedModeBanner: false,
theme: lightTheme,
darkTheme: darkTheme,
themeMode: ref.watch(appSettingsProvider).isDarkMode
? ThemeMode.dark
: ThemeMode.light,
routerConfig: routerConfig,
),
2024-05-08 05:03:49 -04:00
);
}
}
2024-06-06 15:35:30 -04:00
// https://riverpod.dev/docs/essentials/eager_initialization
// Eagerly initialize providers by watching them.
class _EagerInitialization extends ConsumerWidget {
const _EagerInitialization({required this.child});
final Widget child;
@override
Widget build(BuildContext context, WidgetRef ref) {
// Eagerly initialize providers by watching them.
// By using "watch", the provider will stay alive and not be disposed.
try {
ref.watch(simpleAudiobookPlayerProvider);
ref.watch(sleepTimerProvider);
2024-06-15 23:43:08 -04:00
ref.watch(playbackReporterProvider);
2024-06-06 15:35:30 -04:00
} catch (e) {
debugPrintStack(stackTrace: StackTrace.current, label: e.toString());
}
2024-06-15 23:43:08 -04:00
2024-06-06 15:35:30 -04:00
return child;
}
}