Skip to content

Commit

Permalink
DriveFileSelectDialogでDrivePageを使う
Browse files Browse the repository at this point in the history
  • Loading branch information
poppingmoon committed Dec 3, 2023
1 parent efd9758 commit 0c2b9c1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,7 @@ class NoteCreateNotifier extends StateNotifier<NoteCreate> {
if (!mounted) return;
final result = await showDialog<List<DriveFile>?>(
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(
Expand Down
196 changes: 30 additions & 166 deletions lib/view/note_create_page/drive_file_select_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConsumerStatefulWidget> createState() =>
DriveFileSelectDialogState();
}

class DriveFileSelectDialogState extends ConsumerState<DriveFileSelectDialog> {
final List<DriveFolder> path = [];
final List<DriveFile> 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),
);
}
}

0 comments on commit 0c2b9c1

Please sign in to comment.