Skip to content

Commit

Permalink
DriveFileSelectDialogでDrivePageを使う
Browse files Browse the repository at this point in the history
  • Loading branch information
poppingmoon committed Nov 13, 2024
1 parent 1e6d201 commit c906341
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 198 deletions.
2 changes: 1 addition & 1 deletion lib/router/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class AppRouter extends _$AppRouter {
children: [
AutoRoute(page: DriveRoute.page),
AutoRoute(page: DriveFileRoute.page),
AutoDialogRoute<List<DriveFile>>(page: DriveFileSelectRoute.page),
AutoModalRouteSheet(page: DriveCreateModalRoute.page),
AutoModalRouteSheet(page: DriveFileModalRoute.page),
AutoModalRouteSheet(page: DriveFilesModalRoute.page),
Expand Down Expand Up @@ -180,7 +181,6 @@ class AppRouter extends _$AppRouter {
AutoDialogRoute<UsersListSettings>(page: UsersListSettingsRoute.page),
AutoDialogRoute<AntennaSettings>(page: AntennaSettingsRoute.page),
AutoDialogRoute<FolderResult>(page: FolderSelectRoute.page),
AutoDialogRoute<List<DriveFile>>(page: DriveFileSelectRoute.page),
AutoDialogRoute<String>(page: TextFormFieldRoute.page),

// モーダルシート
Expand Down
26 changes: 10 additions & 16 deletions lib/router/app_router.gr.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -555,13 +555,13 @@ class NoteCreateNotifier extends _$NoteCreateNotifier {
.push<DriveModalSheetReturnValue>(const DriveModalRoute());

if (result == DriveModalSheetReturnValue.drive) {
final result = await ref.read(appRouterProvider).push<List<DriveFile>>(
DriveFileSelectRoute(
account: ref.read(accountContextProvider).postAccount,
allowMultiple: true,
final result = await ref.read(appRouterProvider).push<dynamic>(
DriveShellRoute(
accountContext: ref.read(accountContextProvider),
children: [DriveRoute(selectFiles: true)],
),
);
if (result == null) return;
if (result is! List<DriveFile>) return;
final files = await Future.wait(
result.map((file) async {
if (file.type.startsWith("image")) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

182 changes: 7 additions & 175 deletions lib/view/note_create_page/drive_file_select_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,189 +1,21 @@
import "package:auto_route/auto_route.dart";
import "package:flutter/material.dart";
import "package:flutter_gen/gen_l10n/app_localizations.dart";
import "package:flutter_hooks/flutter_hooks.dart";
import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:miria/model/account.dart";
import "package:miria/providers.dart";
import "package:miria/view/common/account_scope.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:miria/view/drive_page/drive_page.dart";
import "package:misskey_dart/misskey_dart.dart";

@RoutePage<List<DriveFile>>()
class DriveFileSelectDialog extends HookConsumerWidget
implements AutoRouteWrapper {
final Account account;
class DriveFileSelectDialog extends HookConsumerWidget {
final bool allowMultiple;

const DriveFileSelectDialog({
required this.account,
super.key,
this.allowMultiple = false,
});

@override
Widget wrappedRoute(BuildContext context) =>
AccountContextScope.as(account: account, child: this);
const DriveFileSelectDialog({this.allowMultiple = false, super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
final path = useState<List<DriveFolder>>([]);
final files = useState<List<DriveFile>>([]);

return AlertDialog(
title: AppBar(
leading: IconButton(
onPressed: path.value.isEmpty
? null
: () => path.value = [...path.value..removeLast()],
icon: const Icon(Icons.arrow_back),
),
title: path.value.isEmpty
? Text(S.of(context).chooseFile)
: Text(path.value.map((e) => e.name).join("/")),
actions: [
if (files.value.isNotEmpty)
Center(
child: Text(
"(${files.value.length})",
style: Theme.of(context).textTheme.titleMedium,
),
),
if (allowMultiple)
IconButton(
onPressed: files.value.isEmpty
? null
: () => Navigator.of(context).pop(files.value),
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(
hideIsEmpty: true,
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
showAd: false,
initializeFuture: () async {
final misskey = ref.read(misskeyGetContextProvider);
final response = await misskey.drive.folders.folders(
DriveFoldersRequest(
folderId: path.value.isEmpty ? null : path.value.last.id,
),
);
return response.toList();
},
nextFuture: (lastItem, _) async {
final misskey = ref.read(misskeyGetContextProvider);
final response = await misskey.drive.folders.folders(
DriveFoldersRequest(
untilId: lastItem.id,
folderId: path.value.isEmpty ? null : path.value.last.id,
),
);
return response.toList();
},
listKey: path.value.map((e) => e.id).join("/"),
itemBuilder: (context, item) {
return ListTile(
leading: const Icon(Icons.folder),
title: Text(item.name),
onTap: () => path.value = [...path.value, item],
);
},
),
PushableListView(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
showAd: false,
initializeFuture: () async {
final misskey = ref.read(misskeyGetContextProvider);
final response = await misskey.drive.files.files(
DriveFilesRequest(
folderId: path.value.isEmpty ? null : path.value.last.id,
),
);
return response.toList();
},
nextFuture: (lastItem, _) async {
final misskey = ref.read(misskeyGetContextProvider);
final response = await misskey.drive.files.files(
DriveFilesRequest(
untilId: lastItem.id,
folderId: path.value.isEmpty ? null : path.value.last.id,
),
);
return response.toList();
},
listKey: path.value.map((e) => e.id).join("/"),
itemBuilder: (context, item) {
final isSelected =
files.value.any((file) => file.id == item.id);
return Padding(
padding: const EdgeInsets.symmetric(vertical: 2),
child: InkWell(
customBorder: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5),
),
onTap: () {
if (allowMultiple) {
if (isSelected) {
files.value = files.value
.where((file) => file.id != item.id)
.toList();
} else {
files.value = [...files.value, item];
}
} else {
Navigator.of(context).pop([item]);
}
},
child: Container(
padding: const EdgeInsets.all(10),
decoration: (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 Dialog(
child: DrivePage(
selectFile: !allowMultiple,
selectFiles: allowMultiple,
),
);
}
Expand Down

0 comments on commit c906341

Please sign in to comment.