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

Ability to re-order accounts #1494

Merged
merged 4 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'upstream/develop' into feature/reorder-…
…accounts
  • Loading branch information
micahmo committed Jul 17, 2024
commit e2c01f38f8a24e1b01da20d1f16ac4b290fcff60
29 changes: 24 additions & 5 deletions lib/account/models/account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,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 @@ -54,7 +54,7 @@ class Account {
jwt: Value(account.jwt),
instance: Value(account.instance),
userId: Value(account.userId),
listIndex: Value(newIndex),
listIndex: newIndex,
),
);

Expand Down Expand Up @@ -97,12 +97,31 @@ class Account {
// 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 ?? '',
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,
instance: account.instance ?? '',
userId: account.userId,
index: account.listIndex,
))
Expand All @@ -123,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
83 changes: 0 additions & 83 deletions lib/account/models/anonymous_instance.dart

This file was deleted.

40 changes: 33 additions & 7 deletions lib/account/pages/login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'package:go_router/go_router.dart';
import 'package:thunder/account/models/anonymous_instance.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:thunder/account/models/account.dart';

import 'package:thunder/core/auth/bloc/auth_bloc.dart';
import 'package:thunder/core/singletons/lemmy_client.dart';
import 'package:thunder/instances.dart';
import 'package:thunder/shared/dialogs.dart';
import 'package:thunder/shared/snackbar.dart';
Expand Down Expand Up @@ -327,7 +329,7 @@ class _LoginPageState extends State<LoginPage> with SingleTickerProviderStateMix
onSubmitted: !widget.anonymous
? (_) => _usernameFieldFocusNode.requestFocus()
: controller.text.isNotEmpty
? (_) => _addAnonymousInstance()
? (_) => _addAnonymousInstance(context)
: null,
),
suggestionsCallback: (String pattern) {
Expand Down Expand Up @@ -472,17 +474,41 @@ class _LoginPageState extends State<LoginPage> with SingleTickerProviderStateMix
final AppLocalizations l10n = AppLocalizations.of(context)!;

if (await isLemmyInstance(_instanceTextEditingController.text)) {
final List<AnonymousInstance> anonymousInstances = await AnonymousInstance.fetchAllInstances();
final List<Account> anonymousInstances = await Account.anonymousInstances();
if (anonymousInstances.any((anonymousInstance) => anonymousInstance.instance == _instanceTextEditingController.text)) {
setState(() {
instanceValidated = false;
instanceError = AppLocalizations.of(context)!.instanceHasAlreadyBenAdded(currentInstance ?? '');
});
} else {
context.read<AuthBloc>().add(const LogOutOfAllAccounts());
await AnonymousInstance.insertInstance(AnonymousInstance(id: '', instance: _instanceTextEditingController.text, index: -1));
context.read<ThunderBloc>().add(OnSetCurrentAnonymousInstance(_instanceTextEditingController.text));
widget.popRegister();
// Check for content warning on anyonmous instance
GetSiteResponse getSiteResponse = await (LemmyClient()..changeBaseUrl(_instanceTextEditingController.text)).lemmyApiV3.run(const GetSite());

bool acceptedContentWarning = true;

if (getSiteResponse.siteView.site.contentWarning?.isNotEmpty == true) {
acceptedContentWarning = false;

await showThunderDialog<void>(
context: context,
title: l10n.contentWarning,
contentText: getSiteResponse.siteView.site.contentWarning,
onSecondaryButtonPressed: (dialogContext) => Navigator.of(dialogContext).pop(),
secondaryButtonText: l10n.decline,
onPrimaryButtonPressed: (dialogContext, _) async {
Navigator.of(dialogContext).pop();
acceptedContentWarning = true;
},
primaryButtonText: l10n.accept,
);
}

if (acceptedContentWarning) {
context.read<AuthBloc>().add(const LogOutOfAllAccounts());
await Account.insertAnonymousInstance(Account(id: '', instance: _instanceTextEditingController.text, index: -1, anonymous: true));
context.read<ThunderBloc>().add(OnSetCurrentAnonymousInstance(_instanceTextEditingController.text));
widget.popRegister();
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/account/widgets/account_placeholder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AccountPlaceholder extends StatelessWidget {
@override
Widget build(BuildContext context) {
final ThemeData theme = Theme.of(context);
String? anonymousInstance = context.watch<ThunderBloc>().state.currentAnonymousInstance ?? '';
String anonymousInstance = context.watch<ThunderBloc>().state.currentAnonymousInstance ?? '';

return Center(
child: Padding(
Expand Down
Loading
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.