yubioath-flutter/lib/oath/views/account_list.dart

115 lines
3.6 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
2022-03-31 12:41:28 +03:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../app/models.dart';
import '../../widgets/list_title.dart';
import '../models.dart';
2022-03-31 12:41:28 +03:00
import '../state.dart';
import 'account_view.dart';
2022-03-31 12:41:28 +03:00
class AccountList extends ConsumerStatefulWidget {
final DevicePath devicePath;
final OathState oathState;
2022-05-12 10:56:55 +03:00
const AccountList(this.devicePath, this.oathState, {super.key});
@override
2022-03-31 12:41:28 +03:00
ConsumerState<AccountList> createState() => _AccountListState();
}
2022-03-31 12:41:28 +03:00
class _AccountListState extends ConsumerState<AccountList> {
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));
}
@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) {
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-03-03 15:55:07 +03:00
_credentials =
pinnedCreds.followedBy(creds).map((e) => e.credential).toList();
_updateFocusNodes();
2021-12-03 17:15:00 +03:00
return Column(
children: [
if (pinnedCreds.isNotEmpty) const ListTitle('Pinned'),
2022-03-03 15:55:07 +03:00
...pinnedCreds.map(
(entry) => AccountView(
entry.credential,
focusNode: _focusNodes[entry.credential],
),
2021-12-03 17:15:00 +03:00
),
if (creds.isNotEmpty) const ListTitle('Accounts'),
2021-12-03 17:15:00 +03:00
...creds.map(
(entry) => AccountView(
entry.credential,
focusNode: _focusNodes[entry.credential],
),
),
],
);
}
}