Skip to content

Commit 9069e3f

Browse files
committed
feat: better ui
1 parent 54ce0b0 commit 9069e3f

6 files changed

Lines changed: 324 additions & 44 deletions

File tree

lib/controllers/home_controller.dart

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:io';
22

33
import 'package:file_picker/file_picker.dart';
4+
import 'package:file_transfer/models/file_info.dart';
45
import 'package:file_transfer/routes.dart';
56
import 'package:file_transfer/utils/platform_helper.dart';
67
import 'package:file_transfer_sdk/file_transfer_sdk.dart';
@@ -14,12 +15,14 @@ import 'package:toastification/toastification.dart';
1415

1516
class 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

lib/models/file_info.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'package:flutter/foundation.dart';
2+
3+
class FileInfo {
4+
final String name;
5+
final int size;
6+
final String? mimeType;
7+
final DateTime? lastModified;
8+
final Uint8List bytes;
9+
10+
FileInfo({
11+
required this.name,
12+
required this.size,
13+
this.mimeType,
14+
this.lastModified,
15+
required this.bytes,
16+
});
17+
}

lib/pages/file_share/widgets/ready_view.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,35 @@ class ReadyView extends GetView<FileShareController> {
8383
child: Padding(
8484
padding: EdgeInsets.all(isSmallScreen ? 12 : 16),
8585
child: Column(
86+
crossAxisAlignment: CrossAxisAlignment.start,
8687
children: [
88+
if (metadata.filename != null) ...[
89+
Row(
90+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
91+
children: [
92+
Text(
93+
'Name:',
94+
style: TextStyle(
95+
fontWeight: FontWeight.bold,
96+
fontSize: isSmallScreen ? 13 : 14,
97+
),
98+
),
99+
Flexible(
100+
child: Text(
101+
metadata.filename!,
102+
textAlign: TextAlign.right,
103+
style: TextStyle(
104+
fontSize: isSmallScreen ? 13 : 14,
105+
color: colorScheme.onSurfaceVariant,
106+
),
107+
maxLines: 2,
108+
overflow: TextOverflow.ellipsis,
109+
),
110+
),
111+
],
112+
),
113+
const Divider(),
114+
],
87115
if (metadata.fileType != null) ...[
88116
Row(
89117
mainAxisAlignment: MainAxisAlignment.spaceBetween,

0 commit comments

Comments
 (0)