Skip to content

Commit

Permalink
feat: add loading dialog when adding dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
mumu-lhl committed Jan 18, 2025
1 parent 569a6fe commit a5b2110
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
20 changes: 11 additions & 9 deletions android/app/src/main/kotlin/org/eu/mumulhl/ciyue/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.flutter.plugin.common.MethodChannel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.withContext
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.launch
import java.io.File
Expand Down Expand Up @@ -67,6 +68,8 @@ class MainActivity : FlutterActivity() {

private fun openDocumentTree(data: Intent?) {
data?.data?.also { uri ->
methodChannel!!.invokeMethod("showLoadingDialog", null)

val takeFlags: Int = Intent.FLAG_GRANT_READ_URI_PERMISSION
applicationContext.contentResolver.takePersistableUriPermission(
uri, takeFlags
Expand All @@ -78,8 +81,7 @@ class MainActivity : FlutterActivity() {
if (!cacheDir.exists()) {
cacheDir.mkdir()
}
val targetFolder =
DocumentFileCompat.fromFile(applicationContext, cacheDir)!!
val targetFolder = DocumentFileCompat.fromFile(applicationContext, cacheDir)!!

ioScope.launch {
documents.copyFolderTo(applicationContext,
Expand All @@ -98,19 +100,19 @@ class MainActivity : FlutterActivity() {
conflictedFiles: MutableList<FileConflict>,
action: FolderContentConflictAction
) {
val newSolution =
ArrayList<FileConflict>(conflictedFiles.size)
val newSolution = ArrayList<FileConflict>(conflictedFiles.size)
conflictedFiles.forEach {
it.solution =
SingleFileConflictCallback.ConflictResolution.SKIP
it.solution = SingleFileConflictCallback.ConflictResolution.SKIP
}
newSolution.addAll(conflictedFiles)
action.confirmResolution(newSolution)

}
}).onCompletion {}.collect { _ -> }
}).onCompletion {
withContext(Dispatchers.Main) {
methodChannel!!.invokeMethod("inputDirectory", null)
}
}.collect { _ -> }
}
methodChannel!!.invokeMethod("inputDirectory", null)
}
}

Expand Down
27 changes: 19 additions & 8 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "package:ciyue/pages/manage_dictionaries/main.dart";
import "package:ciyue/pages/manage_dictionaries/settings_dictionary.dart";
import "package:ciyue/pages/webview_display.dart";
import "package:ciyue/settings.dart";
import "package:ciyue/widget/loading_dialog.dart";
import "package:drift/drift.dart";
import "package:dynamic_color/dynamic_color.dart";
import "package:flutter/material.dart";
Expand Down Expand Up @@ -43,7 +44,7 @@ Future<void> updateAllDictionaries() async {
final cacheDir = Directory(
join((await getApplicationCacheDirectory()).path, "dictionaries_cache"));
final entities = await cacheDir.list().toList();
_addDictionaries(entities);
await _addDictionaries(entities);
}

void main() async {
Expand All @@ -66,13 +67,21 @@ void main() async {
packageInfo = await PackageInfo.fromPlatform();

platform.setMethodCallHandler((call) async {
if (call.method == "processText") {
final text = call.arguments as String;

// Navigate to search result with the text
_router.go("/word", extra: {"word": text});
} else if (call.method == "inputDirectory") {
updateAllDictionaries();
switch (call.method) {
case "processText":
final text = call.arguments as String;

// Navigate to search result with the text
_router.go("/word", extra: {"word": text});
break;
case "inputDirectory":
await updateAllDictionaries();
_router.pop();
updateManageDictionariesPage();
break;
case "showLoadingDialog":
showLoadingDialog(_navigatorKey.currentContext!);
break;
}
});

Expand All @@ -92,7 +101,9 @@ late final VoidCallback refreshAll;
final WordbookDao wordbookDao = WordbookDao(mainDatabase);
final WordbookTagsDao wordbookTagsDao = WordbookTagsDao(mainDatabase);

final _navigatorKey = GlobalKey<NavigatorState>();
final _router = GoRouter(
navigatorKey: _navigatorKey,
routes: [
GoRoute(
path: "/",
Expand Down
24 changes: 16 additions & 8 deletions lib/pages/manage_dictionaries/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import "package:flutter_gen/gen_l10n/app_localizations.dart";
import "package:go_router/go_router.dart";
import "package:path/path.dart";

late VoidCallback updateManageDictionariesPage;

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

Expand All @@ -20,6 +22,13 @@ class ManageDictionaries extends StatefulWidget {
class _ManageDictionariesState extends State<ManageDictionaries> {
var dictionaries = dictionaryListDao.all();

@override
void initState() {
super.initState();

updateManageDictionariesPage = updateDictionaries;
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -192,10 +201,9 @@ class _ManageDictionariesState extends State<ManageDictionaries> {
}
}

setState(() {
context.pop();
updateDictionaries();
});
if (context.mounted) context.pop();

updateDictionaries();
}
},
);
Expand Down Expand Up @@ -287,9 +295,7 @@ class _ManageDictionariesState extends State<ManageDictionaries> {
await tmpDict.close();
}

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

if (context.mounted) context.pop();
},
Expand Down Expand Up @@ -364,6 +370,8 @@ class _ManageDictionariesState extends State<ManageDictionaries> {
}

void updateDictionaries() {
dictionaries = dictionaryListDao.all();
setState(() {
dictionaries = dictionaryListDao.all();
});
}
}

0 comments on commit a5b2110

Please sign in to comment.