mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-18 22:33:42 +00:00
feat: new ui (#1455)
This commit is contained in:
742 files changed
+59951
-5934
No files matched your search
@@ -0,0 +1,170 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../api/api.dart' as api;
|
||||
import '../../api/model/create_task.dart';
|
||||
import '../../api/model/create_task_batch.dart';
|
||||
import '../../api/model/downloader_config.dart';
|
||||
import '../../api/model/extension.dart';
|
||||
import '../../api/model/install_extension.dart';
|
||||
import '../../api/model/resolve_result.dart';
|
||||
import '../../api/model/resolve_task.dart';
|
||||
import '../../api/model/switch_extension.dart';
|
||||
import '../../api/model/task.dart';
|
||||
import '../../api/model/update_check_extension_resp.dart';
|
||||
import '../../api/model/update_extension_settings.dart';
|
||||
import '../../database/database.dart';
|
||||
import '../window/app_window_appearance.dart';
|
||||
import 'capability_rpc.dart';
|
||||
import 'gopeed_capability.dart';
|
||||
import 'storage_capability.dart';
|
||||
|
||||
class AppCapabilities {
|
||||
AppCapabilities(CapabilityInvoker invoker) : gopeed = GopeedService(invoker), storage = AppStorageService(invoker);
|
||||
|
||||
final GopeedService gopeed;
|
||||
final AppStorageService storage;
|
||||
}
|
||||
|
||||
class LocalAppCapabilities {
|
||||
LocalAppCapabilities._() : codecs = createAppCapabilityCodecs() {
|
||||
registry = CapabilityRegistry(codecs);
|
||||
_bindGopeed(registry);
|
||||
_bindStorage(registry);
|
||||
capabilities = AppCapabilities(LocalCapabilityInvoker(registry));
|
||||
}
|
||||
|
||||
static final instance = LocalAppCapabilities._();
|
||||
|
||||
final RpcCodecRegistry codecs;
|
||||
late CapabilityRegistry registry;
|
||||
late AppCapabilities capabilities;
|
||||
}
|
||||
|
||||
final appCapabilitiesProvider = Provider<AppCapabilities>((ref) => LocalAppCapabilities.instance.capabilities);
|
||||
final gopeedServiceProvider = Provider<GopeedService>((ref) => ref.watch(appCapabilitiesProvider).gopeed);
|
||||
final appStorageServiceProvider = Provider<AppStorageService>((ref) => ref.watch(appCapabilitiesProvider).storage);
|
||||
|
||||
RpcCodecRegistry createAppCapabilityCodecs() {
|
||||
final codecs = RpcCodecRegistry();
|
||||
codecs
|
||||
..register<AppWindowAppearance>((json) => AppWindowAppearance.fromJson(json! as Map<String, dynamic>))
|
||||
..register<ResolveTask>((json) => ResolveTask.fromJson(json! as Map<String, dynamic>))
|
||||
..register<ResolveResult>((json) => ResolveResult.fromJson(json! as Map<String, dynamic>))
|
||||
..register<CreateTask>((json) => CreateTask.fromJson(json! as Map<String, dynamic>))
|
||||
..register<CreateTaskBatch>((json) => CreateTaskBatch.fromJson(json! as Map<String, dynamic>))
|
||||
..register<DownloaderConfig>((json) => DownloaderConfig.fromJson(json! as Map<String, dynamic>))
|
||||
..register<TaskRuntimeStatus>((json) => TaskRuntimeStatus.fromJson(json! as Map<String, dynamic>))
|
||||
..register<List<Task>>(
|
||||
(json) =>
|
||||
(json! as List<dynamic>).map((item) => Task.fromJson(item as Map<String, dynamic>)).toList(growable: false),
|
||||
)
|
||||
..register<InstallExtension>((json) => InstallExtension.fromJson(json! as Map<String, dynamic>))
|
||||
..register<UpdateExtensionSettings>((json) => UpdateExtensionSettings.fromJson(json! as Map<String, dynamic>))
|
||||
..register<SwitchExtension>((json) => SwitchExtension.fromJson(json! as Map<String, dynamic>))
|
||||
..register<List<Extension>>(
|
||||
(json) => (json! as List<dynamic>)
|
||||
.map((item) => Extension.fromJson(item as Map<String, dynamic>))
|
||||
.toList(growable: false),
|
||||
)
|
||||
..register<UpdateCheckExtensionResp>((json) => UpdateCheckExtensionResp.fromJson(json! as Map<String, dynamic>));
|
||||
return codecs;
|
||||
}
|
||||
|
||||
void _bindGopeed(CapabilityRegistry registry) {
|
||||
registry
|
||||
..bind(GopeedMethods.resolve, api.resolve)
|
||||
..bind(GopeedMethods.createTask, api.createTask)
|
||||
..bind(GopeedMethods.createTaskBatch, api.createTaskBatch)
|
||||
..bind(GopeedMethods.patchTask, (params) async {
|
||||
await api.patchTask(params['id'] as String, ResolveTask.fromJson(params['request'] as Map<String, dynamic>));
|
||||
return const RpcUnit();
|
||||
})
|
||||
..bind(
|
||||
GopeedMethods.getTasks,
|
||||
(statuses) => api.getTasks(statuses.map((name) => Status.values.byName(name)).toList(growable: false)),
|
||||
)
|
||||
..bind(GopeedMethods.getTaskStatus, api.getTaskStatus)
|
||||
..bind(GopeedMethods.getTaskStats, api.getTaskStats)
|
||||
..bind(GopeedMethods.pauseTask, (id) async {
|
||||
await api.pauseTask(id);
|
||||
return const RpcUnit();
|
||||
})
|
||||
..bind(GopeedMethods.continueTask, (id) async {
|
||||
await api.continueTask(id);
|
||||
return const RpcUnit();
|
||||
})
|
||||
..bind(GopeedMethods.pauseTasks, (params) async {
|
||||
await api.pauseAllTasks(_optionalStringList(params['ids']));
|
||||
return const RpcUnit();
|
||||
})
|
||||
..bind(GopeedMethods.continueTasks, (params) async {
|
||||
await api.continueAllTasks(_optionalStringList(params['ids']));
|
||||
return const RpcUnit();
|
||||
})
|
||||
..bind(GopeedMethods.deleteTask, (params) async {
|
||||
await api.deleteTask(params['id'] as String, params['force'] as bool? ?? false);
|
||||
return const RpcUnit();
|
||||
})
|
||||
..bind(GopeedMethods.deleteTasks, (params) async {
|
||||
await api.deleteTasks(_optionalStringList(params['ids']), params['force'] as bool? ?? false);
|
||||
return const RpcUnit();
|
||||
})
|
||||
..bind(GopeedMethods.getConfig, (_) => api.getConfig())
|
||||
..bind(GopeedMethods.putConfig, (config) async {
|
||||
await api.putConfig(config);
|
||||
return const RpcUnit();
|
||||
})
|
||||
..bind(GopeedMethods.installExtension, api.installExtension)
|
||||
..bind(GopeedMethods.getExtensions, (_) => api.getExtensions())
|
||||
..bind(GopeedMethods.updateExtensionSettings, (params) async {
|
||||
await api.updateExtensionSettings(
|
||||
params['identity'] as String,
|
||||
UpdateExtensionSettings.fromJson(params['request'] as Map<String, dynamic>),
|
||||
);
|
||||
return const RpcUnit();
|
||||
})
|
||||
..bind(GopeedMethods.switchExtension, (params) async {
|
||||
await api.switchExtension(
|
||||
params['identity'] as String,
|
||||
SwitchExtension.fromJson(params['request'] as Map<String, dynamic>),
|
||||
);
|
||||
return const RpcUnit();
|
||||
})
|
||||
..bind(GopeedMethods.deleteExtension, (identity) async {
|
||||
await api.deleteExtension(identity);
|
||||
return const RpcUnit();
|
||||
})
|
||||
..bind(GopeedMethods.checkExtensionUpdate, api.upgradeCheckExtension)
|
||||
..bind(GopeedMethods.updateExtension, (identity) async {
|
||||
await api.updateExtension(identity);
|
||||
return const RpcUnit();
|
||||
})
|
||||
..bind(GopeedMethods.testWebhook, (url) async {
|
||||
await api.testWebhook(url);
|
||||
return const RpcUnit();
|
||||
});
|
||||
}
|
||||
|
||||
void _bindStorage(CapabilityRegistry registry) {
|
||||
registry
|
||||
..bind(StorageMethods.getCreateHistory, (_) async => Database.instance.getCreateHistory() ?? const <String>[])
|
||||
..bind(StorageMethods.saveCreateHistory, (urls) async {
|
||||
for (final url in urls) {
|
||||
Database.instance.saveCreateHistory(url);
|
||||
}
|
||||
return const RpcUnit();
|
||||
})
|
||||
..bind(StorageMethods.removeCreateHistory, (url) async {
|
||||
Database.instance.removeCreateHistory(url);
|
||||
return const RpcUnit();
|
||||
})
|
||||
..bind(StorageMethods.clearCreateHistory, (_) async {
|
||||
Database.instance.clearCreateHistory();
|
||||
return const RpcUnit();
|
||||
});
|
||||
}
|
||||
|
||||
List<String>? _optionalStringList(Object? value) {
|
||||
if (value == null) return null;
|
||||
return (value as List<dynamic>).map((item) => item.toString()).toList(growable: false);
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
import 'dart:async';
|
||||
|
||||
typedef RpcDecoder<T> = T Function(Object? json);
|
||||
|
||||
class RpcUnit {
|
||||
const RpcUnit();
|
||||
}
|
||||
|
||||
class RpcMethod<P, R> {
|
||||
const RpcMethod(this.name);
|
||||
|
||||
final String name;
|
||||
}
|
||||
|
||||
class CapabilityException implements Exception {
|
||||
const CapabilityException(this.code, this.message, [this.details]);
|
||||
|
||||
final String code;
|
||||
final String message;
|
||||
final Object? details;
|
||||
|
||||
@override
|
||||
String toString() => message;
|
||||
}
|
||||
|
||||
class RpcCodecRegistry {
|
||||
final Map<Type, RpcDecoder<Object?>> _decoders = {};
|
||||
|
||||
void register<T>(RpcDecoder<T> decoder) {
|
||||
_decoders[T] = (json) => decoder(json);
|
||||
}
|
||||
|
||||
Object? encode(Object? value) {
|
||||
if (value == null || value is String || value is num || value is bool) {
|
||||
return value;
|
||||
}
|
||||
if (value is RpcUnit) {
|
||||
return const <String, Object?>{};
|
||||
}
|
||||
if (value is Enum) {
|
||||
return value.name;
|
||||
}
|
||||
if (value is Iterable) {
|
||||
return value.map(encode).toList(growable: false);
|
||||
}
|
||||
if (value is Map) {
|
||||
return {for (final entry in value.entries) entry.key.toString(): encode(entry.value)};
|
||||
}
|
||||
try {
|
||||
return encode((value as dynamic).toJson());
|
||||
} catch (_) {
|
||||
throw CapabilityException('encode_error', 'No RPC encoder registered for ${value.runtimeType}');
|
||||
}
|
||||
}
|
||||
|
||||
T decode<T>(Object? value) {
|
||||
final normalized = _normalize(value);
|
||||
if (T == RpcUnit) {
|
||||
return const RpcUnit() as T;
|
||||
}
|
||||
if (normalized == null) {
|
||||
return null as T;
|
||||
}
|
||||
if (T == List<String>) {
|
||||
return (normalized as List<dynamic>).map((item) => item.toString()).toList(growable: false) as T;
|
||||
}
|
||||
if (normalized is T) {
|
||||
return normalized as T;
|
||||
}
|
||||
final decoder = _decoders[T];
|
||||
if (decoder == null) {
|
||||
throw CapabilityException('decode_error', 'No RPC decoder registered for $T');
|
||||
}
|
||||
return decoder(normalized) as T;
|
||||
}
|
||||
|
||||
Object? _normalize(Object? value) {
|
||||
if (value is Map) {
|
||||
return {for (final entry in value.entries) entry.key.toString(): _normalize(entry.value)};
|
||||
}
|
||||
if (value is List) {
|
||||
return value.map(_normalize).toList(growable: false);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
abstract interface class CapabilityInvoker {
|
||||
Future<R> invoke<P, R>(RpcMethod<P, R> method, P params);
|
||||
}
|
||||
|
||||
typedef _RawCapabilityHandler = Future<Object?> Function(Object? payload);
|
||||
|
||||
class CapabilityRegistry {
|
||||
CapabilityRegistry(this.codecs);
|
||||
|
||||
final RpcCodecRegistry codecs;
|
||||
final Map<String, _RawCapabilityHandler> _handlers = {};
|
||||
final Map<String, Object> _localHandlers = {};
|
||||
|
||||
void bind<P, R>(RpcMethod<P, R> method, FutureOr<R> Function(P params) handler) {
|
||||
if (_handlers.containsKey(method.name)) {
|
||||
throw StateError('Capability already registered: ${method.name}');
|
||||
}
|
||||
_localHandlers[method.name] = handler;
|
||||
_handlers[method.name] = (payload) async {
|
||||
final params = codecs.decode<P>(payload);
|
||||
return codecs.encode(await handler(params));
|
||||
};
|
||||
}
|
||||
|
||||
Future<Object?> invoke(String operation, Object? payload) async {
|
||||
final handler = _handlers[operation];
|
||||
if (handler == null) {
|
||||
throw CapabilityException('method_not_found', 'Capability not found: $operation');
|
||||
}
|
||||
return handler(payload);
|
||||
}
|
||||
|
||||
Future<R> invokeLocal<P, R>(RpcMethod<P, R> method, P params) async {
|
||||
final handler = _localHandlers[method.name];
|
||||
if (handler == null) {
|
||||
throw CapabilityException('method_not_found', 'Capability not found: ${method.name}');
|
||||
}
|
||||
return await (handler as FutureOr<R> Function(P))(params);
|
||||
}
|
||||
}
|
||||
|
||||
class LocalCapabilityInvoker implements CapabilityInvoker {
|
||||
const LocalCapabilityInvoker(this.registry);
|
||||
|
||||
final CapabilityRegistry registry;
|
||||
|
||||
@override
|
||||
Future<R> invoke<P, R>(RpcMethod<P, R> method, P params) async {
|
||||
return registry.invokeLocal(method, params);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
import '../../api/model/create_task.dart';
|
||||
import '../../api/model/create_task_batch.dart';
|
||||
import '../../api/model/downloader_config.dart';
|
||||
import '../../api/model/extension.dart';
|
||||
import '../../api/model/install_extension.dart';
|
||||
import '../../api/model/resolve_result.dart';
|
||||
import '../../api/model/resolve_task.dart';
|
||||
import '../../api/model/switch_extension.dart';
|
||||
import '../../api/model/task.dart';
|
||||
import '../../api/model/update_check_extension_resp.dart';
|
||||
import '../../api/model/update_extension_settings.dart';
|
||||
import 'capability_rpc.dart';
|
||||
|
||||
abstract final class GopeedMethods {
|
||||
static const resolve = RpcMethod<ResolveTask, ResolveResult>('gopeed.resolve');
|
||||
static const createTask = RpcMethod<CreateTask, String>('gopeed.task.create');
|
||||
static const createTaskBatch = RpcMethod<CreateTaskBatch, List<String>>('gopeed.task.createBatch');
|
||||
static const patchTask = RpcMethod<Map<String, dynamic>, RpcUnit>('gopeed.task.patch');
|
||||
static const getTasks = RpcMethod<List<String>, List<Task>>('gopeed.task.list');
|
||||
static const getTaskStatus = RpcMethod<String, TaskRuntimeStatus>('gopeed.task.status');
|
||||
static const getTaskStats = RpcMethod<String, Map<String, dynamic>>('gopeed.task.stats');
|
||||
static const pauseTask = RpcMethod<String, RpcUnit>('gopeed.task.pause');
|
||||
static const continueTask = RpcMethod<String, RpcUnit>('gopeed.task.continue');
|
||||
static const pauseTasks = RpcMethod<Map<String, dynamic>, RpcUnit>('gopeed.task.pauseBatch');
|
||||
static const continueTasks = RpcMethod<Map<String, dynamic>, RpcUnit>('gopeed.task.continueBatch');
|
||||
static const deleteTask = RpcMethod<Map<String, dynamic>, RpcUnit>('gopeed.task.delete');
|
||||
static const deleteTasks = RpcMethod<Map<String, dynamic>, RpcUnit>('gopeed.task.deleteBatch');
|
||||
static const getConfig = RpcMethod<RpcUnit, DownloaderConfig>('gopeed.config.get');
|
||||
static const putConfig = RpcMethod<DownloaderConfig, RpcUnit>('gopeed.config.put');
|
||||
static const installExtension = RpcMethod<InstallExtension, String>('gopeed.extension.install');
|
||||
static const getExtensions = RpcMethod<RpcUnit, List<Extension>>('gopeed.extension.list');
|
||||
static const updateExtensionSettings = RpcMethod<Map<String, dynamic>, RpcUnit>('gopeed.extension.updateSettings');
|
||||
static const switchExtension = RpcMethod<Map<String, dynamic>, RpcUnit>('gopeed.extension.switch');
|
||||
static const deleteExtension = RpcMethod<String, RpcUnit>('gopeed.extension.delete');
|
||||
static const checkExtensionUpdate = RpcMethod<String, UpdateCheckExtensionResp>('gopeed.extension.checkUpdate');
|
||||
static const updateExtension = RpcMethod<String, RpcUnit>('gopeed.extension.update');
|
||||
static const testWebhook = RpcMethod<String, RpcUnit>('gopeed.webhook.test');
|
||||
}
|
||||
|
||||
class GopeedService {
|
||||
const GopeedService(this._invoker);
|
||||
|
||||
final CapabilityInvoker _invoker;
|
||||
|
||||
Future<ResolveResult> resolve(ResolveTask request) => _invoker.invoke(GopeedMethods.resolve, request);
|
||||
|
||||
Future<String> createTask(CreateTask request) => _invoker.invoke(GopeedMethods.createTask, request);
|
||||
|
||||
Future<List<String>> createTaskBatch(CreateTaskBatch request) =>
|
||||
_invoker.invoke(GopeedMethods.createTaskBatch, request);
|
||||
|
||||
Future<void> patchTask(String id, ResolveTask request) async {
|
||||
await _invoker.invoke(GopeedMethods.patchTask, {'id': id, 'request': request.toJson()});
|
||||
}
|
||||
|
||||
Future<List<Task>> getTasks(List<Status> statuses) =>
|
||||
_invoker.invoke(GopeedMethods.getTasks, statuses.map((status) => status.name).toList(growable: false));
|
||||
|
||||
Future<TaskRuntimeStatus> getTaskStatus(String id) => _invoker.invoke(GopeedMethods.getTaskStatus, id);
|
||||
|
||||
Future<Map<String, dynamic>> getTaskStats(String id) => _invoker.invoke(GopeedMethods.getTaskStats, id);
|
||||
|
||||
Future<void> pauseTask(String id) async => _invoker.invoke(GopeedMethods.pauseTask, id);
|
||||
|
||||
Future<void> continueTask(String id) async => _invoker.invoke(GopeedMethods.continueTask, id);
|
||||
|
||||
Future<void> pauseAllTasks(List<String>? ids) async {
|
||||
await _invoker.invoke(GopeedMethods.pauseTasks, {'ids': ids});
|
||||
}
|
||||
|
||||
Future<void> continueAllTasks(List<String>? ids) async {
|
||||
await _invoker.invoke(GopeedMethods.continueTasks, {'ids': ids});
|
||||
}
|
||||
|
||||
Future<void> deleteTask(String id, bool force) async {
|
||||
await _invoker.invoke(GopeedMethods.deleteTask, {'id': id, 'force': force});
|
||||
}
|
||||
|
||||
Future<void> deleteTasks(List<String>? ids, bool force) async {
|
||||
await _invoker.invoke(GopeedMethods.deleteTasks, {'ids': ids, 'force': force});
|
||||
}
|
||||
|
||||
Future<DownloaderConfig> getConfig() => _invoker.invoke(GopeedMethods.getConfig, const RpcUnit());
|
||||
|
||||
Future<void> putConfig(DownloaderConfig config) async => _invoker.invoke(GopeedMethods.putConfig, config);
|
||||
|
||||
Future<String> installExtension(InstallExtension request) => _invoker.invoke(GopeedMethods.installExtension, request);
|
||||
|
||||
Future<List<Extension>> getExtensions() => _invoker.invoke(GopeedMethods.getExtensions, const RpcUnit());
|
||||
|
||||
Future<void> updateExtensionSettings(String identity, UpdateExtensionSettings request) async {
|
||||
await _invoker.invoke(GopeedMethods.updateExtensionSettings, {'identity': identity, 'request': request.toJson()});
|
||||
}
|
||||
|
||||
Future<void> switchExtension(String identity, SwitchExtension request) async {
|
||||
await _invoker.invoke(GopeedMethods.switchExtension, {'identity': identity, 'request': request.toJson()});
|
||||
}
|
||||
|
||||
Future<void> deleteExtension(String identity) async => _invoker.invoke(GopeedMethods.deleteExtension, identity);
|
||||
|
||||
Future<UpdateCheckExtensionResp> upgradeCheckExtension(String identity) =>
|
||||
_invoker.invoke(GopeedMethods.checkExtensionUpdate, identity);
|
||||
|
||||
Future<void> updateExtension(String identity) async => _invoker.invoke(GopeedMethods.updateExtension, identity);
|
||||
|
||||
Future<void> testWebhook(String url) async => _invoker.invoke(GopeedMethods.testWebhook, url);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import 'capability_rpc.dart';
|
||||
|
||||
abstract final class StorageMethods {
|
||||
static const getCreateHistory = RpcMethod<RpcUnit, List<String>>('storage.createHistory.get');
|
||||
static const saveCreateHistory = RpcMethod<List<String>, RpcUnit>('storage.createHistory.save');
|
||||
static const removeCreateHistory = RpcMethod<String, RpcUnit>('storage.createHistory.remove');
|
||||
static const clearCreateHistory = RpcMethod<RpcUnit, RpcUnit>('storage.createHistory.clear');
|
||||
}
|
||||
|
||||
class AppStorageService {
|
||||
const AppStorageService(this._invoker);
|
||||
|
||||
final CapabilityInvoker _invoker;
|
||||
|
||||
Future<List<String>> getCreateHistory() => _invoker.invoke(StorageMethods.getCreateHistory, const RpcUnit());
|
||||
|
||||
Future<void> saveCreateHistory(List<String> urls) async {
|
||||
if (urls.isEmpty) return;
|
||||
await _invoker.invoke(StorageMethods.saveCreateHistory, urls);
|
||||
}
|
||||
|
||||
Future<void> removeCreateHistory(String url) async {
|
||||
await _invoker.invoke(StorageMethods.removeCreateHistory, url);
|
||||
}
|
||||
|
||||
Future<void> clearCreateHistory() async {
|
||||
await _invoker.invoke(StorageMethods.clearCreateHistory, const RpcUnit());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user