From 44d59e909b065222f55066ad25de6ea2a301c4c2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Beno=C3=AEt=20Harrault?= <benoit@harrault.fr>
Date: Thu, 9 Nov 2023 16:15:01 +0100
Subject: [PATCH] Allow swipe on pages to navigate

---
 android/gradle.properties                     |  4 +-
 .../metadata/android/en-US/changelogs/23.txt  |  1 +
 .../metadata/android/fr-FR/changelogs/23.txt  |  1 +
 lib/cubit/bottom_nav_cubit.dart               |  3 ++
 lib/ui/screens/skeleton_screen.dart           | 38 +++++++++-----
 lib/ui/widgets/bottom_nav_bar.dart            | 52 ++++++++++---------
 pubspec.yaml                                  |  2 +-
 7 files changed, 61 insertions(+), 40 deletions(-)
 create mode 100644 fastlane/metadata/android/en-US/changelogs/23.txt
 create mode 100644 fastlane/metadata/android/fr-FR/changelogs/23.txt

diff --git a/android/gradle.properties b/android/gradle.properties
index ed86f0f..3487476 100644
--- a/android/gradle.properties
+++ b/android/gradle.properties
@@ -1,5 +1,5 @@
 org.gradle.jvmargs=-Xmx1536M
 android.useAndroidX=true
 android.enableJetifier=true
-app.versionName=0.0.22
-app.versionCode=22
+app.versionName=0.0.23
+app.versionCode=23
diff --git a/fastlane/metadata/android/en-US/changelogs/23.txt b/fastlane/metadata/android/en-US/changelogs/23.txt
new file mode 100644
index 0000000..85b0a5e
--- /dev/null
+++ b/fastlane/metadata/android/en-US/changelogs/23.txt
@@ -0,0 +1 @@
+Allow swipe to navigate between pages.
diff --git a/fastlane/metadata/android/fr-FR/changelogs/23.txt b/fastlane/metadata/android/fr-FR/changelogs/23.txt
new file mode 100644
index 0000000..dab421b
--- /dev/null
+++ b/fastlane/metadata/android/fr-FR/changelogs/23.txt
@@ -0,0 +1 @@
+Navigation entre pages par mouvement de "swipe".
diff --git a/lib/cubit/bottom_nav_cubit.dart b/lib/cubit/bottom_nav_cubit.dart
index 57acf9a..d061b97 100644
--- a/lib/cubit/bottom_nav_cubit.dart
+++ b/lib/cubit/bottom_nav_cubit.dart
@@ -5,6 +5,9 @@ class BottomNavCubit extends HydratedCubit<int> {
 
   void updateIndex(int index) => emit(index);
 
+  void movePrevious() => emit((state > 0) ? state - 1 : state);
+  void moveNext() => emit((state < 2) ? state + 1 : state);
+
   void getHomePage() => emit(0);
   void getDiscoveriesPage() => emit(1);
   void getStatisticsPage() => emit(2);
diff --git a/lib/ui/screens/skeleton_screen.dart b/lib/ui/screens/skeleton_screen.dart
index edef18d..9a10a78 100644
--- a/lib/ui/screens/skeleton_screen.dart
+++ b/lib/ui/screens/skeleton_screen.dart
@@ -27,18 +27,32 @@ class _SkeletonScreenState extends State<SkeletonScreen> {
 
     return BlocProvider<BottomNavCubit>(
       create: (BuildContext context) => BottomNavCubit(),
-      child: Scaffold(
-        appBar: StandardAppBar(notifyParent: refresh),
-        extendBodyBehindAppBar: false,
-        body: BlocBuilder<BottomNavCubit, int>(
-          builder: (BuildContext context, int state) {
-            return AnimatedSwitcher(
-                duration: const Duration(milliseconds: 300),
-                child: pageNavigation.elementAt(state));
-          },
-        ),
-        bottomNavigationBar: const BottomNavBar(),
-        backgroundColor: Theme.of(context).colorScheme.background,
+      child: BlocBuilder<BottomNavCubit, int>(
+        builder: (BuildContext context, int state) {
+          return GestureDetector(
+            onHorizontalDragEnd: (dragDetail) {
+              if (dragDetail.velocity.pixelsPerSecond.dx < 1) {
+                context.read<BottomNavCubit>().moveNext();
+              } else {
+                context.read<BottomNavCubit>().movePrevious();
+              }
+            },
+            child: Scaffold(
+              appBar: StandardAppBar(notifyParent: refresh),
+              extendBodyBehindAppBar: false,
+              body: BlocBuilder<BottomNavCubit, int>(
+                builder: (BuildContext context, int state) {
+                  return AnimatedSwitcher(
+                    duration: const Duration(milliseconds: 300),
+                    child: pageNavigation.elementAt(state),
+                  );
+                },
+              ),
+              bottomNavigationBar: const BottomNavBar(),
+              backgroundColor: Theme.of(context).colorScheme.background,
+            ),
+          );
+        },
       ),
     );
   }
diff --git a/lib/ui/widgets/bottom_nav_bar.dart b/lib/ui/widgets/bottom_nav_bar.dart
index c06cecb..02aa39a 100644
--- a/lib/ui/widgets/bottom_nav_bar.dart
+++ b/lib/ui/widgets/bottom_nav_bar.dart
@@ -21,31 +21,33 @@ class BottomNavBar extends StatelessWidget {
           topRight: Radius.circular(16),
         ),
       ),
-      child: BlocBuilder<BottomNavCubit, int>(builder: (BuildContext context, int state) {
-        return BottomNavigationBar(
-          currentIndex: state,
-          onTap: (int index) => context.read<BottomNavCubit>().updateIndex(index),
-          type: BottomNavigationBarType.fixed,
-          elevation: 0,
-          backgroundColor: Colors.transparent,
-          selectedItemColor: Theme.of(context).colorScheme.primary,
-          unselectedItemColor: Theme.of(context).textTheme.bodySmall!.color,
-          items: <BottomNavigationBarItem>[
-            BottomNavigationBarItem(
-              icon: const Icon(Ionicons.home_outline),
-              label: tr('bottom_nav_home'),
-            ),
-            BottomNavigationBarItem(
-              icon: const Icon(Ionicons.star_outline),
-              label: tr('bottom_nav_discoveries'),
-            ),
-            BottomNavigationBarItem(
-              icon: const Icon(Ionicons.bar_chart_outline),
-              label: tr('bottom_nav_repartition'),
-            ),
-          ],
-        );
-      }),
+      child: BlocBuilder<BottomNavCubit, int>(
+        builder: (BuildContext context, int state) {
+          return BottomNavigationBar(
+            currentIndex: state,
+            onTap: (int index) => context.read<BottomNavCubit>().updateIndex(index),
+            type: BottomNavigationBarType.fixed,
+            elevation: 0,
+            backgroundColor: Colors.transparent,
+            selectedItemColor: Theme.of(context).colorScheme.primary,
+            unselectedItemColor: Theme.of(context).textTheme.bodySmall!.color,
+            items: <BottomNavigationBarItem>[
+              BottomNavigationBarItem(
+                icon: const Icon(Ionicons.home_outline),
+                label: tr('bottom_nav_home'),
+              ),
+              BottomNavigationBarItem(
+                icon: const Icon(Ionicons.star_outline),
+                label: tr('bottom_nav_discoveries'),
+              ),
+              BottomNavigationBarItem(
+                icon: const Icon(Ionicons.bar_chart_outline),
+                label: tr('bottom_nav_repartition'),
+              ),
+            ],
+          );
+        },
+      ),
     );
   }
 }
diff --git a/pubspec.yaml b/pubspec.yaml
index 415477b..4b8265d 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -3,7 +3,7 @@ description: Display scrobbles data and charts
 
 publish_to: 'none'
 
-version: 0.0.22+22
+version: 0.0.23+23
 
 environment:
   sdk: '^3.0.0'
-- 
GitLab