save ordinal image to file

This commit is contained in:
julian 2023-07-27 10:26:52 -06:00
parent 6ccd7a76a5
commit e93ecad744
2 changed files with 167 additions and 30 deletions

View file

@ -1,17 +1,21 @@
import 'dart:async'; import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart'; import 'package:flutter_svg/flutter_svg.dart';
import 'package:http/http.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:stackwallet/models/isar/ordinal.dart'; import 'package:stackwallet/models/isar/ordinal.dart';
import 'package:stackwallet/notifications/show_flush_bar.dart'; import 'package:stackwallet/notifications/show_flush_bar.dart';
import 'package:stackwallet/pages/ordinals/widgets/dialogs.dart';
import 'package:stackwallet/themes/stack_colors.dart'; import 'package:stackwallet/themes/stack_colors.dart';
import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/show_loading.dart';
import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/widgets/background.dart'; import 'package:stackwallet/widgets/background.dart';
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart'; import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
import 'package:stackwallet/widgets/desktop/primary_button.dart';
import 'package:stackwallet/widgets/desktop/secondary_button.dart'; import 'package:stackwallet/widgets/desktop/secondary_button.dart';
import 'package:stackwallet/widgets/rounded_white_container.dart'; import 'package:stackwallet/widgets/rounded_white_container.dart';
@ -184,6 +188,36 @@ class _OrdinalImageGroup extends StatelessWidget {
static const _spacing = 12.0; static const _spacing = 12.0;
Future<void> _savePngToFile() async {
final response = await get(Uri.parse(ordinal.content));
if (response.statusCode != 200) {
throw Exception(
"statusCode=${response.statusCode} body=${response.bodyBytes}");
}
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);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Column( return Column(
@ -197,7 +231,11 @@ class _OrdinalImageGroup extends StatelessWidget {
// const SizedBox( // const SizedBox(
// height: _spacing, // height: _spacing,
// ), // ),
AspectRatio( ClipRRect(
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
child: AspectRatio(
aspectRatio: 1, aspectRatio: 1,
child: Container( child: Container(
color: Colors.transparent, color: Colors.transparent,
@ -209,6 +247,7 @@ class _OrdinalImageGroup extends StatelessWidget {
), ),
), ),
), ),
),
const SizedBox( const SizedBox(
height: _spacing, height: _spacing,
), ),
@ -227,8 +266,37 @@ class _OrdinalImageGroup extends StatelessWidget {
), ),
buttonHeight: ButtonHeight.l, buttonHeight: ButtonHeight.l,
iconSpacing: 4, iconSpacing: 4,
onPressed: () { onPressed: () async {
// TODO: save and download image to device bool didError = false;
await showLoading(
whileFuture: Future.wait([
_savePngToFile(),
Future<void>.delayed(const Duration(seconds: 2)),
]),
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,
message: "Image saved",
context: context,
);
}
}, },
), ),
), ),

View file

@ -1,8 +1,14 @@
import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/flutter_svg.dart'; import 'package:flutter_svg/flutter_svg.dart';
import 'package:http/http.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:stackwallet/models/isar/models/blockchain_data/utxo.dart'; import 'package:stackwallet/models/isar/models/blockchain_data/utxo.dart';
import 'package:stackwallet/models/isar/ordinal.dart'; import 'package:stackwallet/models/isar/ordinal.dart';
import 'package:stackwallet/notifications/show_flush_bar.dart';
import 'package:stackwallet/pages/wallet_view/transaction_views/transaction_details_view.dart'; import 'package:stackwallet/pages/wallet_view/transaction_views/transaction_details_view.dart';
import 'package:stackwallet/providers/db/main_db_provider.dart'; import 'package:stackwallet/providers/db/main_db_provider.dart';
import 'package:stackwallet/providers/global/wallets_provider.dart'; import 'package:stackwallet/providers/global/wallets_provider.dart';
@ -12,10 +18,12 @@ import 'package:stackwallet/utilities/amount/amount_formatter.dart';
import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/constants.dart'; import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/show_loading.dart';
import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart'; import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
import 'package:stackwallet/widgets/desktop/desktop_app_bar.dart'; import 'package:stackwallet/widgets/desktop/desktop_app_bar.dart';
import 'package:stackwallet/widgets/desktop/desktop_scaffold.dart'; import 'package:stackwallet/widgets/desktop/desktop_scaffold.dart';
import 'package:stackwallet/widgets/desktop/secondary_button.dart';
import 'package:stackwallet/widgets/rounded_white_container.dart'; import 'package:stackwallet/widgets/rounded_white_container.dart';
class DesktopOrdinalDetailsView extends ConsumerStatefulWidget { class DesktopOrdinalDetailsView extends ConsumerStatefulWidget {
@ -41,6 +49,36 @@ class _DesktopOrdinalDetailsViewState
late final UTXO? utxo; late final UTXO? utxo;
Future<void> _savePngToFile() async {
final response = await get(Uri.parse(widget.ordinal.content));
if (response.statusCode != 200) {
throw Exception(
"statusCode=${response.statusCode} body=${response.bodyBytes}");
}
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_${widget.ordinal.inscriptionNumber}.png";
File imgFile = File(filePath);
if (imgFile.existsSync()) {
throw Exception("File already exists");
}
await imgFile.writeAsBytes(bytes);
}
@override @override
void initState() { void initState() {
utxo = widget.ordinal.getUTXO(ref.read(mainDBProvider)); utxo = widget.ordinal.getUTXO(ref.read(mainDBProvider));
@ -171,23 +209,54 @@ class _DesktopOrdinalDetailsViewState
// const SizedBox( // const SizedBox(
// width: 16, // width: 16,
// ), // ),
// SecondaryButton( SecondaryButton(
// width: 150, width: 150,
// label: "Download", label: "Download",
// icon: SvgPicture.asset( icon: SvgPicture.asset(
// Assets.svg.arrowDown, Assets.svg.arrowDown,
// width: 13, width: 13,
// height: 18, height: 18,
// color: Theme.of(context) color: Theme.of(context)
// .extension<StackColors>()! .extension<StackColors>()!
// .buttonTextSecondary, .buttonTextSecondary,
// ), ),
// buttonHeight: ButtonHeight.l, buttonHeight: ButtonHeight.l,
// iconSpacing: 8, iconSpacing: 8,
// onPressed: () { onPressed: () async {
// // TODO: save and download image to device bool didError = false;
// }, await showLoading(
// ), whileFuture: Future.wait([
_savePngToFile(),
Future<void>.delayed(
const Duration(seconds: 2)),
]),
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 && mounted) {
await showFloatingFlushBar(
type: FlushBarType.success,
message: "Image saved",
context: context,
);
}
},
),
], ],
), ),
), ),