Browse Source

sync selected file count on bottom action bar

ashilkn 2 years ago
parent
commit
d83cbebadd

+ 50 - 15
lib/ui/components/bottom_action_bar/action_bar_widget.dart

@@ -2,15 +2,36 @@ import 'package:flutter/material.dart';
 import 'package:photos/models/selected_files.dart';
 import 'package:photos/models/selected_files.dart';
 import 'package:photos/theme/ente_theme.dart';
 import 'package:photos/theme/ente_theme.dart';
 
 
-class ActionBarWidget extends StatelessWidget {
+class ActionBarWidget extends StatefulWidget {
   final String? text;
   final String? text;
   final List<Widget> iconButtons;
   final List<Widget> iconButtons;
+  final SelectedFiles? selectedFiles;
   const ActionBarWidget({
   const ActionBarWidget({
-    this.text,
     required this.iconButtons,
     required this.iconButtons,
+    this.text,
+    this.selectedFiles,
     super.key,
     super.key,
   });
   });
 
 
+  @override
+  State<ActionBarWidget> createState() => _ActionBarWidgetState();
+}
+
+class _ActionBarWidgetState extends State<ActionBarWidget> {
+  final ValueNotifier<int> _selectedFilesNotifier = ValueNotifier(0);
+
+  @override
+  void initState() {
+    widget.selectedFiles?.addListener(_selectedFilesListener);
+    super.initState();
+  }
+
+  @override
+  void dispose() {
+    widget.selectedFiles?.removeListener(_selectedFilesListener);
+    super.dispose();
+  }
+
   @override
   @override
   Widget build(BuildContext context) {
   Widget build(BuildContext context) {
     return SizedBox(
     return SizedBox(
@@ -23,13 +44,12 @@ class ActionBarWidget extends StatelessWidget {
 
 
   List<Widget> _actionBarWidgets(BuildContext context) {
   List<Widget> _actionBarWidgets(BuildContext context) {
     final actionBarWidgets = <Widget>[];
     final actionBarWidgets = <Widget>[];
-    final initialLength = iconButtons.length;
+    final initialLength = widget.iconButtons.length;
     final textTheme = getEnteTextTheme(context);
     final textTheme = getEnteTextTheme(context);
     final colorScheme = getEnteColorScheme(context);
     final colorScheme = getEnteColorScheme(context);
-    final hasSelectedFiles = SelectedFiles().files.isNotEmpty;
 
 
-    actionBarWidgets.addAll(iconButtons);
-    if (text != null) {
+    actionBarWidgets.addAll(widget.iconButtons);
+    if (widget.text != null) {
       //adds 12 px spacing at the start and between iconButton elements
       //adds 12 px spacing at the start and between iconButton elements
       for (var i = 0; i < initialLength; i++) {
       for (var i = 0; i < initialLength; i++) {
         actionBarWidgets.insert(
         actionBarWidgets.insert(
@@ -44,20 +64,29 @@ class ActionBarWidget extends StatelessWidget {
         Flexible(
         Flexible(
           child: Row(
           child: Row(
             children: [
             children: [
-              Text(
-                text!,
-                style: hasSelectedFiles
-                    ? textTheme.body
-                    : textTheme.small.copyWith(
-                        color: colorScheme.textMuted,
-                      ),
-              )
+              widget.selectedFiles != null
+                  ? ValueListenableBuilder(
+                      valueListenable: _selectedFilesNotifier,
+                      builder: (context, value, child) {
+                        return Text(
+                          "${_selectedFilesNotifier.value} selected",
+                          style: textTheme.small.copyWith(
+                            color: colorScheme.blurTextBase,
+                          ),
+                        );
+                      },
+                    )
+                  : Text(
+                      widget.text!,
+                      style: textTheme.small
+                          .copyWith(color: colorScheme.textMuted),
+                    ),
             ],
             ],
           ),
           ),
         ),
         ),
       ]);
       ]);
       //to add whitespace of 8pts or 12 pts at the end
       //to add whitespace of 8pts or 12 pts at the end
-      if (iconButtons.length > 1) {
+      if (widget.iconButtons.length > 1) {
         actionBarWidgets.add(
         actionBarWidgets.add(
           const SizedBox(width: 8),
           const SizedBox(width: 8),
         );
         );
@@ -69,4 +98,10 @@ class ActionBarWidget extends StatelessWidget {
     }
     }
     return actionBarWidgets;
     return actionBarWidgets;
   }
   }
+
+  void _selectedFilesListener() {
+    if (widget.selectedFiles!.files.isNotEmpty) {
+      _selectedFilesNotifier.value = widget.selectedFiles!.files.length;
+    }
+  }
 }
 }

+ 1 - 0
lib/ui/components/bottom_action_bar/bottom_action_bar_widget.dart

@@ -58,6 +58,7 @@ class BottomActionBarWidget extends StatelessWidget {
                       horizontal: text == null ? 12 : 0,
                       horizontal: text == null ? 12 : 0,
                     ),
                     ),
                     child: ActionBarWidget(
                     child: ActionBarWidget(
+                      selectedFiles: selectedFiles,
                       text: text,
                       text: text,
                       iconButtons: _iconButtons(),
                       iconButtons: _iconButtons(),
                     ),
                     ),

+ 1 - 1
lib/ui/viewer/gallery/collection_page.dart

@@ -120,7 +120,7 @@ class _CollectionPageState extends State<CollectionPage> {
                 child: BottomActionBarWidget(
                 child: BottomActionBarWidget(
                   selectedFiles: _selectedFiles,
                   selectedFiles: _selectedFiles,
                   expandedMenu: const SizedBox(height: 150),
                   expandedMenu: const SizedBox(height: 150),
-                  text: "3 selected",
+                  text: _selectedFiles.files.length.toString() + ' selected',
                   onCancel: () {
                   onCancel: () {
                     if (_selectedFiles.files.isNotEmpty) {
                     if (_selectedFiles.files.isNotEmpty) {
                       _selectedFiles.clearAll();
                       _selectedFiles.clearAll();