Skip to content

Commit

Permalink
feat(keyboard-shortcuts): home sidebar tab navigation and close app
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Sep 29, 2022
1 parent 2734454 commit 8f258e7
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 19 deletions.
25 changes: 14 additions & 11 deletions lib/components/Home/Home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ List<String> spotifyScopes = [
"playlist-read-collaborative"
];

final selectedIndexState = StateProvider((ref) => 0);

class Home extends HookConsumerWidget {
Home({Key? key}) : super(key: key);
final logger = getLogger(Home);
Expand All @@ -51,8 +53,9 @@ class Home extends HookConsumerWidget {
xxl: 256.0,
);
final extended = ref.watch(sidebarExtendedStateProvider);
final _selectedIndex = useState(0);
_onSelectedIndexChanged(int index) => _selectedIndex.value = index;
final selectedIndex = ref.watch(selectedIndexState);
onSelectedIndexChanged(int index) =>
ref.read(selectedIndexState.notifier).state = index;

final downloader = ref.watch(downloaderProvider);
final isMounted = useIsMounted();
Expand Down Expand Up @@ -115,24 +118,24 @@ class Home extends HookConsumerWidget {

return Scaffold(
bottomNavigationBar: SpotubeNavigationBar(
selectedIndex: _selectedIndex.value,
onSelectedIndexChanged: _onSelectedIndexChanged,
selectedIndex: selectedIndex,
onSelectedIndexChanged: onSelectedIndexChanged,
),
body: Column(
children: [
if (_selectedIndex.value != 3)
if (selectedIndex != 3)
kIsMobile
? titleBarContents
: WindowTitleBarBox(child: titleBarContents),
Expanded(
child: Row(
children: [
Sidebar(
selectedIndex: _selectedIndex.value,
onSelectedIndexChanged: _onSelectedIndexChanged,
selectedIndex: selectedIndex,
onSelectedIndexChanged: onSelectedIndexChanged,
),
// contents of the spotify
if (_selectedIndex.value == 0)
if (selectedIndex == 0)
Expanded(
child: Padding(
padding: const EdgeInsets.only(
Expand Down Expand Up @@ -177,9 +180,9 @@ class Home extends HookConsumerWidget {
}),
),
),
if (_selectedIndex.value == 1) const Search(),
if (_selectedIndex.value == 2) const UserLibrary(),
if (_selectedIndex.value == 3) const SyncedLyrics(),
if (selectedIndex == 1) const Search(),
if (selectedIndex == 2) const UserLibrary(),
if (selectedIndex == 3) const SyncedLyrics(),
],
),
),
Expand Down
31 changes: 29 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,39 @@ class _SpotubeState extends ConsumerState<Spotube> with WidgetsBindingObserver {
...WidgetsApp.defaultShortcuts,
const SingleActivator(LogicalKeyboardKey.space): PlayPauseIntent(ref),
const SingleActivator(LogicalKeyboardKey.comma, control: true):
OpenSettingsIntent(_router),
NavigationIntent(_router, "/settings"),
const SingleActivator(
LogicalKeyboardKey.keyB,
control: true,
shift: true,
): HomeTabIntent(ref, tab: HomeTabs.browse),
const SingleActivator(
LogicalKeyboardKey.keyS,
control: true,
shift: true,
): HomeTabIntent(ref, tab: HomeTabs.search),
const SingleActivator(
LogicalKeyboardKey.keyL,
control: true,
shift: true,
): HomeTabIntent(ref, tab: HomeTabs.library),
const SingleActivator(
LogicalKeyboardKey.keyY,
control: true,
shift: true,
): HomeTabIntent(ref, tab: HomeTabs.lyrics),
const SingleActivator(
LogicalKeyboardKey.keyW,
control: true,
shift: true,
): CloseAppIntent(),
},
actions: {
...WidgetsApp.defaultActions,
PlayPauseIntent: PlayPauseAction(),
OpenSettingsIntent: OpenSettingsAction(),
NavigationIntent: NavigationAction(),
HomeTabIntent: HomeTabAction(),
CloseAppIntent: CloseAppAction(),
},
);
}
Expand Down
65 changes: 59 additions & 6 deletions lib/models/Intents.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import 'package:bitsdojo_window/bitsdojo_window.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:spotify/spotify.dart';
import 'package:spotube/components/Home/Home.dart';
import 'package:spotube/components/Player/PlayerControls.dart';
import 'package:spotube/models/Logger.dart';
import 'package:spotube/provider/Playback.dart';
import 'package:spotube/utils/platform.dart';

class PlayPauseIntent extends Intent {
final WidgetRef ref;
Expand Down Expand Up @@ -40,16 +44,51 @@ class PlayPauseAction extends Action<PlayPauseIntent> {
}
}

class OpenSettingsIntent extends Intent {
class NavigationIntent extends Intent {
final GoRouter router;
const OpenSettingsIntent(this.router);
final String path;
const NavigationIntent(this.router, this.path);
}

class OpenSettingsAction extends Action<OpenSettingsIntent> {
class NavigationAction extends Action<NavigationIntent> {
@override
invoke(intent) async {
intent.router.push("/settings");
FocusManager.instance.primaryFocus?.unfocus();
invoke(intent) {
intent.router.go(intent.path);
return null;
}
}

enum HomeTabs {
browse,
search,
library,
lyrics,
}

class HomeTabIntent extends Intent {
final WidgetRef ref;
final HomeTabs tab;
const HomeTabIntent(this.ref, {required this.tab});
}

class HomeTabAction extends Action<HomeTabIntent> {
@override
invoke(intent) {
final notifier = intent.ref.read(selectedIndexState.notifier);
switch (intent.tab) {
case HomeTabs.browse:
notifier.state = 0;
break;
case HomeTabs.search:
notifier.state = 1;
break;
case HomeTabs.library:
notifier.state = 2;
break;
case HomeTabs.lyrics:
notifier.state = 3;
break;
}
return null;
}
}
Expand Down Expand Up @@ -83,3 +122,17 @@ class SeekAction extends Action<SeekIntent> {
return null;
}
}

class CloseAppIntent extends Intent {}

class CloseAppAction extends Action<CloseAppIntent> {
@override
invoke(intent) {
if (kIsDesktop) {
appWindow.close();
} else {
SystemNavigator.pop();
}
return null;
}
}

0 comments on commit 8f258e7

Please sign in to comment.