2024-02-03 12:36:27 +03:30
|
|
|
import 'package:dartx/dartx.dart';
|
2023-12-04 17:36:02 +03:30
|
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
2024-02-03 12:36:27 +03:30
|
|
|
import 'package:hiddify/core/localization/translations.dart';
|
2023-12-04 17:36:02 +03:30
|
|
|
|
|
|
|
|
part 'range.freezed.dart';
|
|
|
|
|
|
|
|
|
|
@freezed
|
|
|
|
|
class RangeWithOptionalCeil with _$RangeWithOptionalCeil {
|
|
|
|
|
const RangeWithOptionalCeil._();
|
|
|
|
|
|
|
|
|
|
const factory RangeWithOptionalCeil({
|
2024-02-03 12:36:27 +03:30
|
|
|
int? min,
|
2023-12-04 17:36:02 +03:30
|
|
|
int? max,
|
|
|
|
|
}) = _RangeWithOptionalCeil;
|
|
|
|
|
|
2024-02-03 12:36:27 +03:30
|
|
|
String format() => [min, max].whereNotNull().join("-");
|
|
|
|
|
String present(TranslationsEn t) =>
|
|
|
|
|
format().isEmpty ? t.general.notSet : format();
|
2023-12-04 17:36:02 +03:30
|
|
|
|
2024-02-03 12:36:27 +03:30
|
|
|
factory RangeWithOptionalCeil._fromString(
|
|
|
|
|
String input, {
|
|
|
|
|
bool allowEmpty = true,
|
|
|
|
|
}) =>
|
2023-12-04 17:36:02 +03:30
|
|
|
switch (input.split("-")) {
|
2024-02-03 12:36:27 +03:30
|
|
|
[final String val] when val.isEmpty && allowEmpty =>
|
|
|
|
|
const RangeWithOptionalCeil(),
|
2023-12-04 17:36:02 +03:30
|
|
|
[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"),
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-03 12:36:27 +03:30
|
|
|
static RangeWithOptionalCeil? tryParse(
|
|
|
|
|
String input, {
|
|
|
|
|
bool allowEmpty = false,
|
|
|
|
|
}) {
|
2023-12-04 17:36:02 +03:30
|
|
|
try {
|
2024-02-03 12:36:27 +03:30
|
|
|
return RangeWithOptionalCeil._fromString(input);
|
2023-12-04 17:36:02 +03:30
|
|
|
} catch (_) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class RangeWithOptionalCeilJsonConverter
|
|
|
|
|
implements JsonConverter<RangeWithOptionalCeil, String> {
|
|
|
|
|
const RangeWithOptionalCeilJsonConverter();
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
RangeWithOptionalCeil fromJson(String json) =>
|
2024-02-03 12:36:27 +03:30
|
|
|
RangeWithOptionalCeil._fromString(json);
|
2023-12-04 17:36:02 +03:30
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
String toJson(RangeWithOptionalCeil object) => object.format();
|
|
|
|
|
}
|