From 0c2b9c1ee85a0fbee3baef4233f27f44f1b5e703 Mon Sep 17 00:00:00 2001 From: poppingmoon <63451158+poppingmoon@users.noreply.github.com> Date: Sat, 28 Oct 2023 18:48:48 +0900 Subject: [PATCH] =?UTF-8?q?DriveFileSelectDialog=E3=81=A7DrivePage?= =?UTF-8?q?=E3=82=92=E4=BD=BF=E3=81=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../note_create_state_notifier.dart | 5 +- .../drive_file_select_dialog.dart | 196 +++--------------- 2 files changed, 31 insertions(+), 170 deletions(-) diff --git a/lib/state_notifier/note_create_page/note_create_state_notifier.dart b/lib/state_notifier/note_create_page/note_create_state_notifier.dart index b56316996..a0b6890c3 100644 --- a/lib/state_notifier/note_create_page/note_create_state_notifier.dart +++ b/lib/state_notifier/note_create_page/note_create_state_notifier.dart @@ -461,10 +461,7 @@ class NoteCreateNotifier extends StateNotifier { if (!mounted) return; final result = await showDialog?>( context: context, - builder: (context) => DriveFileSelectDialog( - account: state.account, - allowMultiple: true, - ), + builder: (context) => DriveFileSelectDialog(account: state.account), ); if (result == null) return; final files = await Future.wait( diff --git a/lib/view/note_create_page/drive_file_select_dialog.dart b/lib/view/note_create_page/drive_file_select_dialog.dart index d58be56fd..8e73ae675 100644 --- a/lib/view/note_create_page/drive_file_select_dialog.dart +++ b/lib/view/note_create_page/drive_file_select_dialog.dart @@ -2,183 +2,47 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:miria/model/account.dart'; import 'package:miria/providers.dart'; -import 'package:miria/view/common/misskey_notes/network_image.dart'; -import 'package:miria/view/common/pushable_listview.dart'; -import 'package:miria/view/themes/app_theme.dart'; -import 'package:misskey_dart/misskey_dart.dart'; - -class DriveFileSelectDialog extends ConsumerStatefulWidget { - final Account account; - final bool allowMultiple; +import 'package:miria/state_notifier/drive_page/drive_page_notifier.dart'; +import 'package:miria/view/drive_page/drive_page.dart'; +class DriveFileSelectDialog extends StatelessWidget { const DriveFileSelectDialog({ super.key, required this.account, - this.allowMultiple = false, }); - @override - ConsumerState createState() => - DriveFileSelectDialogState(); -} - -class DriveFileSelectDialogState extends ConsumerState { - final List path = []; - final List files = []; + final Account account; @override Widget build(BuildContext context) { - return AlertDialog( - title: AppBar( - leading: IconButton( - onPressed: path.isEmpty - ? null - : () { - setState(() { - path.removeLast(); - }); - }, - icon: const Icon(Icons.arrow_back), - ), - title: path.isEmpty - ? const Text("ファイルを選択") - : Text(path.map((e) => e.name).join("/")), - actions: [ - if (files.isNotEmpty) - Center( - child: Text( - "(${files.length})", - style: Theme.of(context).textTheme.titleMedium, - ), - ), - if (widget.allowMultiple) - IconButton( - onPressed: - files.isEmpty ? null : () => Navigator.of(context).pop(files), - icon: const Icon(Icons.check), - ), - ], - backgroundColor: Colors.transparent, - ), - content: SizedBox( - width: MediaQuery.of(context).size.width * 0.8, - height: MediaQuery.of(context).size.height * 0.8, - child: SingleChildScrollView( - child: Column( - children: [ - PushableListView( - shrinkWrap: true, - physics: const NeverScrollableScrollPhysics(), - initializeFuture: () async { - final misskey = ref.read(misskeyProvider(widget.account)); - final response = await misskey.drive.folders.folders( - DriveFoldersRequest( - folderId: path.isEmpty ? null : path.last.id)); - return response.toList(); - }, - nextFuture: (lastItem, _) async { - final misskey = ref.read(misskeyProvider(widget.account)); - final response = await misskey.drive.folders.folders( - DriveFoldersRequest( - untilId: lastItem.id, - folderId: path.isEmpty ? null : path.last.id)); - return response.toList(); - }, - listKey: path.map((e) => e.id).join("/"), - itemBuilder: (context, item) { - return ListTile( - leading: const Icon(Icons.folder), - title: Text(item.name ?? ""), - onTap: () { - setState(() { - path.add(item); - }); - }, - ); - }), - PushableListView( - shrinkWrap: true, - physics: const NeverScrollableScrollPhysics(), - initializeFuture: () async { - final misskey = ref.read(misskeyProvider(widget.account)); - final response = await misskey.drive.files.files( - DriveFilesRequest( - folderId: path.isEmpty ? null : path.last.id, - ), - ); - return response.toList(); - }, - nextFuture: (lastItem, _) async { - final misskey = ref.read(misskeyProvider(widget.account)); - final response = await misskey.drive.files.files( - DriveFilesRequest( - untilId: lastItem.id, - folderId: path.isEmpty ? null : path.last.id, - ), - ); - return response.toList(); - }, - listKey: path.map((e) => e.id).join("/"), - itemBuilder: (context, item) { - final isSelected = files.any((file) => file.id == item.id); - return Padding( - padding: const EdgeInsets.symmetric(vertical: 2), - child: InkWell( - customBorder: RoundedRectangleBorder( - borderRadius: BorderRadius.circular(5), - ), - onTap: () { - if (widget.allowMultiple) { - setState(() { - if (isSelected) { - files.removeWhere((file) => file.id == item.id); - } else { - files.add(item); - } - }); - } else { - Navigator.of(context).pop(item); - } - }, - child: Container( - padding: const EdgeInsets.all(10), - decoration: (widget.allowMultiple && isSelected) - ? BoxDecoration( - color: AppTheme.of(context) - .currentDisplayTabColor - .withOpacity(0.7), - borderRadius: BorderRadius.circular(5), - ) - : null, - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - SizedBox( - width: double.infinity, - height: 200, - child: item.thumbnailUrl == null - ? const SizedBox.shrink() - : ClipRRect( - borderRadius: BorderRadius.circular(5), - child: NetworkImageView( - fit: BoxFit.cover, - url: item.thumbnailUrl!, - type: ImageType.imageThumbnail, - ), - ), - ), - Text(item.name), - ], - ), - ), - ), - ); - }, - ), - ], - ), + return ProviderScope( + overrides: [ + drivePageNotifierProvider.overrideWith(DrivePageNotifier.new), + ], + child: Dialog( + child: DrivePage( + account: account, + title: const Text("ファイルを選択"), + tapToSelect: true, + floatingActionButtonBuilder: (context) => + const DriveFileSelectDialogFloatingActionButton(), ), ), ); } } + +class DriveFileSelectDialogFloatingActionButton extends ConsumerWidget { + const DriveFileSelectDialogFloatingActionButton({super.key}); + + @override + Widget build(BuildContext context, WidgetRef ref) { + final files = ref.watch(drivePageNotifierProvider).selectedFiles; + return FloatingActionButton.extended( + onPressed: + files.isNotEmpty ? () => Navigator.of(context).pop(files) : null, + label: const Text("選択"), + icon: const Icon(Icons.check), + ); + } +}