Skip to content

Commit 9b41508

Browse files
authored
Merge pull request #69 from mumu-lhl/refactor/69
2 parents 63c8f8c + 5adffbe commit 9b41508

File tree

5 files changed

+74
-86
lines changed

5 files changed

+74
-86
lines changed

lib/dictionary.dart

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,29 @@ class _Dict {
9595
await dictionaryList.updateFont(id!, path);
9696
}
9797

98+
Future<String> readWord(String word) async {
99+
late DictionaryData data;
100+
try {
101+
data = await db!.getOffset(word);
102+
} catch (e) {
103+
data = await db!.getOffset(word.toLowerCase());
104+
}
105+
106+
String content = await dict.reader!.readOne(data.blockOffset,
107+
data.startOffset, data.endOffset, data.compressedSize);
108+
109+
if (content.startsWith("@@@LINK=")) {
110+
// 8: remove @@@LINK=
111+
// content.length - 3: remove \r\n\x00
112+
data = await db!
113+
.getOffset(content.substring(8, content.length - 3).trimRight());
114+
content = await dict.reader!.readOne(data.blockOffset, data.startOffset,
115+
data.endOffset, data.compressedSize);
116+
}
117+
118+
return content;
119+
}
120+
98121
Future<void> removeDictionary(String path) async {
99122
reader = null;
100123
readerResource = null;
@@ -203,20 +226,4 @@ class _Dict {
203226
readerResource = null;
204227
}
205228
}
206-
207-
Future<String> readWord(DictionaryData word) async {
208-
String content = await dict.reader!.readOne(word.blockOffset,
209-
word.startOffset, word.endOffset, word.compressedSize);
210-
211-
if (content.startsWith("@@@LINK=")) {
212-
// 8: remove @@@LINK=
213-
// content.length - 3: remove \r\n\x00
214-
word = await dict.db!
215-
.getOffset(content.substring(8, content.length - 3).trimRight());
216-
content = await dict.reader!.readOne(word.blockOffset, word.startOffset,
217-
word.endOffset, word.compressedSize);
218-
}
219-
220-
return content;
221-
}
222229
}

lib/main.dart

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,8 @@ void main() async {
5151
if (call.method == "processText") {
5252
final text = call.arguments as String;
5353

54-
late DictionaryData word;
55-
try {
56-
word = await dict.db!.getOffset(text);
57-
} catch (e) {
58-
word = await dict.db!.getOffset(text.toLowerCase());
59-
}
60-
61-
final content = await dict.readWord(word);
62-
6354
// Navigate to search result with the text
64-
_router.go("/word", extra: {"content": content, "word": text});
55+
_router.go("/word", extra: {"word": text});
6556
}
6657
});
6758

@@ -86,12 +77,11 @@ final _router = GoRouter(
8677
path: "/word",
8778
builder: (context, state) {
8879
final extra = state.extra as Map<String, String>;
89-
return WebviewDisplay(
90-
content: extra["content"]!,
91-
word: extra["word"]!,
92-
description: extra.containsKey("description"),
93-
);
80+
return WebviewDisplay(word: extra["word"]!);
9481
}),
82+
GoRoute(
83+
path: "/description",
84+
builder: (context, state) => const WebviewDisplayDescription()),
9585
GoRoute(
9686
path: "/settings/dictionaries",
9787
builder: (context, state) => const ManageDictionaries()),

lib/pages/main/home.dart

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,7 @@ class SearchResult extends StatelessWidget {
6666
},
6767
),
6868
onTap: () async {
69-
final content = await dict.readWord(word);
70-
71-
if (context.mounted) {
72-
context
73-
.push("/word", extra: {"content": content, "word": word.key});
74-
}
69+
context.push("/word", extra: {"word": word.key});
7570
}));
7671
}
7772

lib/pages/manage_dictionaries/main.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,7 @@ class _ManageDictionariesState extends State<ManageDictionaries> {
234234
highlightElevation: 0,
235235
child: Icon(Icons.info),
236236
onPressed: () {
237-
context.push("/word", extra: {
238-
"content": dict.reader!.header["Description"]!,
239-
"word": "",
240-
"description": "1",
241-
});
237+
context.push("/description");
242238
},
243239
);
244240
return Column(

lib/pages/webview_display.dart

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,7 @@ class WebView extends StatelessWidget {
118118
id: 2,
119119
title: locale.lookup,
120120
action: () async {
121-
try {
122-
final word = await dict.db!.getOffset(selectedText);
123-
final content = await dict.readWord(word);
124-
125-
if (context.mounted) {
126-
context.push("/word",
127-
extra: {"content": content, "word": word.key});
128-
}
129-
} catch (e) {
130-
final snackBar = SnackBar(
131-
content: Text(locale.notFound),
132-
duration: const Duration(seconds: 1),
133-
);
134-
if (context.mounted) {
135-
ScaffoldMessenger.of(context).showSnackBar(snackBar);
136-
}
137-
}
121+
context.push("/word", extra: {"word": selectedText});
138122
}),
139123
ContextMenuItem(
140124
id: 3,
@@ -188,41 +172,57 @@ document.body.style.fontFamily = 'Custom Font';
188172
}
189173

190174
class WebviewDisplay extends StatelessWidget {
191-
final String content;
192175
final String word;
193-
final bool description;
194176

195-
const WebviewDisplay(
196-
{super.key,
197-
required this.content,
198-
required this.word,
199-
required this.description});
177+
const WebviewDisplay({super.key, required this.word});
200178

201179
@override
202180
Widget build(BuildContext context) {
203-
Widget? floatingActionButton;
204-
late String html;
205-
if (description) {
206-
html = HtmlUnescape().convert(content);
207-
} else {
208-
html = content;
209-
floatingActionButton = Button(word: word);
210-
}
181+
final content = dict.readWord(word);
182+
183+
return Scaffold(
184+
appBar: AppBar(leading: BackButton(
185+
onPressed: () {
186+
if (context.canPop()) {
187+
context.pop();
188+
} else {
189+
// When opened from context menu
190+
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
191+
}
192+
},
193+
)),
194+
floatingActionButton: Button(word: word),
195+
body: FutureBuilder(
196+
future: content,
197+
builder: (context, snapshot) {
198+
if (snapshot.hasData) {
199+
return WebView(content: snapshot.data!);
200+
} else if (snapshot.hasError) {
201+
return Center(
202+
child: Text(AppLocalizations.of(context)!.notFound,
203+
style: Theme.of(context).textTheme.titleLarge));
204+
} else {
205+
return const Center(child: CircularProgressIndicator());
206+
}
207+
}));
208+
}
209+
}
210+
211+
class WebviewDisplayDescription extends StatelessWidget {
212+
const WebviewDisplayDescription({super.key});
213+
214+
@override
215+
Widget build(BuildContext context) {
216+
String html = dict.reader!.header["Description"]!;
217+
html = HtmlUnescape().convert(html);
211218

212219
return Scaffold(
213-
appBar: AppBar(leading: BackButton(
214-
onPressed: () {
215-
if (context.canPop()) {
220+
appBar: AppBar(leading: BackButton(
221+
onPressed: () {
216222
context.pop();
217-
} else {
218-
// When opened from context menu
219-
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
220-
}
221-
},
222-
)),
223-
floatingActionButton: floatingActionButton,
224-
body: WebView(content: html),
225-
);
223+
},
224+
)),
225+
body: WebView(content: html));
226226
}
227227
}
228228

0 commit comments

Comments
 (0)