Skip to content

An open-source cross-platform alternative to AirDrop

feat: send file progress ui

Tien Do Nama265b9a3ce08
diff --git a/lib/model/file_type.dart b/lib/model/file_type.dart
index 9ed448fd..e206a223 100644
--- a/lib/model/file_type.dart
+++ b/lib/model/file_type.dart
@@ -1,9 +1,15 @@
+import 'package:flutter/material.dart';
+
/// Categorization of one file.
/// We use this information for a better UX.
enum FileType {
- image,
- video,
- pdf,
- text,
- other,
+ image(Icons.image),
+ video(Icons.movie),
+ pdf(Icons.picture_as_pdf),
+ text(Icons.subject),
+ other(Icons.description);
+
+ const FileType(this.icon);
+
+ final IconData icon;
}
diff --git a/lib/pages/progress_page.dart b/lib/pages/progress_page.dart
index 2a0a29aa..d1185d47 100644
--- a/lib/pages/progress_page.dart
+++ b/lib/pages/progress_page.dart
@@ -63,70 +63,61 @@ class _ProgressPageState extends ConsumerState<ProgressPage> {
return Scaffold(
body: Stack(
children: [
- CustomScrollView(
- slivers: [
- SliverToBoxAdapter(
- child: SizedBox(height: MediaQuery.of(context).padding.top + 20),
- ),
- SliverPadding(
- padding: const EdgeInsets.symmetric(horizontal: 15),
- sliver: SliverList(
- delegate: SliverChildBuilderDelegate(
- (context, index) {
- final file = _files[index];
- final progress = progressNotifier.getProgress(file.id);
- return Padding(
- padding: const EdgeInsets.symmetric(vertical: 10),
- child: Row(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- const Icon(Icons.description, size: 46),
- const SizedBox(width: 10),
- Expanded(
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Row(
- children: [
- Flexible(
- child: Text(
- file.fileName,
- style: const TextStyle(fontSize: 16),
- maxLines: 1,
- overflow: TextOverflow.ellipsis,
- ),
- ),
- Text(' (${file.size.asReadableFileSize})', style: const TextStyle(fontSize: 16)),
- ],
- ),
- if (progress == 1)
- Text(t.general.done, style: TextStyle(color: Theme.of(context).colorScheme.tertiaryContainer))
- else
- Padding(
- padding: const EdgeInsets.only(top: 5, right: 20),
- child: ClipRRect(
- borderRadius: BorderRadius.circular(10),
- child: LinearProgressIndicator(
- value: progress,
- minHeight: 10,
- ),
- ),
- ),
- ],
+ ListView.builder(
+ padding: EdgeInsets.only(
+ top: MediaQuery.of(context).padding.top + 20,
+ bottom: 150,
+ left: 15,
+ right: 30,
+ ),
+ itemCount: _files.length,
+ itemBuilder: (context, index) {
+ final file = _files[index];
+ final progress = progressNotifier.getProgress(file.id);
+ return Padding(
+ padding: const EdgeInsets.symmetric(vertical: 10),
+ child: Row(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Icon(file.fileType.icon, size: 46),
+ const SizedBox(width: 10),
+ Expanded(
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Row(
+ children: [
+ Flexible(
+ child: Text(
+ file.fileName,
+ style: const TextStyle(fontSize: 16),
+ maxLines: 1,
+ overflow: TextOverflow.ellipsis,
+ ),
+ ),
+ Text(' (${file.size.asReadableFileSize})', style: const TextStyle(fontSize: 16)),
+ ],
+ ),
+ if (progress == 1)
+ Text(t.general.done, style: TextStyle(color: Theme.of(context).colorScheme.tertiaryContainer))
+ else
+ Padding(
+ padding: const EdgeInsets.only(top: 5),
+ child: ClipRRect(
+ borderRadius: BorderRadius.circular(10),
+ child: LinearProgressIndicator(
+ value: progress,
+ minHeight: 10,
+ ),
),
),
- ],
- ),
- );
- },
- childCount: _files.length,
- ),
+ ],
+ ),
+ ),
+ ],
),
- ),
- const SliverToBoxAdapter(
- child: SizedBox(height: 150),
- ),
- ],
+ );
+ },
),
SafeArea(
child: Align(
@@ -161,9 +152,11 @@ class _ProgressPageState extends ConsumerState<ProgressPage> {
onPressed: () {
if (receiveState != null) {
ref.read(serverProvider.notifier).closeSession();
- ref.read(progressProvider.notifier).reset();
- const HomeRoute().go(context);
+ } else if (sendState != null) {
+ ref.read(sendProvider.notifier).cancel();
}
+ ref.read(progressProvider.notifier).reset();
+ const HomeRoute().go(context);
},
icon: const Icon(Icons.check_circle),
label: Text(t.general.done),
diff --git a/lib/provider/network/send_provider.dart b/lib/provider/network/send_provider.dart
index 379000db..65729145 100644
--- a/lib/provider/network/send_provider.dart
+++ b/lib/provider/network/send_provider.dart
@@ -13,6 +13,7 @@ import 'package:localsend_app/model/send/send_state.dart';
import 'package:localsend_app/model/send/sending_file.dart';
import 'package:localsend_app/model/session_status.dart';
import 'package:localsend_app/provider/device_info_provider.dart';
+import 'package:localsend_app/provider/progress_provider.dart';
import 'package:localsend_app/routes.dart';
import 'package:localsend_app/util/api_route_builder.dart';
import 'package:localsend_app/util/file_path_helper.dart';
@@ -92,6 +93,10 @@ class SendNotifier extends StateNotifier<SendState?> {
for (final file in requestState.files.values)
file.file.id: responseMap.containsKey(file.file.id) ? file.copyWith(token: responseMap[file.file.id]) : file,
};
+
+ // ignore: use_build_context_synchronously
+ const ProgressRoute().go(LocalSendApp.routerContext);
+
state = requestState.copyWith(
status: SessionStatus.sending,
files: sendingFiles,
@@ -129,6 +134,9 @@ class SendNotifier extends StateNotifier<SendState?> {
}
),
data: file.path != null ? File(file.path!).openRead() : file.bytes!,
+ onSendProgress: (curr, total) {
+ _ref.read(progressProvider.notifier).setProgress(file.file.id, curr / total);
+ }
);
} on DioError catch (e) {
print(e);
diff --git a/lib/provider/network/server_provider.dart b/lib/provider/network/server_provider.dart
index ee7388ce..e7758f10 100644
--- a/lib/provider/network/server_provider.dart
+++ b/lib/provider/network/server_provider.dart
@@ -272,11 +272,14 @@ class ServerNotifier extends StateNotifier<ServerState?> {
}
void _cancelBySender() {
- state = state?.copyWith(
- receiveState: state?.receiveState?.copyWith(
- status: SessionStatus.canceledBySender,
- ),
- );
+ final currentStatus = state?.receiveState?.status;
+ if (currentStatus != null && (currentStatus == SessionStatus.waiting || currentStatus == SessionStatus.sending)) {
+ state = state?.copyWith(
+ receiveState: state?.receiveState?.copyWith(
+ status: SessionStatus.canceledBySender,
+ ),
+ );
+ }
}
void closeSession() {