freezer/lib/main.dart

1007 lines
34 KiB
Dart
Raw Permalink Normal View History

2023-07-29 02:17:26 +00:00
import 'dart:async';
import 'dart:io';
2023-07-29 02:17:26 +00:00
import 'package:cookie_jar/cookie_jar.dart';
2023-07-29 02:17:26 +00:00
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
2023-07-29 02:17:26 +00:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_cache_manager_hive/flutter_cache_manager_hive.dart';
2023-07-29 02:17:26 +00:00
import 'package:flutter_displaymode/flutter_displaymode.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:freezer/api/cache.dart';
import 'package:freezer/api/cookie_jar_isar_storage.dart';
2023-07-29 02:17:26 +00:00
import 'package:freezer/api/definitions.dart';
import 'package:freezer/api/paths.dart';
import 'package:freezer/icons.dart';
import 'package:freezer/page_routes/blur_slide.dart';
import 'package:freezer/page_routes/fade.dart';
2023-07-29 02:17:26 +00:00
import 'package:freezer/page_routes/scale_fade.dart';
import 'package:freezer/type_adapters/uri.dart';
import 'package:freezer/ui/downloads_screen.dart';
import 'package:freezer/ui/fancy_scaffold.dart';
import 'package:freezer/ui/importer_screen.dart';
2023-07-29 02:17:26 +00:00
import 'package:freezer/ui/library.dart';
import 'package:freezer/ui/login_screen.dart';
import 'package:freezer/ui/player_screen.dart';
2023-07-29 02:17:26 +00:00
import 'package:freezer/ui/search.dart';
import 'package:freezer/ui/settings_screen.dart';
import 'package:get_it/get_it.dart';
2023-07-29 02:17:26 +00:00
import 'package:hive_flutter/adapters.dart';
import 'package:i18n_extension/i18n_extension.dart';
import 'package:logging/logging.dart';
2023-07-29 02:17:26 +00:00
import 'package:freezer/translations.i18n.dart';
import 'package:quick_actions/quick_actions.dart';
import 'package:uni_links/uni_links.dart';
import 'package:freezer/type_adapters/audioservicerepeatmode.dart';
import 'package:freezer/type_adapters/datetime.dart';
import 'package:freezer/type_adapters/duration.dart';
import 'package:freezer/type_adapters/mediaitem.dart';
import 'api/deezer.dart';
import 'api/download.dart';
import 'api/player/audio_handler.dart';
2023-07-29 02:17:26 +00:00
import 'settings.dart';
import 'ui/home_screen.dart';
import 'ui/player_bar.dart';
late Function logOut;
GlobalKey<NavigatorState> mainNavigatorKey = GlobalKey<NavigatorState>();
GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
late final CacheManager cacheManager;
2023-07-29 02:17:26 +00:00
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Hive typeadapters
Hive
..registerAdapter(HomePageSectionAdapter())
..registerAdapter(HomePageItemAdapter())
..registerAdapter(HomePageItemTypeAdapter())
..registerAdapter(HomePageSectionLayoutAdapter())
..registerAdapter(SmartTrackListAdapter())
2023-07-29 02:17:26 +00:00
..registerAdapter(TrackAdapter())
..registerAdapter(DeezerImageDetailsAdapter())
2023-07-29 02:17:26 +00:00
..registerAdapter(LyricsAdapter())
..registerAdapter(LyricAdapter())
..registerAdapter(PlaylistAdapter())
..registerAdapter(ArtistAdapter())
..registerAdapter(AlbumAdapter())
..registerAdapter(UserAdapter())
..registerAdapter(AlbumTypeAdapter())
2023-07-29 02:17:26 +00:00
..registerAdapter(DeezerChannelAdapter())
..registerAdapter(ShowAdapter())
..registerAdapter(DurationAdapter())
..registerAdapter(SortingAdapter())
..registerAdapter(SortTypeAdapter())
..registerAdapter(SortSourceTypesAdapter())
..registerAdapter(CacheEntryAdapter())
2023-07-29 02:17:26 +00:00
..registerAdapter(SearchHistoryItemTypeAdapter())
..registerAdapter(CacheAdapter())
..registerAdapter(ColorAdapter())
2023-07-29 02:17:26 +00:00
..registerAdapter(DateTimeAdapter())
..registerAdapter(MediaItemAdapter())
..registerAdapter(AudioServiceRepeatModeAdapter())
..registerAdapter(SettingsAdapter())
..registerAdapter(SpotifyCredentialsSaveAdapter())
..registerAdapter(AudioQualityAdapter())
..registerAdapter(ThemesAdapter())
..registerAdapter(NavigatorRouteTypeAdapter())
..registerAdapter(UriAdapter())
..registerAdapter(QueueSourceAdapter())
..registerAdapter(HomePageAdapter())
..registerAdapter(NavigationRailAppearanceAdapter())
..registerAdapter(HiveCacheObjectAdapter(typeId: 35)); // not working?
final dataDir = await Paths.dataDirectory();
Hive.init(dataDir);
// photos
cacheManager = CacheManager(Config(
DefaultCacheManager.key,
// cache aggressively
stalePeriod: const Duration(days: 30),
maxNrOfCacheObjects: 5000,
));
2023-07-29 02:17:26 +00:00
//Initialize globals
try {
cache = await Cache.load();
settings = await Settings.load();
await settings.save();
} catch (e) {
// ignore: avoid_print
print(e);
// ignore: avoid_print
print(
'Cannot load cache or settings box. Maybe another instance of the app is running?');
exit(1);
}
downloadManager.init();
// cacheManager = HiveCacheManager(
// boxName: 'freezer-images', boxPath: await Paths.cacheDir());
2023-07-29 02:17:26 +00:00
// TODO: WA
final cookieJar =
GetIt.instance.registerSingleton<CookieJar>(await getCookieJar());
final deezerAPI =
GetIt.instance.registerSingleton<DeezerAPI>(DeezerAPI(cookieJar));
2024-03-31 17:29:06 +00:00
deezerAPI.deezerCountry = settings.deezerCountry;
deezerAPI.deezerLanguage = settings.deezerLanguage;
2023-07-29 02:17:26 +00:00
deezerAPI.favoritesPlaylistId = cache.favoritesPlaylistId;
Logger.root.onRecord.listen((record) {
// ignore: avoid_print
print(
'${record.level.name}: ${record.time}: [${record.loggerName}] ${record.message}');
});
2024-03-31 17:29:06 +00:00
if (kDebugMode) {
Logger.root.level = Level.ALL;
}
2023-07-29 02:17:26 +00:00
//Do on BG
await playerHelper.initAudioHandler();
2024-04-29 19:21:19 +00:00
// make database migrations to avoid problems
if (settings.font == 'Deezer') settings.font = 'Mabry Pro';
runApp(const FreezerApp());
2023-07-29 02:17:26 +00:00
}
Future<PersistCookieJar> getCookieJar() async => PersistCookieJar(
storage: IsarStorage('cookies', await Paths.dataDirectory()));
2023-07-29 02:17:26 +00:00
class FreezerApp extends StatefulWidget {
const FreezerApp({super.key});
2023-07-29 02:17:26 +00:00
@override
State<FreezerApp> createState() => _FreezerAppState();
2023-07-29 02:17:26 +00:00
}
class _FreezerAppState extends State<FreezerApp> with WidgetsBindingObserver {
2023-07-29 02:17:26 +00:00
@override
void initState() {
WidgetsBinding.instance.addObserver(this);
2023-07-29 02:17:26 +00:00
super.initState();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.paused:
2024-04-27 00:26:23 +00:00
playerHelper.pause();
break;
case AppLifecycleState.resumed:
playerHelper.start();
break;
default:
break;
2023-07-29 02:17:26 +00:00
}
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
2023-07-29 02:17:26 +00:00
super.dispose();
}
Locale? _locale() {
if (settings.language == null || settings.language!.split('_').length < 2) {
2023-07-29 02:17:26 +00:00
return null;
}
2023-07-29 02:17:26 +00:00
return Locale(
settings.language!.split('_')[0], settings.language!.split('_')[1]);
}
@override
Widget build(BuildContext context) {
return NotificationListener<UpdateThemeNotification>(
onNotification: (notification) {
setState(() => settings.themeData);
return true;
},
child: ScreenUtilInit(
designSize: const Size(1080, 720),
builder: (context, child) =>
DynamicColorBuilder(builder: (lightScheme, darkScheme) {
final lightTheme = settings.materialYouAccent
? ThemeData(
textTheme: settings.textTheme,
fontFamily: settings.fontFamily,
colorScheme: lightScheme,
useMaterial3: true,
appBarTheme: Settings.appBarThemeLight,
)
: settings.themeData;
final darkTheme = settings.materialYouAccent
? ThemeData(
textTheme: settings.textTheme,
fontFamily: settings.fontFamily,
colorScheme: darkScheme,
useMaterial3: true,
brightness: Brightness.dark,
appBarTheme: Settings.appBarThemeDark,
)
: null;
return MaterialApp(
title: 'Freezer',
shortcuts: <ShortcutActivator, Intent>{
...WidgetsApp.defaultShortcuts,
LogicalKeySet(LogicalKeyboardKey.select):
const ActivateIntent(), // DPAD center key, for remote controls
2023-07-29 02:17:26 +00:00
},
theme: lightTheme,
darkTheme: darkTheme,
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: supportedLocales,
home: I18n(
initialLocale: _locale(),
child: const LoginMainWrapper(),
2023-07-29 02:17:26 +00:00
),
navigatorKey: mainNavigatorKey,
);
}),
),
2023-07-29 02:17:26 +00:00
);
}
}
//Wrapper for login and main screen.
class LoginMainWrapper extends StatefulWidget {
const LoginMainWrapper({super.key});
2023-07-29 02:17:26 +00:00
@override
State<LoginMainWrapper> createState() => _LoginMainWrapperState();
2023-07-29 02:17:26 +00:00
}
class _LoginMainWrapperState extends State<LoginMainWrapper> {
@override
void initState() {
if (settings.arl != null) {
final deezerAPI = GetIt.instance<DeezerAPI>();
2023-07-29 02:17:26 +00:00
//Load token on background
deezerAPI.arl = settings.arl!;
2023-07-29 02:17:26 +00:00
settings.offlineMode = true;
deezerAPI.authorize().then((b) async {
if (b) setState(() => settings.offlineMode = false);
if (cache.libraryTracks.isEmpty) {
// load and cache library tracks.
final favorites =
await deezerAPI.fullPlaylist(deezerAPI.favoritesPlaylistId!);
cache.libraryTracks = favorites.tracks!.map((e) => e.id).toList();
}
2023-07-29 02:17:26 +00:00
});
}
//Global logOut function
logOut = _logOut;
super.initState();
}
Future<void> _logOut() async {
await GetIt.instance<DeezerAPI>().logout();
settings.arl = null;
settings.offlineMode = false;
2024-02-13 16:53:25 +00:00
await settings.save();
await Cache.wipe();
setState(() {});
2023-07-29 02:17:26 +00:00
}
@override
Widget build(BuildContext context) {
if (settings.arl == null) {
return LoginWidget(
callback: () => setState(() => {}),
2023-07-29 02:17:26 +00:00
);
}
return const MainScreen();
2023-07-29 02:17:26 +00:00
}
}
// class _PlayerBarSliverDelegate extends SliverPersistentHeaderDelegate {
// @override
// // TODO: implement minExtent
// double get minExtent => 59.0;
// double get maxExtent => 59.0;
//
// @override
// bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) =>
// false;
//
// @override
// Widget build(
// BuildContext context, double shrinkOffset, bool overlapsContent) {
// return PlayerBar();
// }
// }
2023-07-29 02:17:26 +00:00
class MainScreen extends StatefulWidget {
const MainScreen({super.key});
2023-10-16 14:54:56 +00:00
static MainScreenState of(BuildContext context) =>
context.findAncestorStateOfType<MainScreenState>()!;
2023-07-29 02:17:26 +00:00
@override
2023-10-16 14:54:56 +00:00
MainScreenState createState() => MainScreenState();
2023-07-29 02:17:26 +00:00
}
2023-10-16 14:54:56 +00:00
class MainScreenState extends State<MainScreen>
2023-07-29 02:17:26 +00:00
with SingleTickerProviderStateMixin, WidgetsBindingObserver {
final _logger = Logger('_MainScreenState');
2023-07-29 02:17:26 +00:00
final _selected = ValueNotifier<int>(0);
StreamSubscription? _urlLinkStream;
int _keyPressed = 0;
bool textFieldVisited = false;
final navigationBarFocusNode = FocusScopeNode(); // for bottom navigation bar
final screenFocusNode = FocusNode(); // for CustomNavigator
final playerScreenFocusNode = FocusScopeNode();
final playerBarFocusNode = FocusNode();
final _fancyScaffoldKey = GlobalKey<FancyScaffoldState>();
2023-10-16 14:54:56 +00:00
bool isDesktop = false;
2023-07-29 02:17:26 +00:00
@override
void initState() {
//Set display mode
if (settings.displayMode != null && settings.displayMode! >= 0) {
FlutterDisplayMode.supported.then((modes) async {
if (modes.length - 1 >= settings.displayMode!) {
2023-07-29 02:17:26 +00:00
FlutterDisplayMode.setPreferredMode(modes[settings.displayMode!]);
}
2023-07-29 02:17:26 +00:00
});
}
// _startStreamingServer();
2023-07-29 02:17:26 +00:00
//Start with parameters
_setupUniLinks();
_loadPreloadInfo();
_prepareQuickActions();
unawaited(playerHelper.start());
2023-07-29 02:17:26 +00:00
//Check for updates on background
// Future.delayed(Duration(seconds: 5), () {
// FreezerVersions.checkUpdate();
// });
super.initState();
WidgetsBinding.instance.addObserver(this);
}
/// not needed anymore, replaced by [DeezerAudioSource]
// void _startStreamingServer() async {
// await DownloadManager.platform
// .invokeMethod("startServer", {"arl": settings.arl});
// }
2023-07-29 02:17:26 +00:00
void _prepareQuickActions() {
if (!Platform.isAndroid) return;
const QuickActions quickActions = QuickActions();
2023-07-29 02:17:26 +00:00
quickActions.initialize((type) {
_startPreload(type);
});
//Actions
quickActions.setShortcutItems([
ShortcutItem(
type: 'favorites',
localizedTitle: 'Favorites'.i18n,
icon: 'ic_favorites'),
ShortcutItem(
type: 'flow',
localizedTitle: 'Flow'.i18n,
icon: 'ic_flow',
),
2023-07-29 02:17:26 +00:00
]);
}
void _startPreload(String type) async {
await DeezerAPI.instance.authorize();
2023-07-29 02:17:26 +00:00
if (type == 'flow') {
await playerHelper.playFromSmartTrackList(SmartTrackList(id: 'flow'));
return;
}
if (type == 'favorites') {
Playlist p = await DeezerAPI.instance
.fullPlaylist(DeezerAPI.instance.favoritesPlaylistId);
2023-07-29 02:17:26 +00:00
playerHelper.playFromPlaylist(p, p.tracks![0].id);
}
}
void _loadPreloadInfo() async {
if (!Platform.isAndroid) return;
2023-07-29 02:17:26 +00:00
String? info =
await DownloadManager.platform.invokeMethod('getPreloadInfo');
if (info != null) {
//Used if started from android auto
await DeezerAPI.instance.authorize();
2023-07-29 02:17:26 +00:00
_startPreload(info);
}
}
@override
void dispose() {
_urlLinkStream?.cancel();
screenFocusNode.dispose();
navigationBarFocusNode.dispose();
playerBarFocusNode.dispose();
playerScreenFocusNode.dispose();
2023-07-29 02:17:26 +00:00
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
setState(() {
textFieldVisited = false;
});
}
}
void _setupUniLinks() async {
2023-10-14 14:26:13 +00:00
// supported only on android for now
if (!Platform.isAndroid) return;
2023-07-29 02:17:26 +00:00
//Listen to URLs
_urlLinkStream = linkStream.listen((String? link) {
if (link == null) return;
_logger.fine('received link: $link');
if (!context.mounted) return;
2023-07-29 02:17:26 +00:00
openScreenByURL(context, link);
}, onError: (err) {});
//Get initial link on cold start
try {
String? link = await getInitialLink();
_logger.fine('initial link: $link');
if (!context.mounted) return;
2023-07-29 02:17:26 +00:00
if (link != null && link.length > 4) openScreenByURL(context, link);
} catch (e) {}
}
void _handleKey(RawKeyEvent event) {
FocusNode primaryFocus = FocusManager.instance.primaryFocus!;
// After visiting text field, something goes wrong and KeyDown events are not sent, only KeyUp-s.
// So, set this flag to indicate a transition to other "mode"
if (primaryFocus.context!.widget.runtimeType.toString() == 'EditableText') {
textFieldVisited = true;
2023-07-29 02:17:26 +00:00
}
2023-07-29 02:17:26 +00:00
// Movement to navigation bar and back
if ((event is RawKeyUpEvent && textFieldVisited) ||
event is RawKeyDownEvent) {
2023-10-16 14:54:56 +00:00
// only handle if we're running on android
2023-10-14 14:26:13 +00:00
if (event.data is! RawKeyEventDataAndroid) return;
2023-07-29 02:17:26 +00:00
int keyCode = (event.data as RawKeyEventDataAndroid).keyCode;
_logger.fine('KEY PRESSED: $keyCode');
2023-07-29 02:17:26 +00:00
switch (keyCode) {
case 82: // Menu on FireTV
2023-07-29 02:17:26 +00:00
case 127: // Menu on Android TV
case 327: // EPG on Hisense TV
focusToNavbar();
break;
case 22: // LEFT + RIGHT
case 21:
if (_keyPressed == 21 && keyCode == 22 ||
_keyPressed == 22 && keyCode == 21) {
focusToNavbar();
}
_keyPressed = keyCode;
Future.delayed(
const Duration(milliseconds: 100), () => _keyPressed = 0);
2023-07-29 02:17:26 +00:00
break;
case 20: // DOWN
// If it's bottom row, go to navigation bar
var row = primaryFocus.parent;
if (row != null) {
var column = row.parent!;
if (column.children.last == row) {
focusToNavbar();
}
}
break;
case 19: // UP
if (playerBarFocusNode.hasFocus) {
2023-07-29 02:17:26 +00:00
screenFocusNode.parent!.parent!.children
.last // children.last is used for handling "playlists" screen in library. Under CustomNavigator 2 screens appears.
.nextFocus(); // nextFocus is used instead of requestFocus because it focuses on last, bottom, non-visible tile of main page
}
if (navigationBarFocusNode.hasFocus) {
playerBarFocusNode.requestFocus();
}
2023-07-29 02:17:26 +00:00
break;
}
}
// TODO: is this even necessary?
// After visiting text field, something goes wrong and KeyDown events are not sent, only KeyUp-s.
// Focus moving works only on KeyDown events, so here we simulate keys handling as it's done in Flutter
// if (textFieldVisited && event is RawKeyUpEvent) {
// Map<ShortcutActivator, Intent> shortcuts =
// Shortcuts.of(context).shortcuts;
// final BuildContext? primaryContext = primaryFocus.context;
// Intent? intent = shortcuts[LogicalKeySet(event.logicalKey)];
// if (intent != null) {
// Actions.invoke(primaryContext!, intent);
// }
// // WA for "Search field -> navigator -> UP -> DOWN" case. Prevents focus hanging.
// FocusNode? newFocus = FocusManager.instance.primaryFocus;
// if (newFocus is FocusScopeNode) {
// navigationBarFocusNode.requestFocus();
// }
// }
}
void focusToNavbar() {
if (playerBarFocusNode.hasFocus) {
_logger.fine('Focusing to navBar');
return navigationBarFocusNode.requestFocus();
}
_logger.fine('Focusing to playerBar');
return playerBarFocusNode.requestFocus();
2023-07-29 02:17:26 +00:00
}
2023-10-16 14:54:56 +00:00
final _destinationRoutes = <int, String>{
0: '/',
1: '/podcasts',
2: '/library',
3: '/search',
};
final _destinations = <NavigationDestination>[
NavigationDestination(
icon: const Icon(Icons.home_outlined),
selectedIcon: const Icon(Icons.home),
label: 'Home'.i18n),
NavigationDestination(
icon: const Icon(Icons.podcasts), label: 'Podcasts'.i18n),
NavigationDestination(
icon: const Icon(Icons.library_music_outlined),
selectedIcon: const Icon(Icons.library_music),
label: 'Library'.i18n),
NavigationDestination(icon: const Icon(Icons.search), label: 'Search'.i18n),
];
final _navigationRailDestinationRoutes = <int, String>{
0: '/',
1: '/podcasts',
2: '/library/tracks',
3: '/library/albums',
4: '/library/artists',
5: '/library/playlists',
6: '/library/history',
7: '/downloads',
8: '/settings',
9: '/spotify-importer'
2023-10-16 14:54:56 +00:00
};
final _navigationRailDestinations = <NavigationRailDestination>[
NavigationRailDestination(
icon: const Icon(Icons.home_outlined),
selectedIcon: const Icon(Icons.home),
label: Text('Home'.i18n),
),
NavigationRailDestination(
icon: const Icon(Icons.podcasts),
label: Text('Podcasts'.i18n),
),
NavigationRailDestination(
icon: const Icon(Icons.audiotrack_outlined),
selectedIcon: const Icon(Icons.audiotrack),
label: Text('Tracks'.i18n),
),
NavigationRailDestination(
icon: const Icon(Icons.album_outlined),
selectedIcon: const Icon(Icons.album),
label: Text('Albums'.i18n),
),
NavigationRailDestination(
icon: const Icon(Icons.recent_actors_outlined),
selectedIcon: const Icon(Icons.recent_actors),
label: Text('Artists'.i18n),
),
NavigationRailDestination(
icon: const Icon(Icons.playlist_play),
label: Text('Playlists'.i18n),
),
NavigationRailDestination(
icon: const Icon(Icons.history),
label: Text('History'.i18n),
),
NavigationRailDestination(
icon: const Icon(Icons.download_outlined),
selectedIcon: const Icon(Icons.download),
label: Text('Downloads'.i18n),
),
NavigationRailDestination(
icon: const Icon(Icons.settings_outlined),
selectedIcon: const Icon(Icons.settings),
label: Text('Settings'.i18n),
),
NavigationRailDestination(
icon: const Icon(FreezerIcons.spotify),
label: Text('Importer'.i18n),
),
2023-10-16 14:54:56 +00:00
];
void _onDestinationSelected(int s,
{bool useNavigationRailDestinations = false}) {
//Pop all routes until home screen
navigatorKey.currentState!.popUntil((route) => route.isFirst);
navigatorKey.currentState!.pushReplacementNamed(
useNavigationRailDestinations
? _navigationRailDestinationRoutes[s]!
: _destinationRoutes[s]!,
arguments: true);
if (_selected.value != s) _selected.value = s;
}
Widget? buildBottomBar(bool isDesktop) {
if (isDesktop) return null;
if (_selected.value > _destinations.length - 1) _selected.value = 3;
return FocusScope(
node: navigationBarFocusNode,
child: ValueListenableBuilder<int>(
valueListenable: _selected,
builder: (context, value, _) {
return NavigationBar(
selectedIndex: value,
onDestinationSelected: _onDestinationSelected,
destinations: _destinations,
);
}),
);
}
Widget? _buildNavigationRail(bool isDesktop) {
if (!isDesktop) return null;
return ValueListenableBuilder(
valueListenable: _selected,
builder: (context, selected, _) {
return ExtensibleNavigationRail(
destinations: _navigationRailDestinations,
selectedIndex: selected,
onDestinationSelected: (int s) =>
_onDestinationSelected(s, useNavigationRailDestinations: true),
);
});
}
2023-07-29 02:17:26 +00:00
@override
Widget build(BuildContext context) {
return RawKeyboardListener(
focusNode: FocusNode(),
onKey: _handleKey,
2023-10-16 14:54:56 +00:00
child: LayoutBuilder(builder: (context, constraints) {
// check if we're able to display the desktop layout
final isLandscape = constraints.maxWidth > constraints.maxHeight;
isDesktop = isLandscape &&
constraints.maxWidth >= 1100 &&
constraints.maxHeight >= 600;
2023-10-16 14:54:56 +00:00
return FancyScaffold(
key: _fancyScaffoldKey,
navigationRail: _buildNavigationRail(isDesktop),
bottomNavigationBar: buildBottomBar(isDesktop),
bottomPanel: Builder(
builder: (context) => PlayerBar(
backgroundColor: Theme.of(context).cardColor,
focusNode: playerBarFocusNode,
onTap: FancyScaffold.of(context)!.openPanel,
shouldHaveHero: false,
)),
2023-10-16 14:54:56 +00:00
bottomPanelHeight: 68.0,
expandedPanel: FocusScope(
node: playerScreenFocusNode,
2023-07-29 02:17:26 +00:00
skipTraversal: true,
2023-10-16 14:54:56 +00:00
canRequestFocus: true,
child: const PlayerScreen(),
),
onAnimationStatusChange: (status) {
if (status == AnimationStatus.dismissed) {
return playerBarFocusNode.requestFocus();
}
_logger.fine('requesting focus to playerScreen');
playerScreenFocusNode.requestFocus();
},
body: Focus(
focusNode: screenFocusNode,
skipTraversal: true,
canRequestFocus: false,
child: NavigatorPopHandler(
onPop: () => navigatorKey.currentState!.maybePop(),
child: _MainRouteNavigator(
navigatorKey: navigatorKey,
routes: {
Navigator.defaultRouteName: (context) =>
const HomeScreen(),
'/podcasts': (context) => HomePageScreen(
cacheable: true,
channel: const DeezerChannel(
target: 'channels/podcasts'),
title: 'Podcasts'.i18n,
),
'/library': (context) => const LibraryScreen(),
'/library/tracks': (context) => const LibraryTracks(),
'/library/albums': (context) => const LibraryAlbums(),
'/library/artists': (context) => const LibraryArtists(),
'/library/playlists': (context) =>
const LibraryPlaylists(),
'/library/history': (context) => const HistoryScreen(),
'/search': (context) => const SearchScreen(),
'/settings': (context) => const SettingsScreen(),
'/downloads': (context) => const DownloadsScreen(),
'/spotify-importer': (context) =>
const SpotifyImporterV2(),
},
),
2023-10-16 14:54:56 +00:00
)));
}));
2023-07-29 02:17:26 +00:00
}
}
// hella simple null-safe reimplementation of custom_navigator, which is NOT null-safe
class _MainRouteNavigator extends StatefulWidget {
2023-07-29 02:17:26 +00:00
final Map<String, WidgetBuilder> routes;
final GlobalKey<NavigatorState> navigatorKey;
2023-10-16 14:54:56 +00:00
final List<NavigatorObserver> observers;
const _MainRouteNavigator({
Key? key,
required this.routes,
required this.navigatorKey,
2023-10-16 14:54:56 +00:00
this.observers = const <NavigatorObserver>[],
}) : super(key: key);
@override
State<_MainRouteNavigator> createState() => _MainRouteNavigatorState();
}
class _MainRouteNavigatorState extends State<_MainRouteNavigator>
with WidgetsBindingObserver {
@override
void initState() {
2023-10-16 14:54:56 +00:00
WidgetsBinding.instance.addObserver(this);
super.initState();
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
2023-07-29 02:17:26 +00:00
// A system method that get invoked when user press back button on Android or back slide on iOS
@override
Future<bool> didPopRoute() async {
final NavigatorState? navigator = widget.navigatorKey.currentState;
2023-07-29 02:17:26 +00:00
if (navigator == null) return false;
return await navigator.maybePop();
}
@override
Future<bool> didPushRoute(String route) async {
final NavigatorState? navigator = widget.navigatorKey.currentState;
2023-07-29 02:17:26 +00:00
if (navigator == null) return false;
navigator.pushNamed(route);
return true;
}
@override
Widget build(BuildContext context) {
return Navigator(
2023-10-16 14:54:56 +00:00
observers: widget.observers,
key: widget.navigatorKey,
2023-07-29 02:17:26 +00:00
initialRoute: Navigator.defaultRouteName,
onGenerateRoute: _onGenerateRoute,
);
}
Route<dynamic>? _onGenerateRoute<T>(RouteSettings s) {
final routeBuilder = widget.routes[s.name];
2023-07-29 02:17:26 +00:00
if (routeBuilder == null) return null;
if (s.arguments == true) {
return ScaleFadePageRoute(builder: routeBuilder, settings: s);
}
switch (settings.navigatorRouteType) {
case NavigatorRouteType.blur_slide:
return BlurSlidePageRoute<T>(builder: routeBuilder, settings: s);
case NavigatorRouteType.material:
return MaterialPageRoute<T>(builder: routeBuilder, settings: s);
case NavigatorRouteType.cupertino:
return CupertinoPageRoute<T>(builder: routeBuilder, settings: s);
case NavigatorRouteType.fade:
return FadePageRoute<T>(builder: routeBuilder, settings: s);
case NavigatorRouteType.fade_blur:
return FadePageRoute<T>(builder: routeBuilder, settings: s, blur: true);
}
2023-07-29 02:17:26 +00:00
}
}
2023-10-16 14:54:56 +00:00
class ExtensibleNavigationRail extends StatefulWidget {
final List<NavigationRailDestination> destinations;
final int selectedIndex;
final void Function(int)? onDestinationSelected;
const ExtensibleNavigationRail({
super.key,
required this.destinations,
required this.selectedIndex,
this.onDestinationSelected,
});
@override
State<ExtensibleNavigationRail> createState() =>
_ExtensibleNavigationRailState();
}
class _ExtensibleNavigationRailState extends State<ExtensibleNavigationRail> {
bool _extended = false;
@override
Widget build(BuildContext context) {
final child = NavigationRail(
extended: _extended,
destinations: widget.destinations,
selectedIndex: widget.selectedIndex,
onDestinationSelected: widget.onDestinationSelected,
2023-10-16 14:54:56 +00:00
);
if (settings.navigationRailAppearance !=
NavigationRailAppearance.expand_on_hover) {
_extended = settings.navigationRailAppearance ==
NavigationRailAppearance.always_expanded;
return child;
}
return MouseRegion(
onEnter: (_) => setState(() => _extended = true),
onExit: (_) => setState(() => _extended = false),
child: child);
2023-10-16 14:54:56 +00:00
}
}
class UpdateThemeNotification extends Notification {}
void updateTheme(BuildContext context) {
UpdateThemeNotification().dispatch(context);
}
2023-07-29 02:17:26 +00:00
// class FreezerDrawer extends StatelessWidget {
2023-10-16 14:54:56 +00:00
// final double? width;
// const FreezerDrawer({super.key, this.width});
//
2023-07-29 02:17:26 +00:00
// @override
// Widget build(BuildContext context) {
2023-10-16 14:54:56 +00:00
// return NavigationDrawer(
// onDestinationSelected: print,
// children: [
2023-07-29 02:17:26 +00:00
// const DrawerHeader(child: FreezerTitle()),
2023-10-16 14:54:56 +00:00
// NavigationDrawerDestination(
// icon: const Icon(Icons.home_outlined),
// selectedIcon: const Icon(Icons.home),
// label: Text('Home'.i18n)),
2023-07-29 02:17:26 +00:00
// FreezerDrawerTile(
2023-10-16 14:54:56 +00:00
// title: 'Home'.i18n,
// icon: const Icon(Icons.home),
// route: Navigator.defaultRouteName),
// const Divider(),
2023-07-29 02:17:26 +00:00
// FreezerDrawerTile(
// title: 'Tracks'.i18n,
2023-10-16 14:54:56 +00:00
// icon: const Icon(Icons.audiotrack),
2023-07-29 02:17:26 +00:00
// route: '/library/tracks'),
// FreezerDrawerTile(
// title: 'Albums'.i18n,
2023-10-16 14:54:56 +00:00
// icon: const Icon(Icons.album),
2023-07-29 02:17:26 +00:00
// route: '/library/albums'),
// FreezerDrawerTile(
// title: 'Artists'.i18n,
2023-10-16 14:54:56 +00:00
// icon: const Icon(Icons.recent_actors),
2023-07-29 02:17:26 +00:00
// route: '/library/artists'),
// FreezerDrawerTile(
// title: 'Playlists'.i18n,
2023-10-16 14:54:56 +00:00
// icon: const Icon(Icons.playlist_play),
2023-07-29 02:17:26 +00:00
// route: '/library/playlists'),
2023-10-16 14:54:56 +00:00
// const Divider(),
2023-07-29 02:17:26 +00:00
// FreezerDrawerTile(
// title: 'Downloads'.i18n,
2023-10-16 14:54:56 +00:00
// icon: const Icon(Icons.download),
2023-07-29 02:17:26 +00:00
// route: '/downloads'),
// FreezerDrawerTile(
// title: 'History'.i18n,
2023-10-16 14:54:56 +00:00
// icon: const Icon(Icons.history),
2023-07-29 02:17:26 +00:00
// route: '/library/history'),
// FreezerDrawerTile(
// title: 'Settings'.i18n,
2023-10-16 14:54:56 +00:00
// icon: const Icon(Icons.settings),
2023-07-29 02:17:26 +00:00
// route: '/settings'),
2023-10-16 14:54:56 +00:00
// ],
2023-07-29 02:17:26 +00:00
// );
// }
// }
2023-10-16 14:54:56 +00:00
//
// class FreezerDrawerTile extends StatefulWidget {
2023-07-29 02:17:26 +00:00
// final Widget? icon;
// final String title;
// final String route;
// const FreezerDrawerTile(
// {Key? key, this.icon, required this.title, required this.route})
// : super(key: key);
2023-10-16 14:54:56 +00:00
//
// @override
// State<FreezerDrawerTile> createState() => _FreezerDrawerTileState();
// }
//
// class _FreezerDrawerTileState extends State<FreezerDrawerTile> with RouteAware {
// bool _isHighlighted = false;
// late final RouteObserver _routeObserver;
//
// @override
// void didChangeDependencies() {
// _routeObserver = MainScreen.of(context).routeObserver;
// _routeObserver.subscribe(this, ModalRoute.of(context)!);
// super.didChangeDependencies();
// }
//
// @override
// void dispose() {
// _routeObserver.unsubscribe(this);
// super.dispose();
// }
//
// @override
// void didPushNext() => _update();
//
// @override
// void didPopNext() => _update();
//
// void _update() {
// final highlighted = ModalRoute.of(context)?.settings.name == widget.route;
//
// if (highlighted != _isHighlighted) {
// setState(() => _isHighlighted = highlighted);
// }
// }
//
2023-07-29 02:17:26 +00:00
// @override
// Widget build(BuildContext context) {
// return Padding(
// padding: const EdgeInsets.symmetric(horizontal: 8.0),
// child: Material(
// borderRadius: BorderRadius.circular(8.0),
// clipBehavior: Clip.antiAlias,
2023-10-16 14:54:56 +00:00
// color: _isHighlighted
2023-07-29 02:17:26 +00:00
// ? Theme.of(context).brightness == Brightness.dark
// ? Colors.white12
// : Colors.black12
// : null,
// child: ListTile(
2023-10-16 14:54:56 +00:00
// selected: _isHighlighted,
// leading: widget.icon,
2023-07-29 02:17:26 +00:00
// visualDensity: VisualDensity.compact,
2023-10-16 14:54:56 +00:00
// title: Text(widget.title),
2023-07-29 02:17:26 +00:00
// onTap: () {
2023-10-16 14:54:56 +00:00
// navigatorKey.currentState!.pushReplacementNamed(widget.route);
2023-07-29 02:17:26 +00:00
// }),
// ),
// );
// }
// }