From 0dd91b067e214e170ead4f4a8807872b3dfb0293 Mon Sep 17 00:00:00 2001
From: julian-CStack <julian@cypherstack.com>
Date: Sat, 8 Apr 2023 17:22:14 -0600
Subject: [PATCH] tweaked mobile xpub view style

---
 .../global_settings_view/xpub_view.dart       | 212 +++++++++++-------
 .../wallet_settings_view.dart                 |  22 +-
 2 files changed, 141 insertions(+), 93 deletions(-)

diff --git a/lib/pages/settings_views/global_settings_view/xpub_view.dart b/lib/pages/settings_views/global_settings_view/xpub_view.dart
index dc3ca7e0b..e499218cc 100644
--- a/lib/pages/settings_views/global_settings_view/xpub_view.dart
+++ b/lib/pages/settings_views/global_settings_view/xpub_view.dart
@@ -109,8 +109,10 @@ class _XPubViewState extends ConsumerState<XPubView> {
                           .extension<StackColors>()!
                           .topNavIconPrimary,
                     ),
-                    onPressed: () async {
-                      await _copy();
+                    onPressed: () {
+                      if (xpub != null) {
+                        _copy();
+                      }
                     },
                   ),
                 ),
@@ -118,12 +120,13 @@ class _XPubViewState extends ConsumerState<XPubView> {
             ],
           ),
           body: Padding(
-              padding: const EdgeInsets.only(
-                top: 12,
-                left: 16,
-                right: 16,
-              ),
-              child: child),
+            padding: const EdgeInsets.only(
+              top: 12,
+              left: 16,
+              right: 16,
+            ),
+            child: child,
+          ),
         ),
       ),
       child: ConditionalParent(
@@ -168,76 +171,46 @@ class _XPubViewState extends ConsumerState<XPubView> {
         child: Column(
           children: [
             if (isDesktop) const SizedBox(height: 44),
-            FutureBuilder(
-              future: (manager.wallet as XPubAble).xpub,
-              builder: (context, AsyncSnapshot<String> snapshot) {
-                if (snapshot.connectionState == ConnectionState.done &&
-                    snapshot.hasData) {
-                  xpub = snapshot.data!;
-                }
+            ConditionalParent(
+              condition: !isDesktop,
+              builder: (child) => Expanded(
+                child: child,
+              ),
+              child: FutureBuilder(
+                future: (manager.wallet as XPubAble).xpub,
+                builder: (context, AsyncSnapshot<String> snapshot) {
+                  if (snapshot.connectionState == ConnectionState.done &&
+                      snapshot.hasData) {
+                    xpub = snapshot.data!;
+                  }
 
-                return Column(
-                  children: [
-                    ConditionalParent(
-                      condition: !isDesktop,
-                      builder: (child) => RoundedWhiteContainer(
-                        child: child,
-                      ),
-                      child: xpub == null
-                          ? const SizedBox(
-                              height: 300,
-                              child: LoadingIndicator(),
-                            )
-                          : QrImage(
-                              data: xpub!,
-                              size: 280,
-                              foregroundColor: Theme.of(context)
-                                  .extension<StackColors>()!
-                                  .accentColorDark,
-                            ),
-                    ),
-                    const SizedBox(height: 25),
-                    RoundedWhiteContainer(
-                      padding: const EdgeInsets.all(16),
-                      borderColor: xpub == null
-                          ? null
-                          : Theme.of(context)
-                              .extension<StackColors>()!
-                              .backgroundAppBar,
-                      child: SelectableText(
-                        xpub ?? "",
-                        style: STextStyles.largeMedium14(context),
-                      ),
-                    ),
-                    if (isDesktop) const SizedBox(height: 32),
-                    if (!isDesktop) const Spacer(),
-                    Row(
-                      mainAxisAlignment: MainAxisAlignment.center,
-                      children: [
-                        Expanded(
-                          child: SecondaryButton(
-                            buttonHeight: ButtonHeight.xl,
-                            label: "Cancel",
-                            onPressed: Navigator.of(
-                              context,
-                              rootNavigator: true,
-                            ).pop,
-                          ),
+                  const height = 600.0;
+                  Widget child;
+                  if (xpub == null) {
+                    child = const SizedBox(
+                      key: Key("loadingXPUB"),
+                      height: height,
+                      child: Center(
+                        child: LoadingIndicator(
+                          width: 100,
                         ),
-                        const SizedBox(width: 16),
-                        Expanded(
-                          child: PrimaryButton(
-                            buttonHeight: ButtonHeight.xl,
-                            label: "Copy",
-                            enabled: xpub != null,
-                            onPressed: _copy,
-                          ),
-                        ),
-                      ],
+                      ),
+                    );
+                  } else {
+                    child = _XPub(
+                      xpub: xpub!,
+                      height: height,
+                    );
+                  }
+
+                  return AnimatedSwitcher(
+                    duration: const Duration(
+                      milliseconds: 200,
                     ),
-                  ],
-                );
-              },
+                    child: child,
+                  );
+                },
+              ),
             ),
           ],
         ),
@@ -245,3 +218,92 @@ class _XPubViewState extends ConsumerState<XPubView> {
     );
   }
 }
+
+class _XPub extends StatelessWidget {
+  const _XPub({
+    Key? key,
+    required this.xpub,
+    required this.height,
+    this.clipboardInterface = const ClipboardWrapper(),
+  }) : super(key: key);
+
+  final String xpub;
+  final double height;
+  final ClipboardInterface clipboardInterface;
+
+  @override
+  Widget build(BuildContext context) {
+    final bool isDesktop = Util.isDesktop;
+
+    return SizedBox(
+      height: isDesktop ? height : double.infinity,
+      child: Column(
+        children: [
+          ConditionalParent(
+            condition: !isDesktop,
+            builder: (child) => RoundedWhiteContainer(
+              child: child,
+            ),
+            child: QrImage(
+              data: xpub,
+              size: isDesktop ? 280 : MediaQuery.of(context).size.width / 1.5,
+              foregroundColor:
+                  Theme.of(context).extension<StackColors>()!.accentColorDark,
+            ),
+          ),
+          const SizedBox(height: 25),
+          RoundedWhiteContainer(
+            padding: const EdgeInsets.all(16),
+            borderColor:
+                Theme.of(context).extension<StackColors>()!.backgroundAppBar,
+            child: SelectableText(
+              xpub,
+              style: STextStyles.largeMedium14(context),
+            ),
+          ),
+          const SizedBox(height: 32),
+          Row(
+            children: [
+              if (isDesktop)
+                Expanded(
+                  child: SecondaryButton(
+                    buttonHeight: ButtonHeight.xl,
+                    label: "Cancel",
+                    onPressed: Navigator.of(
+                      context,
+                      rootNavigator: true,
+                    ).pop,
+                  ),
+                ),
+              if (isDesktop) const SizedBox(width: 16),
+              Expanded(
+                child: PrimaryButton(
+                  buttonHeight: ButtonHeight.xl,
+                  label: "Copy",
+                  onPressed: () async {
+                    await clipboardInterface.setData(
+                      ClipboardData(
+                        text: xpub,
+                      ),
+                    );
+                    if (context.mounted) {
+                      unawaited(
+                        showFloatingFlushBar(
+                          type: FlushBarType.info,
+                          message: "Copied to clipboard",
+                          iconAsset: Assets.svg.copy,
+                          context: context,
+                        ),
+                      );
+                    }
+                  },
+                ),
+              ),
+            ],
+          ),
+          if (!isDesktop) const Spacer(),
+        ],
+      ),
+    );
+  }
+}
diff --git a/lib/pages/settings_views/wallet_settings_view/wallet_settings_view.dart b/lib/pages/settings_views/wallet_settings_view/wallet_settings_view.dart
index ca48c8b6b..592630b12 100644
--- a/lib/pages/settings_views/wallet_settings_view/wallet_settings_view.dart
+++ b/lib/pages/settings_views/wallet_settings_view/wallet_settings_view.dart
@@ -1,7 +1,5 @@
 import 'dart:async';
 
-import 'package:bip32/bip32.dart' as bip32;
-import 'package:bip39/bip39.dart' as bip39;
 import 'package:event_bus/event_bus.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter_riverpod/flutter_riverpod.dart';
@@ -288,23 +286,11 @@ class _WalletSettingsViewState extends State<WalletSettingsView> {
                                       return SettingsListButton(
                                         iconAssetName: Assets.svg.eye,
                                         title: "Wallet xPub",
-                                        onPressed: () async {
-                                          final List<String> mnemonic = await ref
-                                              .read(
-                                                  walletsChangeNotifierProvider)
-                                              .getManager(widget.walletId)
-                                              .mnemonic;
-
-                                          final seed = bip39.mnemonicToSeed(
-                                              mnemonic.join(' '));
-                                          final node =
-                                              bip32.BIP32.fromSeed(seed);
-                                          final xpub =
-                                              node.neutered().toBase58();
-
+                                        onPressed: () {
                                           Navigator.of(context).pushNamed(
-                                              XPubView.routeName,
-                                              arguments: xpub);
+                                            XPubView.routeName,
+                                            arguments: widget.walletId,
+                                          );
                                         },
                                       );
                                     },