stack_wallet/lib/pages/ordinals/ordinal_details_view.dart

384 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';
2024-05-27 23:56:22 +00:00
import '../../models/isar/models/blockchain_data/utxo.dart';
import '../../models/isar/ordinal.dart';
import '../../networking/http.dart';
import '../../notifications/show_flush_bar.dart';
import '../../providers/db/main_db_provider.dart';
import '../../providers/global/prefs_provider.dart';
import '../../services/tor_service.dart';
import '../../themes/stack_colors.dart';
import '../../utilities/amount/amount.dart';
import '../../utilities/amount/amount_formatter.dart';
import '../../utilities/assets.dart';
import '../../utilities/constants.dart';
import '../../utilities/show_loading.dart';
import '../../utilities/text_styles.dart';
import '../../wallets/isar/providers/wallet_info_provider.dart';
import '../../widgets/background.dart';
import '../../widgets/custom_buttons/app_bar_icon_button.dart';
import '../../widgets/desktop/secondary_button.dart';
import '../../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({
2024-05-15 21:20:45 +00:00
super.key,
2023-07-18 16:15:05 +00:00
required this.walletId,
required this.ordinal,
2024-05-15 21:20:45 +00:00
});
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(pWalletCoin(widget.walletId));
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),
2024-05-15 21:20:45 +00:00
fractionDigits: coin.fractionDigits,
2023-07-27 16:33:59 +00:00
),
),
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({
2024-05-27 23:56:22 +00:00
super.key,
2023-07-18 17:03:57 +00:00
required this.title,
required this.data,
2024-05-27 23:56:22 +00:00
});
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({
2024-05-27 23:56:22 +00:00
super.key,
2023-07-18 17:03:57 +00:00
required this.walletId,
required this.ordinal,
2024-05-27 23:56:22 +00:00
});
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 {
2024-05-27 23:56:22 +00:00
final 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(
2024-05-27 23:56:22 +00:00
"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";
2024-05-27 23:56:22 +00:00
final File imgFile = File(filePath);
2023-07-27 16:26:52 +00:00
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,
2024-05-03 18:22:19 +00:00
rootNavigator: true,
2023-07-27 16:26:52 +00:00
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
],
),
],
);
}
}