Skip to content

Commit

Permalink
token as a container
Browse files Browse the repository at this point in the history
  • Loading branch information
frankmer committed Jul 30, 2024
1 parent 5de32e6 commit cec050f
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 13 deletions.
2 changes: 1 addition & 1 deletion lib/api/token_container_api_endpoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class TokenContainerApiEndpoint implements ApiEndpioint<TokenContainer, Credenti
Logger.debug('Old label: "$oldLabel" starts with "${containerState.serial}" ?', name: 'TokenContainerApiEndpoint#sync');
if (oldLabel.startsWith(containerState.serial) == true) {
var merged = localTemplate.copyAddAll({
URI_LABEL: oldLabel.replaceRange(oldLabel.length - 1, oldLabel.length, '😀'),
URI_LABEL: '123 😀',
});
Logger.debug('New label: "${merged.data[URI_LABEL]}"', name: 'TokenContainerApiEndpoint#sync');
if (merged.serial == null) {
Expand Down
12 changes: 0 additions & 12 deletions lib/utils/riverpod/state_notifiers/token_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -339,29 +339,23 @@ class TokenNotifier extends StateNotifier<TokenState> {
Future<void> updateContainerTokens(TokenContainer container) async {
await initState;
Logger.info('Updating tokens from container.', name: 'token_notifier.dart#updateContainerTokens');
await Future.delayed(const Duration(milliseconds: 2000));
final templatesToAdd = <TokenTemplate>[];
final templatesToUpdate = <TokenTemplate>[];
final templatesToRemove = <TokenTemplate>[];

final knownContainerTokens = state.tokens.fromContainer(container.serial)..addAll(state.maybePiTokens);
final serverTokenTemplates = container.syncedTokenTemplates;
await Future.delayed(const Duration(milliseconds: 2000));
Logger.debug(
'App knows ${knownContainerTokens.length} of ${serverTokenTemplates.length} server tokens',
name: 'token_notifier.dart#updateContainerTokens',
);
await Future.delayed(const Duration(milliseconds: 2000));
final appTokenTemplates = knownContainerTokens.toTemplates();

Logger.debug('All server templates: ${serverTokenTemplates.join('\n')}', name: 'token_notifier.dart#updateContainerTokens');
await Future.delayed(const Duration(milliseconds: 2000));
for (var serverTokenTemplate in serverTokenTemplates) {
Logger.debug(
'Checking server token template: $serverTokenTemplate',
name: 'token_notifier.dart#updateContainerTokens',
);
await Future.delayed(const Duration(milliseconds: 2000));
// Searches for tokens that are in the container but not in the app to add them.
// If the token is already in the app, it will be updated.
// Reduces the [tokenTemplatesApp] list to only the tokens that are in the app but not in the container. These tokens will be removed.
Expand All @@ -373,29 +367,25 @@ class TokenNotifier extends StateNotifier<TokenState> {
'Token with serial ${serverTokenTemplate.serial} or id ${serverTokenTemplate.id} not found in app. Adding them.',
name: 'token_notifier.dart#updateContainerTokens',
);
await Future.delayed(const Duration(milliseconds: 1000));
templatesToAdd.add(serverTokenTemplate);
} else {
Logger.debug(
'Token with serial ${serverTokenTemplate.serial} or id ${serverTokenTemplate.id} found in app. Checking for update.',
name: 'token_notifier.dart#updateContainerTokens',
);
await Future.delayed(const Duration(milliseconds: 2000));
appTokenTemplates.remove(appTemplate);
if (!appTemplate.hasSameValuesAs(serverTokenTemplate)) {
Logger.debug(
'Token with serial ${serverTokenTemplate.serial} or id ${serverTokenTemplate.id} found in app the Template but is different. Check update.',
name: 'token_notifier.dart#updateContainerTokens',
);
await Future.delayed(const Duration(milliseconds: 1000));
// Only update the token if the template is different
templatesToUpdate.add(serverTokenTemplate);
} else {
Logger.debug(
'Token with serial ${serverTokenTemplate.serial} or id ${serverTokenTemplate.id} found in app. And is the same. Skipping.',
name: 'token_notifier.dart#updateContainerTokens',
);
await Future.delayed(const Duration(milliseconds: 1000));
}
}
}
Expand All @@ -408,7 +398,6 @@ class TokenNotifier extends StateNotifier<TokenState> {
name: 'token_notifier.dart#updateContainerTokens',
);

await Future.delayed(const Duration(milliseconds: 2000));
final tokensToAdd = templatesToAdd.map((e) => e.toToken(container)).toList();
final tokensToUpdate = <Token>[];
for (var template in templatesToUpdate) {
Expand All @@ -431,7 +420,6 @@ class TokenNotifier extends StateNotifier<TokenState> {
name: 'token_notifier.dart#updateContainerTokens',
);

await Future.delayed(const Duration(milliseconds: 2000));
if (tokensToAdd.isNotEmpty) {
await addOrReplaceTokens(tokensToAdd);
}
Expand Down

0 comments on commit cec050f

Please sign in to comment.