Skip to content

Commit

Permalink
support load chat rooms from serverless TiDB
Browse files Browse the repository at this point in the history
  • Loading branch information
tangenta committed Jul 24, 2023
1 parent 2e3d361 commit 7143542
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 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
6 changes: 6 additions & 0 deletions lib/controller/chat_room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class ChatRoomController extends GetxController {
update();
}

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

void reset() {
currentRoomIndex.value = IntegerWrapper(-1);
currentChatRoomUuid.value = "";
Expand Down
35 changes: 32 additions & 3 deletions lib/repository/chat_room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,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 @@ -169,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 @@ -204,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 7143542

Please sign in to comment.