@@ -15,6 +15,7 @@ import "package:go_router/go_router.dart";
1515import "package:html_unescape/html_unescape_small.dart" ;
1616import "package:mime/mime.dart" ;
1717import "package:path/path.dart" ;
18+ import "package:path_provider/path_provider.dart" ;
1819import "package:provider/provider.dart" ;
1920
2021import "../database/dictionary/dictionary.dart" ;
@@ -100,12 +101,6 @@ class Mdict {
100101 Mdict ({required this .path});
101102
102103 Future <bool > add () async {
103- try {
104- await dictionaryListDao.getId (path);
105- return false ;
106- // ignore: empty_catches
107- } catch (e) {}
108-
109104 await _initDictReader (path);
110105
111106 await dictionaryListDao.add (path);
@@ -384,33 +379,35 @@ class Mdict {
384379
385380Future <void > selectMdx (BuildContext context, List <String > paths) async {
386381 for (final path in paths) {
387- if (context.mounted) {
388- Mdict ? tmpDict;
389- try {
390- final pathNoExtension = setExtension (path, "" );
391- tmpDict = Mdict (path: pathNoExtension);
392- await tmpDict.add ();
393- } catch (e) {
394- if (context.mounted) {
395- final snackBar =
396- SnackBar (content: Text (AppLocalizations .of (context)! .notSupport));
397- ScaffoldMessenger .of (context).showSnackBar (snackBar);
398- }
399- } finally {
400- if (tmpDict != null ) {
401- await tmpDict.close ();
402- }
403- }
382+ if (await dictionaryListDao.dictionaryExist (path)) {
383+ continue ;
404384 }
405385
406- if (paths.isNotEmpty && context.mounted) {
407- context.read <ManageDictionariesModel >().update ();
408- context.pop ();
386+ Mdict ? tmpDict;
387+ try {
388+ final pathNoExtension = setExtension (path, "" );
389+ tmpDict = Mdict (path: pathNoExtension);
390+ await tmpDict.add ();
391+ await tmpDict.close ();
392+ } catch (e) {
393+ if (context.mounted) {
394+ final snackBar =
395+ SnackBar (content: Text (AppLocalizations .of (context)! .notSupport));
396+ ScaffoldMessenger .of (context).showSnackBar (snackBar);
397+ }
398+
399+ // Why? Don't know. Strange!
400+ continue ;
409401 }
410402 }
403+
404+ if (paths.isNotEmpty && context.mounted) {
405+ context.read <ManageDictionariesModel >().update ();
406+ context.pop ();
407+ }
411408}
412409
413- Future <void > selectMdd (BuildContext context, List <String > paths) async {
410+ Future <void > selectAudioMdd (BuildContext context, List <String > paths) async {
414411 for (final path in paths) {
415412 if (await mddAudioListDao.existMddAudio (path)) {
416413 continue ;
@@ -466,7 +463,7 @@ Future<void> selectMdd(BuildContext context, List<String> paths) async {
466463 }
467464}
468465
469- Future <void > selectMdxOrMdd (BuildContext context, bool isMdx) async {
466+ Future <void > selectMdxOrMddOnDesktop (BuildContext context, bool isMdx) async {
470467 final XTypeGroup typeGroup = XTypeGroup (
471468 label: "${isMdx ? "MDX" : "MDD" } File" ,
472469 extensions: < String > [isMdx ? "mdx" : "mdd" ],
@@ -484,14 +481,14 @@ Future<void> selectMdxOrMdd(BuildContext context, bool isMdx) async {
484481 if (isMdx) {
485482 await selectMdx (context, files.map ((e) => e.path).toList ());
486483 } else {
487- await selectMdd (context, files.map ((e) => e.path).toList ());
484+ await selectAudioMdd (context, files.map ((e) => e.path).toList ());
488485 }
489486 }
490487}
491488
492489Future <void > findAllFileByExtension (
493- Directory dir , List <String > output, String extension ) async {
494- final entities = await dir .list ().toList ();
490+ Directory startDir , List <String > output, String extension ) async {
491+ final entities = await startDir .list ().toList ();
495492 for (final entity in entities) {
496493 if (entity is File ) {
497494 if (entity.path.endsWith (extension )) {
@@ -502,3 +499,12 @@ Future<void> findAllFileByExtension(
502499 }
503500 }
504501}
502+
503+ Future <List <String >> findMdxFilesOnAndroid () async {
504+ final documentsDir = Directory (
505+ join ((await getApplicationSupportDirectory ()).path, "dictionaries" ));
506+ final mdxFiles = < String > [];
507+ await findAllFileByExtension (documentsDir, mdxFiles, "mdx" );
508+
509+ return mdxFiles;
510+ }
0 commit comments