Skip to content

Commit

Permalink
Fix missing field from db update
Browse files Browse the repository at this point in the history
  • Loading branch information
micahmo committed Jul 17, 2024
1 parent e2c01f3 commit 488982f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
8 changes: 7 additions & 1 deletion lib/account/models/account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Account {
username: username,
jwt: jwt,
instance: instance,
anonymous: anonymous,
userId: userId,
index: index ?? this.index,
);
Expand All @@ -53,6 +54,7 @@ class Account {
username: Value(account.username),
jwt: Value(account.jwt),
instance: Value(account.instance),
anonymous: Value(account.anonymous),
userId: Value(account.userId),
listIndex: newIndex,
),
Expand Down Expand Up @@ -81,8 +83,8 @@ class Account {
username: Value(anonymousInstance.username),
jwt: Value(anonymousInstance.jwt),
instance: Value(anonymousInstance.instance),
userId: Value(anonymousInstance.userId),
anonymous: Value(anonymousInstance.anonymous),
userId: Value(anonymousInstance.userId),
listIndex: newIndex,
),
);
Expand All @@ -103,6 +105,7 @@ class Account {
username: account.username,
jwt: account.jwt,
instance: account.instance ?? '',
anonymous: account.anonymous,
userId: account.userId,
index: account.listIndex,
))
Expand All @@ -122,6 +125,7 @@ class Account {
username: account.username,
jwt: account.jwt,
instance: account.instance ?? '',
anonymous: account.anonymous,
userId: account.userId,
index: account.listIndex,
))
Expand All @@ -143,6 +147,7 @@ class Account {
username: account.username,
jwt: account.jwt,
instance: account.instance ?? '',
anonymous: account.anonymous,
userId: account.userId,
index: account.listIndex,
);
Expand All @@ -160,6 +165,7 @@ class Account {
username: Value(account.username),
jwt: Value(account.jwt),
instance: Value(account.instance),
anonymous: Value(account.anonymous),
userId: Value(account.userId),
listIndex: Value(account.index),
));
Expand Down
21 changes: 15 additions & 6 deletions lib/account/widgets/profile_modal_body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,7 @@ class _ProfileSelectState extends State<ProfileSelect> {
accounts!.insert(newIndex, item);
});

for (AccountExtended accountExtended in accounts!) {
Account.updateAccount(accountExtended.account.copyWith(index: accounts!.indexOf(accountExtended)));
}
_updateAccountIndices();
},
proxyDecorator: (child, index, animation) => Padding(
padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
Expand Down Expand Up @@ -459,9 +457,7 @@ class _ProfileSelectState extends State<ProfileSelect> {
anonymousInstances!.insert(newIndex, item);
});

for (AnonymousInstanceExtended anonymousInstanceExtended in anonymousInstances!) {
Account.updateAccount(anonymousInstanceExtended.anonymousInstance.copyWith(index: anonymousInstances!.indexOf(anonymousInstanceExtended)));
}
_updateAccountIndices();
},
proxyDecorator: (child, index, animation) => Padding(
padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
Expand Down Expand Up @@ -782,6 +778,19 @@ class _ProfileSelectState extends State<ProfileSelect> {
setState(() => anonymousInstanceExtended.latency = pingData.response?.time);
});
}

/// Recalculates the indices of all accounts and anonymous instances in the database, given the current order in the UI.
/// We need to calculate both accounts and anonymous instances, using an offset for the latter,
/// because they are separate lists in the UI but they are in the same database table.
void _updateAccountIndices() {
for (AccountExtended accountExtended in accounts!) {
Account.updateAccount(accountExtended.account.copyWith(index: accounts!.indexOf(accountExtended)));
}

for (AnonymousInstanceExtended anonymousInstanceExtended in anonymousInstances!) {
Account.updateAccount(anonymousInstanceExtended.anonymousInstance.copyWith(index: (accounts?.length ?? 0) + anonymousInstances!.indexOf(anonymousInstanceExtended)));
}
}
}

/// Wrapper class around Account with support for instance icon
Expand Down

0 comments on commit 488982f

Please sign in to comment.