Mudança nos icones de ios.
This commit is contained in:
144
lib/database_helper.dart
Normal file
144
lib/database_helper.dart
Normal file
@@ -0,0 +1,144 @@
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import 'package:path/path.dart';
|
||||
|
||||
class ChatSession {
|
||||
final int? id;
|
||||
final String title;
|
||||
final String timestamp;
|
||||
|
||||
ChatSession({this.id, required this.title, required this.timestamp});
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'id': id,
|
||||
'title': title,
|
||||
'timestamp': timestamp,
|
||||
};
|
||||
}
|
||||
|
||||
factory ChatSession.fromMap(Map<String, dynamic> map) {
|
||||
return ChatSession(
|
||||
id: map['id'],
|
||||
title: map['title'],
|
||||
timestamp: map['timestamp'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ChatMessage {
|
||||
final int? id;
|
||||
final int sessionId;
|
||||
final String text;
|
||||
final bool isAssistant;
|
||||
final String timestamp;
|
||||
|
||||
ChatMessage({
|
||||
this.id,
|
||||
required this.sessionId,
|
||||
required this.text,
|
||||
required this.isAssistant,
|
||||
required this.timestamp,
|
||||
});
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'id': id,
|
||||
'sessionId': sessionId,
|
||||
'text': text,
|
||||
'isAssistant': isAssistant ? 1 : 0,
|
||||
'timestamp': timestamp,
|
||||
};
|
||||
}
|
||||
|
||||
factory ChatMessage.fromMap(Map<String, dynamic> map) {
|
||||
return ChatMessage(
|
||||
id: map['id'],
|
||||
sessionId: map['sessionId'],
|
||||
text: map['text'],
|
||||
isAssistant: map['isAssistant'] == 1,
|
||||
timestamp: map['timestamp'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class DatabaseHelper {
|
||||
static final DatabaseHelper instance = DatabaseHelper._init();
|
||||
static Database? _database;
|
||||
|
||||
DatabaseHelper._init();
|
||||
|
||||
Future<Database> get database async {
|
||||
if (_database != null) return _database!;
|
||||
_database = await _initDB('chat.db');
|
||||
return _database!;
|
||||
}
|
||||
|
||||
Future<Database> _initDB(String filePath) async {
|
||||
final dbPath = await getDatabasesPath();
|
||||
final path = join(dbPath, filePath);
|
||||
|
||||
return await openDatabase(
|
||||
path,
|
||||
version: 1,
|
||||
onCreate: _createDB,
|
||||
);
|
||||
}
|
||||
|
||||
Future _createDB(Database db, int version) async {
|
||||
await db.execute('''
|
||||
CREATE TABLE sessions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
title TEXT NOT NULL,
|
||||
timestamp TEXT NOT NULL
|
||||
)
|
||||
''');
|
||||
|
||||
await db.execute('''
|
||||
CREATE TABLE messages (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
sessionId INTEGER NOT NULL,
|
||||
text TEXT NOT NULL,
|
||||
isAssistant INTEGER NOT NULL,
|
||||
timestamp TEXT NOT NULL,
|
||||
FOREIGN KEY (sessionId) REFERENCES sessions (id) ON DELETE CASCADE
|
||||
)
|
||||
''');
|
||||
}
|
||||
|
||||
// Session operations
|
||||
Future<int> createSession(String title) async {
|
||||
final db = await instance.database;
|
||||
return await db.insert('sessions', {
|
||||
'title': title,
|
||||
'timestamp': DateTime.now().toIso8601String(),
|
||||
});
|
||||
}
|
||||
|
||||
Future<List<ChatSession>> getSessions() async {
|
||||
final db = await instance.database;
|
||||
final result = await db.query('sessions', orderBy: 'timestamp DESC');
|
||||
return result.map((json) => ChatSession.fromMap(json)).toList();
|
||||
}
|
||||
|
||||
Future<void> deleteSession(int id) async {
|
||||
final db = await instance.database;
|
||||
await db.delete('sessions', where: 'id = ?', whereArgs: [id]);
|
||||
}
|
||||
|
||||
// Message operations
|
||||
Future<int> insertMessage(ChatMessage message) async {
|
||||
final db = await instance.database;
|
||||
return await db.insert('messages', message.toMap());
|
||||
}
|
||||
|
||||
Future<List<ChatMessage>> getMessages(int sessionId) async {
|
||||
final db = await instance.database;
|
||||
final result = await db.query(
|
||||
'messages',
|
||||
where: 'sessionId = ?',
|
||||
whereArgs: [sessionId],
|
||||
orderBy: 'timestamp ASC',
|
||||
);
|
||||
return result.map((json) => ChatMessage.fromMap(json)).toList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user