2022-03-03 18:43:36 +03:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
|
|
|
|
import '../../oath/models.dart';
|
|
|
|
|
2022-03-03 19:24:26 +03:00
|
|
|
final androidStateProvider =
|
|
|
|
StateNotifierProvider<_StateProvider, OathState?>((ref) {
|
|
|
|
return _StateProvider(null);
|
2022-03-03 18:43:36 +03:00
|
|
|
});
|
|
|
|
|
2022-03-03 19:24:26 +03:00
|
|
|
class _StateProvider extends StateNotifier<OathState?> {
|
|
|
|
_StateProvider(OathState? oathState) : super(oathState);
|
2022-03-03 18:43:36 +03:00
|
|
|
|
2022-03-04 19:07:12 +03:00
|
|
|
void setFromString(String input) {
|
2022-03-03 18:43:36 +03:00
|
|
|
var resultJson = jsonDecode(input);
|
|
|
|
state = OathState(resultJson['deviceId'],
|
|
|
|
hasKey: resultJson['hasKey'],
|
|
|
|
remembered: resultJson['remembered'],
|
|
|
|
locked: resultJson['locked'],
|
|
|
|
keystore: KeystoreState.unknown);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 19:24:26 +03:00
|
|
|
final androidCredentialsProvider =
|
2022-04-07 17:41:57 +03:00
|
|
|
StateNotifierProvider<_CredentialsProvider, List<OathPair>?>((ref) {
|
|
|
|
return _CredentialsProvider(null);
|
2022-03-03 18:43:36 +03:00
|
|
|
});
|
|
|
|
|
2022-04-07 17:41:57 +03:00
|
|
|
class _CredentialsProvider extends StateNotifier<List<OathPair>?> {
|
|
|
|
_CredentialsProvider(List<OathPair>? credentials) : super(credentials);
|
2022-03-03 18:43:36 +03:00
|
|
|
|
2022-03-04 19:07:12 +03:00
|
|
|
void setFromString(String input) {
|
2022-03-03 18:43:36 +03:00
|
|
|
var result = jsonDecode(input);
|
|
|
|
|
|
|
|
final List<OathPair> pairs = [];
|
|
|
|
for (var e in result['entries']) {
|
|
|
|
final credential = OathCredential.fromJson(e['credential']);
|
|
|
|
final code = e['code'] == null ? null : OathCode.fromJson(e['code']);
|
|
|
|
pairs.add(OathPair(credential, code));
|
|
|
|
}
|
|
|
|
|
|
|
|
state = pairs;
|
|
|
|
}
|
|
|
|
}
|