Skip to content

Commit

Permalink
connect chat_room to repository
Browse files Browse the repository at this point in the history
  • Loading branch information
tangenta committed Jul 21, 2023
1 parent f767799 commit 0b22773
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/components/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class _ChatWindowState extends State<ChatWindow> {
height: 48,
child: ElevatedButton(
onPressed: () {
_sendMessage();
// _sendMessage();
},
style: ElevatedButton.styleFrom(
shape: const RoundedRectangleBorder(
Expand Down
4 changes: 2 additions & 2 deletions lib/components/chat_room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ class NewChatButton extends StatelessWidget {

_addNewChatRoom() {
final comp.ChatRoomController chatRoomController = Get.find();
var uuid = const Uuid().toString();
const uuid = Uuid();
var createTime = DateTime.now();
repo.ChatRoom chatRoom = repo.ChatRoom(
uuid: uuid,
uuid: uuid.v4(),
name: "New Chat Room",
createTime: createTime,
connectionToken: "");
Expand Down
6 changes: 5 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'package:moyubie/configs/translations.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:webview_flutter/webview_flutter.dart';
import 'dart:io' show Platform;
import 'package:path/path.dart';

import 'components/chat_room.dart';
import 'controller/chat_room.dart';
Expand All @@ -28,6 +29,9 @@ void main() async {
if (kIsWeb) {
// Change default factory on the web
databaseFactory = databaseFactoryFfiWeb;
// TODO(tangenta): only used for debug, remove it later.
String path = join(await getDatabasesPath(), 'chatgpt.db');
await deleteDatabase(path);
} else if (Platform.isWindows || Platform.isLinux) {
sqfliteFfiInit();
databaseFactory = databaseFactoryFfi;
Expand Down Expand Up @@ -92,7 +96,7 @@ class MyApp extends StatelessWidget {
final ChatRoomType type = shortestSide < 600 ?
ChatRoomType.phone : ChatRoomType.tablet;
Get.put(SettingsController());
Get.put(ConversationController());
// Get.put(ConversationController());
Get.put(MessageController());
Get.put(PromptController());
Get.put(ChatRoomController());
Expand Down
15 changes: 8 additions & 7 deletions lib/repository/chat_room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ class ChatRoomRepository {
final String path = join(await getDatabasesPath(), 'chatgpt.db');
_database = await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
print("on create!");
await db.execute('''
CREATE TABLE $_tableChatRoom (
$_columnChatRoomUuid VARCHAR(36) PRIMARY KEY,
$_columnChatRoomName TEXT
$_columnChatRoomCreateTime TEXT
$_columnChatRoomName TEXT,
$_columnChatRoomCreateTime TEXT,
$_columnChatRoomConnectionToken TEXT
)
''');
Expand Down Expand Up @@ -119,11 +120,11 @@ class ChatRoomRepository {
conflictAlgorithm: ConflictAlgorithm.replace,
);
await db.execute('''
CREATE TABLE IF NOT EXISTS $chatRoom.uuid (
CREATE TABLE IF NOT EXISTS `${chatRoom.uuid}` (
$_columnMessageUuid VARCHAR(36) PRIMARY KEY,
$_columnMessageUserName TEXT
$_columnMessageCreateTime TEXT
$_columnMessageMessage TEXT
$_columnMessageUserName TEXT,
$_columnMessageCreateTime TEXT,
$_columnMessageMessage TEXT,
$_columnMessageSource TEXT
)
''');
Expand All @@ -148,7 +149,7 @@ class ChatRoomRepository {
whereArgs: [uuid],
);
});
await db.execute('DROP TABLE IF EXISTS $uuid');
await db.execute('DROP TABLE IF EXISTS `$uuid`');
}

Future<List<Message>> getMessagesByChatRoomUUid(String uuid) async {
Expand Down
10 changes: 5 additions & 5 deletions lib/repository/conversation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ class ConversationRepository {
static Database? _database;
static ConversationRepository? _instance;

ConversationRepository._internal();
// ConversationRepository._internal();

factory ConversationRepository() {
_instance ??= ConversationRepository._internal();
return _instance!;
}
// factory ConversationRepository() {
// _instance ??= ConversationRepository._internal();
// return _instance!;
// }

Future<Database> _getDb() async {
if (_database == null) {
Expand Down

0 comments on commit 0b22773

Please sign in to comment.