stack_wallet/lib/pages/ordinals/ordinal_details_view.dart

383 lines
12 KiB
Dart
Raw Normal View History

2023-07-18 17:03:57 +00:00
import 'dart:async';
2023-07-27 16:26:52 +00:00
import 'dart:io';
2023-07-18 17:03:57 +00:00
2023-07-18 16:15:05 +00:00
import 'package:flutter/material.dart';
2023-07-18 17:03:57 +00:00
import 'package:flutter/services.dart';
2023-07-27 16:33:59 +00:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
2023-07-18 16:15:05 +00:00
import 'package:flutter_svg/flutter_svg.dart';
2023-07-27 16:26:52 +00:00
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
2023-07-27 16:33:59 +00:00
import 'package:stackwallet/models/isar/models/blockchain_data/utxo.dart';
import 'package:stackwallet/models/isar/ordinal.dart';
2023-09-08 22:53:09 +00:00
import 'package:stackwallet/networking/http.dart';
2023-07-18 17:03:57 +00:00
import 'package:stackwallet/notifications/show_flush_bar.dart';
2023-07-27 16:33:59 +00:00
import 'package:stackwallet/providers/db/main_db_provider.dart';
2023-09-15 19:51:20 +00:00
import 'package:stackwallet/providers/global/prefs_provider.dart';
2023-07-27 16:33:59 +00:00
import 'package:stackwallet/providers/global/wallets_provider.dart';
2023-09-08 22:53:09 +00:00
import 'package:stackwallet/services/tor_service.dart';
2023-07-18 16:15:05 +00:00
import 'package:stackwallet/themes/stack_colors.dart';
2023-07-27 16:33:59 +00:00
import 'package:stackwallet/utilities/amount/amount.dart';
import 'package:stackwallet/utilities/amount/amount_formatter.dart';
2023-07-18 16:15:05 +00:00
import 'package:stackwallet/utilities/assets.dart';
2023-07-27 16:26:52 +00:00
import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/show_loading.dart';
2023-07-18 16:15:05 +00:00
import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/widgets/background.dart';
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
import 'package:stackwallet/widgets/desktop/secondary_button.dart';
2023-07-18 17:03:57 +00:00
import 'package:stackwallet/widgets/rounded_white_container.dart';
2023-07-18 16:15:05 +00:00
2023-07-27 16:33:59 +00:00
class OrdinalDetailsView extends ConsumerStatefulWidget {
2023-07-18 16:15:05 +00:00
const OrdinalDetailsView({
Key? key,
2023-07-18 16:15:05 +00:00
required this.walletId,
required this.ordinal,
}) : super(key: key);
2023-07-18 16:15:05 +00:00
final String walletId;
final Ordinal ordinal;
2023-07-18 16:15:05 +00:00
static const routeName = "/ordinalDetailsView";
@override
2023-07-27 16:33:59 +00:00
ConsumerState<OrdinalDetailsView> createState() => _OrdinalDetailsViewState();
2023-07-18 16:15:05 +00:00
}
2023-07-27 16:33:59 +00:00
class _OrdinalDetailsViewState extends ConsumerState<OrdinalDetailsView> {
2023-07-18 17:03:57 +00:00
static const _spacing = 12.0;
2023-07-27 16:33:59 +00:00
late final UTXO? utxo;
@override
void initState() {
utxo = widget.ordinal.getUTXO(ref.read(mainDBProvider));
super.initState();
}
2023-07-18 16:15:05 +00:00
@override
Widget build(BuildContext context) {
final coin = ref.watch(
pWallets.select((value) => value.getManager(widget.walletId).coin));
2023-07-27 16:33:59 +00:00
2023-07-18 16:15:05 +00:00
return Background(
child: SafeArea(
child: Scaffold(
backgroundColor:
2023-07-22 00:11:33 +00:00
Theme.of(context).extension<StackColors>()!.background,
2023-07-18 16:15:05 +00:00
appBar: AppBar(
backgroundColor:
2023-07-22 00:11:33 +00:00
Theme.of(context).extension<StackColors>()!.background,
2023-07-18 16:15:05 +00:00
leading: const AppBarBackButton(),
title: Text(
"Ordinal details",
style: STextStyles.navBarTitle(context),
),
),
2023-07-18 17:03:57 +00:00
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(
vertical: 12,
horizontal: 39,
),
child: _OrdinalImageGroup(
ordinal: widget.ordinal,
2023-07-18 17:03:57 +00:00
walletId: widget.walletId,
),
),
_DetailsItemWCopy(
title: "Inscription number",
data: widget.ordinal.inscriptionNumber.toString(),
2023-07-18 17:03:57 +00:00
),
const SizedBox(
height: _spacing,
),
_DetailsItemWCopy(
2023-07-27 16:33:59 +00:00
title: "Inscription ID",
data: widget.ordinal.inscriptionId,
2023-07-18 17:03:57 +00:00
),
2023-07-27 16:33:59 +00:00
// const SizedBox(
// height: _spacing,
// ),
// // todo: add utxo status
2023-07-18 17:03:57 +00:00
const SizedBox(
height: _spacing,
),
2023-07-27 16:33:59 +00:00
_DetailsItemWCopy(
2023-07-18 17:03:57 +00:00
title: "Amount",
2023-07-27 16:33:59 +00:00
data: utxo == null
? "ERROR"
: ref.watch(pAmountFormatter(coin)).format(
Amount(
rawValue: BigInt.from(utxo!.value),
fractionDigits: coin.decimals,
),
),
2023-07-18 17:03:57 +00:00
),
const SizedBox(
height: _spacing,
),
2023-07-27 16:33:59 +00:00
_DetailsItemWCopy(
2023-07-18 17:03:57 +00:00
title: "Owner address",
2023-07-27 16:33:59 +00:00
data: utxo?.address ?? "ERROR",
2023-07-18 17:03:57 +00:00
),
const SizedBox(
height: _spacing,
),
_DetailsItemWCopy(
title: "Transaction ID",
data: widget.ordinal.utxoTXID,
2023-07-18 17:03:57 +00:00
),
const SizedBox(
height: _spacing,
),
],
),
),
),
2023-07-18 16:15:05 +00:00
),
),
);
}
}
2023-07-18 17:03:57 +00:00
class _DetailsItemWCopy extends StatelessWidget {
const _DetailsItemWCopy({
Key? key,
2023-07-18 17:03:57 +00:00
required this.title,
required this.data,
}) : super(key: key);
2023-07-18 17:03:57 +00:00
final String title;
final String data;
@override
Widget build(BuildContext context) {
return RoundedWhiteContainer(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
title,
style: STextStyles.itemSubtitle(context),
),
GestureDetector(
onTap: () async {
await Clipboard.setData(ClipboardData(text: data));
if (context.mounted) {
unawaited(
showFloatingFlushBar(
type: FlushBarType.info,
message: "Copied to clipboard",
context: context,
),
);
}
},
2023-07-27 16:33:59 +00:00
child: Row(
children: [
SvgPicture.asset(
Assets.svg.copy,
color: Theme.of(context)
.extension<StackColors>()!
.infoItemIcons,
width: 12,
),
const SizedBox(
width: 6,
),
Text(
"Copy",
style: STextStyles.infoSmall(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.infoItemIcons,
),
),
],
2023-07-18 17:03:57 +00:00
),
),
],
),
const SizedBox(
height: 4,
),
SelectableText(
data,
style: STextStyles.itemSubtitle12(context),
),
],
),
);
}
}
2023-09-15 19:51:20 +00:00
class _OrdinalImageGroup extends ConsumerWidget {
2023-07-18 16:15:05 +00:00
const _OrdinalImageGroup({
Key? key,
2023-07-18 17:03:57 +00:00
required this.walletId,
required this.ordinal,
}) : super(key: key);
2023-07-18 16:15:05 +00:00
2023-07-18 17:03:57 +00:00
final String walletId;
final Ordinal ordinal;
2023-07-18 16:15:05 +00:00
static const _spacing = 12.0;
2023-09-15 19:51:20 +00:00
Future<String> _savePngToFile(WidgetRef ref) async {
2023-09-08 22:53:09 +00:00
HTTP client = HTTP();
2023-07-27 16:26:52 +00:00
2023-09-08 22:53:09 +00:00
final response = await client.get(
url: Uri.parse(ordinal.content),
2023-09-15 19:51:20 +00:00
proxyInfo: ref.read(prefsChangeNotifierProvider).useTor
? ref.read(pTorService).getProxyInfo()
: null,
2023-09-08 22:53:09 +00:00
);
if (response.code != 200) {
2023-09-20 04:26:23 +00:00
throw Exception(
"OrdinalDetailsView _savePngToFile statusCode=${response.code} body=${response.bodyBytes}");
2023-07-27 16:26:52 +00:00
}
final bytes = response.bodyBytes;
if (Platform.isAndroid) {
await Permission.storage.request();
}
final dir = Platform.isAndroid
? Directory("/storage/emulated/0/Documents")
: await getApplicationDocumentsDirectory();
final docPath = dir.path;
final filePath = "$docPath/ordinal_${ordinal.inscriptionNumber}.png";
File imgFile = File(filePath);
if (imgFile.existsSync()) {
throw Exception("File already exists");
}
await imgFile.writeAsBytes(bytes);
2023-07-27 21:42:58 +00:00
return filePath;
2023-07-27 16:26:52 +00:00
}
2023-07-18 16:15:05 +00:00
@override
2023-09-15 19:51:20 +00:00
Widget build(BuildContext context, WidgetRef ref) {
2023-07-18 16:15:05 +00:00
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
// Text(
// "${ordinal.inscriptionId}", // Use any other property you want
// style: STextStyles.w600_16(context),
// ),
// const SizedBox(
// height: _spacing,
// ),
2023-07-27 16:26:52 +00:00
ClipRRect(
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
child: AspectRatio(
aspectRatio: 1,
child: Container(
color: Colors.transparent,
child: Image.network(
ordinal.content, // Use the preview URL as the image source
fit: BoxFit.cover,
filterQuality:
FilterQuality.none, // Set the filter mode to nearest
),
2023-07-21 15:49:30 +00:00
),
2023-07-18 16:15:05 +00:00
),
),
const SizedBox(
height: _spacing,
),
Row(
children: [
Expanded(
child: SecondaryButton(
label: "Download",
2023-07-18 17:03:57 +00:00
icon: SvgPicture.asset(
Assets.svg.arrowDown,
width: 10,
height: 12,
color: Theme.of(context)
.extension<StackColors>()!
.buttonTextSecondary,
),
buttonHeight: ButtonHeight.l,
iconSpacing: 4,
2023-07-27 16:26:52 +00:00
onPressed: () async {
bool didError = false;
2023-07-27 21:42:58 +00:00
final filePath = await showLoading<String>(
2023-09-15 19:51:20 +00:00
whileFuture: _savePngToFile(ref),
2023-07-27 16:26:52 +00:00
context: context,
isDesktop: true,
message: "Saving ordinal image",
onException: (e) {
didError = true;
String msg = e.toString();
while (msg.isNotEmpty && msg.startsWith("Exception:")) {
msg = msg.substring(10).trim();
}
showFloatingFlushBar(
type: FlushBarType.warning,
message: msg,
context: context,
);
},
);
if (!didError && context.mounted) {
await showFloatingFlushBar(
type: FlushBarType.success,
2023-07-27 21:42:58 +00:00
message: "Image saved to $filePath",
2023-07-27 16:26:52 +00:00
context: context,
);
}
2023-07-18 16:15:05 +00:00
},
),
),
2023-07-23 02:50:49 +00:00
// const SizedBox(
// width: _spacing,
// ),
// Expanded(
// child: PrimaryButton(
// label: "Send",
// icon: SvgPicture.asset(
// Assets.svg.send,
// width: 10,
// height: 10,
// color: Theme.of(context)
// .extension<StackColors>()!
// .buttonTextPrimary,
// ),
// buttonHeight: ButtonHeight.l,
// iconSpacing: 4,
// onPressed: () async {
// final response = await showDialog<String?>(
// context: context,
// builder: (_) => const SendOrdinalUnfreezeDialog(),
// );
// if (response == "unfreeze") {
// // TODO: unfreeze and go to send ord screen
// }
// },
// ),
// ),
2023-07-18 16:15:05 +00:00
],
),
],
);
}
}