Skip to content

Commit

Permalink
fix: fix sort dictionaries
Browse files Browse the repository at this point in the history
Signed-off-by: Mumulhl <[email protected]>
  • Loading branch information
mumu-lhl committed Jan 6, 2025
1 parent 1d9be71 commit fa1d2fa
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 55 deletions.
15 changes: 10 additions & 5 deletions lib/dictionary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import "main.dart";

class DictManager {
final Map<int, Mdict> dicts = {};
Future<List<DictGroupData>> groups = dictGroupDao.getAllGroups();
List<DictGroupData> groups = [];
List<int> dictIds = [];
int groupId = 0;

bool get isEmpty => dicts.isEmpty;
Expand All @@ -22,17 +23,21 @@ class DictManager {
await clear();

groupId = id;
final dictGroup = await dictGroupDao.getDictIds(id);
dictIds = await dictGroupDao.getDictIds(id);
final paths = [
for (final id in dictGroup) await dictionaryListDao.getPath(id)
for (final id in dictIds) await dictionaryListDao.getPath(id)
];
for (final path in paths) {
await add(path);
}
}

void updateGroup() {
groups = dictGroupDao.getAllGroups();
Future<void> updateGroupList() async {
groups = await dictGroupDao.getAllGroups();
}

Future<void> updateDictIds() async {
dictIds = await dictGroupDao.getDictIds(groupId);
}

Future<void> clear() async {
Expand Down
1 change: 1 addition & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void main() async {
await prefs.setInt("currentDictionaryGroupId", groupId);
}
await dictManager.setCurrentGroup(groupId);
await dictManager.updateGroupList();

flutterTts = FlutterTts();

Expand Down
54 changes: 18 additions & 36 deletions lib/pages/main/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import "package:ciyue/database/app.dart";
import "package:ciyue/dictionary.dart";
import "package:ciyue/pages/main/home.dart";
import "package:ciyue/pages/main/settings.dart";
Expand Down Expand Up @@ -69,41 +68,24 @@ class _HomeState extends State<Home> {
];
}

FutureBuilder buildDrawer() {
return FutureBuilder(
future: dictManager.groups,
builder: (context, snapshot) {
if (snapshot.hasData) {
for (final group in snapshot.data as List<DictGroupData>) {
if (group.id == dictManager.groupId) {
return Drawer(
child: ListView(
children: [
for (final group in snapshot.data as List<DictGroupData>)
ListTile(
leading: group.id == dictManager.groupId
? const Icon(Icons.circle)
: const Icon(Icons.circle_outlined),
title: Text(group.name),
onTap: () async {
context.pop();
await dictManager.setCurrentGroup(group.id);
},
),
],
),
);
}
}
return Drawer(child: ListView(children: [ListTile()]));
} else {
return Drawer(
child: const Center(
child: CircularProgressIndicator(),
),
);
}
});
Drawer buildDrawer() {
return Drawer(
child: ListView(
children: [
for (final group in dictManager.groups)
ListTile(
leading: group.id == dictManager.groupId
? const Icon(Icons.circle)
: const Icon(Icons.circle_outlined),
title: Text(group.name),
onTap: () async {
context.pop();
await dictManager.setCurrentGroup(group.id);
},
),
],
),
);
}

IconButton? buildDrawerButton(BuildContext context) {
Expand Down
24 changes: 15 additions & 9 deletions lib/pages/manage_dictionaries/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ class _ManageDictionariesState extends State<ManageDictionaries> {
}

FutureBuilder<List<DictionaryListData>> buildBody(BuildContext context) {
final colorScheme = Theme.of(context).colorScheme;

return FutureBuilder(
future: dictionaries,
builder: (BuildContext context,
Expand All @@ -116,12 +114,18 @@ class _ManageDictionariesState extends State<ManageDictionaries> {

if (snapshot.hasData) {
int index = 0;
final dictionaries = snapshot.data!;
for (final dictionary in dictionaries) {
children.add(
buildDictionaryCard(context, colorScheme, dictionary, index));
final dicts = snapshot.data!;
final dictsMap = {for (final dict in dicts) dict.id: dict};
for (final id in dictManager.dictIds) {
children.add(buildDictionaryCard(context, dictsMap[id]!, index));
index += 1;
}
for (final dict in dicts) {
if (!dictManager.contain(dict.id)) {
children.add(buildDictionaryCard(context, dict, index));
index += 1;
}
}
}

if (children.isEmpty) {
Expand All @@ -141,7 +145,7 @@ class _ManageDictionariesState extends State<ManageDictionaries> {
for (final dict in dicts)
if (dictManager.contain(dict.id)) dict.id
]);
dictManager.updateGroup();
await dictManager.updateDictIds();
}

setState(() {});
Expand All @@ -153,8 +157,10 @@ class _ManageDictionariesState extends State<ManageDictionaries> {
);
}

Card buildDictionaryCard(BuildContext context, ColorScheme colorScheme,
DictionaryListData dictionary, int index) {
Card buildDictionaryCard(
BuildContext context, DictionaryListData dictionary, int index) {
final colorScheme = Theme.of(context).colorScheme;

return Card(
key: ValueKey(dictionary.id),
elevation: 0,
Expand Down
10 changes: 5 additions & 5 deletions lib/pages/webview_display.dart
Original file line number Diff line number Diff line change
Expand Up @@ -206,20 +206,20 @@ class WebviewDisplay extends StatelessWidget {
),
bottom: TabBar(
tabs: [
for (final dict in dictManager.dicts.values)
Tab(text: basename(dict.path))
for (final id in dictManager.dictIds)
Tab(text: basename(dictManager.dicts[id]!.path))
],
)),
floatingActionButton: Button(word: word),
body: TabBarView(children: [
for (final dict in dictManager.dicts.values)
for (final id in dictManager.dictIds)
FutureBuilder(
future: dict.readWord(word),
future: dictManager.dicts[id]!.readWord(word),
builder: (context, snapshot) {
if (snapshot.hasData) {
return WebView(
content: snapshot.data!,
dictId: dict.id,
dictId: id,
);
} else if (snapshot.hasError) {
return Center(
Expand Down

0 comments on commit fa1d2fa

Please sign in to comment.