Skip to content

Commit

Permalink
Merge pull request #123 from mumu-lhl/properties-page
Browse files Browse the repository at this point in the history
  • Loading branch information
mumu-lhl authored Jan 23, 2025
2 parents 15ea1f7 + ecc56ec commit 0c53e67
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 68 deletions.
114 changes: 64 additions & 50 deletions lib/dictionary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import "package:path_provider/path_provider.dart";
import "database/dictionary.dart";
import "main.dart";

final dictManager = DictManager();

class DictManager {
final Map<int, Mdict> dicts = {};
List<DictGroupData> groups = [];
Expand All @@ -17,8 +19,31 @@ class DictManager {

bool get isEmpty => dicts.isEmpty;

Future<void> add(String path) async {
final dict = Mdict(path: path);
await dict.init();
dicts[dict.id] = dict;
}

Future<void> clear() async {
for (final id in dictIds) {
await close(id);
}
}

Future<void> close(int id) async {
await dicts[id]!.close();
dicts.remove(id);
}

bool contain(int id) => dicts.keys.contains(id);

Future<void> remove(int id) async {
await dicts[id]!.removeDictionary();
await dicts[id]!.close();
dicts.remove(id);
}

Future<void> setCurrentGroup(int id) async {
await clear();

Expand All @@ -32,40 +57,15 @@ class DictManager {
}
}

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

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

Future<void> clear() async {
for (final id in dictIds) {
await close(id);
}
}

Future<void> add(String path) async {
final dict = Mdict(path: path);
await dict.init();
dicts[dict.id] = dict;
}

Future<void> close(int id) async {
await dicts[id]!.close();
dicts.remove(id);
}

Future<void> remove(int id) async {
await dicts[id]!.removeDictionary();
await dicts[id]!.close();
dicts.remove(id);
Future<void> updateGroupList() async {
groups = await dictGroupDao.getAllGroups();
}
}

final dictManager = DictManager();

class Mdict {
late final int id;
final String path;
Expand All @@ -74,32 +74,11 @@ class Mdict {
late final DictionaryDatabase db;
late final DictReader reader;
DictReader? readerResource;
late final String title;
late final int entriesTotal;

Mdict({required this.path});

Future<void> init() async {
id = await dictionaryListDao.getId(path);

reader = DictReader("$path.mdx");
await reader.init(false);

try {
readerResource = DictReader("$path.mdd");
await readerResource!.init(false);
} catch (e) {
readerResource = null;
}

db = dictionaryDatabase(id);

final fontPath = await dictionaryListDao.getFontPath(id);
customFont(fontPath);
}

Future<void> close() async {
await db.close();
}

Future<bool> add() async {
try {
await dictionaryListDao.getId(path);
Expand All @@ -120,9 +99,15 @@ class Mdict {

customFont(null);

title = reader.header["Title"] ?? basename(path);

return true;
}

Future<void> close() async {
await db.close();
}

Future<void> customFont(String? path) async {
fontPath = path;
if (path == null) {
Expand All @@ -134,6 +119,35 @@ class Mdict {
await dictionaryListDao.updateFont(id, path);
}

Future<void> initOnlyMetadata() async {
reader = DictReader("$path.mdx");
await reader.init();

title = reader.header["Title"] ?? basename(path);
entriesTotal = reader.numEntries;
}

Future<void> init() async {
id = await dictionaryListDao.getId(path);

reader = DictReader("$path.mdx");
await reader.init(false);

try {
readerResource = DictReader("$path.mdd");
await readerResource!.init(false);
} catch (e) {
readerResource = null;
}

db = dictionaryDatabase(id);

final fontPath = await dictionaryListDao.getFontPath(id);
customFont(fontPath);

title = reader.header["Title"] ?? basename(path);
}

Future<String> readWord(String word) async {
late DictionaryData data;
try {
Expand Down
8 changes: 7 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
"@updateAnnouncement": {},
"description": "Description",
"@description": {},
"properties": "Properties",
"@properties": {},
"removeOneHistory": "Remove History",
"@removeOneHistory": {},
"removeOneHistoryConfirm": "Are you sure you want to remove '%s' from history?",
Expand All @@ -89,5 +91,9 @@
"exportFileName": "Export File Name",
"@exportFileName": {},
"exportDirectory": "Export Directory",
"@exportDirectory": {}
"@exportDirectory": {},
"title": "Title",
"@title": {},
"totalNumberOfEntries": "Total Number of Entries",
"@totalNumberOfEntries": {}
}
11 changes: 9 additions & 2 deletions lib/l10n/app_fa.arb
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,12 @@
"clearHistory": "حذف سوابق",
"@clearHistory": {},
"clearHistoryConfirm": "آیا از حذف سوابق اطمینان دارید؟",
"@clearHistoryConfirm": {}
}
"@clearHistoryConfirm": {},
"properties": "خصوصیات",
"default_": "پیش فرض",
"manageGroups": "مدیریت گروه ها",
"exportFileName": "نام فایل خروجی",
"exportDirectory": "مسیر خروجی",
"title": "عنوان",
"totalNumberOfEntries": "تعداد کل ورودی ها"
}
13 changes: 12 additions & 1 deletion lib/l10n/app_nb.arb
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,16 @@
"tagName": "Tag navn",
"tagList": "Tag liste",
"remove": "Fjern",
"confirm": "Bekreft"
"confirm": "Bekreft",
"properties": "Egenskaper",
"removeOneHistory": "Fjern historikk",
"removeOneHistoryConfirm": "Er du sikker på at du vil fjerne '%s' fra historikken?",
"clearHistory": "Tøm historikk",
"clearHistoryConfirm": "Er du sikker på at du vil tømme historikken?",
"default_": "Standard",
"manageGroups": "Administrer grupper",
"exportFileName": "Eksporter filnavn",
"exportDirectory": "Eksporter katalog",
"title": "Tittel",
"totalNumberOfEntries": "Totalt antall oppføringer"
}
13 changes: 12 additions & 1 deletion lib/l10n/app_ru.arb
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,16 @@
"confirm": "Подтвердить",
"noResult": "Ничего не найдено",
"description": "Описание",
"updateAnnouncement": "Есть обновление до %s"
"updateAnnouncement": "Есть обновление до %s",
"properties": "Свойства",
"removeOneHistory": "Удалить из истории",
"removeOneHistoryConfirm": "Вы уверены, что хотите удалить '%s' из истории?",
"clearHistory": "Очистить историю",
"clearHistoryConfirm": "Вы уверены, что хотите очистить историю?",
"default_": "По умолчанию",
"manageGroups": "Управление группами",
"exportFileName": "Имя файла экспорта",
"exportDirectory": "Каталог экспорта",
"title": "Заголовок",
"totalNumberOfEntries": "Общее количество записей"
}
13 changes: 12 additions & 1 deletion lib/l10n/app_sc.arb
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,16 @@
"tags": "Etichetas",
"noResult": "Nenhum resultado",
"description": "Descritzione",
"updateAnnouncement": "S'annùntziu de atualizatzione a %s"
"updateAnnouncement": "S'annùntziu de atualizatzione a %s",
"properties": "Propriedades",
"removeOneHistory": "Boga s'istòria",
"removeOneHistoryConfirm": "Ses seguru de bòlere bogare '%s' dae s'istòria?",
"clearHistory": "Neta s'istòria",
"clearHistoryConfirm": "Ses seguru de bòlere netare s'istòria?",
"default_": "Predefinidu",
"manageGroups": "Manìgia sos grupos",
"exportFileName": "Nùmene de s'archìviu de esportatzione",
"exportDirectory": "Repertòriu de esportatzione",
"title": "Tìtulu",
"totalNumberOfEntries": "Nùmeru totale de intradas"
}
13 changes: 12 additions & 1 deletion lib/l10n/app_ta.arb
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,16 @@
"home": "வீடு",
"wordBook": "சொல் நூல்",
"settings": "அமைப்புகள்",
"description": "விளக்கம்"
"description": "விளக்கம்",
"properties": "பண்புகள்",
"removeOneHistory": "வரலாற்றை அகற்று",
"removeOneHistoryConfirm": "வரலாற்றிலிருந்து '%s' ஐ அகற்ற விரும்புகிறீர்களா?",
"clearHistory": "வரலாற்றை அழி",
"clearHistoryConfirm": "வரலாற்றை அழிக்க விரும்புகிறீர்களா?",
"default_": "இயல்புநிலை",
"manageGroups": "குழுக்களை நிர்வகிக்கவும்",
"exportFileName": "ஏற்றுமதி கோப்பு பெயர்",
"exportDirectory": "ஏற்றுமதி அடைவு",
"title": "தலைப்பு",
"totalNumberOfEntries": "மொத்த உள்ளீடுகளின் எண்ணிக்கை"
}
5 changes: 4 additions & 1 deletion lib/l10n/app_zh.arb
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@
"default_": "默认",
"manageGroups": "管理分组",
"exportFileName": "导出文件名",
"exportDirectory": "导出目录"
"exportDirectory": "导出目录",
"properties": "属性",
"title": "标题",
"totalNumberOfEntries": "总条目数"
}
5 changes: 4 additions & 1 deletion lib/l10n/app_zh_HK.arb
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@
"default_": "默認",
"manageGroups": "管理分組",
"exportFileName": "導出文件名",
"exportDirectory": "導出目錄"
"exportDirectory": "導出目錄",
"properties": "屬性",
"title": "標題",
"totalNumberOfEntries": "總條目數"
}
5 changes: 4 additions & 1 deletion lib/l10n/app_zh_TW.arb
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@
"default_": "默認",
"manageGroups": "管理分組",
"exportFileName": "導出文件名",
"exportDirectory": "導出目錄"
"exportDirectory": "導出目錄",
"properties": "屬性",
"title": "標題",
"totalNumberOfEntries": "總條目數"
}
14 changes: 10 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "package:ciyue/database/app.dart";
import "package:ciyue/dictionary.dart";
import "package:ciyue/pages/main/main.dart";
import "package:ciyue/pages/manage_dictionaries/main.dart";
import "package:ciyue/pages/manage_dictionaries/properties.dart";
import "package:ciyue/pages/manage_dictionaries/settings_dictionary.dart";
import "package:ciyue/pages/webview_display.dart";
import "package:ciyue/settings.dart";
Expand Down Expand Up @@ -127,20 +128,25 @@ final _router = GoRouter(
GoRoute(
path: "/description/:dictId",
builder: (context, state) => WebviewDisplayDescription(
dictId: int.parse(state.pathParameters['dictId']!),
dictId: int.parse(state.pathParameters["dictId"]!),
)),
GoRoute(
path: "/settings/dictionaries",
builder: (context, state) => const ManageDictionaries()),
GoRoute(
path: "/settings/dictionary/:dictId",
path: "/settings/:dictId",
builder: (context, state) => SettingsDictionary(
dictId: int.parse(state.pathParameters['dictId']!),
dictId: int.parse(state.pathParameters["dictId"]!),
)),
GoRoute(
path: "/settings/autoExport",
builder: (context, state) => const AutoExportPage(),
builder: (context, state) => const AutoExport(),
),
GoRoute(
path: "/properties",
builder: (context, state) => PropertiesDictionary(
path: (state.extra as Map<String, dynamic>)["path"],
)),
],
);

Expand Down
4 changes: 2 additions & 2 deletions lib/pages/auto_export.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import 'package:flutter/material.dart';
import "package:flutter_gen/gen_l10n/app_localizations.dart";
import 'package:go_router/go_router.dart';

class AutoExportPage extends StatelessWidget {
const AutoExportPage({super.key});
class AutoExport extends StatelessWidget {
const AutoExport({super.key});

@override
Widget build(BuildContext context) {
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/main/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,4 @@ class HomeScreen extends StatelessWidget {
}
});
}
}
}
14 changes: 13 additions & 1 deletion lib/pages/manage_dictionaries/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,18 @@ class _ManageDictionariesState extends State<ManageDictionaries> {
return SimpleDialog(
title: Text(basename(dictionary.path)),
children: <Widget>[
SimpleDialogOption(
onPressed: () {
context.pop();
context.push("/properties", extra: {
"path": dictionary.path,
});
},
child: ListTile(
leading: Icon(Icons.settings),
title: Text(AppLocalizations.of(context)!.properties),
),
),
SimpleDialogOption(
onPressed: () async {
if (dictManager.contain(dictionary.id)) {
Expand Down Expand Up @@ -317,7 +329,7 @@ class _ManageDictionariesState extends State<ManageDictionaries> {
SimpleDialogOption(
onPressed: () {
context.pop();
context.push("/settings/dictionary/${dictionary.id}");
context.push("/settings/${dictionary.id}");
},
child: ListTile(
leading: Icon(Icons.settings),
Expand Down
Loading

0 comments on commit 0c53e67

Please sign in to comment.