Add support to list galleries with sort

This commit is contained in:
2023-09-08 22:09:41 +08:00
parent f6956e0989
commit de18ca304c
6 changed files with 91 additions and 8 deletions

View File

@@ -3,13 +3,15 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:go_router/go_router.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:logging/logging.dart';
import 'api/client.dart';
import 'api/gallery.dart';
import 'globals.dart';
final _log = Logger("GalleriesPage");
class GalleriesPage extends StatefulWidget {
const GalleriesPage({Key? key}) : super(key: key);
const GalleriesPage({Key? key, this.sortByGid}) : super(key: key);
final SortByGid? sortByGid;
static const String routeName = '/galleries';
@@ -19,14 +21,17 @@ class GalleriesPage extends StatefulWidget {
class _GalleriesPage extends State<GalleriesPage> with ThemeModeWidget {
static const int _pageSize = 20;
bool? _sortByGid;
SortByGid _sortByGid2 = SortByGid.none;
final PagingController<int, GMeta> _pagingController =
PagingController(firstPageKey: 0);
Future<void> _fetchPage(int pageKey) async {
try {
final list =
(await api.listGalleries(offset: pageKey, limit: _pageSize)).unwrap();
final list = (await api.listGalleries(
offset: pageKey, limit: _pageSize, sortByGid: _sortByGid))
.unwrap();
final isLastPage = list.length < _pageSize;
if (isLastPage) {
_pagingController.appendLastPage(list);
@@ -42,6 +47,14 @@ class _GalleriesPage extends State<GalleriesPage> with ThemeModeWidget {
@override
void initState() {
try {
_sortByGid2 = widget.sortByGid != null
? widget.sortByGid!
: SortByGid.values[prefs.getInt("sortByGid") ?? 0];
_sortByGid = _sortByGid2.toBool();
} catch (e) {
_log.warning("Failed to load sortByGid from prefs:", e);
}
_pagingController.addPageRequestListener((pageKey) {
_fetchPage(pageKey);
});
@@ -61,6 +74,37 @@ class _GalleriesPage extends State<GalleriesPage> with ThemeModeWidget {
),
title: Text(AppLocalizations.of(context)!.galleries),
actions: [
Padding(
padding: const EdgeInsets.only(top: 4.0),
child: DropdownMenu<SortByGid>(
initialSelection: _sortByGid2,
onSelected: (v) {
if (v != null) {
prefs.setInt("sortByGid", v!.index).catchError((error) {
_log.warning(
"Failed to save sortByGid to prefs:", error);
});
context.pushReplacementNamed("/galleries",
queryParameters: {
"sortByGid": [v!.index.toString()],
});
}
},
label: Text(AppLocalizations.of(context)!.sortByGid,
style: Theme.of(context).primaryTextTheme.labelMedium),
dropdownMenuEntries: [
DropdownMenuEntry(
value: SortByGid.none,
label: AppLocalizations.of(context)!.none),
DropdownMenuEntry(
value: SortByGid.asc,
label: AppLocalizations.of(context)!.asc),
DropdownMenuEntry(
value: SortByGid.desc,
label: AppLocalizations.of(context)!.desc),
],
leadingIcon: const Icon(Icons.sort),
)),
buildThemeModeIcon(context),
buildMoreVertSettingsButon(context),
]),