Skip to content

Commit

Permalink
feat: improve loading
Browse files Browse the repository at this point in the history
  • Loading branch information
dtscalac committed Sep 26, 2024
1 parent 316cb22 commit 58fd647
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:catalyst_voices/widgets/indicators/voices_circular_progress_indicator.dart';
import 'package:catalyst_voices/widgets/indicators/voices_error_indicator.dart';
import 'package:catalyst_voices_localization/catalyst_voices_localization.dart';
import 'package:catalyst_voices_shared/catalyst_voices_shared.dart';
import 'package:flutter/material.dart';

/// A callback that generates a new [Future] of type [T].
Expand Down Expand Up @@ -122,10 +123,7 @@ class _VoicesFutureBuilderState<T extends Object>
}

Future<T> _makeDelayedFuture() async {
final delay = Future<void>.delayed(widget.minimumDelay);
final result = widget.future();
await Future.wait([delay, result]);
return result;
return widget.future().withMinimumDelay();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:catalyst_cardano/catalyst_cardano.dart';
import 'package:catalyst_voices_blocs/src/registration/registration_navigator.dart';
import 'package:catalyst_voices_blocs/src/registration/registration_state.dart';
import 'package:catalyst_voices_models/catalyst_voices_models.dart';
import 'package:catalyst_voices_shared/catalyst_voices_shared.dart';
import 'package:catalyst_voices_view_models/catalyst_voices_view_models.dart';
import 'package:flutter/foundation.dart';

Expand Down Expand Up @@ -77,7 +78,10 @@ final class RegistrationWalletLinkController
@override
Future<void> refreshCardanoWallets() async {
try {
final wallets = await CatalystCardano.instance.getWallets();
_wallets.value = const UninitializedCardanoWallets();

final wallets =
await CatalystCardano.instance.getWallets().withMinimumDelay();
_wallets.value = CardanoWalletsList(wallets: wallets);
} catch (error) {
_wallets.value = CardanoWalletsError(error: error);
Expand Down
2 changes: 2 additions & 0 deletions catalyst_voices/packages/catalyst_voices_blocs/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ dependencies:
path: ../catalyst_voices_repositories
catalyst_voices_services:
path: ../catalyst_voices_services
catalyst_voices_shared:
path: ../catalyst_voices_shared
catalyst_voices_view_models:
path: ../catalyst_voices_view_models
collection: ^1.18.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export 'responsive/responsive_builder.dart';
export 'responsive/responsive_child.dart';
export 'responsive/responsive_padding.dart';
export 'utils/date_time_ext.dart';
export 'utils/future_ext.dart';
export 'utils/iterable_ext.dart';
export 'utils/typedefs.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
extension FutureExt<T> on Future<T> {
/// The minimum loading delay after which the state changes from loading
/// to success/failure will not be perceived too jumpy.
///
/// Use it to avoid showing a loading state for split second.
static const Duration minimumDelay = Duration(milliseconds: 300);

/// Returns the result of awaiting the [Future]
/// but applies a [minimumDelay] to it.
Future<T> withMinimumDelay() async {
final delay = Future<void>.delayed(minimumDelay);
final result = await this;
await delay;
return result;
}
}

0 comments on commit 58fd647

Please sign in to comment.