11import 'dart:io' ;
22
33import 'package:file_picker/file_picker.dart' ;
4+ import 'package:file_transfer/models/file_info.dart' ;
45import 'package:file_transfer/routes.dart' ;
56import 'package:file_transfer/utils/platform_helper.dart' ;
67import 'package:file_transfer_sdk/file_transfer_sdk.dart' ;
@@ -14,12 +15,14 @@ import 'package:toastification/toastification.dart';
1415
1516class HomePageController extends GetxController {
1617 final _sharedFile = Rxn <SharedFile >();
18+ final _fileInfo = Rxn <FileInfo >();
1719 final _isUploading = false .obs;
1820 final _error = RxnString ();
1921 final _isDragging = false .obs;
2022 final dropzoneController = Rxn <DropzoneViewController >();
2123
2224 SharedFile ? get sharedFile => _sharedFile.value;
25+ FileInfo ? get fileInfo => _fileInfo.value;
2326 bool get isUploading => _isUploading.value;
2427 String ? get error => _error.value;
2528 bool get isDragging => _isDragging.value;
@@ -38,11 +41,44 @@ class HomePageController extends GetxController {
3841 );
3942 }
4043
41- Future <void > handleDroppedFile (dynamic file) async {
42- _isDragging.value = false ;
44+ Future <void > _uploadFile (Uint8List bytes, String filename) async {
4345 _isUploading.value = true ;
4446 _error.value = null ;
4547
48+ try {
49+ final mimeType = lookupMimeType (
50+ filename,
51+ headerBytes: bytes.take (8 ).toList (),
52+ );
53+
54+ final sharedFile = await shareFile (
55+ ndk: Get .find (),
56+ bytes: bytes,
57+ contentType: mimeType,
58+ filename: filename,
59+ );
60+
61+ _sharedFile.value = sharedFile;
62+ _fileInfo.value = null ;
63+ } catch (e) {
64+ _error.value = e.toString ();
65+ } finally {
66+ _isUploading.value = false ;
67+ }
68+ }
69+
70+ void _setFileInfo (Uint8List bytes, String filename, String ? mimeType) {
71+ _fileInfo.value = FileInfo (
72+ name: filename,
73+ size: bytes.length,
74+ mimeType: mimeType,
75+ bytes: bytes,
76+ );
77+ }
78+
79+ Future <void > handleDroppedFile (dynamic file) async {
80+ _isDragging.value = false ;
81+
4682 try {
4783 String filename;
4884 Uint8List bytes;
@@ -66,15 +102,7 @@ class HomePageController extends GetxController {
66102 headerBytes: bytes.take (8 ).toList (),
67103 );
68104
69- final sharedFile = await shareFile (
70- ndk: Get .find (),
71- bytes: bytes,
72- contentType: mimeType,
73- filename: filename,
74- );
75-
76- _sharedFile.value = sharedFile;
77- _isUploading.value = false ;
105+ _setFileInfo (bytes, filename, mimeType);
78106 } catch (e) {
79107 _error.value = e.toString ();
80108 _isUploading.value = false ;
@@ -90,45 +118,37 @@ class HomePageController extends GetxController {
90118 }
91119
92120 Future <void > pickAndShareFile () async {
93- _isUploading.value = true ;
94- _error.value = null ;
95-
96- try {
97- final pickResult = await FilePicker .platform.pickFiles (
98- type: FileType .any,
99- withData: true ,
100- );
121+ final pickResult = await FilePicker .platform.pickFiles (
122+ type: FileType .any,
123+ withData: true ,
124+ );
101125
102- if (pickResult == null || pickResult.files.isEmpty) {
103- _isUploading.value = false ;
104- return ;
105- }
126+ if (pickResult == null || pickResult.files.isEmpty) {
127+ return ;
128+ }
106129
107- final file = pickResult.files.first;
108- if (file.bytes == null ) {
109- _error.value = 'Unable to read file data' ;
110- _isUploading.value = false ;
111- return ;
112- }
130+ final file = pickResult.files.first;
131+ if (file.bytes == null ) {
132+ _error.value = 'Unable to read file data' ;
133+ return ;
134+ }
113135
114- final mimeType = lookupMimeType (
115- file.name,
116- headerBytes: file.bytes? .take (8 ).toList (),
117- );
136+ final mimeType = lookupMimeType (
137+ file.name,
138+ headerBytes: file.bytes? .take (8 ).toList (),
139+ );
118140
119- final sharedFile = await shareFile (
120- ndk: Get .find (),
121- bytes: file.bytes! ,
122- contentType: mimeType,
123- filename: file.name,
124- );
141+ _setFileInfo (file.bytes! , file.name, mimeType);
142+ }
125143
126- _sharedFile.value = sharedFile;
127- _isUploading.value = false ;
128- } catch (e ) {
129- _error.value = e. toString () ;
130- _isUploading.value = false ;
144+ Future < void > startUpload () async {
145+ final fileInfo = _fileInfo.value ;
146+ if (fileInfo == null ) {
147+ _error.value = 'No file selected' ;
148+ return ;
131149 }
150+
151+ await _uploadFile (fileInfo.bytes, fileInfo.name);
132152 }
133153
134154 void copyToClipboard (String text, String label) {
@@ -147,6 +167,7 @@ class HomePageController extends GetxController {
147167
148168 void reset () {
149169 _sharedFile.value = null ;
170+ _fileInfo.value = null ;
150171 _error.value = null ;
151172 }
152173
0 commit comments