Refactor
This commit is contained in:
10
lib/features/stats/data/stats_data_providers.dart
Normal file
10
lib/features/stats/data/stats_data_providers.dart
Normal file
@@ -0,0 +1,10 @@
|
||||
import 'package:hiddify/features/stats/data/stats_repository.dart';
|
||||
import 'package:hiddify/singbox/service/singbox_service_provider.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
part 'stats_data_providers.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
StatsRepository statsRepository(StatsRepositoryRef ref) {
|
||||
return StatsRepositoryImpl(singbox: ref.watch(singboxServiceProvider));
|
||||
}
|
||||
33
lib/features/stats/data/stats_repository.dart
Normal file
33
lib/features/stats/data/stats_repository.dart
Normal file
@@ -0,0 +1,33 @@
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:hiddify/core/utils/exception_handler.dart';
|
||||
import 'package:hiddify/features/stats/model/stats_entity.dart';
|
||||
import 'package:hiddify/features/stats/model/stats_failure.dart';
|
||||
import 'package:hiddify/singbox/service/singbox_service.dart';
|
||||
import 'package:hiddify/utils/custom_loggers.dart';
|
||||
|
||||
abstract interface class StatsRepository {
|
||||
Stream<Either<StatsFailure, StatsEntity>> watchStats();
|
||||
}
|
||||
|
||||
class StatsRepositoryImpl
|
||||
with ExceptionHandler, InfraLogger
|
||||
implements StatsRepository {
|
||||
StatsRepositoryImpl({required this.singbox});
|
||||
|
||||
final SingboxService singbox;
|
||||
|
||||
@override
|
||||
Stream<Either<StatsFailure, StatsEntity>> watchStats() {
|
||||
return singbox
|
||||
.watchStats()
|
||||
.map(
|
||||
(event) => StatsEntity(
|
||||
uplink: event.uplink,
|
||||
downlink: event.downlink,
|
||||
uplinkTotal: event.downlink,
|
||||
downlinkTotal: event.downlinkTotal,
|
||||
),
|
||||
)
|
||||
.handleExceptions(StatsUnexpectedFailure.new);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user