ui: Move file picker invocation to the SendFiles bloc
This commit is contained in:
parent
cf8180d587
commit
222521f3f7
@ -291,15 +291,8 @@ class ConversationBloc extends Bloc<ConversationEvent, ConversationState> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onFileUploadRequested(FileUploadRequestedEvent event, Emitter<ConversationState> emit) async {
|
Future<void> _onFileUploadRequested(FileUploadRequestedEvent event, Emitter<ConversationState> emit) async {
|
||||||
final result = await FilePicker.platform.pickFiles(type: FileType.image, allowMultiple: true);
|
GetIt.I.get<SendFilesBloc>().add(
|
||||||
|
SendFilesPageRequestedEvent(state.conversation!.jid),
|
||||||
if (result != null) {
|
);
|
||||||
GetIt.I.get<SendFilesBloc>().add(
|
|
||||||
SendFilesPageRequestedEvent(
|
|
||||||
result.files.map((PlatformFile file) => file.path!).toList(),
|
|
||||||
state.conversation!.jid,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,14 +19,29 @@ class SendFilesBloc extends Bloc<SendFilesEvent, SendFilesState> {
|
|||||||
on<FileSendingRequestedEvent>(_onFileSendingRequested);
|
on<FileSendingRequestedEvent>(_onFileSendingRequested);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Pick files. Returns either a list of paths to attach or null if the process has
|
||||||
|
/// been cancelled.
|
||||||
|
Future<List<String>?> _pickFiles() async {
|
||||||
|
// TODO(PapaTutuWawa): Allow multiple file types
|
||||||
|
final result = await FilePicker.platform.pickFiles(type: FileType.image, allowMultiple: true);
|
||||||
|
|
||||||
|
if (result == null) return null;
|
||||||
|
|
||||||
|
return result.files.map((PlatformFile file) => file.path!).toList();
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _sendFilesRequested(SendFilesPageRequestedEvent event, Emitter<SendFilesState> emit) async {
|
Future<void> _sendFilesRequested(SendFilesPageRequestedEvent event, Emitter<SendFilesState> emit) async {
|
||||||
|
final files = await _pickFiles();
|
||||||
|
if (files == null) return;
|
||||||
|
|
||||||
emit(
|
emit(
|
||||||
state.copyWith(
|
state.copyWith(
|
||||||
files: event.files,
|
files: files,
|
||||||
index: 0,
|
index: 0,
|
||||||
conversationJid: event.jid,
|
conversationJid: event.jid,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
GetIt.I.get<NavigationBloc>().add(
|
GetIt.I.get<NavigationBloc>().add(
|
||||||
PushedNamedEvent(
|
PushedNamedEvent(
|
||||||
const NavigationDestination(
|
const NavigationDestination(
|
||||||
@ -41,18 +56,14 @@ class SendFilesBloc extends Bloc<SendFilesEvent, SendFilesState> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onAddFilesRequested(AddFilesRequestedEvent event, Emitter<SendFilesState> emit) async {
|
Future<void> _onAddFilesRequested(AddFilesRequestedEvent event, Emitter<SendFilesState> emit) async {
|
||||||
final result = await FilePicker.platform.pickFiles(type: FileType.image, allowMultiple: true);
|
final files = await _pickFiles();
|
||||||
|
if (files == null) return;
|
||||||
|
|
||||||
if (result != null) {
|
emit(
|
||||||
emit(
|
state.copyWith(
|
||||||
state.copyWith(
|
files: List.from(state.files)..addAll(files),
|
||||||
files: List.from(state.files)
|
),
|
||||||
..addAll(
|
);
|
||||||
result.files.map((PlatformFile file) => file.path!).toList(),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onFileSendingRequested(FileSendingRequestedEvent event, Emitter<SendFilesState> emitter) async {
|
Future<void> _onFileSendingRequested(FileSendingRequestedEvent event, Emitter<SendFilesState> emitter) async {
|
||||||
|
@ -4,8 +4,7 @@ abstract class SendFilesEvent {}
|
|||||||
|
|
||||||
class SendFilesPageRequestedEvent extends SendFilesEvent {
|
class SendFilesPageRequestedEvent extends SendFilesEvent {
|
||||||
|
|
||||||
SendFilesPageRequestedEvent(this.files, this.jid);
|
SendFilesPageRequestedEvent(this.jid);
|
||||||
final List<String> files;
|
|
||||||
final String jid;
|
final String jid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user