Skip to content

Commit

Permalink
Set default account from account selection
Browse files Browse the repository at this point in the history
  • Loading branch information
jcgurango committed Sep 19, 2023
1 parent c564cd8 commit 6d91fc7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 44 deletions.
41 changes: 20 additions & 21 deletions lib/pages/home_tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:math' show max;

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_initicon/flutter_initicon.dart';
import 'package:lemmy_api_client/v3.dart';

import '../hooks/infinite_scroll.dart';
Expand All @@ -14,7 +15,6 @@ import '../stores/accounts_store.dart';
import '../stores/config_store.dart';
import '../util/extensions/api.dart';
import '../util/goto.dart';
import '../widgets/avatar.dart';
import '../widgets/bottom_modal.dart';
import '../widgets/cached_network_image.dart';
import '../widgets/infinite_scroll.dart';
Expand Down Expand Up @@ -90,29 +90,21 @@ class HomeTab extends HookWidget {
]);

// specific account selection
final selectedUsername = useState<String?>(null);
final defaultUserData =
accStore.defaultUserDataFor(selectedList.value.instanceHost!);
final selectedUserData = selectedList.value.instanceHost != null
? (selectedUsername.value != null
? (accStore.userDataFor(selectedList.value.instanceHost!,
selectedUsername.value!) ??
defaultUserData)
: defaultUserData)
: null;
final personDetails = useMemoFuture(() async {
if (selectedUserData != null) {
return await LemmyApiV3(selectedUserData.instanceHost)
if (defaultUserData != null) {
return await LemmyApiV3(defaultUserData.instanceHost)
.run(GetPersonDetails(
personId: selectedUserData.userId,
personId: defaultUserData.userId,
savedOnly: false,
sort: SortType.active,
auth: selectedUserData.jwt.raw,
auth: defaultUserData.jwt.raw,
));
}

return null;
}, [selectedUserData]);
}, [defaultUserData]);

handleListChange() async {
final val = await showBottomModal<SelectedList>(
Expand Down Expand Up @@ -335,13 +327,20 @@ class HomeTab extends HookWidget {
.length >
1)
IconButton(
icon: Avatar(
originPreferredName: selectedUserData!.username,
url: personDetails.data?.personView.person.avatar,
radius: 16,
),
icon: personDetails.hasData &&
personDetails.data != null &&
personDetails.data!.personView.person.avatar !=
null
? CircleAvatar(
backgroundImage: NetworkImage(personDetails
.data!.personView.person.avatar!))
: Initicon(
text: defaultUserData!.username,
size: 32,
),
onPressed: () => ViewOnMenu.open(context, (userData) {
selectedUsername.value = userData.username;
accStore.setDefaultAccountFor(
userData.instanceHost, userData.username);
Navigator.of(context).pop();
}, title: Text(L10n.of(context).switch_account))),
PopupMenuButton(itemBuilder: (context) {
Expand Down Expand Up @@ -389,7 +388,7 @@ class HomeTab extends HookWidget {
body: InfiniteHomeList(
controller: isc,
selectedList: selectedList.value,
selectedUserData: selectedUserData,
selectedUserData: defaultUserData,
),
),
),
Expand Down
6 changes: 2 additions & 4 deletions lib/pages/manage_account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ class _ManageAccount extends HookWidget {
removeDelayedLoading.start();

try {
await accountsStore.removeAccount(
user.instanceHost, user.person.name);
accountsStore.removeAccount(user.instanceHost, user.person.name);
Navigator.of(context).pop();
} on Exception catch (err) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
Expand Down Expand Up @@ -285,8 +284,7 @@ class _ManageAccount extends HookWidget {
// auth: token.raw,
// ));

await accountsStore.removeAccount(
user.instanceHost, user.person.name);
accountsStore.removeAccount(user.instanceHost, user.person.name);
Navigator.of(context).pop();
} on Exception catch (err) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
Expand Down
4 changes: 2 additions & 2 deletions lib/pages/settings/add_account_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class AddAccountPage extends HookWidget {
// succeeded. This means that a users' very first account creation will
// run properly.
if (isFirstAccount) {
await accountsStore.setDefaultAccount(
accountsStore.setDefaultAccount(
selectedInstance.value, usernameController.text);
}

Expand All @@ -73,7 +73,7 @@ class AddAccountPage extends HookWidget {

// addAccount() run failed, so clear the account.
if (isFirstAccount && accountsStore.hasNoAccount) {
await accountsStore.clearDefaultAccount();
accountsStore.clearDefaultAccount();
}

// if first account try to import settings
Expand Down
4 changes: 2 additions & 2 deletions lib/pages/settings/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ class _AccountOptions extends HookWidget {
),
) ??
false) {
await accountsStore.removeAccount(instanceHost, username);
accountsStore.removeAccount(instanceHost, username);
if (context.mounted) {
Navigator.of(context).pop();
}
Expand Down Expand Up @@ -892,7 +892,7 @@ class AccountsConfigPage extends HookWidget {
),
) ??
false) {
await accountsStore.removeInstance(instanceHost);
accountsStore.removeInstance(instanceHost);
if (context.mounted) {
Navigator.of(context).pop();
}
Expand Down
38 changes: 23 additions & 15 deletions lib/stores/accounts_store.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:collection';
import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:json_annotation/json_annotation.dart';
Expand Down Expand Up @@ -32,8 +33,10 @@ class AccountsStore extends ChangeNotifier {
@JsonKey(defaultValue: {})
late Map<String, int> notificationCount = {};

AccountsStore? parent;

/// automatically sets default accounts
Future<void> _assignDefaultAccounts() async {
void _assignDefaultAccounts() {
// remove dangling defaults
defaultAccounts.entries
.map((dft) {
Expand Down Expand Up @@ -63,8 +66,7 @@ class AccountsStore extends ChangeNotifier {
if (!defaultAccounts.containsKey(instanceHost)) {
// select first account in this instance, if any
if (!isAnonymousFor(instanceHost)) {
await setDefaultAccountFor(
instanceHost, usernamesFor(instanceHost).first);
setDefaultAccountFor(instanceHost, usernamesFor(instanceHost).first);
}
}
}
Expand All @@ -75,8 +77,7 @@ class AccountsStore extends ChangeNotifier {
for (final instanceHost in instances) {
// select first account in this instance, if any
if (!isAnonymousFor(instanceHost)) {
await setDefaultAccount(
instanceHost, usernamesFor(instanceHost).first);
setDefaultAccount(instanceHost, usernamesFor(instanceHost).first);
}
}
}
Expand Down Expand Up @@ -182,22 +183,21 @@ class AccountsStore extends ChangeNotifier {
accounts[instanceHost]?.keys ?? const Iterable.empty();

/// sets globally default account
Future<void> setDefaultAccount(String instanceHost, String username) async {
void setDefaultAccount(String instanceHost, String username) {
defaultAccount = '$username@$instanceHost';

notifyListeners();
}

/// clear the globally default account
Future<void> clearDefaultAccount() async {
void clearDefaultAccount() {
defaultAccount = null;

notifyListeners();
}

/// sets default account for given instance
Future<void> setDefaultAccountFor(
String instanceHost, String username) async {
void setDefaultAccountFor(String instanceHost, String username) {
defaultAccounts[instanceHost] = username;

notifyListeners();
Expand Down Expand Up @@ -244,7 +244,7 @@ class AccountsStore extends ChangeNotifier {
username: userData.name,
);

await _assignDefaultAccounts();
_assignDefaultAccounts();
notifyListeners();
}

Expand All @@ -269,26 +269,26 @@ class AccountsStore extends ChangeNotifier {

accounts[instanceHost] = HashMap();

await _assignDefaultAccounts();
_assignDefaultAccounts();
notifyListeners();
}

/// This also removes all accounts assigned to this instance
Future<void> removeInstance(String instanceHost) async {
void removeInstance(String instanceHost) {
accounts.remove(instanceHost);

await _assignDefaultAccounts();
_assignDefaultAccounts();
notifyListeners();
}

Future<void> removeAccount(String instanceHost, String username) async {
void removeAccount(String instanceHost, String username) {
if (!accounts.containsKey(instanceHost)) {
throw Exception("instance doesn't exist");
}

accounts[instanceHost]!.remove(username);

await _assignDefaultAccounts();
_assignDefaultAccounts();
notifyListeners();
}

Expand All @@ -299,6 +299,14 @@ class AccountsStore extends ChangeNotifier {
Map<String, dynamic> toJson() {
return _$AccountsStoreToJson(this);
}

/// Returns a new account store with another account selected as the default instead.
AccountsStore selectAccount(String instanceHost, String username) {
return AccountsStore.fromJson(jsonDecode(jsonEncode(toJson())))
..setDefaultAccountFor(instanceHost, username)
..setDefaultAccount(instanceHost, username)
..parent = this;
}
}

/// Stores data associated with a logged in user
Expand Down

0 comments on commit 6d91fc7

Please sign in to comment.