2022-08-30 22:38:07 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2023-03-28 23:17:59 +00:00
|
|
|
import 'package:stackwallet/models/isar/models/isar_models.dart';
|
2022-10-03 19:18:57 +00:00
|
|
|
import 'package:stackwallet/pages/exchange_view/exchange_form.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'package:stackwallet/pages/exchange_view/sub_widgets/step_row.dart';
|
2023-02-10 14:04:12 +00:00
|
|
|
import 'package:stackwallet/providers/exchange/exchange_form_state_provider.dart';
|
2023-02-08 14:43:42 +00:00
|
|
|
import 'package:stackwallet/providers/global/prefs_provider.dart';
|
|
|
|
import 'package:stackwallet/services/exchange/exchange_data_loading_service.dart';
|
2023-05-09 21:57:40 +00:00
|
|
|
import 'package:stackwallet/themes/stack_colors.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
|
|
|
import 'package:stackwallet/utilities/text_styles.dart';
|
2022-11-25 19:24:01 +00:00
|
|
|
import 'package:stackwallet/widgets/background.dart';
|
2023-02-08 14:43:42 +00:00
|
|
|
import 'package:stackwallet/widgets/conditional_parent.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
|
2023-02-08 14:43:42 +00:00
|
|
|
import 'package:stackwallet/widgets/custom_loading_overlay.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
|
|
|
|
class WalletInitiatedExchangeView extends ConsumerStatefulWidget {
|
|
|
|
const WalletInitiatedExchangeView({
|
|
|
|
Key? key,
|
|
|
|
required this.walletId,
|
|
|
|
required this.coin,
|
2023-03-28 23:17:59 +00:00
|
|
|
this.contract,
|
2022-08-26 08:11:35 +00:00
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
static const String routeName = "/walletInitiatedExchange";
|
|
|
|
|
|
|
|
final String walletId;
|
|
|
|
final Coin coin;
|
2023-03-28 23:17:59 +00:00
|
|
|
final EthContract? contract;
|
2022-08-26 08:11:35 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
ConsumerState<WalletInitiatedExchangeView> createState() =>
|
|
|
|
_WalletInitiatedExchangeViewState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _WalletInitiatedExchangeViewState
|
|
|
|
extends ConsumerState<WalletInitiatedExchangeView> {
|
|
|
|
late final String walletId;
|
|
|
|
late final Coin coin;
|
|
|
|
|
2023-02-08 14:43:42 +00:00
|
|
|
bool _initialCachePopulationUnderway = false;
|
|
|
|
|
2022-08-26 08:11:35 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
walletId = widget.walletId;
|
|
|
|
coin = widget.coin;
|
2023-02-08 14:43:42 +00:00
|
|
|
|
|
|
|
if (!ref.read(prefsChangeNotifierProvider).externalCalls) {
|
|
|
|
if (ExchangeDataLoadingService.currentCacheVersion <
|
|
|
|
ExchangeDataLoadingService.cacheVersion) {
|
|
|
|
_initialCachePopulationUnderway = true;
|
|
|
|
ExchangeDataLoadingService.instance.onLoadingComplete = () {
|
2023-02-10 14:04:12 +00:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
|
|
|
|
await ExchangeDataLoadingService.instance.setCurrenciesIfEmpty(
|
2023-05-03 20:03:31 +00:00
|
|
|
ref.read(efCurrencyPairProvider),
|
|
|
|
ref.read(efRateTypeProvider),
|
2023-02-10 14:04:12 +00:00
|
|
|
);
|
2023-02-08 14:43:42 +00:00
|
|
|
setState(() {
|
|
|
|
_initialCachePopulationUnderway = false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2023-04-18 13:56:14 +00:00
|
|
|
ExchangeDataLoadingService.instance.loadAll();
|
2023-02-08 14:43:42 +00:00
|
|
|
} else if (ExchangeDataLoadingService.instance.isLoading &&
|
|
|
|
ExchangeDataLoadingService.currentCacheVersion <
|
|
|
|
ExchangeDataLoadingService.cacheVersion) {
|
|
|
|
_initialCachePopulationUnderway = true;
|
|
|
|
ExchangeDataLoadingService.instance.onLoadingComplete = () {
|
2023-02-10 14:04:12 +00:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
|
|
|
|
await ExchangeDataLoadingService.instance.setCurrenciesIfEmpty(
|
2023-05-03 20:03:31 +00:00
|
|
|
ref.read(efCurrencyPairProvider),
|
|
|
|
ref.read(efRateTypeProvider),
|
2023-02-10 14:04:12 +00:00
|
|
|
);
|
2023-02-08 14:43:42 +00:00
|
|
|
setState(() {
|
|
|
|
_initialCachePopulationUnderway = false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-26 08:11:35 +00:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
debugPrint("BUILD: $runtimeType");
|
|
|
|
|
2023-02-08 14:43:42 +00:00
|
|
|
return ConditionalParent(
|
|
|
|
condition: _initialCachePopulationUnderway,
|
|
|
|
builder: (child) {
|
|
|
|
return Stack(
|
|
|
|
children: [
|
|
|
|
child,
|
|
|
|
Material(
|
|
|
|
color: Theme.of(context)
|
|
|
|
.extension<StackColors>()!
|
|
|
|
.overlay
|
|
|
|
.withOpacity(0.6),
|
|
|
|
child: const CustomLoadingOverlay(
|
|
|
|
message: "Updating exchange data",
|
|
|
|
subMessage: "This could take a few minutes",
|
|
|
|
eventBus: null,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
child: Background(
|
|
|
|
child: Scaffold(
|
|
|
|
backgroundColor:
|
|
|
|
Theme.of(context).extension<StackColors>()!.background,
|
|
|
|
appBar: AppBar(
|
|
|
|
leading: AppBarBackButton(
|
|
|
|
onPressed: () async {
|
|
|
|
if (FocusScope.of(context).hasFocus) {
|
|
|
|
FocusScope.of(context).unfocus();
|
|
|
|
await Future<void>.delayed(const Duration(milliseconds: 75));
|
|
|
|
}
|
|
|
|
if (mounted) {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
title: Text(
|
2023-03-10 22:01:34 +00:00
|
|
|
"Swap",
|
2023-02-08 14:43:42 +00:00
|
|
|
style: STextStyles.navBarTitle(context),
|
|
|
|
),
|
2022-11-25 19:24:01 +00:00
|
|
|
),
|
2023-02-08 14:43:42 +00:00
|
|
|
body: LayoutBuilder(
|
|
|
|
builder: (context, constraints) {
|
|
|
|
final width = MediaQuery.of(context).size.width - 32;
|
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.all(12),
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
child: ConstrainedBox(
|
|
|
|
constraints: BoxConstraints(
|
|
|
|
minHeight: constraints.maxHeight - 24,
|
|
|
|
),
|
|
|
|
child: IntrinsicHeight(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(4),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
|
|
children: [
|
|
|
|
StepRow(
|
|
|
|
count: 4,
|
|
|
|
current: 0,
|
|
|
|
width: width,
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 14,
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
"Exchange amount",
|
|
|
|
style: STextStyles.pageTitleH1(context),
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 8,
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
"Network fees and other exchange charges are included in the rate.",
|
|
|
|
style: STextStyles.itemSubtitle(context),
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 24,
|
|
|
|
),
|
|
|
|
ExchangeForm(
|
|
|
|
walletId: walletId,
|
|
|
|
coin: coin,
|
2023-03-28 23:17:59 +00:00
|
|
|
contract: widget.contract,
|
2023-02-08 14:43:42 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2022-11-25 19:24:01 +00:00
|
|
|
),
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-02-08 14:43:42 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2022-11-25 19:24:01 +00:00
|
|
|
),
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|