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/native/autostart_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/cross_file_converters.dart';
|
||||
import 'package:localsend_app/util/native/device_info_helper.dart';
|
||||
@@ -188,13 +187,7 @@ Future<RefenaContainer> preInit(List<String> args) async {
|
||||
}),
|
||||
);
|
||||
|
||||
await container
|
||||
.redux(parentIsolateProvider)
|
||||
.dispatchAsync(
|
||||
IsolateSetupAction(
|
||||
uriContentStreamResolver: AndroidUriContentStreamResolver(),
|
||||
),
|
||||
);
|
||||
await container.redux(parentIsolateProvider).dispatchAsync(IsolateSetupAction());
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
@@ -14,8 +14,7 @@ export 'package:localsend_app/isolate/src/isolate/child/upload_isolate.dart'
|
||||
HttpUploadFileFailedEvent,
|
||||
HttpUploadFileFinishedEvent,
|
||||
HttpUploadFileProgressEvent,
|
||||
HttpUploadFileStartedEvent,
|
||||
UriContentStreamResolver;
|
||||
HttpUploadFileStartedEvent;
|
||||
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/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/model/device.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 {}
|
||||
|
||||
class HttpUploadSetContentStreamResolverTask implements BaseHttpUploadTask {
|
||||
final UriContentStreamResolver resolver;
|
||||
|
||||
HttpUploadSetContentStreamResolverTask({
|
||||
required this.resolver,
|
||||
});
|
||||
}
|
||||
|
||||
class HttpUploadFile {
|
||||
final String remoteFileToken;
|
||||
final String fileId;
|
||||
@@ -102,15 +93,6 @@ class HttpUploadFileFailedEvent extends HttpUploadEvent {
|
||||
/// Task ID -> CancelToken
|
||||
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(
|
||||
Stream<SendToIsolateData<IsolateTask<BaseHttpUploadTask>>> receiveFromMain,
|
||||
void Function(IsolateTaskStreamResult<HttpUploadEvent>) sendToMain,
|
||||
@@ -121,15 +103,16 @@ Future<void> setupHttpUploadIsolate(
|
||||
receiveFromMain: receiveFromMain,
|
||||
sendToMain: sendToMain,
|
||||
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 {
|
||||
final HttpUploadFilesTask uploadTask;
|
||||
switch (task.data) {
|
||||
case HttpUploadSetContentStreamResolverTask task:
|
||||
final rootIsolateToken = ref.read(syncProvider).rootIsolateToken;
|
||||
task.resolver.init(
|
||||
rootIsolateToken: rootIsolateToken,
|
||||
);
|
||||
return;
|
||||
case HttpUploadFilesTask task:
|
||||
uploadTask = task;
|
||||
break;
|
||||
|
||||
@@ -64,13 +64,6 @@ class IsolateController extends ReduxNotifier<ParentIsolateState> {
|
||||
/// Starts the required isolates.
|
||||
/// Should be called by the main isolate.
|
||||
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
|
||||
Future<ParentIsolateState> reduce() async {
|
||||
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(
|
||||
httpScanDiscovery: httpScanDiscovery,
|
||||
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 {
|
||||
/// Converts
|
||||
/// content://com.android.externalstorage.documents/tree/primary%3ADocuments
|
||||
@@ -88,18 +84,3 @@ class ContentUriHelper {
|
||||
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