ui: Move file picker invocation to the SendFiles bloc

This commit is contained in:
PapaTutuWawa 2022-07-24 19:36:23 +02:00
parent cf8180d587
commit 222521f3f7
3 changed files with 27 additions and 24 deletions

View File

@ -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);
if (result != null) {
GetIt.I.get<SendFilesBloc>().add( GetIt.I.get<SendFilesBloc>().add(
SendFilesPageRequestedEvent( SendFilesPageRequestedEvent(state.conversation!.jid),
result.files.map((PlatformFile file) => file.path!).toList(),
state.conversation!.jid,
),
); );
} }
}
} }

View File

@ -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,19 +56,15 @@ 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) files: List.from(state.files)..addAll(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 {
await MoxplatformPlugin.handler.getDataSender().sendData( await MoxplatformPlugin.handler.getDataSender().sendData(

View File

@ -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;
} }