@@ -5,6 +5,7 @@ import 'package:bruig/components/buttons.dart';
55import 'package:bruig/components/snackbars.dart' ;
66import 'package:bruig/components/text.dart' ;
77import 'package:bruig/models/client.dart' ;
8+ import 'package:bruig/models/uploads.dart' ;
89import 'package:bruig/screens/startupscreen.dart' ;
910import 'package:bruig/util.dart' ;
1011import 'package:flutter/material.dart' ;
@@ -23,6 +24,7 @@ Future<SendFileScreenResult?> showSendFileScreen(
2324 BuildContext context, {
2425 required ChatModel chat,
2526 required File file,
27+ required UploadsModel uploads,
2628}) async {
2729 if (chat.isGC) {
2830 var mimeType = lookupMimeType (file.path) ?? "binary/octet-stream" ;
@@ -35,15 +37,16 @@ Future<SendFileScreenResult?> showSendFileScreen(
3537 useRootNavigator: true ,
3638 context: context,
3739 builder: (context) {
38- final Widget child = SendFileScreen (file, chat);
40+ final Widget child = SendFileScreen (file, chat, uploads );
3941 return Dialog .fullscreen (child: child);
4042 });
4143}
4244
4345class SendFileScreen extends StatefulWidget {
4446 final File file;
4547 final ChatModel chat;
46- const SendFileScreen (this .file, this .chat, {super .key});
48+ final UploadsModel uploads;
49+ const SendFileScreen (this .file, this .chat, this .uploads, {super .key});
4750
4851 @override
4952 State <SendFileScreen > createState () => _SendFileScreenState ();
@@ -55,6 +58,7 @@ class _SendFileScreenState extends State<SendFileScreen> {
5558 String filename = "" ;
5659 int fileSize = 0 ;
5760 bool sending = false ;
61+ FileUploadModel ? uploadModel;
5862
5963 void cancel () {
6064 Navigator .of (context).pop ();
@@ -74,18 +78,12 @@ class _SendFileScreenState extends State<SendFileScreen> {
7478 }
7579
7680 Future <void > sendAsFileTransfer () async {
77- var chatMsg =
78- SynthChatEvent ("Sending file \" $filename \" to user" , SCE_sending );
79- chat.append (ChatEventModel (chatMsg, null ), false );
80-
81- try {
82- await Golib .sendFile (chat.id, file.absolute.path);
83- chatMsg.state = SCE_sent ;
84- showSuccessSnackbar (this , "Sent file \" $filename \" to ${chat .nick }" );
85- } catch (exception) {
86- chatMsg.error = Exception (exception);
87- showErrorSnackbar (this , "Unable to send file: $exception " );
88- }
81+ var model = widget.uploads.sendFile (chat.id, file.absolute.path);
82+ chat.append (ChatEventModel (model, null ), false );
83+ setState (() {
84+ uploadModel = model;
85+ model.addListener (uploadModelChanged);
86+ });
8987 }
9088
9189 void send () async {
@@ -112,6 +110,10 @@ class _SendFileScreenState extends State<SendFileScreen> {
112110 if (mounted) Navigator .pop (context);
113111 }
114112
113+ void uploadModelChanged () {
114+ setState (() {});
115+ }
116+
115117 @override
116118 void initState () {
117119 super .initState ();
@@ -124,6 +126,12 @@ class _SendFileScreenState extends State<SendFileScreen> {
124126 })();
125127 }
126128
129+ @override
130+ void dispose () {
131+ uploadModel? .removeListener (uploadModelChanged);
132+ super .dispose ();
133+ }
134+
127135 @override
128136 Widget build (BuildContext context) {
129137 return StartupScreen (hideAboutButton: true , [
@@ -133,6 +141,9 @@ class _SendFileScreenState extends State<SendFileScreen> {
133141 const SizedBox (height: 5 ),
134142 Txt .M ("Size: ${humanReadableSize (fileSize )}" ),
135143 // const Expanded(child: Empty()),
144+ if (uploadModel != null && uploadModel? .totalChunks != 0 ) ...[
145+ LinearProgressIndicator (value: uploadModel? .progress)
146+ ],
136147 const SizedBox (height: 20 ),
137148 Wrap (spacing: 5 , children: [
138149 ElevatedButton (onPressed: sending ? null : send, child: Text ("Send" )),
0 commit comments