Skip to content

Commit

Permalink
Team query use offset instead of pageIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
SharpnelXu committed Aug 13, 2024
1 parent 1436130 commit 8a745e0
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lib/app/modules/battle/teams/teams_query_page.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import 'package:flutter_easyloading/flutter_easyloading.dart';

import 'package:chaldea/app/api/chaldea.dart';
Expand Down Expand Up @@ -51,7 +53,7 @@ class _TeamsQueryPageState extends State<TeamsQueryPage> with SearchableListStat

TeamQueryMode get mode => widget.mode;

int pageIndex = 0;
int offset = 0;
TeamQueryResult queryResult = TeamQueryResult(data: []);
final filterData = TeamFilterData(true);

Expand All @@ -61,7 +63,7 @@ class _TeamsQueryPageState extends State<TeamsQueryPage> with SearchableListStat
@override
void initState() {
super.initState();
_queryTeams(pageIndex);
_queryTeams(offset);
}

@override
Expand Down Expand Up @@ -153,19 +155,19 @@ class _TeamsQueryPageState extends State<TeamsQueryPage> with SearchableListStat
),
const SizedBox(width: 8),
TextButton(
onPressed: pageIndex == 0 ? null : () => _queryTeams(pageIndex - 1),
onPressed: offset == 0 ? null : () => _queryTeams(offset - pageSize),
style: kTextButtonDenseStyle,
child: Text(S.current.prev_page),
),
TextButton(
onPressed: !queryResult.hasNextPage ? null : () => _queryTeams(pageIndex + 1),
onPressed: !queryResult.hasNextPage ? null : () => _queryTeams(offset + pageSize),
style: kTextButtonDenseStyle,
child: Text(S.current.next_page),
),
TextButton(
onPressed: () async {
EasyThrottle.throttle('team_query_refresh', const Duration(seconds: 2), () {
_queryTeams(pageIndex, refresh: true);
_queryTeams(offset, refresh: true);
});
},
style: kTextButtonDenseStyle,
Expand Down Expand Up @@ -195,7 +197,7 @@ class _TeamsQueryPageState extends State<TeamsQueryPage> with SearchableListStat
Widget listItemBuilder(UserBattleData record) {
final index = queryResult.data.indexOf(record);
final shareData = record.decoded;
final shownIndex = pageSize * pageIndex + index + 1;
final shownIndex = offset + index + 1;

Widget child = Column(
children: [
Expand Down Expand Up @@ -537,15 +539,17 @@ class _TeamsQueryPageState extends State<TeamsQueryPage> with SearchableListStat
return filterData.filter(data);
}

Future<void> _queryTeams(final int page, {bool refresh = false}) async {
Future<void> _queryTeams(int offset, {bool refresh = false}) async {
offset = max(0, offset);

if (widget.mode == TeamQueryMode.user && !secrets.isLoggedIn) return;
Future<TeamQueryResult?> task;
switch (mode) {
case TeamQueryMode.user:
final userId = widget.userId ?? (widget.username != null ? int.tryParse(widget.username!) : null);
task = showEasyLoading(() => ChaldeaWorkerApi.teamsByUser(
limit: pageSize,
offset: pageSize * page,
offset: offset,
expireAfter: refresh ? Duration.zero : const Duration(days: 2),
userId: userId,
username: widget.username,
Expand All @@ -559,7 +563,7 @@ class _TeamsQueryPageState extends State<TeamsQueryPage> with SearchableListStat
phase: phase,
enemyHash: widget.phaseInfo?.enemyHash,
limit: pageSize,
offset: pageSize * page,
offset: offset,
expireAfter: refresh ? Duration.zero : null,
));
case TeamQueryMode.id:
Expand Down Expand Up @@ -591,7 +595,7 @@ class _TeamsQueryPageState extends State<TeamsQueryPage> with SearchableListStat
r.parse();
}
queryResult = result;
pageIndex = page;
this.offset = result.offset;
}
if (mounted) setState(() {});
}
Expand Down

0 comments on commit 8a745e0

Please sign in to comment.