From 09d67cc67cf6bcc0015483d27150ecf8aaff06eb Mon Sep 17 00:00:00 2001 From: flow Date: Tue, 25 Jul 2023 17:59:32 +0800 Subject: [PATCH] add dangerous zone, could be convenient for debug --- lib/components/setting.dart | 95 +++++++++++++++++++++++++++-------- lib/repository/chat_room.dart | 16 ++++++ 2 files changed, 89 insertions(+), 22 deletions(-) diff --git a/lib/components/setting.dart b/lib/components/setting.dart index acb6a8e..9291e09 100644 --- a/lib/components/setting.dart +++ b/lib/components/setting.dart @@ -14,15 +14,51 @@ class SettingPage extends StatefulWidget { } class _SettingPageState extends State { - _onPressedReset() { - ChatRoomRepository().removeDatabase(); + _popDone(String content) { + if (context.mounted) { + showDialog( + context: context, + builder: (BuildContext context) => AlertDialog( + title: const Text('Done!'), + content: Text(content), + actions: [ + TextButton( + onPressed: () => Navigator.pop(context, 'OK'), + child: const Text('OK'), + ), + ], + ), + ); + } + } + + _onClearLocalMessage() async { + await ChatRoomRepository().removeDatabase(); ChatRoomController controller = Get.find(); controller.reset(); + _popDone("Cleared all local messages."); + } + + _onClearRemoteMessage() async { + final res = await ChatRoomRepository().removeDatabaseRemote(); + if (res) { + _popDone("Cleared all remote messages."); + } else { + _popDone("Clear remote messages failed."); + } } @override Widget build(BuildContext context) { const SizedBox sizedBoxSpace = SizedBox(height: 24); + const divider = Divider( + color: Colors.grey, + height: 10, + thickness: 1, + indent: 0, + endIndent: 0, + ); + return GestureDetector( onTap: () => FocusManager.instance.primaryFocus?.unfocus(), child: Scaffold( @@ -36,13 +72,10 @@ class _SettingPageState extends State { return ListView( padding: const EdgeInsets.only(left: 20.0, right: 20.0), children: [ + sizedBoxSpace, Row( mainAxisAlignment: MainAxisAlignment.end, children: [ - ElevatedButton( - onPressed: _onPressedReset, - child: const Text("Remove Cache")), - const SizedBox(width: 8), ElevatedButton( onPressed: () { controller.saveTmpOption(context: context); @@ -51,7 +84,7 @@ class _SettingPageState extends State { ), ], ), - sizedBoxSpace, + // sizedBoxSpace, Row( mainAxisAlignment: MainAxisAlignment.start, children: [ @@ -68,13 +101,7 @@ class _SettingPageState extends State { ), ], ), - const Divider( - color: Colors.grey, - height: 10, - thickness: 1, - indent: 0, - endIndent: 0, - ), + divider, sizedBoxSpace, Row( mainAxisAlignment: MainAxisAlignment.center, @@ -240,13 +267,7 @@ class _SettingPageState extends State { ), ], ), - const Divider( - color: Colors.grey, - height: 10, - thickness: 1, - indent: 0, - endIndent: 0, - ), + divider, sizedBoxSpace, SizedBox( height: 200, @@ -273,7 +294,37 @@ class _SettingPageState extends State { controller.setServerlessCmd(value); }, ), - ) + ), + Row( + mainAxisAlignment: MainAxisAlignment.start, + children: [ + const Text("Dangerous Zone"), + Tooltip( + message: "Don't use it!", + child: IconButton( + iconSize: 10.0, + splashRadius: 10, + color: Theme.of(context).colorScheme.primary, + onPressed: () {}, + icon: const Icon(Icons.question_mark), + ), + ), + ], + ), + divider, + sizedBoxSpace, + Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + ElevatedButton( + onPressed: _onClearLocalMessage, + child: const Text("Clear local messages")), + const SizedBox(width: 8), + ElevatedButton( + onPressed: _onClearRemoteMessage, + child: const Text("Clear remote messages")), + ], + ), ], ); }), diff --git a/lib/repository/chat_room.dart b/lib/repository/chat_room.dart index 9e09b7b..9445de3 100644 --- a/lib/repository/chat_room.dart +++ b/lib/repository/chat_room.dart @@ -138,6 +138,22 @@ class ChatRoomRepository { _database = null; } + Future removeDatabaseRemote() async { + final db = await getRemoteDb(); + if (db != null) { + var res = await db.execute("SHOW DATABASES LIKE 'moyubie';"); + if (res.rows.isNotEmpty) { + await db.execute("DROP DATABASE moyubie;"); + } + + await _remoteDatabase?.close(); + _remoteDatabase = null; + return true; + } else { + return false; + } + } + String remoteDBToString() { return "hose: $host, port: $port, userName: $userName, password: $password"; }