Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into feature/reorder-…
Browse files Browse the repository at this point in the history
…accounts
  • Loading branch information
micahmo committed Jul 17, 2024
2 parents 1d38752 + 3233696 commit e2c01f3
Show file tree
Hide file tree
Showing 50 changed files with 883 additions and 390 deletions.
74 changes: 66 additions & 8 deletions lib/account/models/account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class Account {
final String? username;
final String? displayName;
final String? jwt;
final String? instance;
final String instance;
final bool anonymous;
final int? userId;
final int index;

Expand All @@ -19,7 +20,8 @@ class Account {
this.username,
this.displayName,
this.jwt,
this.instance,
this.anonymous = false,
required this.instance,
this.userId,
required this.index,
});
Expand All @@ -37,7 +39,7 @@ class Account {

static Future<Account?> insertAccount(Account account) async {
// If we are given a brand new account to insert with an existing id, something is wrong.
assert(account.id.isEmpty && account.index == -1);
assert(account.id.isEmpty && account.index == -1 && !account.anonymous);

try {
// Find the highest index in the current accounts
Expand All @@ -52,7 +54,7 @@ class Account {
jwt: Value(account.jwt),
instance: Value(account.instance),
userId: Value(account.userId),
listIndex: Value(newIndex),
listIndex: newIndex,
),
);

Expand All @@ -63,15 +65,63 @@ class Account {
}
}

// A method that retrieves all accounts from the database
static Future<Account?> insertAnonymousInstance(Account anonymousInstance) async {
// If we are given a brand new account to insert with an existing id, something is wrong.
assert(anonymousInstance.id.isEmpty && anonymousInstance.index == -1 && anonymousInstance.anonymous);

try {
// Find the highest index in the current accounts
final int maxIndex = await (database.selectOnly(database.accounts)..addColumns([database.accounts.listIndex.max()])).getSingle().then((row) => row.read(database.accounts.listIndex.max()) ?? 0);

// Assign the next index
final int newIndex = maxIndex + 1;

int id = await database.into(database.accounts).insert(
AccountsCompanion.insert(
username: Value(anonymousInstance.username),
jwt: Value(anonymousInstance.jwt),
instance: Value(anonymousInstance.instance),
userId: Value(anonymousInstance.userId),
anonymous: Value(anonymousInstance.anonymous),
listIndex: newIndex,
),
);

return anonymousInstance.copyWith(id: id.toString(), index: newIndex);
} catch (e) {
debugPrint(e.toString());
return null;
}
}

// A method that retrieves all accounts from the database. Does not include anonymous instances
static Future<List<Account>> accounts() async {
try {
return (await database.accounts.all().get())
return (await (database.select(database.accounts)..where((t) => t.anonymous.equals(false))).get())
.map((account) => Account(
id: account.id.toString(),
username: account.username,
jwt: account.jwt,
instance: account.instance,
instance: account.instance ?? '',
userId: account.userId,
index: account.listIndex,
))
.toList();
} catch (e) {
debugPrint(e.toString());
return [];
}
}

// A method that retrieves all anonymous instances from the database. Does not include logged in accounts.
static Future<List<Account>> anonymousInstances() async {
try {
return (await (database.select(database.accounts)..where((t) => t.anonymous.equals(true))).get())
.map((account) => Account(
id: account.id.toString(),
username: account.username,
jwt: account.jwt,
instance: account.instance ?? '',
userId: account.userId,
index: account.listIndex,
))
Expand All @@ -92,7 +142,7 @@ class Account {
id: account.id.toString(),
username: account.username,
jwt: account.jwt,
instance: account.instance,
instance: account.instance ?? '',
userId: account.userId,
index: account.listIndex,
);
Expand Down Expand Up @@ -125,4 +175,12 @@ class Account {
debugPrint(e.toString());
}
}

static Future<void> deleteAnonymousInstance(String instance) async {
try {
await (database.delete(database.accounts)..where((t) => t.instance.equals(instance) & t.anonymous.equals(true))).go();
} catch (e) {
debugPrint(e.toString());
}
}
}
83 changes: 0 additions & 83 deletions lib/account/models/anonymous_instance.dart

This file was deleted.

11 changes: 10 additions & 1 deletion lib/account/models/draft.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class Draft {
/// The URL of the post
final String? url;

/// The custom thumbnail of the post
final String? customThumbnail;

/// The body of the post/comment
final String? body;

Expand All @@ -34,6 +37,7 @@ class Draft {
this.replyId,
this.title,
this.url,
this.customThumbnail,
this.body,
});

Expand All @@ -44,6 +48,7 @@ class Draft {
int? replyId,
String? title,
String? url,
String? customThumbnail,
String? body,
}) =>
Draft(
Expand All @@ -53,11 +58,12 @@ class Draft {
replyId: replyId ?? this.replyId,
title: title ?? this.title,
url: url ?? this.url,
customThumbnail: customThumbnail ?? this.customThumbnail,
body: body ?? this.body,
);

/// See whether this draft contains enough info to save for a post
bool get isPostNotEmpty => title?.isNotEmpty == true || url?.isNotEmpty == true || body?.isNotEmpty == true;
bool get isPostNotEmpty => title?.isNotEmpty == true || url?.isNotEmpty == true || customThumbnail?.isNotEmpty == true || body?.isNotEmpty == true;

/// See whether this draft contains enough info to save for a comment
bool get isCommentNotEmpty => body?.isNotEmpty == true;
Expand All @@ -79,6 +85,7 @@ class Draft {
replyId: Value(draft.replyId),
title: Value(draft.title),
url: Value(draft.url),
customThumbnail: Value(draft.customThumbnail),
body: Value(draft.body),
),
);
Expand All @@ -92,6 +99,7 @@ class Draft {
replyId: Value(draft.replyId),
title: Value(draft.title),
url: Value(draft.url),
customThumbnail: Value(draft.customThumbnail),
body: Value(draft.body),
),
);
Expand Down Expand Up @@ -121,6 +129,7 @@ class Draft {
replyId: draft.replyId,
title: draft.title,
url: draft.url,
customThumbnail: draft.customThumbnail,
body: draft.body,
);
} catch (e) {
Expand Down
Loading

0 comments on commit e2c01f3

Please sign in to comment.