Change mapping and bug fixes
This commit is contained in:
@@ -15,11 +15,9 @@ DioHttpClient httpClient(HttpClientRef ref) {
|
||||
);
|
||||
|
||||
ref.listen(
|
||||
configOptionNotifierProvider,
|
||||
(_, next) {
|
||||
if (next case AsyncData(value: final options)) {
|
||||
client.setProxyPort(options.mixedPort);
|
||||
}
|
||||
configOptionNotifierProvider.selectAsync((data) => data.mixedPort),
|
||||
(_, next) async {
|
||||
client.setProxyPort(await next);
|
||||
},
|
||||
fireImmediately: true,
|
||||
);
|
||||
|
||||
54
lib/core/model/optional_range.dart
Normal file
54
lib/core/model/optional_range.dart
Normal file
@@ -0,0 +1,54 @@
|
||||
import 'package:dart_mappable/dart_mappable.dart';
|
||||
import 'package:dartx/dartx.dart';
|
||||
import 'package:hiddify/core/localization/translations.dart';
|
||||
|
||||
part 'optional_range.mapper.dart';
|
||||
|
||||
@MappableClass()
|
||||
class OptionalRange with OptionalRangeMappable {
|
||||
const OptionalRange({this.min, this.max});
|
||||
|
||||
final int? min;
|
||||
final int? max;
|
||||
|
||||
String format() => [min, max].whereNotNull().join("-");
|
||||
String present(TranslationsEn t) =>
|
||||
format().isEmpty ? t.general.notSet : format();
|
||||
|
||||
factory OptionalRange._fromString(
|
||||
String input, {
|
||||
bool allowEmpty = true,
|
||||
}) =>
|
||||
switch (input.split("-")) {
|
||||
[final String val] when val.isEmpty && allowEmpty =>
|
||||
const OptionalRange(),
|
||||
[final String min] => OptionalRange(min: int.parse(min)),
|
||||
[final String min, final String max] => OptionalRange(
|
||||
min: int.parse(min),
|
||||
max: int.parse(max),
|
||||
),
|
||||
_ => throw Exception("Invalid range: $input"),
|
||||
};
|
||||
|
||||
static OptionalRange? tryParse(
|
||||
String input, {
|
||||
bool allowEmpty = false,
|
||||
}) {
|
||||
try {
|
||||
return OptionalRange._fromString(input);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class OptionalRangeJsonMapper extends SimpleMapper<OptionalRange> {
|
||||
const OptionalRangeJsonMapper();
|
||||
|
||||
@override
|
||||
OptionalRange decode(dynamic value) =>
|
||||
OptionalRange._fromString(value as String);
|
||||
|
||||
@override
|
||||
dynamic encode(OptionalRange self) => self.format();
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
import 'package:dartx/dartx.dart';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:hiddify/core/localization/translations.dart';
|
||||
|
||||
part 'range.freezed.dart';
|
||||
|
||||
@freezed
|
||||
class RangeWithOptionalCeil with _$RangeWithOptionalCeil {
|
||||
const RangeWithOptionalCeil._();
|
||||
|
||||
const factory RangeWithOptionalCeil({
|
||||
int? min,
|
||||
int? max,
|
||||
}) = _RangeWithOptionalCeil;
|
||||
|
||||
String format() => [min, max].whereNotNull().join("-");
|
||||
String present(TranslationsEn t) =>
|
||||
format().isEmpty ? t.general.notSet : format();
|
||||
|
||||
factory RangeWithOptionalCeil._fromString(
|
||||
String input, {
|
||||
bool allowEmpty = true,
|
||||
}) =>
|
||||
switch (input.split("-")) {
|
||||
[final String val] when val.isEmpty && allowEmpty =>
|
||||
const RangeWithOptionalCeil(),
|
||||
[final String min] => RangeWithOptionalCeil(min: int.parse(min)),
|
||||
[final String min, final String max] => RangeWithOptionalCeil(
|
||||
min: int.parse(min),
|
||||
max: int.parse(max),
|
||||
),
|
||||
_ => throw Exception("Invalid range: $input"),
|
||||
};
|
||||
|
||||
static RangeWithOptionalCeil? tryParse(
|
||||
String input, {
|
||||
bool allowEmpty = false,
|
||||
}) {
|
||||
try {
|
||||
return RangeWithOptionalCeil._fromString(input);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class RangeWithOptionalCeilJsonConverter
|
||||
implements JsonConverter<RangeWithOptionalCeil, String> {
|
||||
const RangeWithOptionalCeilJsonConverter();
|
||||
|
||||
@override
|
||||
RangeWithOptionalCeil fromJson(String json) =>
|
||||
RangeWithOptionalCeil._fromString(json);
|
||||
|
||||
@override
|
||||
String toJson(RangeWithOptionalCeil object) => object.format();
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:dart_mappable/dart_mappable.dart';
|
||||
|
||||
class IntervalInSecondsConverter implements JsonConverter<Duration, int> {
|
||||
const IntervalInSecondsConverter();
|
||||
class IntervalInSecondsMapper extends SimpleMapper<Duration> {
|
||||
const IntervalInSecondsMapper();
|
||||
|
||||
@override
|
||||
Duration fromJson(int json) => Duration(seconds: json);
|
||||
Duration decode(dynamic value) => Duration(seconds: value as int);
|
||||
|
||||
@override
|
||||
int toJson(Duration object) => object.inSeconds;
|
||||
dynamic encode(Duration self) => self.inSeconds;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user