Refactor profiles
This commit is contained in:
57
lib/features/profile/model/profile_entity.dart
Normal file
57
lib/features/profile/model/profile_entity.dart
Normal file
@@ -0,0 +1,57 @@
|
||||
import 'package:dartx/dartx.dart';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
|
||||
part 'profile_entity.freezed.dart';
|
||||
|
||||
enum ProfileType { remote, local }
|
||||
|
||||
@freezed
|
||||
sealed class ProfileEntity with _$ProfileEntity {
|
||||
const ProfileEntity._();
|
||||
|
||||
const factory ProfileEntity.remote({
|
||||
required String id,
|
||||
required bool active,
|
||||
required String name,
|
||||
required String url,
|
||||
required DateTime lastUpdate,
|
||||
ProfileOptions? options,
|
||||
SubscriptionInfo? subInfo,
|
||||
}) = RemoteProfileEntity;
|
||||
|
||||
const factory ProfileEntity.local({
|
||||
required String id,
|
||||
required bool active,
|
||||
required String name,
|
||||
required DateTime lastUpdate,
|
||||
}) = LocalProfileEntity;
|
||||
}
|
||||
|
||||
@freezed
|
||||
class ProfileOptions with _$ProfileOptions {
|
||||
const factory ProfileOptions({
|
||||
required Duration updateInterval,
|
||||
}) = _ProfileOptions;
|
||||
}
|
||||
|
||||
@freezed
|
||||
class SubscriptionInfo with _$SubscriptionInfo {
|
||||
const SubscriptionInfo._();
|
||||
|
||||
const factory SubscriptionInfo({
|
||||
required int upload,
|
||||
required int download,
|
||||
required int total,
|
||||
required DateTime expire,
|
||||
String? webPageUrl,
|
||||
String? supportUrl,
|
||||
}) = _SubscriptionInfo;
|
||||
|
||||
bool get isExpired => expire <= DateTime.now();
|
||||
|
||||
int get consumption => upload + download;
|
||||
|
||||
double get ratio => (consumption / total).clamp(0, 1);
|
||||
|
||||
Duration get remaining => expire.difference(DateTime.now());
|
||||
}
|
||||
47
lib/features/profile/model/profile_failure.dart
Normal file
47
lib/features/profile/model/profile_failure.dart
Normal file
@@ -0,0 +1,47 @@
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:hiddify/core/prefs/prefs.dart';
|
||||
import 'package:hiddify/domain/failures.dart';
|
||||
|
||||
part 'profile_failure.freezed.dart';
|
||||
|
||||
@freezed
|
||||
sealed class ProfileFailure with _$ProfileFailure, Failure {
|
||||
const ProfileFailure._();
|
||||
|
||||
@With<UnexpectedFailure>()
|
||||
const factory ProfileFailure.unexpected([
|
||||
Object? error,
|
||||
StackTrace? stackTrace,
|
||||
]) = ProfileUnexpectedFailure;
|
||||
|
||||
const factory ProfileFailure.notFound() = ProfileNotFoundFailure;
|
||||
|
||||
@With<ExpectedFailure>()
|
||||
const factory ProfileFailure.invalidUrl() = ProfileInvalidUrlFailure;
|
||||
|
||||
@With<ExpectedFailure>()
|
||||
const factory ProfileFailure.invalidConfig([String? message]) =
|
||||
ProfileInvalidConfigFailure;
|
||||
|
||||
@override
|
||||
({String type, String? message}) present(TranslationsEn t) {
|
||||
return switch (this) {
|
||||
ProfileUnexpectedFailure() => (
|
||||
type: t.failure.profiles.unexpected,
|
||||
message: null,
|
||||
),
|
||||
ProfileNotFoundFailure() => (
|
||||
type: t.failure.profiles.notFound,
|
||||
message: null
|
||||
),
|
||||
ProfileInvalidUrlFailure() => (
|
||||
type: t.failure.profiles.invalidUrl,
|
||||
message: null,
|
||||
),
|
||||
ProfileInvalidConfigFailure(:final message) => (
|
||||
type: t.failure.profiles.invalidConfig,
|
||||
message: message
|
||||
),
|
||||
};
|
||||
}
|
||||
}
|
||||
21
lib/features/profile/model/profile_sort_enum.dart
Normal file
21
lib/features/profile/model/profile_sort_enum.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hiddify/core/prefs/prefs.dart';
|
||||
|
||||
enum ProfilesSort {
|
||||
lastUpdate,
|
||||
name;
|
||||
|
||||
String present(TranslationsEn t) {
|
||||
return switch (this) {
|
||||
lastUpdate => t.profile.sortBy.lastUpdate,
|
||||
name => t.profile.sortBy.name,
|
||||
};
|
||||
}
|
||||
|
||||
IconData get icon => switch (this) {
|
||||
lastUpdate => Icons.update,
|
||||
name => Icons.sort_by_alpha,
|
||||
};
|
||||
}
|
||||
|
||||
enum SortMode { ascending, descending }
|
||||
Reference in New Issue
Block a user