An open-source cross-platform alternative to AirDrop
feat: message handling optimization
Tien Do Namdf620a7ee3ed
diff --git a/assets/CHANGELOG.md b/assets/CHANGELOG.mdindex c34b59de..433e121a 100644--- a/assets/CHANGELOG.md+++ b/assets/CHANGELOG.md@@ -1,3 +1,9 @@+## 1.9.0 (2023-)++- feat: allow deleting history entry when file could not be opened (@TheGB0077)+- feat: close message request when copied / opened link+- fix: clear selection after finished message transfer+## 1.8.0 (2023-03-05)- feat: add send modes (single recipient, multiple recipients)diff --git a/lib/pages/receive_page.dart b/lib/pages/receive_page.dartindex a161eec1..8ff6e4a1 100644--- a/lib/pages/receive_page.dart+++ b/lib/pages/receive_page.dart@@ -5,7 +5,6 @@ import 'package:flutter/services.dart';import 'package:flutter_riverpod/flutter_riverpod.dart';import 'package:localsend_app/gen/strings.g.dart';import 'package:localsend_app/model/session_status.dart';-import 'package:localsend_app/model/state/server/receive_session_state.dart';import 'package:localsend_app/pages/progress_page.dart';import 'package:localsend_app/pages/receive_options_page.dart';import 'package:localsend_app/provider/network/server_provider.dart';@@ -29,6 +28,7 @@ class ReceivePage extends ConsumerStatefulWidget {class _ReceivePageState extends ConsumerState<ReceivePage> {String? _message;bool _isLink = false;+ bool _showFullIp = false;@overridevoid initState() {@@ -50,11 +50,11 @@ class _ReceivePageState extends ConsumerState<ReceivePage> {});}- void _acceptNothing(WidgetRef ref, ReceiveSessionState receiveState) {+ void _acceptNothing(WidgetRef ref) {ref.read(serverProvider.notifier).acceptFileRequest({});}- void _accept(WidgetRef ref, ReceiveSessionState receiveState) {+ void _accept(WidgetRef ref) {final selectedFiles = ref.read(selectedReceivingFilesProvider);ref.read(serverProvider.notifier).acceptFileRequest(selectedFiles);}@@ -112,9 +112,14 @@ class _ReceivePageState extends ConsumerState<ReceivePage> {Row(mainAxisAlignment: MainAxisAlignment.center,children: [- DeviceBadge(- color: Theme.of(context).colorScheme.tertiaryContainer,- label: '#${receiveSession.sender.ip.visualId}',+ InkWell(+ onTap: () {+ setState(() => _showFullIp = !_showFullIp);+ },+ child: DeviceBadge(+ color: Theme.of(context).colorScheme.tertiaryContainer,+ label: _showFullIp ? receiveSession.sender.ip : '#${receiveSession.sender.ip.visualId}',+ ),),if (receiveSession.sender.deviceModel != null)...[@@ -166,6 +171,8 @@ class _ReceivePageState extends ConsumerState<ReceivePage> {if (checkPlatformIsDesktop()) {context.showSnackBar(t.general.copiedToClipboard);}+ _acceptNothing(ref);+ context.pop();},child: Text(t.general.copy),),@@ -177,8 +184,10 @@ class _ReceivePageState extends ConsumerState<ReceivePage> {backgroundColor: Theme.of(context).buttonTheme.colorScheme!.primary,foregroundColor: Theme.of(context).buttonTheme.colorScheme!.onPrimary,),- onPressed: () async {- await launchUrl(Uri.parse(_message!));+ onPressed: () {+ unawaited(launchUrl(Uri.parse(_message!)));+ _acceptNothing(ref);+ context.pop();},child: Text(t.general.open),),@@ -230,7 +239,7 @@ class _ReceivePageState extends ConsumerState<ReceivePage> {foregroundColor: Theme.of(context).colorScheme.onSurface,),onPressed: () {- _acceptNothing(ref, receiveSession);+ _acceptNothing(ref);context.pop();},icon: const Icon(Icons.close),@@ -266,7 +275,7 @@ class _ReceivePageState extends ConsumerState<ReceivePage> {if (sessionId == null) {return;}- _accept(ref, receiveSession);+ _accept(ref);await context.pushAndRemoveUntilImmediately(removeUntil: ReceivePage,builder: () => ProgressPage(diff --git a/lib/provider/network/send_provider.dart b/lib/provider/network/send_provider.dartindex da3b04c0..c9a57169 100644--- a/lib/provider/network/send_provider.dart+++ b/lib/provider/network/send_provider.dart@@ -149,13 +149,19 @@ class SendNotifier extends StateNotifier<Map<String, SendSessionState>> {final responseMap = response.data as Map;if (responseMap.isEmpty) {// receiver has nothing selected+ state = state.updateSession(+ sessionId: sessionId,+ state: (s) => s?.copyWith(+ status: SessionStatus.finished,+ ),+ );if (state[sessionId]?.background == false) {// ignore: use_build_context_synchronously, unawaited_futuresRouterino.context.pushRootImmediately(() => const HomePage(initialTab: HomeTab.send, appStart: false));}- state = state.removeSession(_ref, sessionId);+ closeSession(sessionId);return;}@@ -252,8 +258,10 @@ class SendNotifier extends StateNotifier<Map<String, SendSessionState>> {}if (!hasError && state[sessionId]?.background == true) {- state = state.removeSession(_ref, sessionId);+ // close session because everything is fine and it is in background+ closeSession(sessionId);} else {+ // keep session alive when there are errors or currently in foregroundstate = state.updateSession(sessionId: sessionId,state: (s) => s?.copyWith(@@ -272,20 +280,30 @@ class SendNotifier extends StateNotifier<Map<String, SendSessionState>> {if (sessionState == null) {return;}- final target = sessionState.target;sessionState.cancelToken?.cancel(); // cancel current request- state = state.removeSession(_ref, sessionId);- if (sessionState.status == SessionStatus.finished && _ref.read(settingsProvider.select((s) => s.sendMode == SendMode.single))) {- // clear selected files- _ref.read(selectedSendingFilesProvider.notifier).reset();- }// notify the receiverunawaited(- _ref.read(dioProvider(DioType.discovery)).post(ApiRoute.cancel.target(target)).then((_) {}).catchError((e) {+ _ref.read(dioProvider(DioType.discovery)).post(ApiRoute.cancel.target(sessionState.target)).then((_) {}).catchError((e) {print(e);}),);++ // finally, close session locally+ closeSession(sessionId);+ }++ /// Closes the session+ void closeSession(String sessionId) {+ final sessionState = state[sessionId];+ if (sessionState == null) {+ return;+ }+ state = state.removeSession(_ref, sessionId);+ if (sessionState.status == SessionStatus.finished && _ref.read(settingsProvider.select((s) => s.sendMode == SendMode.single))) {+ // clear selected files+ _ref.read(selectedSendingFilesProvider.notifier).reset();+ }}void clearAllSessions() {diff --git a/lib/provider/network/server_provider.dart b/lib/provider/network/server_provider.dartindex 70c9e65a..f2ab3655 100644--- a/lib/provider/network/server_provider.dart+++ b/lib/provider/network/server_provider.dart@@ -234,54 +234,55 @@ class ServerNotifier extends StateNotifier<ServerState?> {return _response(500, message: 'Server is in invalid state');}- if (selection != null) {- if (selection.isEmpty) {- // nothing selected, send this to sender and close session- closeSession();- return _response(200);- }+ if (selection == null) {+ closeSession();+ return _response(403, message: 'File request declined by recipient');+ }- final receiveState = state!.session!;- state = state!.copyWith(- session: receiveState.copyWith(- status: SessionStatus.sending,- files: Map.fromEntries(- receiveState.files.values.map((entry) {- final desiredName = selection![entry.file.id];- return MapEntry(- entry.file.id,- ReceivingFile(- file: entry.file,- status: desiredName != null ? FileStatus.queue : FileStatus.skipped,- token: desiredName != null ? _uuid.v4() : null,- desiredName: desiredName,- path: null,- savedToGallery: false,- errorMessage: null,- ),- );- }),- ),- responseHandler: null,- ),- );+ if (selection.isEmpty) {+ // nothing selected, send this to sender and close session+ // This usually happens for message transfers+ closeSession();+ return _response(200);+ }- if (quickSave) {- // ignore: use_build_context_synchronously, unawaited_futures- Routerino.context.pushImmediately(() => ProgressPage(- showAppBar: false,- closeSessionOnClose: true,- sessionId: sessionId,- ));- }+ final receiveState = state!.session!;+ state = state!.copyWith(+ session: receiveState.copyWith(+ status: SessionStatus.sending,+ files: Map.fromEntries(+ receiveState.files.values.map((entry) {+ final desiredName = selection![entry.file.id];+ return MapEntry(+ entry.file.id,+ ReceivingFile(+ file: entry.file,+ status: desiredName != null ? FileStatus.queue : FileStatus.skipped,+ token: desiredName != null ? _uuid.v4() : null,+ desiredName: desiredName,+ path: null,+ savedToGallery: false,+ errorMessage: null,+ ),+ );+ }),+ ),+ responseHandler: null,+ ),+ );- return _response(200, body: {- for (final file in state!.session!.files.values.where((f) => f.token != null)) file.file.id: file.token,- });- } else {- closeSession();- return _response(403, message: 'File request declined by recipient');+ if (quickSave) {+ // ignore: use_build_context_synchronously, unawaited_futures+ Routerino.context.pushImmediately(() => ProgressPage(+ showAppBar: false,+ closeSessionOnClose: true,+ sessionId: sessionId,+ ));}++ return _response(200, body: {+ for (final file in state!.session!.files.values.where((f) => f.token != null)) file.file.id: file.token,+ });});router.post(ApiRoute.send.path, (Request request) async {