Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Migrate from sqflite to drift packages #1266

Merged
merged 6 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ PODS:
- sqflite (0.0.3):
- Flutter
- FlutterMacOS
- sqlite3 (3.45.1):
- sqlite3/common (= 3.45.1)
- sqlite3/common (3.45.1)
- sqlite3/fts5 (3.45.1):
- sqlite3/common
- sqlite3/perf-threadsafe (3.45.1):
- sqlite3/common
- sqlite3/rtree (3.45.1):
- sqlite3/common
- sqlite3_flutter_libs (0.0.1):
- Flutter
- sqlite3 (~> 3.45.1)
- sqlite3/fts5
- sqlite3/perf-threadsafe
- sqlite3/rtree
- uni_links (0.0.1):
- Flutter
- url_launcher_ios (0.0.1):
Expand Down Expand Up @@ -67,10 +82,15 @@ DEPENDENCIES:
- share_plus (from `.symlinks/plugins/share_plus/ios`)
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)
- sqflite (from `.symlinks/plugins/sqflite/darwin`)
- sqlite3_flutter_libs (from `.symlinks/plugins/sqlite3_flutter_libs/ios`)
- uni_links (from `.symlinks/plugins/uni_links/ios`)
- url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`)
- webview_flutter_wkwebview (from `.symlinks/plugins/webview_flutter_wkwebview/ios`)

SPEC REPOS:
trunk:
- sqlite3

EXTERNAL SOURCES:
background_fetch:
:path: ".symlinks/plugins/background_fetch/ios"
Expand Down Expand Up @@ -110,6 +130,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/shared_preferences_foundation/darwin"
sqflite:
:path: ".symlinks/plugins/sqflite/darwin"
sqlite3_flutter_libs:
:path: ".symlinks/plugins/sqlite3_flutter_libs/ios"
uni_links:
:path: ".symlinks/plugins/uni_links/ios"
url_launcher_ios:
Expand Down Expand Up @@ -137,6 +159,8 @@ SPEC CHECKSUMS:
share_plus: 8875f4f2500512ea181eef553c3e27dba5135aad
shared_preferences_foundation: b4c3b4cddf1c21f02770737f147a3f5da9d39695
sqflite: 673a0e54cc04b7d6dba8d24fb8095b31c3a99eec
sqlite3: 73b7fc691fdc43277614250e04d183740cb15078
sqlite3_flutter_libs: af0e8fe9bce48abddd1ffdbbf839db0302d72d80
uni_links: d97da20c7701486ba192624d99bffaaffcfc298a
url_launcher_ios: 6116280ddcfe98ab8820085d8d76ae7449447586
webview_flutter_wkwebview: be0f0d33777f1bfd0c9fdcb594786704dbf65f36
Expand Down
90 changes: 38 additions & 52 deletions lib/account/models/account.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import 'package:sqflite/sqflite.dart';
import 'package:thunder/core/singletons/database.dart';
import 'package:flutter/foundation.dart';

import 'package:drift/drift.dart';

import 'package:thunder/core/database/database.dart';
import 'package:thunder/main.dart';

class Account {
final String id;
Expand All @@ -16,74 +20,56 @@ class Account {
this.userId,
});

Map<String, dynamic> toMap() {
return {'accountId': id.toString(), 'username': username, 'jwt': jwt, 'instance': instance, 'userId': userId};
}

@override
String toString() {
return 'Account{accountId: $id, username: $username, instance: $instance, userId: $userId}';
}

static Future<void> insertAccount(Account account) async {
Database? database = await DB.instance.database;
if (database == null) return;

await database.insert(
'accounts',
account.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
try {
await database
.into(database.accounts)
.insert(AccountsCompanion.insert(username: Value(account.username), jwt: Value(account.jwt), instance: Value(account.instance), userId: Value(account.userId)));
} catch (e) {
debugPrint(e.toString());
}
}

// A method that retrieves all accounts from the database
static Future<List<Account>> accounts() async {
try {
Database? database = await DB.instance.database;
if (database == null) return [];

final List<Map<String, dynamic>> maps = await database.query('accounts');

return List.generate(maps.length, (i) {
return Account(
id: maps[i]['accountId'].toString(),
username: maps[i]['username'],
jwt: maps[i]['jwt'],
instance: maps[i]['instance'],
userId: maps[i]['userId'],
);
});
return (await database.accounts.all().get())
.map((account) => Account(id: account.id.toString(), username: account.username, jwt: account.jwt, instance: account.instance, userId: account.userId))
.toList();
} catch (e) {
debugPrint(e.toString());
return [];
}
}

static Future<Account?> fetchAccount(String accountId) async {
Database? database = await DB.instance.database;
if (accountId.isEmpty) return null;

final List<Map<String, dynamic>>? maps = await database?.query('accounts', where: 'accountId = ?', whereArgs: [accountId]);
if (maps == null || maps.isEmpty) return null;

return Account(
id: maps.first['accountId'].toString(),
username: maps.first['username'],
jwt: maps.first['jwt'],
instance: maps.first['instance'],
userId: maps.first['userId'],
);
try {
return await (database.select(database.accounts)..where((t) => t.id.equals(int.parse(accountId)))).getSingleOrNull().then((account) {
if (account == null) return null;
return Account(id: account.id.toString(), username: account.username, jwt: account.jwt, instance: account.instance, userId: account.userId);
});
} catch (e) {
debugPrint(e.toString());
}
}

static Future<void> updateAccount(Account account) async {
Database? database = await DB.instance.database;
if (database == null) return;

await database.update('accounts', account.toMap(), where: 'accountId = ?', whereArgs: [account.id]);
try {
await database
.update(database.accounts)
.replace(AccountsCompanion(id: Value(int.parse(account.id)), username: Value(account.username), jwt: Value(account.jwt), instance: Value(account.instance), userId: Value(account.userId)));
} catch (e) {
debugPrint(e.toString());
}
}

static Future<void> deleteAccount(String id) async {
Database? database = await DB.instance.database;
if (database == null) return;

await database.delete('accounts', where: 'accountId = ?', whereArgs: [id]);
try {
await (database.delete(database.accounts)..where((t) => t.id.equals(int.parse(id)))).go();
} catch (e) {
debugPrint(e.toString());
}
}
}
99 changes: 43 additions & 56 deletions lib/account/models/favourite.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:sqflite/sqflite.dart';
import 'package:drift/drift.dart';
import 'package:flutter/foundation.dart';

import 'package:thunder/core/singletons/database.dart';
import 'package:thunder/core/database/database.dart';
import 'package:thunder/main.dart';

class Favorite {
final String id;
Expand All @@ -13,78 +15,63 @@ class Favorite {
required this.accountId,
});

Map<String, dynamic> toMap() {
return {'id': id, 'communityId': communityId, 'accountId': accountId};
}

@override
String toString() {
return 'Favourite{id: $id, communityId: $communityId, accountId: $accountId}';
}

static Future<void> insertFavorite(Favorite favourite) async {
Database? database = await DB.instance.database;
if (database == null) return;

await database.insert(
'favorites',
favourite.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
try {
await database.into(database.favorites).insert(FavoritesCompanion.insert(
accountId: int.parse(favourite.accountId),
communityId: favourite.communityId,
));
} catch (e) {
debugPrint(e.toString());
}
}

// A method that retrieves all favourites from the database
static Future<List<Favorite>> favorites(String accountId) async {
try {
Database? database = await DB.instance.database;
if (database == null) return [];

final List<Map<String, dynamic>> maps = await database.query('favorites', where: 'accountId = ?', whereArgs: [accountId]);

return List.generate(maps.length, (i) {
return Favorite(
id: maps[i]['id'].toString(),
communityId: maps[i]['communityId'],
accountId: maps[i]['accountId'].toString(),
);
});
return (await database.favorites.all().get()).map((favorite) => Favorite(id: favorite.id.toString(), accountId: favorite.accountId.toString(), communityId: favorite.communityId)).toList();
} catch (e) {
debugPrint(e.toString());
return [];
}
}

static Future<Favorite?> fetchFavourite(String id) async {
Database? database = await DB.instance.database;

final List<Map<String, dynamic>>? maps = await database?.query('favorites', where: 'id = ?', whereArgs: [id]);
if (maps == null || maps.isEmpty) return null;

return Favorite(
id: maps.first['id'],
communityId: maps.first['communityId'],
accountId: maps.first['accountId'],
);
try {
return await (database.select(database.favorites)..where((t) => t.id.equals(int.parse(id)))).getSingleOrNull().then((favorite) {
if (favorite == null) return null;
return Favorite(id: favorite.id.toString(), accountId: favorite.accountId.toString(), communityId: favorite.communityId);
});
} catch (e) {
debugPrint(e.toString());
}
}

static Future<void> updateFavourite(Favorite favorite) async {
Database? database = await DB.instance.database;
if (database == null) return;

await database.update('favorites', favorite.toMap(), where: 'id = ?', whereArgs: [favorite.id]);
try {
await database.update(database.favorites).replace(FavoritesCompanion(
id: Value(int.parse(favorite.id)),
accountId: Value(int.parse(favorite.accountId)),
communityId: Value(favorite.communityId),
));
} catch (e) {
debugPrint(e.toString());
}
}

static Future<void> deleteFavorite({String? id, int? communityId}) async {
Database? database = await DB.instance.database;
if (database == null) return;

if (id != null) {
await database.delete('favorites', where: 'id = ?', whereArgs: [id]);
return;
}

if (communityId != null) {
await database.delete('favorites', where: 'communityId = ?', whereArgs: [communityId]);
return;
try {
if (id != null) {
await (database.delete(database.favorites)..where((t) => t.id.equals(int.parse(id)))).go();
return;
}

if (communityId != null) {
await (database.delete(database.favorites)..where((t) => t.communityId.equals(communityId))).go();
return;
}
} catch (e) {
debugPrint(e.toString());
}
}
}
56 changes: 26 additions & 30 deletions lib/community/models/anonymous_subscriptions.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import 'package:sqflite/sqflite.dart';
import 'package:thunder/core/singletons/database.dart';
import 'package:flutter/foundation.dart';

import 'package:drift/drift.dart';

import 'package:thunder/core/database/database.dart';
import 'package:thunder/main.dart';

class LocalCommunity {
final int id;
Expand All @@ -26,41 +30,33 @@ class AnonymousSubscriptions {

// To insert multiple communities to database
static Future<void> insertCommunities(Set<LocalCommunity> communities) async {
Database? database = await DB.instance.database;
if (database == null) return;

Batch batch = database.batch();
for (var element in communities) {
batch.insert("anonymous_subscriptions", element.toMap());
try {
for (LocalCommunity community in communities) {
await database
.into(database.localSubscriptions)
.insert(LocalSubscriptionsCompanion.insert(name: community.name, title: community.title, actorId: community.actorId, icon: Value(community.icon)));
}
} catch (e) {
debugPrint(e.toString());
}
batch.commit();
}

static Future<void> deleteCommunities(Set<int> ids) async {
Database? database = await DB.instance.database;
if (database == null) return;

Batch batch = database.batch();
for (var element in ids) {
batch.delete("anonymous_subscriptions", where: 'id = ?', whereArgs: [element]);
try {
await (database.delete(database.localSubscriptions)..where((t) => t.id.isIn(ids))).go();
} catch (e) {
debugPrint(e.toString());
}
batch.commit();
}

static Future<List<LocalCommunity>> getSubscribedCommunities() async {
Database? database = await DB.instance.database;
if (database == null) return [];

final List<Map<String, dynamic>> maps = await database.query('anonymous_subscriptions');

return List.generate(maps.length, (i) {
return LocalCommunity(
id: maps[i]["id"],
name: maps[i]["name"],
title: maps[i]["title"],
actorId: maps[i]["actorId"],
icon: maps[i]["icon"],
);
});
try {
return (await database.localSubscriptions.all().get())
.map((favorite) => LocalCommunity(id: favorite.id, name: favorite.name, title: favorite.title, actorId: favorite.actorId, icon: favorite.icon))
.toList();
} catch (e) {
debugPrint(e.toString());
return [];
}
}
}
Loading
Loading