mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2024-12-23 11:59:30 +00:00
89 lines
1.9 KiB
Dart
89 lines
1.9 KiB
Dart
/*
|
|
* This file is part of Stack Wallet.
|
|
*
|
|
* Copyright (c) 2023 Cypher Stack
|
|
* All Rights Reserved.
|
|
* The code is distributed under GPLv3 license, see LICENSE file for details.
|
|
* Generated by Cypher Stack on 2023-05-26
|
|
*
|
|
*/
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../themes/stack_colors.dart';
|
|
import '../widgets/custom_loading_overlay.dart';
|
|
import 'logger.dart';
|
|
|
|
Future<T> minWaitFuture<T>(
|
|
Future<T> future, {
|
|
required Duration delay,
|
|
}) async {
|
|
final results = await Future.wait(
|
|
[
|
|
future,
|
|
Future<dynamic>.delayed(delay),
|
|
],
|
|
);
|
|
|
|
return results.first as T;
|
|
}
|
|
|
|
Future<T?> showLoading<T>({
|
|
required Future<T> whileFuture,
|
|
required BuildContext context,
|
|
required String message,
|
|
String? subMessage,
|
|
bool rootNavigator = false,
|
|
bool opaqueBG = false,
|
|
void Function(Exception)? onException,
|
|
Duration? delay,
|
|
}) async {
|
|
unawaited(
|
|
showDialog<void>(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (_) => WillPopScope(
|
|
onWillPop: () async => false,
|
|
child: Container(
|
|
color: Theme.of(context)
|
|
.extension<StackColors>()!
|
|
.overlay
|
|
.withOpacity(opaqueBG ? 1.0 : 0.6),
|
|
child: CustomLoadingOverlay(
|
|
message: message,
|
|
subMessage: subMessage,
|
|
eventBus: null,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
|
|
Exception? ex;
|
|
T? result;
|
|
|
|
try {
|
|
if (delay != null) {
|
|
result = await minWaitFuture(whileFuture, delay: delay);
|
|
} else {
|
|
result = await whileFuture;
|
|
}
|
|
} catch (e, s) {
|
|
Logging.instance.log(
|
|
"showLoading caught: $e\n$s",
|
|
level: LogLevel.Warning,
|
|
);
|
|
ex = e is Exception ? e : Exception(e.toString());
|
|
}
|
|
|
|
if (context.mounted) {
|
|
Navigator.of(context, rootNavigator: rootNavigator).pop();
|
|
if (ex != null) {
|
|
onException?.call(ex);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|