18e0e565ca
The Claude account dialog shows 'Auto-switch account when usage runs out' (claude_account_auto_switch) when the server reports it. The usage-limit banner schedules no auto-continue while the server's auto-switch continue prompt is queued. A chat refused after another chat already switched the Claude home gets a fresh switch stamp, so its banner clears too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
250 lines
7.8 KiB
Dart
250 lines
7.8 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../../core/error_reason.dart';
|
|
import '../../../ui/adaptive/app_popups.dart';
|
|
import '../../../ui/layout/app_sheet_header.dart';
|
|
import '../domain/chat_claude_accounts.dart';
|
|
import 'chat_claude_accounts_controller.dart';
|
|
|
|
/// Switches the chat's Claude home to the saved account [account] and says
|
|
/// so in a snack bar; a refusal is shown the same way. [messenger] is taken
|
|
/// before any sheet closes, so the confirmation survives it.
|
|
Future<void> switchChatClaudeAccount(
|
|
ScaffoldMessengerState? messenger,
|
|
ChatClaudeAccountsController accounts,
|
|
ChatClaudeAccount account,
|
|
) async {
|
|
try {
|
|
final active = await accounts.use(account.name);
|
|
messenger?.showSnackBar(
|
|
SnackBar(content: Text(chatClaudeSwitchedMessage(active ?? account))),
|
|
);
|
|
} catch (error) {
|
|
_failed(messenger, error, 'The Claude account was not switched.');
|
|
}
|
|
}
|
|
|
|
void _failed(ScaffoldMessengerState? messenger, Object error, String fallback) {
|
|
messenger?.showSnackBar(
|
|
SnackBar(content: Text(describeErrorReason(error, fallback: fallback))),
|
|
);
|
|
}
|
|
|
|
/// The chat menu's "Claude account": the saved accounts of the chat's Claude
|
|
/// home with the active one checked (one tap switches), "Save current
|
|
/// account…" for the active login and the home's "Auto-switch account when
|
|
/// usage runs out" choice. A dialog on a wide app, a bottom sheet on
|
|
/// a phone.
|
|
Future<void> showChatClaudeAccountsDialog(
|
|
BuildContext context,
|
|
ChatClaudeAccountsController accounts,
|
|
) {
|
|
unawaited(accounts.refresh());
|
|
final messenger = ScaffoldMessenger.maybeOf(context);
|
|
return showAppDetails<void>(
|
|
context,
|
|
maxWidth: 440,
|
|
builder: (sheetContext) => Column(
|
|
key: const Key('chat-claude-accounts'),
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
const AppSheetHeader(title: 'Claude account'),
|
|
ListenableBuilder(
|
|
listenable: accounts,
|
|
builder: (context, _) => _AccountList(
|
|
accounts: accounts,
|
|
onSwitch: (account) {
|
|
Navigator.of(sheetContext).pop();
|
|
unawaited(switchChatClaudeAccount(messenger, accounts, account));
|
|
},
|
|
onSave: () async {
|
|
final name = await _askName(sheetContext, accounts.accounts);
|
|
if (name == null) return;
|
|
try {
|
|
await accounts.save(name);
|
|
} catch (error) {
|
|
_failed(messenger, error, 'The account was not saved.');
|
|
}
|
|
},
|
|
onAutoSwitch: (on) async {
|
|
try {
|
|
await accounts.setAutoSwitch(on);
|
|
} catch (error) {
|
|
_failed(messenger, error, 'The setting was not changed.');
|
|
}
|
|
},
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
class _AccountList extends StatelessWidget {
|
|
const _AccountList({
|
|
required this.accounts,
|
|
required this.onSwitch,
|
|
required this.onSave,
|
|
required this.onAutoSwitch,
|
|
});
|
|
|
|
final ChatClaudeAccountsController accounts;
|
|
final ValueChanged<ChatClaudeAccount> onSwitch;
|
|
final VoidCallback onSave;
|
|
final ValueChanged<bool> onAutoSwitch;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final list = accounts.accounts;
|
|
final error = accounts.error;
|
|
if (list == null) {
|
|
return Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 8),
|
|
child: error == null
|
|
? const LinearProgressIndicator()
|
|
: Text(
|
|
describeErrorReason(
|
|
error,
|
|
fallback: 'Claude accounts are not available.',
|
|
),
|
|
key: const Key('chat-claude-accounts-error'),
|
|
),
|
|
);
|
|
}
|
|
final unsaved = list.canSaveActive ? list.active : null;
|
|
return Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
if (list.accounts.isEmpty && unsaved == null)
|
|
const ListTile(title: Text('No saved accounts')),
|
|
for (final account in list.accounts)
|
|
_AccountTile(
|
|
account: account,
|
|
onTap: account.active || accounts.busy
|
|
? null
|
|
: () => onSwitch(account),
|
|
),
|
|
if (unsaved != null)
|
|
_AccountTile(account: unsaved, onTap: null, note: 'Not saved'),
|
|
if (accounts.busy) const LinearProgressIndicator(),
|
|
const Divider(),
|
|
ListTile(
|
|
key: const Key('chat-claude-account-save'),
|
|
leading: const Icon(Icons.bookmark_add_outlined),
|
|
title: const Text('Save current account…'),
|
|
enabled: list.active != null && !accounts.busy,
|
|
onTap: onSave,
|
|
),
|
|
if (list.autoSwitch case final on?)
|
|
CheckboxListTile(
|
|
key: const Key('chat-claude-account-auto-switch'),
|
|
value: on,
|
|
onChanged: accounts.busy
|
|
? null
|
|
: (value) => onAutoSwitch(value ?? false),
|
|
title: const Text('Auto-switch account when usage runs out'),
|
|
subtitle: const Text(
|
|
'Continues on a saved account with usage left, after asking '
|
|
'the agent to check it is on track',
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
class _AccountTile extends StatelessWidget {
|
|
const _AccountTile({required this.account, required this.onTap, this.note});
|
|
|
|
final ChatClaudeAccount account;
|
|
final VoidCallback? onTap;
|
|
final String? note;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final detail = [
|
|
if (account.name != account.label) account.name,
|
|
if (account.organization.isNotEmpty) account.organization,
|
|
if (account.plan.isNotEmpty) account.plan,
|
|
?note,
|
|
].join(' · ');
|
|
return ListTile(
|
|
key: Key('chat-claude-account-${account.name}'),
|
|
leading: const Icon(Icons.account_circle_outlined),
|
|
title: Text(account.label),
|
|
subtitle: detail.isEmpty ? null : Text(detail),
|
|
trailing: account.active
|
|
? Icon(
|
|
Icons.check,
|
|
color: Theme.of(context).colorScheme.primary,
|
|
semanticLabel: 'Active',
|
|
)
|
|
: null,
|
|
selected: account.active,
|
|
// An active row is informative, not disabled-looking.
|
|
enabled: onTap != null || account.active,
|
|
onTap: onTap,
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Asks for the saved account's name, prefilled with the login's email.
|
|
Future<String?> _askName(BuildContext context, ChatClaudeAccounts? accounts) =>
|
|
showDialog<String>(
|
|
context: context,
|
|
builder: (context) => _NameDialog(initial: accounts?.active?.email ?? ''),
|
|
);
|
|
|
|
class _NameDialog extends StatefulWidget {
|
|
const _NameDialog({required this.initial});
|
|
|
|
final String initial;
|
|
|
|
@override
|
|
State<_NameDialog> createState() => _NameDialogState();
|
|
}
|
|
|
|
class _NameDialogState extends State<_NameDialog> {
|
|
late final TextEditingController _field = TextEditingController(
|
|
text: widget.initial,
|
|
);
|
|
|
|
@override
|
|
void dispose() {
|
|
_field.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _save() => Navigator.of(context).pop(_field.text.trim());
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return AlertDialog(
|
|
title: const Text('Save current account'),
|
|
content: TextField(
|
|
key: const Key('chat-claude-account-name'),
|
|
controller: _field,
|
|
autofocus: true,
|
|
decoration: const InputDecoration(labelText: 'Name'),
|
|
onSubmitted: (_) => _save(),
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
child: const Text('Cancel'),
|
|
),
|
|
FilledButton(
|
|
key: const Key('chat-claude-account-name-save'),
|
|
onPressed: _save,
|
|
child: const Text('Save'),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|