Skip to content

Commit

Permalink
Merge pull request #2058 from acterglobal/ben-filex-open
Browse files Browse the repository at this point in the history
Filex to open files
  • Loading branch information
gnunicorn authored Aug 14, 2024
2 parents 481db61 + 42f9870 commit 0db59a3
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 130 deletions.
40 changes: 0 additions & 40 deletions app/lib/common/widgets/download_button.dart

This file was deleted.

14 changes: 2 additions & 12 deletions app/lib/common/widgets/image_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import 'dart:io';
import 'package:acter/common/themes/app_theme.dart';
import 'package:acter/common/widgets/download_button.dart';
import 'package:acter/features/files/widgets/share_file_button.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:share_plus/share_plus.dart';
import 'package:zoom_hover_pinch_image/zoom_hover_pinch_image.dart';

class ImageDialog extends ConsumerWidget {
Expand All @@ -19,7 +17,6 @@ class ImageDialog extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final canShare = !isDesktop;
return Dialog(
insetPadding: EdgeInsets.zero,
child: Scaffold(
Expand All @@ -32,14 +29,7 @@ class ImageDialog extends ConsumerWidget {
overflow: TextOverflow.ellipsis,
),
actions: [
if (canShare)
IconButton(
onPressed: () {
Share.shareXFiles([XFile(imageFile.path)]);
},
icon: const Icon(Icons.share),
),
DownloadButton(file: imageFile),
ShareFileButton(file: imageFile),
IconButton(
onPressed: () => Navigator.pop(context),
icon: const Icon(Icons.close),
Expand Down
14 changes: 2 additions & 12 deletions app/lib/common/widgets/video_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import 'dart:io';
import 'package:acter/common/themes/app_theme.dart';
import 'package:acter/common/widgets/acter_video_player.dart';
import 'package:acter/common/widgets/download_button.dart';
import 'package:acter/features/files/widgets/share_file_button.dart';
import 'package:flutter/material.dart';
import 'package:share_plus/share_plus.dart';

class VideoDialog extends StatelessWidget {
final String title;
Expand All @@ -17,7 +15,6 @@ class VideoDialog extends StatelessWidget {

@override
Widget build(BuildContext context) {
final canShare = !isDesktop;
return Dialog(
insetPadding: EdgeInsets.zero,
child: Container(
Expand All @@ -37,14 +34,7 @@ class VideoDialog extends StatelessWidget {
overflow: TextOverflow.ellipsis,
),
),
if (canShare)
IconButton(
onPressed: () {
Share.shareXFiles([XFile(videoFile.path)]);
},
icon: const Icon(Icons.share),
),
DownloadButton(file: videoFile),
ShareFileButton(file: videoFile),
IconButton(
onPressed: () => Navigator.pop(context),
icon: const Icon(Icons.close),
Expand Down
10 changes: 2 additions & 8 deletions app/lib/features/attachments/widgets/attachment_item.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import 'package:acter/common/actions/redact_content.dart';
import 'package:acter/common/models/attachment_media_state/attachment_media_state.dart';
import 'package:acter/common/models/types.dart';
import 'package:acter/common/themes/app_theme.dart';
import 'package:acter/common/utils/utils.dart';
import 'package:acter/common/widgets/download_button.dart';
import 'package:acter/common/widgets/image_dialog.dart';
import 'package:acter/common/widgets/video_dialog.dart';
import 'package:acter/features/attachments/providers/attachment_providers.dart';
import 'package:acter/features/files/actions/file_share.dart';
import 'package:acter/features/pins/actions/attachment_leading_icon.dart';
import 'package:acter_flutter_sdk/acter_flutter_sdk_ffi.dart' show Attachment;
import 'package:atlas_icons/atlas_icons.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:share_plus/share_plus.dart';

// Attachment item UI
class AttachmentItem extends ConsumerWidget {
Expand Down Expand Up @@ -167,11 +165,7 @@ class AttachmentItem extends ConsumerWidget {
}
// If attachment is downloaded and file or others
else {
if (isDesktop) {
downloadFile(context, mediaState.mediaFile!);
} else {
Share.shareXFiles([XFile(mediaState.mediaFile!.path)]);
}
openFileShareDialog(context: context, file: mediaState.mediaFile!);
}
}
}
Expand Down
19 changes: 6 additions & 13 deletions app/lib/features/attachments/widgets/views/file_view.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import 'package:acter/common/models/attachment_media_state/attachment_media_state.dart';
import 'package:acter/common/themes/app_theme.dart';
import 'package:acter/common/widgets/download_button.dart';
import 'package:acter/features/attachments/providers/attachment_providers.dart';
import 'package:acter/features/files/actions/file_share.dart';
import 'package:acter_flutter_sdk/acter_flutter_sdk_ffi.dart' show Attachment;
import 'package:flutter/material.dart';
import 'package:flutter_chat_ui/flutter_chat_ui.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:share_plus/share_plus.dart';

class FileView extends ConsumerWidget {
final Attachment attachment;
Expand Down Expand Up @@ -47,11 +45,7 @@ class FileView extends ConsumerWidget {
return InkWell(
onTap: () async {
if (mediaState.mediaFile != null) {
if (isDesktop) {
downloadFile(context, mediaState.mediaFile!);
} else {
Share.shareXFiles([XFile(mediaState.mediaFile!.path)]);
}
openFileShareDialog(context: context, file: mediaState.mediaFile!);
} else {
ref
.read(attachmentMediaStateProvider(attachment).notifier)
Expand Down Expand Up @@ -97,11 +91,10 @@ class FileView extends ConsumerWidget {
return InkWell(
onTap: openView!
? () async {
if (isDesktop) {
downloadFile(context, mediaState.mediaFile!);
} else {
Share.shareXFiles([XFile(mediaState.mediaFile!.path)]);
}
openFileShareDialog(
context: context,
file: mediaState.mediaFile!,
);
}
: null,
child: ClipRRect(
Expand Down
10 changes: 2 additions & 8 deletions app/lib/features/chat/widgets/file_message_builder.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import 'package:acter/common/models/types.dart';
import 'package:acter/common/themes/app_theme.dart';
import 'package:acter/common/widgets/download_button.dart';
import 'package:acter/features/chat/models/media_chat_state/media_chat_state.dart';
import 'package:acter/features/chat/providers/chat_providers.dart';
import 'package:acter/features/files/actions/file_share.dart';
import 'package:atlas_icons/atlas_icons.dart';
import 'package:flutter/material.dart';
import 'package:flutter_chat_types/flutter_chat_types.dart' as types;
import 'package:flutter_chat_ui/flutter_chat_ui.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:share_plus/share_plus.dart';

class FileMessageBuilder extends ConsumerWidget {
final types.FileMessage message;
Expand All @@ -31,11 +29,7 @@ class FileMessageBuilder extends ConsumerWidget {
return InkWell(
onTap: () async {
if (mediaState.mediaFile != null) {
if (isDesktop) {
downloadFile(context, mediaState.mediaFile!);
} else {
Share.shareXFiles([XFile(mediaState.mediaFile!.path)]);
}
openFileShareDialog(context: context, file: mediaState.mediaFile!);
} else {
await ref
.read(mediaChatStateProvider(messageInfo).notifier)
Expand Down
53 changes: 16 additions & 37 deletions app/lib/features/events/pages/event_details_page.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'dart:io';

import 'package:acter/common/actions/redact_content.dart';
import 'package:acter/common/actions/report_content.dart';
import 'package:acter/common/providers/common_providers.dart';
import 'package:acter/common/providers/room_providers.dart';
import 'package:acter/common/themes/app_theme.dart';
import 'package:acter/common/utils/utils.dart';
import 'package:acter/features/events/actions/get_event_type.dart';
import 'package:acter/features/events/widgets/change_date_sheet.dart';
Expand All @@ -19,14 +20,14 @@ import 'package:acter/features/events/utils/events_utils.dart';
import 'package:acter/features/events/widgets/event_date_widget.dart';
import 'package:acter/features/events/widgets/participants_list.dart';
import 'package:acter/features/events/widgets/skeletons/event_details_skeleton_widget.dart';
import 'package:acter/features/files/actions/file_share.dart';
import 'package:acter/features/home/providers/client_providers.dart';
import 'package:acter/features/home/widgets/space_chip.dart';
import 'package:acter/features/space/widgets/member_avatar.dart';
import 'package:acter_avatar/acter_avatar.dart';
import 'package:acter_flutter_sdk/acter_flutter_sdk.dart';
import 'package:acter_flutter_sdk/acter_flutter_sdk_ffi.dart';
import 'package:atlas_icons/atlas_icons.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
Expand All @@ -35,7 +36,7 @@ import 'package:jiffy/jiffy.dart';
import 'package:logging/logging.dart';
import 'package:path/path.dart' show join;
import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart';
import 'package:phosphor_flutter/phosphor_flutter.dart';

final _log = Logger('a3::event::details');

Expand Down Expand Up @@ -419,51 +420,29 @@ class _EventDetailPageConsumerState extends ConsumerState<EventDetailPage> {
}

Widget _buildShareAction(CalendarEvent calendarEvent) {
return PopupMenuButton(
icon: const Icon(Icons.share),
itemBuilder: (context) => [
PopupMenuItem(
onTap: () => onShareEvent(calendarEvent),
child: Row(
children: <Widget>[
const Icon(Icons.share),
const SizedBox(width: 10),
Text(L10n.of(context).shareIcal),
],
),
),
],
return IconButton(
icon: PhosphorIcon(PhosphorIcons.shareFat()),
onPressed: () => onShareEvent(calendarEvent),
);
}

Future<void> onShareEvent(CalendarEvent event) async {
try {
final filename = event.title().replaceAll(RegExp(r'[^A-Za-z0-9_-]'), '_');

if (isDesktop) {
String? outputFile = await FilePicker.platform.saveFile(
dialogTitle: 'Please select where to store the file',
fileName: '$filename.ics',
);

if (outputFile != null) {
// User canceled the picker
event.icalForSharing(outputFile);
EasyLoading.showToast('File saved to $outputFile');
}
return;
}

final tempDir = await getTemporaryDirectory();
final icalPath = join(tempDir.path, '$filename.ics');
event.icalForSharing(icalPath);

await Share.shareXFiles([
XFile(
icalPath,
if (context.mounted) {
await openFileShareDialog(
// ignore: use_build_context_synchronously
context: context,
// ignore: use_build_context_synchronously
header: Text(L10n.of(context).shareIcal),
file: File(icalPath),
mimeType: 'text/calendar',
),
]);
);
}
} catch (error, stack) {
_log.severe('Creating iCal Share Event failed:', error, stack);
// ignore: use_build_context_synchronously
Expand Down
24 changes: 24 additions & 0 deletions app/lib/features/files/actions/download_file.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'dart:io';
import 'package:path/path.dart';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

Future<bool> downloadFile(BuildContext context, File file) async {
final lang = L10n.of(context);
final filename = basename(file.path);
String? outputFile = await FilePicker.platform.saveFile(
dialogTitle: lang.downloadFileDialogTitle,
fileName: filename,
);

if (outputFile == null) {
return false;
}

await file.copy(outputFile);
EasyLoading.showToast(lang.downloadFileSuccess(outputFile));
return true;
}
Loading

0 comments on commit 0db59a3

Please sign in to comment.