Refactor
This commit is contained in:
75
lib/features/connection/model/connection_failure.dart
Normal file
75
lib/features/connection/model/connection_failure.dart
Normal file
@@ -0,0 +1,75 @@
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:hiddify/core/localization/translations.dart';
|
||||
import 'package:hiddify/core/model/failures.dart';
|
||||
|
||||
part 'connection_failure.freezed.dart';
|
||||
|
||||
@freezed
|
||||
sealed class ConnectionFailure with _$ConnectionFailure, Failure {
|
||||
const ConnectionFailure._();
|
||||
|
||||
@With<UnexpectedFailure>()
|
||||
const factory ConnectionFailure.unexpected([
|
||||
Object? error,
|
||||
StackTrace? stackTrace,
|
||||
]) = UnexpectedConnectionFailure;
|
||||
|
||||
@With<ExpectedMeasuredFailure>()
|
||||
const factory ConnectionFailure.missingVpnPermission([String? message]) =
|
||||
MissingVpnPermission;
|
||||
|
||||
@With<ExpectedMeasuredFailure>()
|
||||
const factory ConnectionFailure.missingNotificationPermission([
|
||||
String? message,
|
||||
]) = MissingNotificationPermission;
|
||||
|
||||
@With<ExpectedMeasuredFailure>()
|
||||
const factory ConnectionFailure.missingPrivilege() = MissingPrivilege;
|
||||
|
||||
@With<ExpectedMeasuredFailure>()
|
||||
const factory ConnectionFailure.missingGeoAssets() = MissingGeoAssets;
|
||||
|
||||
@With<ExpectedMeasuredFailure>()
|
||||
const factory ConnectionFailure.invalidConfigOption([
|
||||
String? message,
|
||||
]) = InvalidConfigOption;
|
||||
|
||||
@With<ExpectedMeasuredFailure>()
|
||||
const factory ConnectionFailure.invalidConfig([
|
||||
String? message,
|
||||
]) = InvalidConfig;
|
||||
|
||||
@override
|
||||
({String type, String? message}) present(TranslationsEn t) {
|
||||
return switch (this) {
|
||||
UnexpectedConnectionFailure() => (
|
||||
type: t.failure.connectivity.unexpected,
|
||||
message: null,
|
||||
),
|
||||
MissingVpnPermission(:final message) => (
|
||||
type: t.failure.connectivity.missingVpnPermission,
|
||||
message: message
|
||||
),
|
||||
MissingNotificationPermission(:final message) => (
|
||||
type: t.failure.connectivity.missingNotificationPermission,
|
||||
message: message
|
||||
),
|
||||
MissingPrivilege() => (
|
||||
type: t.failure.singbox.missingPrivilege,
|
||||
message: t.failure.singbox.missingPrivilegeMsg,
|
||||
),
|
||||
MissingGeoAssets() => (
|
||||
type: t.failure.singbox.missingGeoAssets,
|
||||
message: t.failure.singbox.missingGeoAssetsMsg,
|
||||
),
|
||||
InvalidConfigOption(:final message) => (
|
||||
type: t.failure.singbox.invalidConfigOptions,
|
||||
message: message,
|
||||
),
|
||||
InvalidConfig(:final message) => (
|
||||
type: t.failure.singbox.invalidConfig,
|
||||
message: message,
|
||||
),
|
||||
};
|
||||
}
|
||||
}
|
||||
41
lib/features/connection/model/connection_status.dart
Normal file
41
lib/features/connection/model/connection_status.dart
Normal file
@@ -0,0 +1,41 @@
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:hiddify/core/localization/translations.dart';
|
||||
import 'package:hiddify/features/connection/model/connection_failure.dart';
|
||||
|
||||
part 'connection_status.freezed.dart';
|
||||
|
||||
@freezed
|
||||
sealed class ConnectionStatus with _$ConnectionStatus {
|
||||
const ConnectionStatus._();
|
||||
|
||||
const factory ConnectionStatus.disconnected([
|
||||
ConnectionFailure? connectionFailure,
|
||||
]) = Disconnected;
|
||||
const factory ConnectionStatus.connecting() = Connecting;
|
||||
const factory ConnectionStatus.connected() = Connected;
|
||||
const factory ConnectionStatus.disconnecting() = Disconnecting;
|
||||
|
||||
bool get isConnected => switch (this) { Connected() => true, _ => false };
|
||||
|
||||
bool get isSwitching => switch (this) {
|
||||
Connecting() => true,
|
||||
Disconnecting() => true,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
String format() => switch (this) {
|
||||
Disconnected(:final connectionFailure) => connectionFailure != null
|
||||
? "CONNECTION FAILURE: $connectionFailure"
|
||||
: "DISCONNECTED",
|
||||
Connecting() => "CONNECTING",
|
||||
Connected() => "CONNECTED",
|
||||
Disconnecting() => "DISCONNECTING",
|
||||
};
|
||||
|
||||
String present(TranslationsEn t) => switch (this) {
|
||||
Disconnected() => t.home.connection.tapToConnect,
|
||||
Connecting() => t.home.connection.connecting,
|
||||
Connected() => t.home.connection.connected,
|
||||
Disconnecting() => t.home.connection.disconnecting,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user