mirror of
https://github.com/localsend/localsend.git
synced 2026-08-07 07:14:52 +00:00
feat: simplify method channel setup in isolate
This commit is contained in:
@@ -37,7 +37,6 @@ import 'package:localsend_app/rust/frb_generated.dart';
|
|||||||
import 'package:localsend_app/util/i18n.dart';
|
import 'package:localsend_app/util/i18n.dart';
|
||||||
import 'package:localsend_app/util/native/autostart_helper.dart';
|
import 'package:localsend_app/util/native/autostart_helper.dart';
|
||||||
import 'package:localsend_app/util/native/cache_helper.dart';
|
import 'package:localsend_app/util/native/cache_helper.dart';
|
||||||
import 'package:localsend_app/util/native/content_uri_helper.dart';
|
|
||||||
import 'package:localsend_app/util/native/context_menu_helper.dart';
|
import 'package:localsend_app/util/native/context_menu_helper.dart';
|
||||||
import 'package:localsend_app/util/native/cross_file_converters.dart';
|
import 'package:localsend_app/util/native/cross_file_converters.dart';
|
||||||
import 'package:localsend_app/util/native/device_info_helper.dart';
|
import 'package:localsend_app/util/native/device_info_helper.dart';
|
||||||
@@ -188,13 +187,7 @@ Future<RefenaContainer> preInit(List<String> args) async {
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
await container
|
await container.redux(parentIsolateProvider).dispatchAsync(IsolateSetupAction());
|
||||||
.redux(parentIsolateProvider)
|
|
||||||
.dispatchAsync(
|
|
||||||
IsolateSetupAction(
|
|
||||||
uriContentStreamResolver: AndroidUriContentStreamResolver(),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
return container;
|
return container;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,8 +14,7 @@ export 'package:localsend_app/isolate/src/isolate/child/upload_isolate.dart'
|
|||||||
HttpUploadFileFailedEvent,
|
HttpUploadFileFailedEvent,
|
||||||
HttpUploadFileFinishedEvent,
|
HttpUploadFileFinishedEvent,
|
||||||
HttpUploadFileProgressEvent,
|
HttpUploadFileProgressEvent,
|
||||||
HttpUploadFileStartedEvent,
|
HttpUploadFileStartedEvent;
|
||||||
UriContentStreamResolver;
|
|
||||||
export 'package:localsend_app/isolate/src/isolate/parent/actions.dart';
|
export 'package:localsend_app/isolate/src/isolate/parent/actions.dart';
|
||||||
export 'package:localsend_app/isolate/src/isolate/parent/actions_sync.dart';
|
export 'package:localsend_app/isolate/src/isolate/parent/actions_sync.dart';
|
||||||
export 'package:localsend_app/isolate/src/isolate/parent/parent_isolate_provider.dart';
|
export 'package:localsend_app/isolate/src/isolate/parent/parent_isolate_provider.dart';
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import 'dart:typed_data';
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
import 'package:localsend_app/isolate/isolate.dart';
|
import 'package:localsend_app/isolate/isolate.dart';
|
||||||
import 'package:localsend_app/isolate/model/device.dart';
|
import 'package:localsend_app/isolate/model/device.dart';
|
||||||
import 'package:localsend_app/isolate/src/isolate/child/main.dart';
|
import 'package:localsend_app/isolate/src/isolate/child/main.dart';
|
||||||
@@ -13,14 +12,6 @@ import 'package:typed_isolates/typed_isolates.dart';
|
|||||||
|
|
||||||
sealed class BaseHttpUploadTask {}
|
sealed class BaseHttpUploadTask {}
|
||||||
|
|
||||||
class HttpUploadSetContentStreamResolverTask implements BaseHttpUploadTask {
|
|
||||||
final UriContentStreamResolver resolver;
|
|
||||||
|
|
||||||
HttpUploadSetContentStreamResolverTask({
|
|
||||||
required this.resolver,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
class HttpUploadFile {
|
class HttpUploadFile {
|
||||||
final String remoteFileToken;
|
final String remoteFileToken;
|
||||||
final String fileId;
|
final String fileId;
|
||||||
@@ -102,15 +93,6 @@ class HttpUploadFileFailedEvent extends HttpUploadEvent {
|
|||||||
/// Task ID -> CancelToken
|
/// Task ID -> CancelToken
|
||||||
final _cancelTokenProvider = Provider((ref) => <int, RsCancellationToken>{});
|
final _cancelTokenProvider = Provider((ref) => <int, RsCancellationToken>{});
|
||||||
|
|
||||||
abstract class UriContentStreamResolver {
|
|
||||||
/// Separate initialization method to create instance in the child isolate.
|
|
||||||
/// Cannot reference the RootIsolateToken class because it is not part of Dart.
|
|
||||||
void init({required Object? rootIsolateToken});
|
|
||||||
|
|
||||||
/// Resolves the content stream for the given URI.
|
|
||||||
Stream<Uint8List> resolve(Uri uri);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> setupHttpUploadIsolate(
|
Future<void> setupHttpUploadIsolate(
|
||||||
Stream<SendToIsolateData<IsolateTask<BaseHttpUploadTask>>> receiveFromMain,
|
Stream<SendToIsolateData<IsolateTask<BaseHttpUploadTask>>> receiveFromMain,
|
||||||
void Function(IsolateTaskStreamResult<HttpUploadEvent>) sendToMain,
|
void Function(IsolateTaskStreamResult<HttpUploadEvent>) sendToMain,
|
||||||
@@ -121,15 +103,16 @@ Future<void> setupHttpUploadIsolate(
|
|||||||
receiveFromMain: receiveFromMain,
|
receiveFromMain: receiveFromMain,
|
||||||
sendToMain: sendToMain,
|
sendToMain: sendToMain,
|
||||||
initialData: initialData,
|
initialData: initialData,
|
||||||
|
init: (ref) async {
|
||||||
|
// Initialize the platform method channel so getFileDescriptorAndroid
|
||||||
|
// (used to resolve "content://" files) works inside this isolate.
|
||||||
|
BackgroundIsolateBinaryMessenger.ensureInitialized(
|
||||||
|
ref.read(syncProvider).rootIsolateToken as RootIsolateToken,
|
||||||
|
);
|
||||||
|
},
|
||||||
handler: (ref, task) async {
|
handler: (ref, task) async {
|
||||||
final HttpUploadFilesTask uploadTask;
|
final HttpUploadFilesTask uploadTask;
|
||||||
switch (task.data) {
|
switch (task.data) {
|
||||||
case HttpUploadSetContentStreamResolverTask task:
|
|
||||||
final rootIsolateToken = ref.read(syncProvider).rootIsolateToken;
|
|
||||||
task.resolver.init(
|
|
||||||
rootIsolateToken: rootIsolateToken,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
case HttpUploadFilesTask task:
|
case HttpUploadFilesTask task:
|
||||||
uploadTask = task;
|
uploadTask = task;
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -64,13 +64,6 @@ class IsolateController extends ReduxNotifier<ParentIsolateState> {
|
|||||||
/// Starts the required isolates.
|
/// Starts the required isolates.
|
||||||
/// Should be called by the main isolate.
|
/// Should be called by the main isolate.
|
||||||
class IsolateSetupAction extends AsyncReduxAction<IsolateController, ParentIsolateState> {
|
class IsolateSetupAction extends AsyncReduxAction<IsolateController, ParentIsolateState> {
|
||||||
/// If provided, file paths starting with "content://" will be resolved using this resolver.
|
|
||||||
final UriContentStreamResolver? uriContentStreamResolver;
|
|
||||||
|
|
||||||
IsolateSetupAction({
|
|
||||||
required this.uriContentStreamResolver,
|
|
||||||
});
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<ParentIsolateState> reduce() async {
|
Future<ParentIsolateState> reduce() async {
|
||||||
final httpScanDiscovery =
|
final httpScanDiscovery =
|
||||||
@@ -108,18 +101,6 @@ class IsolateSetupAction extends AsyncReduxAction<IsolateController, ParentIsola
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (uriContentStreamResolver != null) {
|
|
||||||
httpUpload.sendToIsolate(
|
|
||||||
SendToIsolateData(
|
|
||||||
syncState: null,
|
|
||||||
data: IsolateTask(
|
|
||||||
id: -1,
|
|
||||||
data: HttpUploadSetContentStreamResolverTask(resolver: uriContentStreamResolver!),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return state.copyWith(
|
return state.copyWith(
|
||||||
httpScanDiscovery: httpScanDiscovery,
|
httpScanDiscovery: httpScanDiscovery,
|
||||||
multicastDiscovery: multicastDiscovery,
|
multicastDiscovery: multicastDiscovery,
|
||||||
|
|||||||
@@ -1,7 +1,3 @@
|
|||||||
import 'package:flutter/services.dart';
|
|
||||||
import 'package:localsend_app/isolate/isolate.dart';
|
|
||||||
import 'package:uri_content/uri_content.dart';
|
|
||||||
|
|
||||||
class ContentUriHelper {
|
class ContentUriHelper {
|
||||||
/// Converts
|
/// Converts
|
||||||
/// content://com.android.externalstorage.documents/tree/primary%3ADocuments
|
/// content://com.android.externalstorage.documents/tree/primary%3ADocuments
|
||||||
@@ -88,18 +84,3 @@ class ContentUriHelper {
|
|||||||
return uri.substring(0, treeIndex + 6) + encodedPath;
|
return uri.substring(0, treeIndex + 6) + encodedPath;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class AndroidUriContentStreamResolver implements UriContentStreamResolver {
|
|
||||||
UriContent? _uriContent;
|
|
||||||
|
|
||||||
@override
|
|
||||||
void init({required Object? rootIsolateToken}) {
|
|
||||||
BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken as RootIsolateToken);
|
|
||||||
_uriContent = UriContent();
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Stream<Uint8List> resolve(Uri uri) {
|
|
||||||
return _uriContent!.getContentStream(uri);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user