An open-source cross-platform alternative to AirDrop
feat: receive file history
Tien Do Namb39c613b2b89
diff --git a/assets/CHANGELOG.md b/assets/CHANGELOG.mdindex 084897c9..ab431311 100644--- a/assets/CHANGELOG.md+++ b/assets/CHANGELOG.md@@ -1,5 +1,6 @@## 1.7.0 (2023-)+- feat: received file history- feat: add unencrypted HTTP mode- feat: message input is horizontally scrollable when multiline is unselected- feat: open message normally in QuickSave mode (instead of saving it into a file)diff --git a/assets/i18n/strings.i18n.json b/assets/i18n/strings.i18n.jsonindex 43c4ebd6..1451bfb3 100644--- a/assets/i18n/strings.i18n.json+++ b/assets/i18n/strings.i18n.json@@ -100,9 +100,10 @@},"receiveHistoryPage": {"title": "History",+ "empty": "The history is empty.","entryActions": {- "open": "@:general.open",- "info": "Info",+ "open": "Open file",+ "info": "Information","deleteFromHistory": "Delete from history"}},@@ -244,6 +245,14 @@"title": "Encryption disabled","content": "Communication now takes place via the unencrypted HTTP protocol. To use HTTPS, enable encryption again."},+ "fileInfo": {+ "title": "File information",+ "fileName": "File name:",+ "path": "Path:",+ "size": "Size:",+ "sender": "Sender:",+ "time": "Time:"+ },"fileNameInput": {"title": "Enter file name","original": "Original: {original}"diff --git a/lib/model/receive_history_entry.dart b/lib/model/receive_history_entry.dartindex 5af3ab5f..ddc3794d 100644--- a/lib/model/receive_history_entry.dart+++ b/lib/model/receive_history_entry.dart@@ -1,4 +1,5 @@import 'package:freezed_annotation/freezed_annotation.dart';+import 'package:intl/intl.dart';import 'package:localsend_app/model/file_type.dart';part 'receive_history_entry.freezed.dart';@@ -6,6 +7,8 @@ part 'receive_history_entry.g.dart';@freezedclass ReceiveHistoryEntry with _$ReceiveHistoryEntry {+ const ReceiveHistoryEntry._(); // allow custom getters+const factory ReceiveHistoryEntry({required String id,required String fileName,@@ -18,4 +21,11 @@ class ReceiveHistoryEntry with _$ReceiveHistoryEntry {}) = _ReceiveHistoryEntry;factory ReceiveHistoryEntry.fromJson(Map<String, Object?> json) => _$ReceiveHistoryEntryFromJson(json);++ /// Format string using the intl package.+ /// Because the raw timestamp is saved in UTC, we need to transform it to local time zone first.+ String get timestampString {+ final localTimestamp = timestamp.toLocal();+ return '${DateFormat.yMd().format(localTimestamp)} ${DateFormat.jm().format(localTimestamp)}';+ }}diff --git a/lib/pages/receive_history_page.dart b/lib/pages/receive_history_page.dartindex 9f0c0cdd..773e1302 100644--- a/lib/pages/receive_history_page.dart+++ b/lib/pages/receive_history_page.dart@@ -1,10 +1,12 @@import 'package:flutter/material.dart';import 'package:flutter_riverpod/flutter_riverpod.dart';-import 'package:intl/intl.dart';import 'package:localsend_app/gen/strings.g.dart';+import 'package:localsend_app/model/receive_history_entry.dart';import 'package:localsend_app/provider/receive_history_provider.dart';+import 'package:localsend_app/theme.dart';import 'package:localsend_app/util/file_size_helper.dart';import 'package:localsend_app/widget/dialogs/cannot_open_file_dialog.dart';+import 'package:localsend_app/widget/dialogs/file_info_dialog.dart';import 'package:localsend_app/widget/file_thumbnail.dart';import 'package:localsend_app/widget/responsive_list_view.dart';import 'package:open_filex/open_filex.dart';@@ -29,6 +31,15 @@ enum _EntryOption {class ReceiveHistoryPage extends ConsumerWidget {const ReceiveHistoryPage({Key? key}) : super(key: key);+ Future<void> _openFile(BuildContext context, ReceiveHistoryEntry entry) async {+ final result = await OpenFilex.open(entry.path);+ if (result.type != ResultType.done) {+ // TODO: use context.mounted when migrated to Flutter 3.7+ // ignore: use_build_context_synchronously+ CannotOpenFileDialog.open(context, entry.path!);+ }+ }+@overrideWidget build(BuildContext context, WidgetRef ref) {final entries = ref.watch(receiveHistoryProvider);@@ -37,78 +48,91 @@ class ReceiveHistoryPage extends ConsumerWidget {appBar: AppBar(title: Text(t.receiveHistoryPage.title),),- body: ResponsiveListView(- padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 20),- children: [- ...entries.map((e) {- return Padding(- padding: const EdgeInsets.symmetric(vertical: 8),- child: InkWell(- splashColor: Colors.transparent,- splashFactory: NoSplash.splashFactory,- highlightColor: Colors.transparent,- hoverColor: Colors.transparent,- onTap: e.path != null- ? () async {- final result = await OpenFilex.open(e.path);- if (result.type != ResultType.done) {- // TODO: use context.mounted when migrated to Flutter 3.7- // ignore: use_build_context_synchronously- CannotOpenFileDialog.open(context, e.path!);- }- }- : null,- child: Row(- crossAxisAlignment: CrossAxisAlignment.start,- children: [- FilePathThumbnail(- path: e.path,- fileType: e.fileType,- ),- const SizedBox(width: 10),- Expanded(- child: Column(- crossAxisAlignment: CrossAxisAlignment.start,- children: [- const SizedBox(height: 3),- Text(- e.fileName,- style: const TextStyle(fontSize: 16),- maxLines: 1,- overflow: TextOverflow.fade,- softWrap: false,- ),- Text(- '${DateFormat.yMd().format(e.timestamp)} ${DateFormat.jm().format(e.timestamp)} - ${e.fileSize.asReadableFileSize} - ${e.senderAlias}',- maxLines: 1,- overflow: TextOverflow.fade,- softWrap: false,- style: const TextStyle(color: Colors.grey),- ),- ],+ body: Builder(builder: (context) {+ if (entries.isEmpty) {+ return Center(+ child: Text(t.receiveHistoryPage.empty, style: Theme.of(context).textTheme.headline4),+ );+ }++ return ResponsiveListView(+ padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 20),+ children: [+ ...entries.map((entry) {+ return Padding(+ padding: const EdgeInsets.symmetric(vertical: 8),+ child: InkWell(+ splashColor: Colors.transparent,+ splashFactory: NoSplash.splashFactory,+ highlightColor: Colors.transparent,+ hoverColor: Colors.transparent,+ onTap: entry.path != null ? () => _openFile(context, entry) : null,+ child: Row(+ crossAxisAlignment: CrossAxisAlignment.start,+ children: [+ FilePathThumbnail(+ path: entry.path,+ fileType: entry.fileType,),- ),- const SizedBox(width: 10),- PopupMenuButton<_EntryOption>(- onSelected: (_EntryOption item) {- print(item);- },- itemBuilder: (BuildContext context) {- return _EntryOption.values.map((e) {- return PopupMenuItem<_EntryOption>(- value: e,- child: Text(e.label),- );- }).toList();- },- ),- ],+ const SizedBox(width: 10),+ Expanded(+ child: Column(+ crossAxisAlignment: CrossAxisAlignment.start,+ children: [+ const SizedBox(height: 3),+ Text(+ entry.fileName,+ style: const TextStyle(fontSize: 16),+ maxLines: 1,+ overflow: TextOverflow.fade,+ softWrap: false,+ ),+ Text(+ '${entry.timestampString} - ${entry.fileSize.asReadableFileSize} - ${entry.senderAlias}',+ maxLines: 1,+ overflow: TextOverflow.fade,+ softWrap: false,+ style: const TextStyle(color: Colors.grey),+ ),+ ],+ ),+ ),+ const SizedBox(width: 10),+ PopupMenuButton<_EntryOption>(+ onSelected: (_EntryOption item) {+ switch (item) {+ case _EntryOption.open:+ _openFile(context, entry);+ break;+ case _EntryOption.info:+ showDialog(+ context: context,+ builder: (_) => FileInfoDialog(entry: entry),+ );+ break;+ case _EntryOption.delete:+ ref.read(receiveHistoryProvider.notifier).removeEntry(entry.id);+ break;+ }+ },+ color: Theme.of(context).cardColorWithElevation,+ itemBuilder: (BuildContext context) {+ return _EntryOption.values.map((e) {+ return PopupMenuItem<_EntryOption>(+ value: e,+ child: Text(e.label),+ );+ }).toList();+ },+ ),+ ],+ ),),- ),- );- }),- ],- ),+ );+ }),+ ],+ );+ }),);}}diff --git a/lib/provider/receive_history_provider.dart b/lib/provider/receive_history_provider.dartindex a83a0a65..ef9c66e8 100644--- a/lib/provider/receive_history_provider.dart+++ b/lib/provider/receive_history_provider.dart@@ -40,8 +40,8 @@ class ReceiveHistoryNotifier extends StateNotifier<List<ReceiveHistoryEntry>> {state = updated;}- Future<void> removeEntry(int index) async {- final updated = [...state]..removeAt(index);+ Future<void> removeEntry(String id) async {+ final updated = [...state]..removeWhere((e) => e.id == id);await _service.setReceiveHistory(updated);state = updated;}diff --git a/lib/widget/dialogs/file_info_dialog.dart b/lib/widget/dialogs/file_info_dialog.dartnew file mode 100644index 00000000..1c325478--- /dev/null+++ b/lib/widget/dialogs/file_info_dialog.dart@@ -0,0 +1,68 @@+import 'package:flutter/material.dart';+import 'package:localsend_app/gen/strings.g.dart';+import 'package:localsend_app/model/receive_history_entry.dart';+import 'package:localsend_app/util/file_size_helper.dart';+import 'package:routerino/routerino.dart';++class FileInfoDialog extends StatelessWidget {+ final ReceiveHistoryEntry entry;++ const FileInfoDialog({required this.entry, super.key});++ @override+ Widget build(BuildContext context) {+ return AlertDialog(+ title: Text(t.dialogs.fileInfo.title),+ content: Table(+ columnWidths: const {+ 0: IntrinsicColumnWidth(),+ 1: IntrinsicColumnWidth(),+ 2: IntrinsicColumnWidth(),+ },+ children: [+ TableRow(+ children: [+ Text(t.dialogs.fileInfo.fileName, maxLines: 1),+ const SizedBox(width: 10),+ SelectableText(entry.fileName),+ ],+ ),+ TableRow(+ children: [+ Text(t.dialogs.fileInfo.path),+ const SizedBox(width: 10),+ SelectableText(entry.savedToGallery ? t.progressPage.savedToGallery : (entry.path ?? '')),+ ],+ ),+ TableRow(+ children: [+ Text(t.dialogs.fileInfo.size),+ const SizedBox(width: 10),+ SelectableText(entry.fileSize.asReadableFileSize),+ ],+ ),+ TableRow(+ children: [+ Text(t.dialogs.fileInfo.sender),+ const SizedBox(width: 10),+ SelectableText(entry.senderAlias),+ ],+ ),+ TableRow(+ children: [+ Text(t.dialogs.fileInfo.time),+ const SizedBox(width: 10),+ SelectableText(entry.timestampString),+ ],+ ),+ ],+ ),+ actions: [+ TextButton(+ onPressed: () => context.pop(),+ child: Text(t.general.close),+ )+ ],+ );+ }+}