Refactor
This commit is contained in:
12
lib/features/proxy/data/proxy_data_providers.dart
Normal file
12
lib/features/proxy/data/proxy_data_providers.dart
Normal file
@@ -0,0 +1,12 @@
|
||||
import 'package:hiddify/features/proxy/data/proxy_repository.dart';
|
||||
import 'package:hiddify/singbox/service/singbox_service_provider.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
part 'proxy_data_providers.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
ProxyRepository proxyRepository(ProxyRepositoryRef ref) {
|
||||
return ProxyRepositoryImpl(
|
||||
singbox: ref.watch(singboxServiceProvider),
|
||||
);
|
||||
}
|
||||
78
lib/features/proxy/data/proxy_repository.dart
Normal file
78
lib/features/proxy/data/proxy_repository.dart
Normal file
@@ -0,0 +1,78 @@
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:hiddify/core/utils/exception_handler.dart';
|
||||
import 'package:hiddify/features/proxy/model/proxy_entity.dart';
|
||||
import 'package:hiddify/features/proxy/model/proxy_failure.dart';
|
||||
import 'package:hiddify/singbox/service/singbox_service.dart';
|
||||
import 'package:hiddify/utils/custom_loggers.dart';
|
||||
|
||||
abstract interface class ProxyRepository {
|
||||
Stream<Either<ProxyFailure, List<ProxyGroupEntity>>> watchProxies();
|
||||
TaskEither<ProxyFailure, Unit> selectProxy(
|
||||
String groupTag,
|
||||
String outboundTag,
|
||||
);
|
||||
TaskEither<ProxyFailure, Unit> urlTest(String groupTag);
|
||||
}
|
||||
|
||||
class ProxyRepositoryImpl
|
||||
with ExceptionHandler, InfraLogger
|
||||
implements ProxyRepository {
|
||||
ProxyRepositoryImpl({required this.singbox});
|
||||
|
||||
final SingboxService singbox;
|
||||
|
||||
@override
|
||||
Stream<Either<ProxyFailure, List<ProxyGroupEntity>>> watchProxies() {
|
||||
return singbox.watchOutbounds().map((event) {
|
||||
final groupWithSelected = {
|
||||
for (final group in event) group.tag: group.selected,
|
||||
};
|
||||
return event
|
||||
.map(
|
||||
(e) => ProxyGroupEntity(
|
||||
tag: e.tag,
|
||||
type: e.type,
|
||||
selected: e.selected,
|
||||
items: e.items
|
||||
.map(
|
||||
(e) => ProxyItemEntity(
|
||||
tag: e.tag,
|
||||
type: e.type,
|
||||
urlTestDelay: e.urlTestDelay,
|
||||
selectedTag: groupWithSelected[e.tag],
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
}).handleExceptions(
|
||||
(error, stackTrace) {
|
||||
loggy.error("error watching proxies", error, stackTrace);
|
||||
return ProxyUnexpectedFailure(error, stackTrace);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<ProxyFailure, Unit> selectProxy(
|
||||
String groupTag,
|
||||
String outboundTag,
|
||||
) {
|
||||
return exceptionHandler(
|
||||
() => singbox
|
||||
.selectOutbound(groupTag, outboundTag)
|
||||
.mapLeft(ProxyUnexpectedFailure.new)
|
||||
.run(),
|
||||
ProxyUnexpectedFailure.new,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<ProxyFailure, Unit> urlTest(String groupTag) {
|
||||
return exceptionHandler(
|
||||
() => singbox.urlTest(groupTag).mapLeft(ProxyUnexpectedFailure.new).run(),
|
||||
ProxyUnexpectedFailure.new,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user