Skip to content

An open-source cross-platform alternative to AirDrop

refactor: migrate more providers to redux

Tien Do Namac90baa5f2b0
diff --git a/app/lib/pages/receive_history_page.dart b/app/lib/pages/receive_history_page.dart
index 2d4e2b79..662c2c2a 100644
--- a/app/lib/pages/receive_history_page.dart
+++ b/app/lib/pages/receive_history_page.dart
@@ -38,21 +38,24 @@ final _optionsWithoutOpen = [_EntryOption.info, _EntryOption.delete];
class ReceiveHistoryPage extends StatelessWidget {
const ReceiveHistoryPage({super.key});
- Future<void> _openFile(BuildContext context, ReceiveHistoryEntry entry, ReceiveHistoryNotifier filesRef) async {
+ Future<void> _openFile(
+ BuildContext context,
+ ReceiveHistoryEntry entry,
+ Dispatcher<ReceiveHistoryNotifier, List<ReceiveHistoryEntry>> dispatcher,
+ ) async {
if (entry.path != null) {
await openFile(
context,
entry.fileType,
entry.path!,
- onDeleteTap: () => filesRef.removeEntry(entry.id),
+ onDeleteTap: () => dispatcher.dispatchAsync(RemoveHistoryEntryAction(entry.id)),
);
}
}
@override
Widget build(BuildContext context) {
- final ref = context.ref;
- final entries = ref.watch(receiveHistoryProvider);
+ final entries = context.watch(receiveHistoryProvider);
return Scaffold(
appBar: AppBar(
@@ -74,7 +77,8 @@ class ReceiveHistoryPage extends StatelessWidget {
onPressed: checkPlatform([TargetPlatform.iOS])
? null
: () async {
- final destination = ref.read(settingsProvider).destination ?? await getDefaultDestinationDirectory();
+ // ignore: use_build_context_synchronously
+ final destination = context.read(settingsProvider).destination ?? await getDefaultDestinationDirectory();
await openFolder(destination);
},
icon: const Icon(Icons.folder),
@@ -94,8 +98,8 @@ class ReceiveHistoryPage extends StatelessWidget {
builder: (_) => const HistoryClearDialog(),
);
- if (result == true) {
- await ref.notifier(receiveHistoryProvider).removeAll();
+ if (context.mounted && result == true) {
+ await context.redux(receiveHistoryProvider).dispatchAsync(RemoveAllHistoryEntriesAction());
}
},
icon: const Icon(Icons.delete),
@@ -119,7 +123,7 @@ class ReceiveHistoryPage extends StatelessWidget {
splashFactory: NoSplash.splashFactory,
highlightColor: Colors.transparent,
hoverColor: Colors.transparent,
- onTap: entry.path != null ? () async => _openFile(context, entry, ref.notifier(receiveHistoryProvider)) : null,
+ onTap: entry.path != null ? () async => _openFile(context, entry, context.redux(receiveHistoryProvider)) : null,
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
@@ -155,7 +159,7 @@ class ReceiveHistoryPage extends StatelessWidget {
onSelected: (_EntryOption item) async {
switch (item) {
case _EntryOption.open:
- await _openFile(context, entry, ref.notifier(receiveHistoryProvider));
+ await _openFile(context, entry, context.redux(receiveHistoryProvider));
break;
case _EntryOption.info:
// ignore: use_build_context_synchronously
@@ -165,7 +169,8 @@ class ReceiveHistoryPage extends StatelessWidget {
);
break;
case _EntryOption.delete:
- await ref.notifier(receiveHistoryProvider).removeEntry(entry.id);
+ // ignore: use_build_context_synchronously
+ await context.redux(receiveHistoryProvider).dispatchAsync(RemoveHistoryEntryAction(entry.id));
break;
}
},
diff --git a/app/lib/provider/app_arguments_provider.dart b/app/lib/provider/app_arguments_provider.dart
index 5e88d60d..706b8f0b 100644
--- a/app/lib/provider/app_arguments_provider.dart
+++ b/app/lib/provider/app_arguments_provider.dart
@@ -1,4 +1,7 @@
import 'package:refena_flutter/refena_flutter.dart';
/// Contains the arguments with which the app was started.
-final appArgumentsProvider = Provider((ref) => <String>[], debugLabel: 'appArgumentsProvider');
+final appArgumentsProvider = Provider(
+ (ref) => <String>[],
+ debugLabel: 'appArgumentsProvider',
+);
diff --git a/app/lib/provider/favorites_provider.dart b/app/lib/provider/favorites_provider.dart
index b069245f..1a7f3247 100644
--- a/app/lib/provider/favorites_provider.dart
+++ b/app/lib/provider/favorites_provider.dart
@@ -2,6 +2,8 @@ import 'package:localsend_app/model/persistence/favorite_device.dart';
import 'package:localsend_app/provider/persistence_provider.dart';
import 'package:refena_flutter/refena_flutter.dart';
+/// This provider stores the list of favorite devices.
+/// It automatically saves the list to the device's storage.
final favoritesProvider = ReduxProvider<FavoritesService, List<FavoriteDevice>>((ref) {
return FavoritesService(ref.read(persistenceProvider));
});
@@ -12,11 +14,10 @@ class FavoritesService extends ReduxNotifier<List<FavoriteDevice>> {
FavoritesService(this._persistence);
@override
- List<FavoriteDevice> init() {
- return _persistence.getFavorites();
- }
+ List<FavoriteDevice> init() => _persistence.getFavorites();
}
+/// Adds a favorite device.
class AddFavoriteAction extends AsyncReduxAction<FavoritesService, List<FavoriteDevice>> {
final FavoriteDevice device;
@@ -33,6 +34,7 @@ class AddFavoriteAction extends AsyncReduxAction<FavoritesService, List<Favorite
}
}
+/// Updates a favorite device.
class UpdateFavoriteAction extends AsyncReduxAction<FavoritesService, List<FavoriteDevice>> {
final FavoriteDevice device;
@@ -49,6 +51,7 @@ class UpdateFavoriteAction extends AsyncReduxAction<FavoritesService, List<Favor
}
}
+/// Removes a favorite device.
class RemoveFavoriteAction extends AsyncReduxAction<FavoritesService, List<FavoriteDevice>> {
final String deviceFingerprint;
diff --git a/app/lib/provider/last_devices.provider.dart b/app/lib/provider/last_devices.provider.dart
index 6b09e6cc..11f96968 100644
--- a/app/lib/provider/last_devices.provider.dart
+++ b/app/lib/provider/last_devices.provider.dart
@@ -1,20 +1,29 @@
import 'package:localsend_app/model/device.dart';
import 'package:refena_flutter/refena_flutter.dart';
-final lastDevicesProvider = NotifierProvider<LastDevicesNotifier, List<Device>>((ref) {
+/// This provider stores the last devices that the user sent a file to.
+/// It stores only the last 5 devices that were selected by be [AddressInputDialog].
+/// The information is **not** persisted.
+final lastDevicesProvider = ReduxProvider<LastDevicesNotifier, List<Device>>((ref) {
return LastDevicesNotifier();
});
-class LastDevicesNotifier extends Notifier<List<Device>> {
- LastDevicesNotifier();
-
+class LastDevicesNotifier extends ReduxNotifier<List<Device>> {
@override
List<Device> init() => [];
+}
+
+/// Adds a device to the list of last devices.
+class AddLastDeviceAction extends ReduxAction<LastDevicesNotifier, List<Device>> {
+ final Device device;
- void addDevice(Device device) {
- state = {
+ AddLastDeviceAction(this.device);
+
+ @override
+ List<Device> reduce() {
+ return {
device,
...state,
- }.toList().take(5).toList();
+ }.take(5).toList();
}
}
diff --git a/app/lib/provider/network/server/controller/receive_controller.dart b/app/lib/provider/network/server/controller/receive_controller.dart
index 859b18ad..0c391783 100644
--- a/app/lib/provider/network/server/controller/receive_controller.dart
+++ b/app/lib/provider/network/server/controller/receive_controller.dart
@@ -440,8 +440,8 @@ class ReceiveController {
);
// Track it in history
- await server.ref.notifier(receiveHistoryProvider).addEntry(
- id: fileId,
+ await server.ref.redux(receiveHistoryProvider).dispatchAsync(AddHistoryEntryAction(
+ entryId: fileId,
fileName: receivingFile.desiredName!,
fileType: receivingFile.file.fileType,
path: saveToGallery ? null : destinationPath,
@@ -449,7 +449,7 @@ class ReceiveController {
fileSize: receivingFile.file.size,
senderAlias: receiveState.senderAlias,
timestamp: DateTime.now().toUtc(),
- );
+ ));
_logger.info('Saved ${receivingFile.file.fileName}.');
} catch (e, st) {
diff --git a/app/lib/provider/receive_history_provider.dart b/app/lib/provider/receive_history_provider.dart
index 6d2c1807..738fe5d3 100644
--- a/app/lib/provider/receive_history_provider.dart
+++ b/app/lib/provider/receive_history_provider.dart
@@ -3,38 +3,52 @@ import 'package:localsend_app/model/persistence/receive_history_entry.dart';
import 'package:localsend_app/provider/persistence_provider.dart';
import 'package:refena_flutter/refena_flutter.dart';
-final receiveHistoryProvider = NotifierProvider<ReceiveHistoryNotifier, List<ReceiveHistoryEntry>>((ref) {
- return ReceiveHistoryNotifier();
+/// This provider stores the history of received files.
+/// It automatically saves the history to the device's storage.
+final receiveHistoryProvider = ReduxProvider<ReceiveHistoryNotifier, List<ReceiveHistoryEntry>>((ref) {
+ return ReceiveHistoryNotifier(ref.read(persistenceProvider));
});
-class ReceiveHistoryNotifier extends Notifier<List<ReceiveHistoryEntry>> {
- late PersistenceService _service;
+class ReceiveHistoryNotifier extends ReduxNotifier<List<ReceiveHistoryEntry>> {
+ final PersistenceService _persistence;
- ReceiveHistoryNotifier();
+ ReceiveHistoryNotifier(this._persistence);
@override
- List<ReceiveHistoryEntry> init() {
- _service = ref.read(persistenceProvider);
- return _service.getReceiveHistory();
- }
+ List<ReceiveHistoryEntry> init() => _persistence.getReceiveHistory();
+}
+
+/// Adds a history entry.
+class AddHistoryEntryAction extends AsyncReduxAction<ReceiveHistoryNotifier, List<ReceiveHistoryEntry>> {
+ final String entryId;
+ final String fileName;
+ final FileType fileType;
+ final String? path;
+ final bool savedToGallery;
+ final int fileSize;
+ final String senderAlias;
+ final DateTime timestamp;
- Future<void> addEntry({
- required String id,
- required String fileName,
- required FileType fileType,
- required String? path,
- required bool savedToGallery,
- required int fileSize,
- required String senderAlias,
- required DateTime timestamp,
- }) async {
- if (!_service.isSaveToHistory()) {
- return;
+ AddHistoryEntryAction({
+ required this.entryId,
+ required this.fileName,
+ required this.fileType,
+ required this.path,
+ required this.savedToGallery,
+ required this.fileSize,
+ required this.senderAlias,
+ required this.timestamp,
+ });
+
+ @override
+ Future<List<ReceiveHistoryEntry>> reduce() async {
+ if (!notifier._persistence.isSaveToHistory()) {
+ return state;
}
final updated = [
ReceiveHistoryEntry(
- id: id,
+ id: entryId,
fileName: fileName,
fileType: fileType,
path: path,
@@ -45,18 +59,30 @@ class ReceiveHistoryNotifier extends Notifier<List<ReceiveHistoryEntry>> {
),
...state,
].take(200).toList();
- await _service.setReceiveHistory(updated);
- state = updated;
+ await notifier._persistence.setReceiveHistory(updated);
+ return updated;
}
+}
- Future<void> removeEntry(String id) async {
- final updated = [...state]..removeWhere((e) => e.id == id);
- await _service.setReceiveHistory(updated);
- state = updated;
+/// Removes a history entry.
+class RemoveHistoryEntryAction extends AsyncReduxAction<ReceiveHistoryNotifier, List<ReceiveHistoryEntry>> {
+ final String entryId;
+
+ RemoveHistoryEntryAction(this.entryId);
+
+ @override
+ Future<List<ReceiveHistoryEntry>> reduce() async {
+ final updated = [...state]..removeWhere((e) => e.id == entryId);
+ await notifier._persistence.setReceiveHistory(updated);
+ return updated;
}
+}
- Future<void> removeAll() async {
- await _service.setReceiveHistory([]);
- state = [];
+/// Removes all history entries.
+class RemoveAllHistoryEntriesAction extends AsyncReduxAction<ReceiveHistoryNotifier, List<ReceiveHistoryEntry>> {
+ @override
+ Future<List<ReceiveHistoryEntry>> reduce() async {
+ await notifier._persistence.setReceiveHistory([]);
+ return [];
}
}
diff --git a/app/lib/widget/dialogs/address_input_dialog.dart b/app/lib/widget/dialogs/address_input_dialog.dart
index 8959381e..f4a9a61f 100644
--- a/app/lib/widget/dialogs/address_input_dialog.dart
+++ b/app/lib/widget/dialogs/address_input_dialog.dart
@@ -69,7 +69,7 @@ class _AddressInputDialogState extends State<AddressInputDialog> with Refena {
if (device != null) {
found = true;
if (mounted) {
- ref.notifier(lastDevicesProvider).addDevice(device);
+ ref.redux(lastDevicesProvider).dispatch(AddLastDeviceAction(device));
context.pop(device);
}
break;