Skip to content

Commit

Permalink
Merge pull request #369 from poppingmoon/file-picker-allow-multiple
Browse files Browse the repository at this point in the history
ノートの添付ファイル選択時に複数のファイルを同時に選択できるように
  • Loading branch information
shiosyakeyakini-info authored Oct 8, 2023
2 parents 6f8c37a + 46b47ba commit 85da508
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 133 deletions.
29 changes: 16 additions & 13 deletions lib/model/image_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ import 'dart:typed_data';
sealed class MisskeyPostFile {
final String fileName;
final bool isNsfw;
final String caption;
final String? caption;

const MisskeyPostFile(
{required this.fileName, required this.isNsfw, required this.caption});
const MisskeyPostFile({
required this.fileName,
this.isNsfw = false,
this.caption,
});
}

class ImageFile extends MisskeyPostFile {
final Uint8List data;
const ImageFile({
required this.data,
required super.fileName,
required super.isNsfw,
required super.caption,
super.isNsfw,
super.caption,
});
}

Expand All @@ -26,10 +29,10 @@ class ImageFileAlreadyPostedFile extends MisskeyPostFile {
const ImageFileAlreadyPostedFile({
required this.data,
required this.id,
required this.isEdited,
this.isEdited = false,
required super.fileName,
required super.isNsfw,
required super.caption,
super.isNsfw,
super.caption,
});
}

Expand All @@ -38,8 +41,8 @@ class UnknownFile extends MisskeyPostFile {
const UnknownFile({
required this.data,
required super.fileName,
required super.isNsfw,
required super.caption,
super.isNsfw,
super.caption,
});
}

Expand All @@ -50,9 +53,9 @@ class UnknownAlreadyPostedFile extends MisskeyPostFile {
const UnknownAlreadyPostedFile({
required this.url,
required this.id,
required this.isEdited,
this.isEdited = false,
required super.fileName,
required super.isNsfw,
required super.caption,
super.isNsfw,
super.caption,
});
}
128 changes: 73 additions & 55 deletions lib/state_notifier/note_create_page/note_create_state_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,11 @@ class NoteCreateNotifier extends StateNotifier<NoteCreate> {
return ImageFile(
data: contents,
fileName: fileName,
isNsfw: false,
caption: "",
);
} else {
return UnknownFile(
data: contents,
fileName: fileName,
isNsfw: false,
caption: "",
);
}
}),
Expand All @@ -161,22 +157,25 @@ class NoteCreateNotifier extends StateNotifier<NoteCreate> {
if (file.type.startsWith("image")) {
final response = await dio.get(file.url,
options: Options(responseType: ResponseType.bytes));
files.add(ImageFileAlreadyPostedFile(
files.add(
ImageFileAlreadyPostedFile(
fileName: file.name,
data: response.data,
id: file.id,
isNsfw: file.isSensitive,
caption: file.comment ?? "",
isEdited: false));
caption: file.comment,
),
);
} else {
files.add(UnknownAlreadyPostedFile(
url: file.url,
id: file.id,
fileName: file.name,
isNsfw: file.isSensitive,
caption: file.comment ?? "",
isEdited: false,
));
files.add(
UnknownAlreadyPostedFile(
url: file.url,
id: file.id,
fileName: file.name,
isNsfw: file.isSensitive,
caption: file.comment,
),
);
}
}
final deletedNoteChannel = note.channel;
Expand Down Expand Up @@ -436,54 +435,73 @@ class NoteCreateNotifier extends StateNotifier<NoteCreate> {

if (result == DriveModalSheetReturnValue.drive) {
if (!mounted) return;
final result = await showDialog<DriveFile?>(
context: context,
builder: (context) => DriveFileSelectDialog(account: state.account));
if (result == null) return;
if (result.type.startsWith("image")) {
final fileContentResponse = await dio.get(result.url,
options: Options(responseType: ResponseType.bytes));

state = state.copyWith(files: [
...state.files,
ImageFileAlreadyPostedFile(
data: fileContentResponse.data,
fileName: result.name,
id: result.id,
isEdited: false,
isNsfw: result.isSensitive,
caption: result.comment ?? "",
)
]);
} else {
state = state.copyWith(files: [
...state.files,
UnknownAlreadyPostedFile(
url: result.url,
id: result.id,
isEdited: false,
fileName: result.name,
isNsfw: result.isSensitive,
caption: result.comment ?? "")
]);
}
} else if (result == DriveModalSheetReturnValue.upload) {
final result = await FilePicker.platform.pickFiles(type: FileType.image);
final result = await showDialog<List<DriveFile>?>(
context: context,
builder: (context) => DriveFileSelectDialog(
account: state.account,
allowMultiple: true,
),
);
if (result == null) return;

final path = result.files.single.path;
if (path == null) return;
final file = fileSystem.file(path);
final files = await Future.wait(
result.map((file) async {
if (file.type.startsWith("image")) {
final fileContentResponse = await dio.get<Uint8List>(
file.url,
options: Options(responseType: ResponseType.bytes),
);
return ImageFileAlreadyPostedFile(
data: fileContentResponse.data!,
id: file.id,
fileName: file.name,
isNsfw: file.isSensitive,
caption: file.comment,
);
}
return UnknownAlreadyPostedFile(
url: file.url,
id: file.id,
fileName: file.name,
isNsfw: file.isSensitive,
caption: file.comment,
);
}),
);
if (!mounted) return;
state = state.copyWith(
files: [
...state.files,
ImageFile(
...files,
],
);
} else if (result == DriveModalSheetReturnValue.upload) {
final result = await FilePicker.platform.pickFiles(
type: FileType.image,
allowMultiple: true,
);
if (result == null || result.files.isEmpty) return;

final fsFiles = result.files.map((file) {
final path = file.path;
if (path != null) {
return fileSystem.file(path);
}
return null;
}).nonNulls;
final files = await Future.wait(
fsFiles.map(
(file) async => ImageFile(
data: await file.readAsBytes(),
fileName: file.basename,
isNsfw: false,
caption: "",
),
),
);

if (!mounted) return;
state = state.copyWith(
files: [
...state.files,
...files,
],
);
}
Expand Down
Loading

0 comments on commit 85da508

Please sign in to comment.