Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
YuJuncen committed Jul 24, 2023
2 parents cdb61f7 + 7143542 commit 13fa35e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 11 deletions.
27 changes: 23 additions & 4 deletions lib/components/chat_room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class ListPane extends StatelessWidget {
appBar: AppBar(
title: const Text("Chat Room"),
foregroundColor: Colors.white,
backgroundColor: Color.fromARGB(255, 70, 70, 70),
backgroundColor: const Color.fromARGB(255, 70, 70, 70),
toolbarHeight: 40,
automaticallyImplyLeading: false,
actions: const [NewChatButton()]),
Expand Down Expand Up @@ -139,7 +139,7 @@ class DetailsPane extends StatelessWidget {
child: Scaffold(
appBar: AppBar(
foregroundColor: Colors.white,
backgroundColor: Color.fromARGB(255, 70, 70, 70),
backgroundColor: const Color.fromARGB(255, 70, 70, 70),
toolbarHeight: 40,
automaticallyImplyLeading: false,
leading: onClose == null
Expand Down Expand Up @@ -198,6 +198,18 @@ class NewChatButton extends StatelessWidget {
),
),
),
PopupMenuItem(
child: ListTile(
leading: const Icon(Icons.download),
title: const Align(
alignment: Alignment(-1.2, 0),
child: Text("Load Chat Rooms"),
),
onTap: () {
_loadChatRooms(context);
},
)
),
];
},
);
Expand All @@ -217,6 +229,13 @@ class NewChatButton extends StatelessWidget {
messageController.messageList.value = [];
Navigator.pop(context);
}

_loadChatRooms(BuildContext context) {
final comp.ChatRoomController chatRoomController = Get.find();
chatRoomController.loadChatRooms();
Navigator.pop(context);

}
}

class ChatDetailButton extends StatefulWidget {
Expand Down Expand Up @@ -332,12 +351,12 @@ class _ChatDetailButtonState extends State<ChatDetailButton>
"Do you want to dismiss this chat room?",
style: dialogTextStyle,
),
actions: [
actions: const [
_DialogButton(
text: "Dismiss",
onPressed: _deleteChatRoom,
),
const _DialogButton(
_DialogButton(
text: "Cancel",
),
],
Expand Down
13 changes: 13 additions & 0 deletions lib/components/setting.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:moyubie/controller/chat_room.dart';
import 'package:moyubie/controller/settings.dart';
import 'package:get/get.dart';
import 'package:moyubie/repository/chat_room.dart';

class SettingPage extends StatefulWidget {
const SettingPage({super.key});
Expand All @@ -12,6 +14,12 @@ class SettingPage extends StatefulWidget {
}

class _SettingPageState extends State<SettingPage> {
_onPressedReset() {
ChatRoomRepository().removeDatabase();
ChatRoomController controller = Get.find();
controller.reset();
}

@override
Widget build(BuildContext context) {
const SizedBox sizedBoxSpace = SizedBox(height: 24);
Expand All @@ -31,6 +39,11 @@ class _SettingPageState extends State<SettingPage> {
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
ElevatedButton(
onPressed: _onPressedReset,
child: const Text("Remove Cache")
),
const SizedBox(width: 8),
ElevatedButton(
onPressed: () {
controller.saveTmpOption();
Expand Down
13 changes: 13 additions & 0 deletions lib/controller/chat_room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ class ChatRoomController extends GetxController {
currentChatRoomUuid.value = chatRoom.uuid;
update();
}

void loadChatRooms() async {
roomList.value = await ChatRoomRepository().getChatRoomsRemote();
await ChatRoomRepository().replaceLocalChatRooms(roomList);
update();
}

void reset() {
currentRoomIndex.value = IntegerWrapper(-1);
currentChatRoomUuid.value = "";
roomList.clear();
update();
}
}

class IntegerWrapper {
Expand Down
4 changes: 0 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ void main() async {
sqfliteFfiInit();
databaseFactory = databaseFactoryFfi;
}
// TODO(tangenta): only used for debug, remove it later.
// TODO: I think we should add a button in Settings panel to do the job.
// String path = join(await getDatabasesPath(), 'moyubie.db');
// await deleteDatabase(path);
runApp(const MyApp());
}

Expand Down
42 changes: 39 additions & 3 deletions lib/repository/chat_room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:mysql_client/exception.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import 'package:mysql_client/mysql_client.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';

class ChatRoom {
String uuid;
Expand Down Expand Up @@ -130,6 +131,12 @@ class ChatRoomRepository {
ChatRoomRepository.password = password;
}

removeDatabase() async {
String path = join(await getDatabasesPath(), 'moyubie.db');
await deleteDatabase(path);
_database = null;
}

String remoteDBToString() {
return "hose: $host, port: $port, userName: $userName, password: $password";
}
Expand All @@ -139,7 +146,7 @@ class ChatRoomRepository {
}

Future<MySQLConnection?> getRemoteDb({bool forceInit = false}) async {
bool shouldInit = _remoteDatabase == null || forceInit;
bool shouldInit = _remoteDatabase == null || !_remoteDatabase!.connected || forceInit;
if (host.isEmpty || (!isRemoteDBValid && !forceInit)) {
shouldInit = false;
}
Expand All @@ -162,9 +169,12 @@ class ChatRoomRepository {
_remoteDatabase = null;
});

await conn.execute("CREATE DATABASE IF NOT EXISTS moyubie;");
var res = await conn.execute("SHOW DATABASES LIKE 'moyubie';");
if (res.rows.isEmpty) {
await conn.execute("CREATE DATABASE IF NOT EXISTS moyubie;");
}
await conn.execute("USE moyubie;");
var res = await conn.execute("SHOW TABLES LIKE 'chat_room';");
res = await conn.execute("SHOW TABLES LIKE 'chat_room';");
if (res.rows.isEmpty) {
await conn.execute('''
CREATE TABLE IF NOT EXISTS $_tableChatRoom (
Expand Down Expand Up @@ -197,6 +207,32 @@ class ChatRoomRepository {
});
}

Future<List<ChatRoom>> getChatRoomsRemote() async {
final db = await getRemoteDb();
if (db == null) return Future(() => []);
var res = await db.execute("SELECT * FROM $_tableChatRoom;");
return res.rows.map((e) {
var maps = e.assoc();
return ChatRoom(
uuid: maps[_columnChatRoomUuid]!,
name: maps[_columnChatRoomName]!,
createTime: DateTime.parse(maps[_columnChatRoomCreateTime]!),
connectionToken: maps[_columnChatRoomConnectionToken]!,
);
}).toList();
}

Future<void> replaceLocalChatRooms(List<ChatRoom> rooms) async {
final db = await _getDb();
await db.execute("DELETE FROM $_tableChatRoom;");
for (var room in rooms) {
await db.insert(
_tableChatRoom,
room.toSQLMap(),
);
}
}

Future<void> addChatRoom(ChatRoom chatRoom) async {
final db = await _getDb();
await db.insert(
Expand Down

0 comments on commit 13fa35e

Please sign in to comment.