mirror of
https://github.com/Dr-Blank/Vaani.git
synced 2025-12-06 11:09:28 +00:00
intercept back button to minimize player first
and also navigate stack of branches like youtube
This commit is contained in:
parent
ed236ef117
commit
402e264137
4 changed files with 106 additions and 68 deletions
|
|
@ -58,10 +58,4 @@ double playerHeight(
|
||||||
return playerExpandProgress.value;
|
return playerExpandProgress.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// a final MiniplayerController controller = MiniplayerController();
|
final audioBookMiniplayerController = MiniplayerController();
|
||||||
@Riverpod(keepAlive: true)
|
|
||||||
Raw<MiniplayerController> miniplayerController(
|
|
||||||
MiniplayerControllerRef ref,
|
|
||||||
) {
|
|
||||||
return MiniplayerController();
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -38,22 +38,5 @@ final playerHeightProvider = Provider<double>.internal(
|
||||||
);
|
);
|
||||||
|
|
||||||
typedef PlayerHeightRef = ProviderRef<double>;
|
typedef PlayerHeightRef = ProviderRef<double>;
|
||||||
String _$miniplayerControllerHash() =>
|
|
||||||
r'a3677e63a9823881f45a12855e74bf558322e0ec';
|
|
||||||
|
|
||||||
/// See also [miniplayerController].
|
|
||||||
@ProviderFor(miniplayerController)
|
|
||||||
final miniplayerControllerProvider =
|
|
||||||
Provider<Raw<MiniplayerController>>.internal(
|
|
||||||
miniplayerController,
|
|
||||||
name: r'miniplayerControllerProvider',
|
|
||||||
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
|
|
||||||
? null
|
|
||||||
: _$miniplayerControllerHash,
|
|
||||||
dependencies: null,
|
|
||||||
allTransitiveDependencies: null,
|
|
||||||
);
|
|
||||||
|
|
||||||
typedef MiniplayerControllerRef = ProviderRef<Raw<MiniplayerController>>;
|
|
||||||
// ignore_for_file: type=lint
|
// ignore_for_file: type=lint
|
||||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member
|
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member
|
||||||
|
|
|
||||||
|
|
@ -93,7 +93,7 @@ class AudiobookPlayer extends HookConsumerWidget {
|
||||||
minHeight: playerMinHeight,
|
minHeight: playerMinHeight,
|
||||||
// subtract the height of notches and other system UI
|
// subtract the height of notches and other system UI
|
||||||
maxHeight: playerMaxHeight,
|
maxHeight: playerMaxHeight,
|
||||||
controller: ref.watch(miniplayerControllerProvider),
|
controller: audioBookMiniplayerController,
|
||||||
elevation: 4,
|
elevation: 4,
|
||||||
onDismissed: () {
|
onDismissed: () {
|
||||||
// add a delay before closing the player
|
// add a delay before closing the player
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,16 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||||
|
import 'package:miniplayer/miniplayer.dart';
|
||||||
import 'package:whispering_pages/features/explore/providers/search_controller.dart';
|
import 'package:whispering_pages/features/explore/providers/search_controller.dart';
|
||||||
import 'package:whispering_pages/features/player/providers/player_form.dart';
|
import 'package:whispering_pages/features/player/providers/player_form.dart';
|
||||||
import 'package:whispering_pages/features/player/view/audiobook_player.dart';
|
import 'package:whispering_pages/features/player/view/audiobook_player.dart';
|
||||||
|
|
||||||
|
// stack to track changes in navigationShell.currentIndex
|
||||||
|
// home is always at index 0 and at the start and should be the last before popping
|
||||||
|
// if stack is empty, push home, if already contains home, pop it
|
||||||
|
final Set<int> navigationShellStack = {};
|
||||||
|
|
||||||
/// Builds the "shell" for the app by building a Scaffold with a
|
/// Builds the "shell" for the app by building a Scaffold with a
|
||||||
/// BottomNavigationBar, where [child] is placed in the body of the Scaffold.
|
/// BottomNavigationBar, where [child] is placed in the body of the Scaffold.
|
||||||
class ScaffoldWithNavBar extends HookConsumerWidget {
|
class ScaffoldWithNavBar extends HookConsumerWidget {
|
||||||
|
|
@ -30,10 +36,57 @@ class ScaffoldWithNavBar extends HookConsumerWidget {
|
||||||
// Clamp the value between 0 and 1
|
// Clamp the value between 0 and 1
|
||||||
percentExpanded = percentExpanded.clamp(0.0, 1.0);
|
percentExpanded = percentExpanded.clamp(0.0, 1.0);
|
||||||
|
|
||||||
final SearchController searchController =
|
onBackButtonPressed() async {
|
||||||
ref.watch(globalSearchControllerProvider);
|
final isPlayerExpanded = playerProgress != playerMinHeight;
|
||||||
|
|
||||||
return Scaffold(
|
debugPrint(
|
||||||
|
'BackButtonListener: Back button pressed, isPlayerExpanded: $isPlayerExpanded, stack: $navigationShellStack',
|
||||||
|
);
|
||||||
|
// close miniplayer if it is open
|
||||||
|
if (isPlayerExpanded) {
|
||||||
|
debugPrint(
|
||||||
|
'BackButtonListener: closing the player',
|
||||||
|
);
|
||||||
|
audioBookMiniplayerController.animateToHeight(state: PanelState.MIN);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// do the the following only if the current branch has nothing to pop
|
||||||
|
final canPop = GoRouter.of(context).canPop();
|
||||||
|
|
||||||
|
if (canPop) {
|
||||||
|
debugPrint(
|
||||||
|
'BackButtonListener: passing it to the router as canPop is true',
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (navigationShellStack.isNotEmpty) {
|
||||||
|
// pop the last index from the stack and navigate to it
|
||||||
|
final index = navigationShellStack.last;
|
||||||
|
navigationShellStack.remove(index);
|
||||||
|
debugPrint('BackButtonListener: popping the stack, index: $index');
|
||||||
|
|
||||||
|
// if the stack is empty, navigate to home else navigate to the last index
|
||||||
|
if (navigationShellStack.isNotEmpty) {
|
||||||
|
navigationShell.goBranch(navigationShellStack.last);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (navigationShell.currentIndex != 0) {
|
||||||
|
// if the stack is empty and the current branch is not home, navigate to home
|
||||||
|
debugPrint('BackButtonListener: navigating to home');
|
||||||
|
navigationShell.goBranch(0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
debugPrint('BackButtonListener: passing it to the router');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return BackButtonListener(
|
||||||
|
onBackButtonPressed: onBackButtonPressed,
|
||||||
|
child: Scaffold(
|
||||||
body: Stack(
|
body: Stack(
|
||||||
children: [
|
children: [
|
||||||
navigationShell,
|
navigationShell,
|
||||||
|
|
@ -79,6 +132,7 @@ class ScaffoldWithNavBar extends HookConsumerWidget {
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -97,6 +151,13 @@ class ScaffoldWithNavBar extends HookConsumerWidget {
|
||||||
initialLocation: index == navigationShell.currentIndex,
|
initialLocation: index == navigationShell.currentIndex,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// add the index to the stack but remove it if it is already there
|
||||||
|
if (navigationShellStack.contains(index)) {
|
||||||
|
navigationShellStack.remove(index);
|
||||||
|
}
|
||||||
|
navigationShellStack.add(index);
|
||||||
|
debugPrint('Tapped index: $index, stack: $navigationShellStack');
|
||||||
|
|
||||||
// Check if the current branch is the same as the branch that was tapped.
|
// Check if the current branch is the same as the branch that was tapped.
|
||||||
// If it is, debugPrint a message to the console.
|
// If it is, debugPrint a message to the console.
|
||||||
if (index == navigationShell.currentIndex) {
|
if (index == navigationShell.currentIndex) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue