Skip to content

Commit

Permalink
refactor: refactor manage dictionaries code
Browse files Browse the repository at this point in the history
  • Loading branch information
mumu-lhl committed Dec 21, 2024
1 parent 59c8ca5 commit b812f7a
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 156 deletions.
332 changes: 177 additions & 155 deletions lib/pages/manage_dictionaries/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@ import "package:go_router/go_router.dart";
import "package:path/path.dart";
import "package:permission_handler/permission_handler.dart";

const XTypeGroup typeGroup = XTypeGroup(
label: "custom",
extensions: <String>["mdx"],
);

void showPermissionDenied(BuildContext context) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(AppLocalizations.of(context)!.permissionDenied),
action: SnackBarAction(
label: AppLocalizations.of(context)!.close, onPressed: () {}),
));
}

class ManageDictionaries extends StatefulWidget {
const ManageDictionaries({super.key});

Expand All @@ -39,69 +26,19 @@ class _ManageDictionariesState extends State<ManageDictionaries> {

@override
Widget build(BuildContext context) {
final colorScheme = Theme.of(context).colorScheme;
final body = FutureBuilder(
future: dictionaries,
builder: (BuildContext context,
AsyncSnapshot<List<DictionaryListData>> snapshot) {
final children = <Widget>[];

if (snapshot.hasData) {
final dictionaries = snapshot.data!;
for (final dictionary in dictionaries) {
children.add(Card(
elevation: 0,
color: colorScheme.onInverseSurface,
child: RadioListTile(
title: Text(basename(dictionary.path)),
value: dictionary.id,
groupValue: dict!.id,
onChanged: (int? id) async {
await _changeDictionary(dictionary.path);
setState(() {});
},
secondary: IconButton(
icon: const Icon(Icons.delete),
onPressed: () async {
if (dictionary.id == dict!.id) {
dict!.removeDictionary();

if (dictionaries.length == 1) {
dict = null;
await prefs.remove("currentDictionaryPath");
} else {
final index = dictionaries.indexOf(dictionary);
if (index + 1 == dictionaries.length) {
await _changeDictionary(
dictionaries[index - 1].path);
} else {
await _changeDictionary(dictionaries.last.path);
}
}
} else {
final tmpDict = Mdict(path: dictionary.path);
await tmpDict.init();
await tmpDict.removeDictionary();
await tmpDict.close();
}

setState(() {
updateDictionaries();
});
},
))));
}
}

if (children.isEmpty) {
return Center(child: Text(AppLocalizations.of(context)!.empty));
} else {
return ListView(children: children);
}
},
return Scaffold(
appBar: AppBar(leading: buildReturnButton(context), actions: [
buildSettingScanPathButton(context),
buildRefreshButton(context),
buildAddButton(context)
]),
body: buildBody(context),
floatingActionButton: buildFloatingActionButton(),
);
}

final addButton = IconButton(
IconButton buildAddButton(BuildContext context) {
return IconButton(
icon: const Icon(Icons.add),
onPressed: () async {
String? path;
Expand Down Expand Up @@ -132,6 +69,11 @@ class _ManageDictionariesState extends State<ManageDictionaries> {
rootDirectory: Directory("/storage/emulated/0/"));
}
} else {
const XTypeGroup typeGroup = XTypeGroup(
label: "custom",
extensions: <String>["mdx"],
);

final file = await openFile(acceptedTypeGroups: [typeGroup]);
path = file?.path;
}
Expand Down Expand Up @@ -164,8 +106,148 @@ class _ManageDictionariesState extends State<ManageDictionaries> {
}
},
);
}

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

return FutureBuilder(
future: dictionaries,
builder: (BuildContext context,
AsyncSnapshot<List<DictionaryListData>> snapshot) {
final children = <Widget>[];

if (snapshot.hasData) {
final dictionaries = snapshot.data!;
for (final dictionary in dictionaries) {
children.add(
buildDictionaryCard(colorScheme, dictionary, dictionaries));
}
}

if (children.isEmpty) {
return Center(child: Text(AppLocalizations.of(context)!.empty));
} else {
return ListView(children: children);
}
},
);
}

Card buildDictionaryCard(ColorScheme colorScheme,
DictionaryListData dictionary, List<DictionaryListData> dictionaries) {
return Card(
elevation: 0,
color: colorScheme.onInverseSurface,
child: RadioListTile(
title: Text(basename(dictionary.path)),
value: dictionary.id,
groupValue: dict!.id,
onChanged: (int? id) async {
await _changeDictionary(dictionary.path);
setState(() {});
},
secondary: buildRemoveButton(dictionary, dictionaries)));
}

FutureBuilder<List<DictionaryListData>> buildFloatingActionButton() {
return FutureBuilder(
future: dictionaries,
builder: (BuildContext context,
AsyncSnapshot<List<DictionaryListData>> snapshot) {
if (snapshot.hasData && snapshot.data!.isNotEmpty) {
final settingsButton = FloatingActionButton.small(
elevation: 0,
highlightElevation: 0,
child: Icon(Icons.settings),
onPressed: () {
context.push("/settings/dictionary");
},
);
final infoButton = FloatingActionButton.small(
elevation: 0,
highlightElevation: 0,
child: Icon(Icons.info),
onPressed: () {
context.push("/description");
},
);
return Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [settingsButton, infoButton],
);
}

return Text("");
},
);
}

IconButton buildRefreshButton(BuildContext context) {
return IconButton(
icon: const Icon(Icons.refresh),
onPressed: () async {
final paths = prefs.getStringList("scanPaths");
if (paths != null) {
showLoadingDialog(context);

try {
await _scanDictionaries(paths);
} finally {
setState(() {
context.pop();
updateDictionaries();
});
}
}
},
);
}

IconButton buildRemoveButton(
DictionaryListData dictionary, List<DictionaryListData> dictionaries) {
return IconButton(
icon: const Icon(Icons.delete),
onPressed: () async {
if (dictionary.id == dict!.id) {
dict!.removeDictionary();

final settingScanPathButton = IconButton(
if (dictionaries.length == 1) {
dict = null;
await prefs.remove("currentDictionaryPath");
} else {
final index = dictionaries.indexOf(dictionary);
if (index + 1 == dictionaries.length) {
await _changeDictionary(dictionaries[index - 1].path);
} else {
await _changeDictionary(dictionaries.last.path);
}
}
} else {
final tmpDict = Mdict(path: dictionary.path);
await tmpDict.init();
await tmpDict.removeDictionary();
await tmpDict.close();
}

setState(() {
updateDictionaries();
});
},
);
}

IconButton buildReturnButton(BuildContext context) {
return IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () {
context.pop();
},
);
}

IconButton buildSettingScanPathButton(BuildContext context) {
return IconButton(
icon: const Icon(Icons.folder),
onPressed: () async {
final paths = prefs.getStringList("scanPaths") ?? [];
Expand Down Expand Up @@ -211,74 +293,14 @@ class _ManageDictionariesState extends State<ManageDictionaries> {
}
},
);
}

final refreshButton = IconButton(
icon: const Icon(Icons.refresh),
onPressed: () async {
final paths = prefs.getStringList("scanPaths");
if (paths != null) {
showLoadingDialog(context);

try {
await _scanDictionaries(paths);
} finally {
setState(() {
context.pop();
updateDictionaries();
});
}
}
},
);

final returnButton = IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () {
context.pop();
},
);

final appBar = AppBar(
leading: returnButton,
actions: [settingScanPathButton, refreshButton, addButton]);

Widget? floatingActionButton;
floatingActionButton = FutureBuilder(
future: dictionaries,
builder: (BuildContext context,
AsyncSnapshot<List<DictionaryListData>> snapshot) {
if (snapshot.hasData && snapshot.data!.isNotEmpty) {
final settingsButton = FloatingActionButton.small(
elevation: 0,
highlightElevation: 0,
child: Icon(Icons.settings),
onPressed: () {
context.push("/settings/dictionary");
},
);
final infoButton = FloatingActionButton.small(
elevation: 0,
highlightElevation: 0,
child: Icon(Icons.info),
onPressed: () {
context.push("/description");
},
);
return Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [settingsButton, infoButton],
);
}

return Text("");
},
);

return Scaffold(
appBar: appBar,
body: body,
floatingActionButton: floatingActionButton,
);
void showPermissionDenied(BuildContext context) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(AppLocalizations.of(context)!.permissionDenied),
action: SnackBarAction(
label: AppLocalizations.of(context)!.close, onPressed: () {}),
));
}

void updateDictionaries() {
Expand Down Expand Up @@ -311,6 +333,19 @@ class _ManageDictionariesState extends State<ManageDictionaries> {
}
}

Future<void> _changeDictionary(String path) async {
await prefs.setString("currentDictionaryPath", path);
await dict!.close();
dict = Mdict(path: path);
await dict!.init();
}

Future<void> _removeScanPath(String path) async {
final paths = prefs.getStringList("scanPaths")!;
paths.remove(path);
prefs.setStringList("scanPaths", paths);
}

Future<void> _scanDictionaries(List<String> paths) async {
for (final path in paths) {
final dir = Directory(path);
Expand All @@ -324,17 +359,4 @@ class _ManageDictionariesState extends State<ManageDictionaries> {
}
}
}

Future<void> _changeDictionary(String path) async {
await prefs.setString("currentDictionaryPath", path);
await dict!.close();
dict = Mdict(path: path);
await dict!.init();
}

Future<void> _removeScanPath(String path) async {
final paths = prefs.getStringList("scanPaths")!;
paths.remove(path);
prefs.setStringList("scanPaths", paths);
}
}
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1272,5 +1272,5 @@ packages:
source: hosted
version: "3.1.2"
sdks:
dart: ">=3.5.0 <4.0.0"
dart: ">=3.6.0 <4.0.0"
flutter: ">=3.24.0"

0 comments on commit b812f7a

Please sign in to comment.