stack_wallet/lib/pages/cashfusion/cashfusion_view.dart

455 lines
18 KiB
Dart
Raw Normal View History

2023-07-26 22:07:06 +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 sneurlax on 2023-07-26
*
*/
2023-10-16 20:15:55 +00:00
import 'dart:async';
2023-07-26 22:07:06 +00:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
2023-07-26 22:07:06 +00:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/svg.dart';
2024-05-27 23:56:22 +00:00
import '../../providers/cash_fusion/fusion_progress_ui_state_provider.dart';
import '../../providers/global/prefs_provider.dart';
import '../../providers/global/wallets_provider.dart';
import '../../themes/stack_colors.dart';
import '../../utilities/assets.dart';
import '../../utilities/constants.dart';
import '../../utilities/extensions/extensions.dart';
import '../../utilities/text_styles.dart';
import '../../wallets/crypto_currency/crypto_currency.dart';
import '../../wallets/isar/providers/wallet_info_provider.dart';
import '../../wallets/wallet/wallet_mixin_interfaces/cash_fusion_interface.dart';
import '../../widgets/background.dart';
import '../../widgets/custom_buttons/app_bar_icon_button.dart';
import '../../widgets/custom_buttons/blue_text_button.dart';
import '../../widgets/desktop/primary_button.dart';
import '../../widgets/rounded_container.dart';
import '../../widgets/rounded_white_container.dart';
import '../../widgets/stack_text_field.dart';
2024-05-27 23:56:22 +00:00
import 'fusion_progress_view.dart';
import 'fusion_rounds_selection_sheet.dart';
2023-07-26 22:07:06 +00:00
class CashFusionView extends ConsumerStatefulWidget {
const CashFusionView({
super.key,
required this.walletId,
});
static const routeName = "/cashFusionView";
final String walletId;
@override
ConsumerState<CashFusionView> createState() => _CashFusionViewState();
}
class _CashFusionViewState extends ConsumerState<CashFusionView> {
2023-10-16 18:48:02 +00:00
late final TextEditingController serverController;
late final FocusNode serverFocusNode;
late final TextEditingController portController;
late final FocusNode portFocusNode;
2023-10-16 21:04:27 +00:00
late final TextEditingController fusionRoundController;
late final FocusNode fusionRoundFocusNode;
2024-05-15 21:20:45 +00:00
late final CryptoCurrency coin;
2023-10-16 18:48:02 +00:00
2023-10-16 19:49:50 +00:00
bool _enableSSLCheckbox = false;
2023-10-16 20:15:55 +00:00
bool _enableStartButton = false;
2023-10-16 18:48:02 +00:00
2023-10-16 18:00:35 +00:00
FusionOption _option = FusionOption.continuous;
2023-07-26 22:07:06 +00:00
2023-10-18 15:02:25 +00:00
Future<void> _startFusion() async {
final fusionWallet =
ref.read(pWallets).getWallet(widget.walletId) as CashFusionInterface;
2023-10-18 15:02:25 +00:00
try {
fusionWallet.uiState = ref.read(
fusionProgressUIStateProvider(widget.walletId),
);
} catch (e) {
if (!e.toString().contains(
2024-05-27 23:56:22 +00:00
"FusionProgressUIState was already set for ${widget.walletId}",
)) {
2023-10-18 15:02:25 +00:00
rethrow;
}
}
final int rounds = _option == FusionOption.continuous
? 0
: int.parse(fusionRoundController.text);
final newInfo = FusionInfo(
host: serverController.text,
port: int.parse(portController.text),
ssl: _enableSSLCheckbox,
rounds: rounds,
);
// update user prefs (persistent)
2023-11-14 22:23:57 +00:00
ref.read(prefsChangeNotifierProvider).setFusionServerInfo(coin, newInfo);
2023-10-18 15:02:25 +00:00
unawaited(
fusionWallet.fuse(
fusionInfo: newInfo,
),
);
await Navigator.of(context).pushNamed(
FusionProgressView.routeName,
arguments: widget.walletId,
);
}
2023-10-16 18:48:02 +00:00
@override
void initState() {
serverController = TextEditingController();
portController = TextEditingController();
2023-10-16 21:04:27 +00:00
fusionRoundController = TextEditingController();
2023-10-16 18:48:02 +00:00
serverFocusNode = FocusNode();
portFocusNode = FocusNode();
2023-10-16 21:04:27 +00:00
fusionRoundFocusNode = FocusNode();
2023-10-16 20:15:55 +00:00
2023-11-14 22:44:03 +00:00
coin = ref.read(pWalletCoin(widget.walletId));
2023-11-14 22:33:54 +00:00
2023-11-14 22:23:57 +00:00
final info =
ref.read(prefsChangeNotifierProvider).getFusionServerInfo(coin);
2023-11-14 22:33:54 +00:00
2023-10-16 21:04:27 +00:00
serverController.text = info.host;
portController.text = info.port.toString();
_enableSSLCheckbox = info.ssl;
_option = info.rounds == 0 ? FusionOption.continuous : FusionOption.custom;
fusionRoundController.text = info.rounds.toString();
2023-10-16 20:15:55 +00:00
_enableStartButton =
serverController.text.isNotEmpty && portController.text.isNotEmpty;
super.initState();
2023-10-16 18:48:02 +00:00
}
@override
void dispose() {
serverController.dispose();
portController.dispose();
2023-10-16 21:04:27 +00:00
fusionRoundController.dispose();
2023-10-16 18:48:02 +00:00
serverFocusNode.dispose();
portFocusNode.dispose();
2023-10-16 21:04:27 +00:00
fusionRoundFocusNode.dispose();
2023-10-16 18:48:02 +00:00
super.dispose();
}
2023-07-26 22:07:06 +00:00
@override
Widget build(BuildContext context) {
return Background(
child: SafeArea(
child: Scaffold(
backgroundColor:
Theme.of(context).extension<StackColors>()!.background,
appBar: AppBar(
automaticallyImplyLeading: false,
2023-10-24 22:43:41 +00:00
leading: const AppBarBackButton(),
2023-07-26 22:07:06 +00:00
title: Text(
"Fusion",
2023-07-26 22:07:06 +00:00
style: STextStyles.navBarTitle(context),
),
titleSpacing: 0,
actions: [
AspectRatio(
aspectRatio: 1,
child: AppBarIconButton(
size: 36,
icon: SvgPicture.asset(
2023-10-16 17:04:18 +00:00
Assets.svg.circleQuestion,
2023-07-26 22:07:06 +00:00
width: 20,
height: 20,
color: Theme.of(context)
.extension<StackColors>()!
.topNavIconPrimary,
),
onPressed: () async {
2023-10-16 17:04:18 +00:00
//' TODO show about?
2023-07-26 22:07:06 +00:00
},
),
),
],
),
2023-10-16 17:04:18 +00:00
body: LayoutBuilder(
builder: (builderContext, constraints) {
return SingleChildScrollView(
child: ConstrainedBox(
constraints: BoxConstraints(
minHeight: constraints.maxHeight,
2023-07-26 22:07:06 +00:00
),
2023-10-16 17:04:18 +00:00
child: IntrinsicHeight(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
RoundedWhiteContainer(
child: Text(
"Fusion helps anonymize your coins by mixing them.",
2023-10-16 17:04:18 +00:00
style: STextStyles.w500_12(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.textSubtitle1,
),
),
),
const SizedBox(
height: 16,
),
2023-10-17 22:24:13 +00:00
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Server settings",
style: STextStyles.w500_14(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.textDark3,
),
),
CustomTextButton(
text: "Default",
onTap: () {
2023-11-14 22:44:03 +00:00
final def = kFusionServerInfoDefaults[coin]!;
2023-10-17 22:24:13 +00:00
serverController.text = def.host;
portController.text = def.port.toString();
fusionRoundController.text =
def.rounds.toString();
_option = FusionOption.continuous;
setState(() {
_enableSSLCheckbox = def.ssl;
});
},
),
],
2023-10-16 17:04:18 +00:00
),
const SizedBox(
height: 12,
),
ClipRRect(
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
child: TextField(
2023-10-16 18:48:02 +00:00
autocorrect: false,
enableSuggestions: false,
controller: serverController,
focusNode: serverFocusNode,
onChanged: (value) {
setState(() {
2023-10-16 21:04:27 +00:00
_enableStartButton = value.isNotEmpty &&
portController.text.isNotEmpty &&
fusionRoundController.text.isNotEmpty;
2023-10-16 18:48:02 +00:00
});
},
style: STextStyles.field(context),
decoration: standardInputDecoration(
"Server",
serverFocusNode,
context,
desktopMed: true,
),
),
),
2023-10-16 17:04:18 +00:00
const SizedBox(
height: 10,
),
ClipRRect(
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
child: TextField(
autocorrect: false,
enableSuggestions: false,
controller: portController,
focusNode: portFocusNode,
inputFormatters: [
2024-05-27 23:56:22 +00:00
FilteringTextInputFormatter.digitsOnly,
],
keyboardType: TextInputType.number,
onChanged: (value) {
setState(() {
2023-10-16 21:04:27 +00:00
_enableStartButton = value.isNotEmpty &&
serverController.text.isNotEmpty &&
fusionRoundController.text.isNotEmpty;
});
},
style: STextStyles.field(context),
decoration: standardInputDecoration(
"Port",
portFocusNode,
context,
),
2023-10-16 18:48:02 +00:00
),
),
2023-10-16 17:04:18 +00:00
const SizedBox(
height: 10,
),
2023-10-16 19:49:50 +00:00
GestureDetector(
onTap: () {
setState(() {
_enableSSLCheckbox = !_enableSSLCheckbox;
});
},
child: Container(
color: Colors.transparent,
child: Row(
children: [
SizedBox(
width: 20,
height: 20,
child: Checkbox(
materialTapTargetSize:
MaterialTapTargetSize.shrinkWrap,
value: _enableSSLCheckbox,
onChanged: (newValue) {
setState(
() {
_enableSSLCheckbox =
!_enableSSLCheckbox;
},
);
},
),
),
const SizedBox(
width: 12,
),
Text(
"Use SSL",
style: STextStyles.itemSubtitle12(context),
),
],
),
),
),
2023-10-16 17:04:18 +00:00
const SizedBox(
height: 16,
),
Text(
"Rounds of fusion",
style: STextStyles.w500_14(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.textDark3,
),
),
const SizedBox(
height: 12,
),
2023-10-16 18:00:35 +00:00
RoundedContainer(
onPressed: () async {
final option =
await showModalBottomSheet<FusionOption?>(
backgroundColor: Colors.transparent,
context: context,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
top: Radius.circular(20),
),
),
builder: (_) {
return FusionRoundCountSelectSheet(
currentOption: _option,
);
},
);
if (option != null) {
setState(() {
_option = option;
});
}
},
color: Theme.of(context)
.extension<StackColors>()!
.textFieldActiveBG,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Text(
_option.name.capitalize(),
style: STextStyles.w500_12(context),
),
SvgPicture.asset(
Assets.svg.chevronDown,
width: 12,
color: Theme.of(context)
.extension<StackColors>()!
.textSubtitle1,
),
],
),
),
2023-10-16 17:04:18 +00:00
),
2023-10-16 21:04:27 +00:00
if (_option == FusionOption.custom)
const SizedBox(
height: 10,
),
if (_option == FusionOption.custom)
ClipRRect(
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
child: TextField(
autocorrect: false,
enableSuggestions: false,
controller: fusionRoundController,
focusNode: fusionRoundFocusNode,
inputFormatters: [
2024-05-27 23:56:22 +00:00
FilteringTextInputFormatter.digitsOnly,
2023-10-16 21:04:27 +00:00
],
keyboardType: TextInputType.number,
onChanged: (value) {
setState(() {
_enableStartButton = value.isNotEmpty &&
serverController.text.isNotEmpty &&
portController.text.isNotEmpty;
});
},
style: STextStyles.field(context),
decoration: standardInputDecoration(
"Number of fusions",
fusionRoundFocusNode,
context,
).copyWith(
2024-05-27 23:56:22 +00:00
labelText: "Enter number of fusions..",
),
2023-10-16 21:04:27 +00:00
),
),
2023-10-16 17:04:18 +00:00
const SizedBox(
height: 16,
),
const Spacer(),
PrimaryButton(
label: "Start",
2023-10-16 20:15:55 +00:00
enabled: _enableStartButton,
2023-10-18 15:02:25 +00:00
onPressed: _startFusion,
2023-10-16 17:04:18 +00:00
),
],
),
),
2023-07-26 22:07:06 +00:00
),
2023-10-16 17:04:18 +00:00
),
);
},
2023-07-26 22:07:06 +00:00
),
),
),
);
}
}