Skip to content
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
1 change: 1 addition & 0 deletions lib/comment/view/create_comment_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ class _CreateCommentPageState extends State<CreateCommentPage> {
parentCommentId = parentCommentView.comment.id;
},
onUserChanged: () => userChanged = true,
enableAccountSwitching: widget.commentView == null,
),
),
const SizedBox(height: 10),
Expand Down
1 change: 1 addition & 0 deletions lib/community/pages/create_post_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ class _CreatePostPageState extends State<CreatePostPage> {
_validateSubmission();
},
onUserChanged: () => userChanged = true,
enableAccountSwitching: widget.postView == null,
),
const SizedBox(height: 12.0),
TypeAheadField<String>(
Expand Down
157 changes: 82 additions & 75 deletions lib/user/widgets/user_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class UserSelector extends StatefulWidget {
final String? parentCommentActorId;
final void Function(CommentView)? onParentCommentChanged;

/// Whether the user is allowed to change the active account
/// (e.g., it should not be allowed during edit)
final bool enableAccountSwitching;

const UserSelector({
super.key,
this.onUserChanged,
Expand All @@ -40,6 +44,7 @@ class UserSelector extends StatefulWidget {
this.onPostChanged,
this.parentCommentActorId,
this.onParentCommentChanged,
this.enableAccountSwitching = true,
});

@override
Expand All @@ -55,86 +60,88 @@ class _UserSelectorState extends State<UserSelector> {
offset: const Offset(-8, 0),
child: InkWell(
borderRadius: const BorderRadius.all(Radius.circular(50)),
onTap: () async {
final Account? originalUser = context.read<AuthBloc>().state.account;

await showProfileModalSheet(
context,
quickSelectMode: true,
customHeading: widget.profileModalHeading,
reloadOnSwitch: false,
);

// Wait slightly longer than the duration that is waited in the account switcher logic.
await Future.delayed(const Duration(milliseconds: 1500));

if (context.mounted) {
Account? newUser = context.read<AuthBloc>().state.account;

if (originalUser != null && newUser != null && originalUser.id != newUser.id) {
// The user changed. Reload the widget.
setState(() {});
widget.onUserChanged?.call();

// If there is a selected community, see if we can resolve it to the new user's instance.
if (widget.communityActorId?.isNotEmpty == true && widget.onCommunityChanged != null) {
CommunityView? resolvedCommunity;
try {
final ResolveObjectResponse resolveObjectResponse = await LemmyApiV3(newUser.instance!).run(ResolveObject(q: widget.communityActorId!));
resolvedCommunity = resolveObjectResponse.community;
} catch (e) {
// We'll just return null if we can't find it.
}
widget.onCommunityChanged!(resolvedCommunity);
}

// If there is a selected post, see if we can resolve it to the new user's instance.
if (widget.postActorId?.isNotEmpty == true && widget.onPostChanged != null) {
PostView? resolvedPost;
try {
final ResolveObjectResponse resolveObjectResponse = await LemmyApiV3(newUser.instance!).run(ResolveObject(q: widget.postActorId!));
resolvedPost = resolveObjectResponse.post;
if (resolvedPost != null) {
widget.onPostChanged!(resolvedPost);
}
} catch (e) {
// We will handle this below.
}
if (resolvedPost == null) {
// This is not allowed, so we must block the account switch.
showSnackbar(l10n.accountSwitchPostNotFound(newUser.instance!));
if (context.mounted) context.read<AuthBloc>().add(SwitchAccount(accountId: originalUser.id, reload: false));
}
}

// If there is a selected parent comment, see if we can resolve it to the new user's instance.
if (widget.parentCommentActorId?.isNotEmpty == true && widget.onParentCommentChanged != null) {
CommentView? resolvedComment;
try {
final ResolveObjectResponse resolveObjectResponse = await LemmyApiV3(newUser.instance!).run(ResolveObject(q: widget.parentCommentActorId!));
resolvedComment = resolveObjectResponse.comment;
if (resolvedComment != null) {
widget.onParentCommentChanged!(resolvedComment);
onTap: !widget.enableAccountSwitching
? null
: () async {
final Account? originalUser = context.read<AuthBloc>().state.account;

await showProfileModalSheet(
context,
quickSelectMode: true,
customHeading: widget.profileModalHeading,
reloadOnSwitch: false,
);

// Wait slightly longer than the duration that is waited in the account switcher logic.
await Future.delayed(const Duration(milliseconds: 1500));

if (context.mounted) {
Account? newUser = context.read<AuthBloc>().state.account;

if (originalUser != null && newUser != null && originalUser.id != newUser.id) {
// The user changed. Reload the widget.
setState(() {});
widget.onUserChanged?.call();

// If there is a selected community, see if we can resolve it to the new user's instance.
if (widget.communityActorId?.isNotEmpty == true && widget.onCommunityChanged != null) {
CommunityView? resolvedCommunity;
try {
final ResolveObjectResponse resolveObjectResponse = await LemmyApiV3(newUser.instance!).run(ResolveObject(q: widget.communityActorId!));
resolvedCommunity = resolveObjectResponse.community;
} catch (e) {
// We'll just return null if we can't find it.
}
widget.onCommunityChanged!(resolvedCommunity);
}

// If there is a selected post, see if we can resolve it to the new user's instance.
if (widget.postActorId?.isNotEmpty == true && widget.onPostChanged != null) {
PostView? resolvedPost;
try {
final ResolveObjectResponse resolveObjectResponse = await LemmyApiV3(newUser.instance!).run(ResolveObject(q: widget.postActorId!));
resolvedPost = resolveObjectResponse.post;
if (resolvedPost != null) {
widget.onPostChanged!(resolvedPost);
}
} catch (e) {
// We will handle this below.
}
if (resolvedPost == null) {
// This is not allowed, so we must block the account switch.
showSnackbar(l10n.accountSwitchPostNotFound(newUser.instance!));
if (context.mounted) context.read<AuthBloc>().add(SwitchAccount(accountId: originalUser.id, reload: false));
}
}

// If there is a selected parent comment, see if we can resolve it to the new user's instance.
if (widget.parentCommentActorId?.isNotEmpty == true && widget.onParentCommentChanged != null) {
CommentView? resolvedComment;
try {
final ResolveObjectResponse resolveObjectResponse = await LemmyApiV3(newUser.instance!).run(ResolveObject(q: widget.parentCommentActorId!));
resolvedComment = resolveObjectResponse.comment;
if (resolvedComment != null) {
widget.onParentCommentChanged!(resolvedComment);
}
} catch (e) {
// We will handle this below.
}
if (resolvedComment == null) {
// This is not allowed, so we must block the accout switch.
showSnackbar(l10n.accountSwitchParentCommentNotFound(newUser.instance!));
if (context.mounted) context.read<AuthBloc>().add(SwitchAccount(accountId: originalUser.id, reload: false));
}
}
}
} catch (e) {
// We will handle this below.
}
if (resolvedComment == null) {
// This is not allowed, so we must block the accout switch.
showSnackbar(l10n.accountSwitchParentCommentNotFound(newUser.instance!));
if (context.mounted) context.read<AuthBloc>().add(SwitchAccount(accountId: originalUser.id, reload: false));
}
}
}
}
},
child: const Padding(
padding: EdgeInsets.only(left: 8, top: 4, bottom: 4),
},
child: Padding(
padding: const EdgeInsets.only(left: 8, top: 4, bottom: 4),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
UserIndicator(),
Icon(Icons.chevron_right_rounded),
const UserIndicator(),
if (widget.enableAccountSwitching) const Icon(Icons.chevron_right_rounded),
],
),
),
Expand Down