Skip to content

Commit

Permalink
✨ add collect ording setting
Browse files Browse the repository at this point in the history
  • Loading branch information
niuhuan committed Jun 14, 2024
1 parent f4436ff commit 94c0afb
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 44 deletions.
2 changes: 1 addition & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 819463e6a0290f5a72f145ba7cde16e8b6ef0796

COCOAPODS: 1.12.1
COCOAPODS: 1.15.2
13 changes: 13 additions & 0 deletions lib/configs/collect_ordering.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'map_config.dart';

final collectOrderingSetting = MapConfig(
valueName: {
"-datetime_modifier": "收藏时间倒序",
"datetime_modifier": "收藏时间正序",
"-datetime_updated": "更新时间倒序",
"datetime_updated": "更新时间正序",
},
defaultValue: "-datetime_modifier",
propertyKey: "collect_ordering",
propertyName: "收藏排序",
);
2 changes: 2 additions & 0 deletions lib/configs/configs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:kobi/configs/reader_type.dart';
import 'package:kobi/configs/versions.dart';

import 'cache_time.dart';
import 'collect_ordering.dart';

Future initConfigs() async {
await initProxy();
Expand All @@ -17,5 +18,6 @@ Future initConfigs() async {
await initReaderType();
await initLogin();
await initVersion();
await collectOrderingSetting.initConfig();
autoCheckNewVersion();
}
65 changes: 65 additions & 0 deletions lib/configs/map_config.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import 'package:event/event.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:kobi/src/rust/api/api.dart';

import '../screens/components/commons.dart';

class MapConfig {
late final Map<String, String> valueName;
late final Map<String, String> nameValue;
final String propertyName;
final String propertyKey;
late String value;
late final Event changeEvent;
final String defaultValue;

MapConfig({
required Map<String, String> valueName,
required this.defaultValue,
required this.propertyKey,
required this.propertyName,
}) {
this.valueName = valueName;
this.nameValue = {};
valueName.forEach((key, value) {
nameValue[value] = key;
});
this.changeEvent = Event();
if (!valueName.containsKey(defaultValue)) {
throw ArgumentError("defaultValue not in valueName");
}
}

Future initConfig() async {
value = await loadProperty(k: propertyKey);
if (!valueName.containsKey(value)) {
value = defaultValue;
await saveProperty(k: propertyKey, v: value);
}
}

Widget configWidget(BuildContext context) {
return StatefulBuilder(
builder: (BuildContext context, void Function(void Function()) setState) {
return ListTile(
title: Text(propertyName),
subtitle: Text(valueName[value] ?? ""),
onTap: () async {
String? result = await chooseMapDialog<String>(
context,
title: propertyName,
values: nameValue,
);
if (result != null) {
await saveProperty(k: propertyKey, v: result);
value = result;
setState(() {});
changeEvent.broadcast();
}
},
);
},
);
}
}
59 changes: 33 additions & 26 deletions lib/screens/collected_comics_account_screen.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:kobi/screens/components/comic_pager.dart';

import '../configs/collect_ordering.dart';
import '../configs/login.dart';
import '../src/rust/api/api.dart' as api;
import '../src/rust/udto.dart';
import 'components/comic_card.dart';

class CollectedComicsAccountScreen extends StatefulWidget {
Expand All @@ -20,11 +22,13 @@ class _CollectedComicsAccountScreenState
void initState() {
super.initState();
loginEvent.subscribe(_setState);
collectOrderingSetting.changeEvent.subscribe(_setState);
}

@override
void dispose() {
loginEvent.unsubscribe(_setState);
collectOrderingSetting.changeEvent.unsubscribe(_setState);
super.dispose();
}

Expand Down Expand Up @@ -52,30 +56,33 @@ class _CollectedComicsAccountScreenState
if (loginState.state == 2) {
return const Center(child: Text("登录失败"));
}
return ComicPager(fetcher: (offset, limit) async {
final result = await api.collectFromAccount(
freeType: 1,
ordering: "-datetime_modifier",
offset: offset,
limit: limit,
);
return CommonPage<CommonComicInfo>(
list: result.list
.map((e) => CommonComicInfo(
author: e.comic.author,
cover: e.comic.cover,
imgType: 1,
name: e.comic.name,
pathWord: e.comic.pathWord,
popular: e.comic.popular,
males: e.comic.males,
females: e.comic.females,
))
.toList(),
total: result.total,
limit: result.limit,
offset: result.offset,
);
});
return ComicPager(
key: Key("collected_comics_account:${collectOrderingSetting.value}"),
fetcher: (offset, limit) async {
final result = await api.collectFromAccount(
freeType: 1,
ordering: "-datetime_modifier",
offset: offset,
limit: limit,
);
return CommonPage<CommonComicInfo>(
list: result.list
.map((e) => CommonComicInfo(
author: e.comic.author,
cover: e.comic.cover,
imgType: 1,
name: e.comic.name,
pathWord: e.comic.pathWord,
popular: e.comic.popular,
males: e.comic.males,
females: e.comic.females,
))
.toList(),
total: result.total,
limit: result.limit,
offset: result.offset,
);
},
);
}
}
17 changes: 0 additions & 17 deletions lib/screens/local_collect_screen.dart

This file was deleted.

2 changes: 2 additions & 0 deletions lib/screens/settings_screen.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:kobi/configs/collect_ordering.dart';

import '../configs/cache_time.dart';
import '../configs/proxy.dart';
Expand All @@ -14,6 +15,7 @@ class SettingsScreen extends StatelessWidget {
children: [
proxySetting(),
cacheTimeNameSetting(),
collectOrderingSetting.configWidget(context),
],
),
);
Expand Down

0 comments on commit 94c0afb

Please sign in to comment.