Skip to content

Commit

Permalink
feat(neon_files): Implement sharing file natively
Browse files Browse the repository at this point in the history
Signed-off-by: jld3103 <[email protected]>
  • Loading branch information
provokateurin committed Sep 17, 2023
1 parent 7bc7cc2 commit 7415278
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 9 deletions.
3 changes: 3 additions & 0 deletions packages/neon/neon/lib/src/platform/android.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class AndroidNeonPlatform implements NeonPlatform {
@override
bool get canUseWindowManager => false;

@override
bool get canUseSharing => true;

@override
Future<String> get userAccessibleAppDataPath async {
if (!await Permission.storage.request().isGranted) {
Expand Down
3 changes: 3 additions & 0 deletions packages/neon/neon/lib/src/platform/linux.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class LinuxNeonPlatform implements NeonPlatform {
@override
bool get canUsePushNotifications => false;

@override
bool get canUseSharing => false;

@override
String get userAccessibleAppDataPath => p.join(Platform.environment['HOME']!, 'Neon');

Expand Down
2 changes: 2 additions & 0 deletions packages/neon/neon/lib/src/platform/platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ abstract interface class NeonPlatform {

abstract final bool canUsePushNotifications;

abstract final bool canUseSharing;

FutureOr<String> get userAccessibleAppDataPath;

FutureOr<void> init();
Expand Down
40 changes: 31 additions & 9 deletions packages/neon/neon_files/lib/blocs/files.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ abstract interface class FilesBlocEvents {

void openFile(final List<String> path, final String etag, final String? mimeType);

void shareFileNative(final List<String> path, final String etag);

void delete(final List<String> path);

void rename(final List<String> path, final String name);
Expand Down Expand Up @@ -84,15 +86,8 @@ class FilesBloc extends InteractiveBloc implements FilesBlocEvents, FilesBlocSta
void openFile(final List<String> path, final String etag, final String? mimeType) {
wrapAction(
() async {
final cacheDir = await getApplicationCacheDirectory();
final file = File(p.join(cacheDir.path, 'files', etag.replaceAll('"', ''), path.last));
if (!file.existsSync()) {
debugPrint('Downloading ${Uri(pathSegments: path)} since it does not exist');
if (!file.parent.existsSync()) {
await file.parent.create(recursive: true);
}
await _downloadFile(path, file);
}
final file = await _cacheFile(path, etag);

final result = await OpenFile.open(file.path, type: mimeType);
if (result.type != ResultType.done) {
throw UnableToOpenFileException();
Expand All @@ -102,6 +97,18 @@ class FilesBloc extends InteractiveBloc implements FilesBlocEvents, FilesBlocSta
);
}

@override
void shareFileNative(final List<String> path, final String etag) {
wrapAction(
() async {
final file = await _cacheFile(path, etag);

await Share.shareXFiles([XFile(file.path)]);
},
disableTimeout: true,
);
}

@override
Future<void> refresh() async {
await browser.refresh();
Expand Down Expand Up @@ -164,6 +171,21 @@ class FilesBloc extends InteractiveBloc implements FilesBlocEvents, FilesBlocSta
);
}

Future<File> _cacheFile(final List<String> path, final String etag) async {
final cacheDir = await getApplicationCacheDirectory();
final file = File(p.join(cacheDir.path, 'files', etag.replaceAll('"', ''), path.last));

if (!file.existsSync()) {
debugPrint('Downloading ${Uri(pathSegments: path)} since it does not exist');
if (!file.parent.existsSync()) {
await file.parent.create(recursive: true);
}
await _downloadFile(path, file);
}

return file;
}

Future<void> _downloadFile(
final List<String> path,
final File file,
Expand Down
1 change: 1 addition & 0 deletions packages/neon/neon_files/lib/l10n/en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"actionMove": "Move",
"actionCopy": "Copy",
"actionSync": "Sync",
"actionShare": "Share",
"general": "General",
"goToPath": "Go to /{path}",
"@goToPath": {
Expand Down
6 changes: 6 additions & 0 deletions packages/neon/neon_files/lib/l10n/localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ abstract class AppLocalizations {
/// **'Sync'**
String get actionSync;

/// No description provided for @actionShare.
///
/// In en, this message translates to:
/// **'Share'**
String get actionShare;

/// No description provided for @general.
///
/// In en, this message translates to:
Expand Down
3 changes: 3 additions & 0 deletions packages/neon/neon_files/lib/l10n/localizations_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class AppLocalizationsEn extends AppLocalizations {
@override
String get actionSync => 'Sync';

@override
String get actionShare => 'Share';

@override
String get general => 'General';

Expand Down
1 change: 1 addition & 0 deletions packages/neon/neon_files/lib/neon_files.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import 'package:path_provider/path_provider.dart';
import 'package:provider/provider.dart';
import 'package:queue/queue.dart';
import 'package:rxdart/rxdart.dart';
import 'package:share_plus/share_plus.dart';

part 'blocs/browser.dart';
part 'blocs/files.dart';
Expand Down
10 changes: 10 additions & 0 deletions packages/neon/neon_files/lib/widgets/actions.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart';
import 'package:neon/platform.dart';
import 'package:neon/utils.dart';
import 'package:neon_files/l10n/localizations.dart';
import 'package:neon_files/neon_files.dart';
Expand All @@ -17,6 +18,8 @@ class FileActions extends StatelessWidget {
final bloc = Provider.of<FilesBloc>(context, listen: false);
final browserBloc = bloc.browser;
switch (action) {
case FilesFileAction.share:
bloc.shareFileNative(details.path, details.etag!);
case FilesFileAction.toggleFavorite:
if (details.isFavorite ?? false) {
bloc.removeFavorite(details.path);
Expand Down Expand Up @@ -118,6 +121,12 @@ class FileActions extends StatelessWidget {
@override
Widget build(final BuildContext context) => PopupMenuButton<FilesFileAction>(
itemBuilder: (final context) => [
if (!details.isDirectory && NeonPlatform.instance.canUseSharing) ...[
PopupMenuItem(
value: FilesFileAction.share,
child: Text(AppLocalizations.of(context).actionShare),
),
],
if (details.isFavorite != null) ...[
PopupMenuItem(
value: FilesFileAction.toggleFavorite,
Expand Down Expand Up @@ -161,6 +170,7 @@ class FileActions extends StatelessWidget {
}

enum FilesFileAction {
share,
toggleFavorite,
details,
rename,
Expand Down
1 change: 1 addition & 0 deletions packages/neon/neon_files/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies:
provider: ^6.0.5
queue: ^3.1.0+2
rxdart: ^0.27.7
share_plus: ^4.5.3

dev_dependencies:
build_runner: ^2.4.6
Expand Down

0 comments on commit 7415278

Please sign in to comment.