2021-11-22 11:49:52 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2022-02-10 14:11:44 +03:00
|
|
|
import 'package:flutter/services.dart';
|
2022-03-31 12:41:28 +03:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2021-11-22 11:49:52 +03:00
|
|
|
|
|
|
|
import '../../app/models.dart';
|
2022-06-02 15:52:00 +03:00
|
|
|
import '../../widgets/list_title.dart';
|
2021-11-22 11:49:52 +03:00
|
|
|
import '../models.dart';
|
2022-03-31 12:41:28 +03:00
|
|
|
import '../state.dart';
|
2021-11-22 11:49:52 +03:00
|
|
|
import 'account_view.dart';
|
|
|
|
|
2022-03-31 12:41:28 +03:00
|
|
|
class AccountList extends ConsumerStatefulWidget {
|
2022-03-28 13:58:45 +03:00
|
|
|
final DevicePath devicePath;
|
|
|
|
final OathState oathState;
|
2022-05-12 10:56:55 +03:00
|
|
|
const AccountList(this.devicePath, this.oathState, {super.key});
|
2021-11-22 11:49:52 +03:00
|
|
|
|
2022-02-10 14:11:44 +03:00
|
|
|
@override
|
2022-03-31 12:41:28 +03:00
|
|
|
ConsumerState<AccountList> createState() => _AccountListState();
|
2022-02-10 14:11:44 +03:00
|
|
|
}
|
|
|
|
|
2022-03-31 12:41:28 +03:00
|
|
|
class _AccountListState extends ConsumerState<AccountList> {
|
2022-02-10 14:11:44 +03:00
|
|
|
List<OathCredential> _credentials = [];
|
|
|
|
Map<OathCredential, FocusNode> _focusNodes = {};
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
for (var e in _focusNodes.values) {
|
|
|
|
e.dispose();
|
|
|
|
}
|
|
|
|
_focusNodes.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _updateFocusNodes() {
|
|
|
|
_focusNodes = {
|
|
|
|
for (var cred in _credentials)
|
|
|
|
cred: _focusNodes[cred] ??
|
|
|
|
FocusNode(
|
|
|
|
onKeyEvent: (node, event) {
|
|
|
|
if (event is KeyDownEvent) {
|
|
|
|
int index = -1;
|
|
|
|
ScrollPositionAlignmentPolicy policy =
|
|
|
|
ScrollPositionAlignmentPolicy.explicit;
|
|
|
|
if (event.logicalKey == LogicalKeyboardKey.arrowDown) {
|
|
|
|
index = _credentials.indexOf(cred) + 1;
|
|
|
|
policy = ScrollPositionAlignmentPolicy.keepVisibleAtEnd;
|
|
|
|
} else if (event.logicalKey == LogicalKeyboardKey.arrowUp) {
|
|
|
|
index = _credentials.indexOf(cred) - 1;
|
|
|
|
policy = ScrollPositionAlignmentPolicy.keepVisibleAtStart;
|
|
|
|
}
|
|
|
|
if (index >= 0 && index < _credentials.length) {
|
|
|
|
final targetNode = _focusNodes[_credentials[index]]!;
|
|
|
|
targetNode.requestFocus();
|
|
|
|
Scrollable.ensureVisible(
|
|
|
|
targetNode.context!,
|
|
|
|
alignmentPolicy: policy,
|
|
|
|
);
|
|
|
|
return KeyEventResult.handled;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return KeyEventResult.ignored;
|
|
|
|
},
|
|
|
|
)
|
|
|
|
};
|
|
|
|
_focusNodes.removeWhere((cred, _) => !_credentials.contains(cred));
|
|
|
|
}
|
|
|
|
|
2021-11-22 11:49:52 +03:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-03-31 12:41:28 +03:00
|
|
|
final accounts = ref.watch(credentialListProvider(widget.devicePath));
|
|
|
|
if (accounts == null) {
|
|
|
|
return Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: const [
|
|
|
|
Center(child: CircularProgressIndicator()),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
final credentials = ref.watch(filteredCredentialsProvider(accounts));
|
|
|
|
final favorites = ref.watch(favoritesProvider);
|
|
|
|
if (credentials.isEmpty) {
|
2021-11-22 11:49:52 +03:00
|
|
|
return const Center(
|
|
|
|
child: Text('No credentials'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-31 12:41:28 +03:00
|
|
|
final pinnedCreds =
|
|
|
|
credentials.where((entry) => favorites.contains(entry.credential.id));
|
|
|
|
final creds =
|
|
|
|
credentials.where((entry) => !favorites.contains(entry.credential.id));
|
2022-02-10 14:11:44 +03:00
|
|
|
|
2022-03-03 15:55:07 +03:00
|
|
|
_credentials =
|
|
|
|
pinnedCreds.followedBy(creds).map((e) => e.credential).toList();
|
2022-02-10 14:11:44 +03:00
|
|
|
_updateFocusNodes();
|
2021-12-03 17:15:00 +03:00
|
|
|
|
2022-04-03 12:06:22 +03:00
|
|
|
return Column(
|
2021-11-22 11:49:52 +03:00
|
|
|
children: [
|
2022-06-02 15:52:00 +03:00
|
|
|
if (pinnedCreds.isNotEmpty) const ListTitle('Pinned'),
|
2022-03-03 15:55:07 +03:00
|
|
|
...pinnedCreds.map(
|
2022-02-10 14:11:44 +03:00
|
|
|
(entry) => AccountView(
|
|
|
|
entry.credential,
|
|
|
|
focusNode: _focusNodes[entry.credential],
|
|
|
|
),
|
2021-12-03 17:15:00 +03:00
|
|
|
),
|
2022-06-02 15:52:00 +03:00
|
|
|
if (creds.isNotEmpty) const ListTitle('Accounts'),
|
2021-12-03 17:15:00 +03:00
|
|
|
...creds.map(
|
2022-02-10 14:11:44 +03:00
|
|
|
(entry) => AccountView(
|
|
|
|
entry.credential,
|
|
|
|
focusNode: _focusNodes[entry.credential],
|
|
|
|
),
|
2021-11-22 11:49:52 +03:00
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|