import 'dart:async';
|
|
import 'package:flutter/widgets.dart';
|
|
import 'package:path/path.dart';
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
|
Future<void> deleteDb() async {
|
|
final path = join(await getDatabasesPath(), 'envelope.db');
|
|
deleteDatabase(path);
|
|
}
|
|
|
|
Future<Database> getDatabaseConnection() async {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
final path = join(await getDatabasesPath(), 'envelope.db');
|
|
|
|
final database = openDatabase(
|
|
path,
|
|
onCreate: (db, version) async {
|
|
await db.execute(
|
|
'''
|
|
CREATE TABLE IF NOT EXISTS friends(
|
|
id TEXT PRIMARY KEY,
|
|
user_id TEXT,
|
|
username TEXT,
|
|
friend_id TEXT,
|
|
symmetric_key TEXT,
|
|
asymmetric_public_key TEXT,
|
|
accepted_at TEXT
|
|
);
|
|
''');
|
|
|
|
// TODO: Change users to use its own table, as it is a json blob
|
|
await db.execute(
|
|
'''
|
|
CREATE TABLE IF NOT EXISTS conversations(
|
|
id TEXT PRIMARY KEY,
|
|
user_id TEXT,
|
|
conversation_detail_id TEXT,
|
|
symmetric_key TEXT,
|
|
admin INTEGER,
|
|
name TEXT,
|
|
);
|
|
''');
|
|
|
|
await db.execute(
|
|
'''
|
|
CREATE TABLE IF NOT EXISTS conversation_users(
|
|
id TEXT PRIMARY KEY,
|
|
conversation_id TEXT,
|
|
username TEXT,
|
|
data TEXT,
|
|
association_key TEXT,
|
|
admin INTEGER
|
|
);
|
|
''');
|
|
|
|
await db.execute(
|
|
'''
|
|
CREATE TABLE IF NOT EXISTS messages(
|
|
id TEXT PRIMARY KEY,
|
|
symmetric_key TEXT,
|
|
user_symmetric_key TEXT,
|
|
data TEXT,
|
|
sender_id TEXT,
|
|
sender_username TEXT,
|
|
association_key TEXT,
|
|
created_at TEXT,
|
|
failed_to_send INTEGER
|
|
);
|
|
''');
|
|
|
|
},
|
|
// Set the version. This executes the onCreate function and provides a
|
|
// path to perform database upgrades and downgrades.
|
|
version: 1,
|
|
);
|
|
|
|
return database;
|
|
}
|