Skip to content

Commit

Permalink
Bond filter supports compare direction
Browse files Browse the repository at this point in the history
  • Loading branch information
narumi147 committed Aug 11, 2024
1 parent cb7e669 commit 3d2c984
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
52 changes: 39 additions & 13 deletions lib/app/modules/servant/filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ class ServantFilterPage extends FilterPage<SvtFilterData> {
if (!filterData.svtDuplicated.matchOne(svt.isDupSvt)) {
return false;
}
if (!filterData.bond.matchOne(SvtBondStage.fromBond(svtStat.bond))) {
return false;
final comparedBond = filterData.bondValue.radioValue;
if (comparedBond != null && filterData.bondCompare.options.isNotEmpty) {
if (filterData.bondCompare.options.every((c) => !c.test(svtStat.bond, comparedBond))) {
return false;
}
}

// class name
Expand Down Expand Up @@ -408,17 +411,40 @@ class _ServantFilterPageState extends FilterPageState<SvtFilterData, ServantFilt
});
},
),
FilterGroup<SvtBondStage>(
title: Text(S.current.bond),
options: SvtBondStage.values,
values: filterData.bond,
optionBuilder: (v) => Text(v.text),
onFilterChanged: (v, _) {
setState(() {
update();
});
},
),
getGroup(header: S.current.bond, children: [
FilterGroup<CompareOperator>(
combined: true,
padding: EdgeInsets.zero,
options: CompareOperator.values,
values: filterData.bondCompare,
optionBuilder: (v) => Text(v.text),
onFilterChanged: (v, last) {
if (v.contain(CompareOperator.lessThan) && v.contain(CompareOperator.moreThan)) {
if (last == CompareOperator.lessThan) {
v.options.remove(CompareOperator.moreThan);
} else if (last == CompareOperator.moreThan) {
v.options.remove(CompareOperator.lessThan);
}
}
if (v.options.isEmpty && last != null) {
v.options.add(last);
}
setState(() {
update();
});
},
),
FilterGroup<int>(
combined: true,
options: const [5, 6, 10, 15],
values: filterData.bondValue,
onFilterChanged: (v, _) {
setState(() {
update();
});
},
),
]),
buildGroupDivider(text: S.current.gamedata),
FilterGroup<Region>(
title: Text(S.current.game_server, style: textStyle),
Expand Down
30 changes: 25 additions & 5 deletions lib/models/userdata/filter_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ class SvtFilterData with FilterDataMixin {
final priority = FilterGroupData<int>(onChanged: () {
db.itemCenter.updateSvts(all: true);
});
final bond = FilterGroupData<SvtBondStage>();

final bondCompare = FilterGroupData<CompareOperator>(options: {CompareOperator.lessThan});
final bondValue = FilterRadioData<int>();
final region = FilterRadioData<Region>();
late final obtain = FilterGroupData<SvtObtain>(
options: SvtObtain.values.where((e) => e != SvtObtain.unavailable).toSet(),
Expand Down Expand Up @@ -287,7 +289,8 @@ class SvtFilterData with FilterDataMixin {
curStatus,
planCompletion,
svtDuplicated,
bond,
bondCompare,
bondValue,
// priority,
region,
obtain,
Expand Down Expand Up @@ -1064,17 +1067,34 @@ enum SvtStatusState {
}
}

enum CompareOperator {
lessThan('<', -1),
moreThan('>', 1),
equal('=', 0),
;

final String text;
final int value;
const CompareOperator(this.text, this.value);

bool test<T extends Comparable>(T left, T right) {
return left.compareTo(right).sign == value;
}
}

enum SvtBondStage {
lessThan5('<5'), // <5
lessThan10('<10'), // <10
greaterThan10('≤15'), // 10<=x<=15
lessThan5('<5'),
lessThan6('<6'),
lessThan10('<10'),
greaterThan10('≤15'),
;

final String text;
const SvtBondStage(this.text);

static SvtBondStage fromBond(int bond) {
if (bond < 5) return lessThan5;
if (bond < 6) return lessThan6;
if (bond < 10) return lessThan10;
return greaterThan10;
}
Expand Down

0 comments on commit 3d2c984

Please sign in to comment.