app_state.dart 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import 'package:pb_authenticator_state/state.dart';
  2. import 'package:pb_authenticator_totp/totp.dart';
  3. import 'package:flutter/widgets.dart';
  4. import 'package:collection/collection.dart' show ListEquality;
  5. // TODO: Transition to new type
  6. typedef BaseItemType = LegacyAuthenticatorItem;
  7. /// Represents app state.
  8. class AppState extends ChangeNotifier {
  9. final RepositoryBase<BaseItemType> _repository;
  10. AppState(this._repository);
  11. /// List of TOTP items (internal implementation).
  12. List<BaseItemType>? _items;
  13. /// TOTP items as list.
  14. List<BaseItemType>? get items {
  15. if (_items == null) {
  16. loadItems();
  17. }
  18. return _items;
  19. }
  20. Future loadItems() async {
  21. _items = await _repository.loadItems();
  22. notifyListeners();
  23. }
  24. /// Adds a TOTP item to the list.
  25. Future addItem(TotpItem item) async {
  26. await _repository.addItem(item);
  27. await loadItems();
  28. }
  29. /// Replace list of TOTP items.
  30. Future replaceItems(List<BaseItemType> items) async {
  31. await _repository.replaceItems(items);
  32. await loadItems();
  33. }
  34. bool itemsChanged(List<BaseItemType>? newItems) {
  35. return !const ListEquality().equals(items, newItems);
  36. }
  37. }