2023-05-26 21:21:16 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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-01-12 04:45:32 +00:00
|
|
|
import 'package:isar/isar.dart';
|
2023-04-18 13:56:14 +00:00
|
|
|
import 'package:stackwallet/db/isar/main_db.dart';
|
2023-01-12 04:45:32 +00:00
|
|
|
import 'package:stackwallet/models/isar/models/blockchain_data/transaction.dart';
|
2022-10-03 18:24: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/trade_details_view.dart';
|
|
|
|
import 'package:stackwallet/providers/global/trades_service_provider.dart';
|
|
|
|
import 'package:stackwallet/providers/providers.dart';
|
2023-02-08 14:43:42 +00:00
|
|
|
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/constants.dart';
|
|
|
|
import 'package:stackwallet/utilities/text_styles.dart';
|
2023-02-08 14:43:42 +00:00
|
|
|
import 'package:stackwallet/widgets/conditional_parent.dart';
|
|
|
|
import 'package:stackwallet/widgets/custom_loading_overlay.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'package:stackwallet/widgets/trade_card.dart';
|
|
|
|
import 'package:tuple/tuple.dart';
|
|
|
|
|
|
|
|
class ExchangeView extends ConsumerStatefulWidget {
|
|
|
|
const ExchangeView({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
ConsumerState<ExchangeView> createState() => _ExchangeViewState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ExchangeViewState extends ConsumerState<ExchangeView> {
|
2023-02-08 14:43:42 +00:00
|
|
|
bool _initialCachePopulationUnderway = false;
|
|
|
|
|
2022-08-26 08:11:35 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
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,
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
|
|
|
)
|
2023-02-08 14:43:42 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
child: SafeArea(
|
|
|
|
child: NestedScrollView(
|
|
|
|
floatHeaderSlivers: true,
|
|
|
|
headerSliverBuilder: (context, innerBoxIsScrolled) {
|
|
|
|
return [
|
|
|
|
SliverOverlapAbsorber(
|
|
|
|
handle:
|
|
|
|
NestedScrollView.sliverOverlapAbsorberHandleFor(context),
|
|
|
|
sliver: const SliverToBoxAdapter(
|
|
|
|
child: Padding(
|
|
|
|
padding: EdgeInsets.only(
|
|
|
|
left: 16,
|
|
|
|
right: 16,
|
|
|
|
top: 16,
|
2022-10-03 18:24:57 +00:00
|
|
|
),
|
2023-02-08 14:43:42 +00:00
|
|
|
child: ExchangeForm(),
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
2023-02-08 14:43:42 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
];
|
|
|
|
},
|
|
|
|
body: Builder(
|
|
|
|
builder: (buildContext) {
|
|
|
|
final trades = ref
|
|
|
|
.watch(tradesServiceProvider.select((value) => value.trades));
|
|
|
|
final tradeCount = trades.length;
|
|
|
|
final hasHistory = tradeCount > 0;
|
|
|
|
|
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 12),
|
|
|
|
child: CustomScrollView(
|
|
|
|
slivers: [
|
|
|
|
SliverOverlapInjector(
|
|
|
|
handle: NestedScrollView.sliverOverlapAbsorberHandleFor(
|
|
|
|
buildContext,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
SliverToBoxAdapter(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 4),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
|
|
children: [
|
|
|
|
const SizedBox(
|
|
|
|
height: 12,
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
2023-02-08 14:43:42 +00:00
|
|
|
Text(
|
|
|
|
"Trades",
|
|
|
|
style: STextStyles.itemSubtitle(context).copyWith(
|
|
|
|
color: Theme.of(context)
|
|
|
|
.extension<StackColors>()!
|
|
|
|
.textDark3,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 12,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
|
|
|
),
|
2023-02-08 14:43:42 +00:00
|
|
|
if (hasHistory)
|
|
|
|
SliverList(
|
|
|
|
delegate: SliverChildBuilderDelegate((context, index) {
|
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.all(4),
|
|
|
|
child: TradeCard(
|
|
|
|
key: Key("tradeCard_${trades[index].uuid}"),
|
|
|
|
trade: trades[index],
|
|
|
|
onTap: () async {
|
|
|
|
final String tradeId = trades[index].tradeId;
|
|
|
|
|
|
|
|
final lookup = ref
|
|
|
|
.read(tradeSentFromStackLookupProvider)
|
|
|
|
.all;
|
2022-08-26 08:11:35 +00:00
|
|
|
|
2022-12-13 00:17:02 +00:00
|
|
|
//todo: check if print needed
|
2023-02-08 14:43:42 +00:00
|
|
|
// debugPrint("ALL: $lookup");
|
|
|
|
|
|
|
|
final String? txid = ref
|
|
|
|
.read(tradeSentFromStackLookupProvider)
|
|
|
|
.getTxidForTradeId(tradeId);
|
|
|
|
final List<String>? walletIds = ref
|
|
|
|
.read(tradeSentFromStackLookupProvider)
|
|
|
|
.getWalletIdsForTradeId(tradeId);
|
|
|
|
|
|
|
|
if (txid != null &&
|
|
|
|
walletIds != null &&
|
|
|
|
walletIds.isNotEmpty) {
|
|
|
|
final manager = ref
|
2023-10-31 17:15:59 +00:00
|
|
|
.read(pWallets)
|
2023-02-08 14:43:42 +00:00
|
|
|
.getManager(walletIds.first);
|
|
|
|
|
|
|
|
//todo: check if print needed
|
|
|
|
// debugPrint("name: ${manager.walletName}");
|
|
|
|
|
|
|
|
final tx = await MainDB.instance
|
|
|
|
.getTransactions(walletIds.first)
|
|
|
|
.filter()
|
|
|
|
.txidEqualTo(txid)
|
|
|
|
.findFirst();
|
|
|
|
|
|
|
|
if (mounted) {
|
|
|
|
unawaited(Navigator.of(context).pushNamed(
|
|
|
|
TradeDetailsView.routeName,
|
|
|
|
arguments: Tuple4(tradeId, tx,
|
|
|
|
walletIds.first, manager.walletName),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-30 22:38:07 +00:00
|
|
|
unawaited(Navigator.of(context).pushNamed(
|
2022-08-26 08:11:35 +00:00
|
|
|
TradeDetailsView.routeName,
|
2023-02-08 14:43:42 +00:00
|
|
|
arguments: Tuple4(
|
|
|
|
tradeId, null, walletIds?.first, null),
|
2022-08-30 22:38:07 +00:00
|
|
|
));
|
2022-08-26 08:11:35 +00:00
|
|
|
}
|
2023-02-08 14:43:42 +00:00
|
|
|
},
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
2023-02-08 14:43:42 +00:00
|
|
|
);
|
|
|
|
}, childCount: tradeCount),
|
|
|
|
),
|
|
|
|
if (!hasHistory)
|
|
|
|
SliverToBoxAdapter(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 4),
|
|
|
|
child: Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: Theme.of(context)
|
|
|
|
.extension<StackColors>()!
|
|
|
|
.popupBG,
|
|
|
|
borderRadius: BorderRadius.circular(
|
|
|
|
Constants.size.circularBorderRadius,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(12),
|
|
|
|
child: Text(
|
|
|
|
"Trades will appear here",
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: STextStyles.itemSubtitle(context),
|
|
|
|
),
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-02-08 14:43:42 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|