import 'dart:async'; import 'package:fpdart/fpdart.dart'; import 'package:hiddify/data/repository/exception_handlers.dart'; import 'package:hiddify/domain/clash/clash.dart'; import 'package:hiddify/domain/constants.dart'; import 'package:hiddify/services/clash/clash.dart'; import 'package:hiddify/services/files_editor_service.dart'; import 'package:hiddify/utils/utils.dart'; class ClashFacadeImpl with ExceptionHandler, InfraLogger implements ClashFacade { ClashFacadeImpl({ required ClashService clashService, required FilesEditorService filesEditor, }) : _clash = clashService, _filesEditor = filesEditor; final ClashService _clash; final FilesEditorService _filesEditor; @override TaskEither getConfigs() { return exceptionHandler( () async => _clash.getConfigs().mapLeft(ClashFailure.core).run(), ClashFailure.unexpected, ); } @override TaskEither validateConfig(String configFileName) { return exceptionHandler( () async { final path = _filesEditor.configPath(configFileName); return _clash.validateConfig(path).mapLeft(ClashFailure.core).run(); }, ClashFailure.unexpected, ); } @override TaskEither changeConfigs(String configFileName) { return exceptionHandler( () async { loggy.debug("changing config, file name: [$configFileName]"); final path = _filesEditor.configPath(configFileName); return _clash.updateConfigs(path).mapLeft(ClashFailure.core).run(); }, ClashFailure.unexpected, ); } @override TaskEither patchOverrides(ClashConfig overrides) { return exceptionHandler( () async => _clash.patchConfigs(overrides).mapLeft(ClashFailure.core).run(), ClashFailure.unexpected, ); } @override TaskEither> getProxies() { return exceptionHandler( () async => _clash.getProxies().mapLeft(ClashFailure.core).run(), ClashFailure.unexpected, ); } @override TaskEither changeProxy( String selectorName, String proxyName, ) { return exceptionHandler( () async => _clash .changeProxy(selectorName, proxyName) .mapLeft(ClashFailure.core) .run(), ClashFailure.unexpected, ); } @override TaskEither getTraffic() { return exceptionHandler( () async => _clash.getTraffic().mapLeft(ClashFailure.core).run(), ClashFailure.unexpected, ); } @override TaskEither testDelay( String proxyName, { String testUrl = Constants.delayTestUrl, }) { return exceptionHandler( () async { final result = _clash .getProxyDelay(proxyName, testUrl) .mapLeft(ClashFailure.core) .run(); return result; }, ClashFailure.unexpected, ); } @override Stream> watchLogs() { return _clash .watchLogs(LogLevel.info) .handleExceptions(ClashFailure.unexpected); } }