From e78a239bf59103fba63a1deb8f4e5b2abf2a1c0d Mon Sep 17 00:00:00 2001
From: Sander van Grieken <sander@outrightsolutions.nl>
Date: Fri, 6 May 2022 20:27:07 +0200
Subject: [PATCH] bugfixes, lifecycle improvements

---
 electrum/gui/qml/components/Addresses.qml           |  4 ++--
 .../gui/qml/components/ConfirmPaymentDialog.qml     |  1 +
 electrum/gui/qml/components/Receive.qml             |  4 +++-
 electrum/gui/qml/components/RequestDialog.qml       | 10 +++++-----
 .../qml/components/controls/NotificationPopup.qml   | 12 ++++++++----
 electrum/gui/qml/components/main.qml                | 12 ++++++------
 electrum/gui/qml/qeaddresslistmodel.py              |  6 ++++--
 electrum/gui/qml/qeinvoicelistmodel.py              | 11 ++++++++---
 electrum/gui/qml/qetransactionlistmodel.py          |  4 ++--
 electrum/gui/qml/qewallet.py                        | 13 +++++--------
 10 files changed, 44 insertions(+), 33 deletions(-)

diff --git a/electrum/gui/qml/components/Addresses.qml b/electrum/gui/qml/components/Addresses.qml
index 111d47ee2..c58d237a0 100644
--- a/electrum/gui/qml/components/Addresses.qml
+++ b/electrum/gui/qml/components/Addresses.qml
@@ -103,12 +103,12 @@ Pane {
                                 Label {
                                     font.family: FixedFont
                                     text: Config.formatSats(model.balance, false)
-                                    visible: model.balance > 0
+                                    visible: model.balance.satsInt != 0
                                 }
                                 Label {
                                     color: Material.accentColor
                                     text: Config.baseUnit + ','
-                                    visible: model.balance > 0
+                                    visible: model.balance.satsInt != 0
                                 }
                                 Label {
                                     text: model.numtx
diff --git a/electrum/gui/qml/components/ConfirmPaymentDialog.qml b/electrum/gui/qml/components/ConfirmPaymentDialog.qml
index fdcd91c7c..a0f04cd2b 100644
--- a/electrum/gui/qml/components/ConfirmPaymentDialog.qml
+++ b/electrum/gui/qml/components/ConfirmPaymentDialog.qml
@@ -206,6 +206,7 @@ Dialog {
                 enabled: finalizer.valid
                 onClicked: {
                     finalizer.send_onchain()
+                    dialog.close()
                 }
             }
         }
diff --git a/electrum/gui/qml/components/Receive.qml b/electrum/gui/qml/components/Receive.qml
index b04a89f5f..b6648001e 100644
--- a/electrum/gui/qml/components/Receive.qml
+++ b/electrum/gui/qml/components/Receive.qml
@@ -215,7 +215,9 @@ Pane {
 
     Component {
         id: requestdialog
-        RequestDialog {}
+        RequestDialog {
+            onClosed: destroy()
+        }
     }
 
     function createRequest(ignoreGaplimit = false) {
diff --git a/electrum/gui/qml/components/RequestDialog.qml b/electrum/gui/qml/components/RequestDialog.qml
index c12da6026..0d5bd9e48 100644
--- a/electrum/gui/qml/components/RequestDialog.qml
+++ b/electrum/gui/qml/components/RequestDialog.qml
@@ -130,17 +130,17 @@ Dialog {
             }
 
             Label {
-                visible: modelItem.amount != 0
+                visible: modelItem.amount.satsInt != 0
                 text: qsTr('Amount')
             }
             Label {
-                visible: modelItem.amount != 0
+                visible: modelItem.amount.satsInt != 0
                 text: Config.formatSats(modelItem.amount)
                 font.family: FixedFont
                 font.pixelSize: constants.fontSizeLarge
             }
             Label {
-                visible: modelItem.amount != 0
+                visible: modelItem.amount.satsInt != 0
                 text: Config.baseUnit
                 color: Material.accentColor
                 font.pixelSize: constants.fontSizeLarge
@@ -148,7 +148,7 @@ Dialog {
 
             Label {
                 id: fiatValue
-                visible: modelItem.amount != 0
+                visible: modelItem.amount.satsInt != 0
                 Layout.fillWidth: true
                 Layout.columnSpan: 2
                 text: Daemon.fx.enabled
@@ -191,7 +191,7 @@ Dialog {
 
     Connections {
         target: Daemon.currentWallet
-        function onRequestStatusChanged(key, code) {
+        function onRequestStatusChanged(key, status) {
             if (key != modelItem.key)
                 return
             modelItem = Daemon.currentWallet.get_request(key)
diff --git a/electrum/gui/qml/components/controls/NotificationPopup.qml b/electrum/gui/qml/components/controls/NotificationPopup.qml
index ed6595bd4..adff81c09 100644
--- a/electrum/gui/qml/components/controls/NotificationPopup.qml
+++ b/electrum/gui/qml/components/controls/NotificationPopup.qml
@@ -32,14 +32,22 @@ Rectangle {
         }
     ]
 
+    function show(message) {
+        root.text = message
+        root.hide = false
+        closetimer.start()
+    }
+
     RowLayout {
         id: layout
         width: parent.width
         Text {
             id: textItem
             Layout.alignment: Qt.AlignHCenter
+            Layout.fillWidth: true
             font.pixelSize: constants.fontSizeLarge
             color: Material.foreground
+            wrapMode: Text.Wrap
         }
     }
 
@@ -50,8 +58,4 @@ Rectangle {
         onTriggered: hide = true
     }
 
-    Component.onCompleted: {
-        hide = false
-        closetimer.start()
-    }
 }
diff --git a/electrum/gui/qml/components/main.qml b/electrum/gui/qml/components/main.qml
index 40f73b3b7..5d438d24a 100644
--- a/electrum/gui/qml/components/main.qml
+++ b/electrum/gui/qml/components/main.qml
@@ -174,13 +174,13 @@ ApplicationWindow
     property alias messageDialog: _messageDialog
     Component {
         id: _messageDialog
-        MessageDialog {}
+        MessageDialog {
+            onClosed: destroy()
+        }
     }
 
-    property alias notificationPopup: _notificationPopup
-    Component {
-        id: _notificationPopup
-        NotificationPopup {}
+    NotificationPopup {
+        id: notificationPopup
     }
 
     Component.onCompleted: {
@@ -226,7 +226,7 @@ ApplicationWindow
     Connections {
         target: AppController
         function onUserNotify(message) {
-            var item = app.notificationPopup.createObject(app, {'text': message})
+            notificationPopup.show(message)
         }
     }
 }
diff --git a/electrum/gui/qml/qeaddresslistmodel.py b/electrum/gui/qml/qeaddresslistmodel.py
index 4abe8a7f6..eb042b8f2 100644
--- a/electrum/gui/qml/qeaddresslistmodel.py
+++ b/electrum/gui/qml/qeaddresslistmodel.py
@@ -4,6 +4,8 @@ from PyQt5.QtCore import Qt, QAbstractListModel, QModelIndex
 from electrum.logging import get_logger
 from electrum.util import Satoshis
 
+from .qetypes import QEAmount
+
 class QEAddressListModel(QAbstractListModel):
     def __init__(self, wallet, parent=None):
         super().__init__(parent)
@@ -32,7 +34,7 @@ class QEAddressListModel(QAbstractListModel):
             address = self.receive_addresses[index.row()]
         role_index = role - Qt.UserRole
         value = address[self._ROLE_NAMES[role_index]]
-        if isinstance(value, bool) or isinstance(value, list) or isinstance(value, int) or value is None:
+        if isinstance(value, (bool, list, int, str, QEAmount)) or value is None:
             return value
         if isinstance(value, Satoshis):
             return value.value
@@ -50,7 +52,7 @@ class QEAddressListModel(QAbstractListModel):
         item['numtx'] = self.wallet.get_address_history_len(address)
         item['label'] = self.wallet.get_label(address)
         c, u, x = self.wallet.get_addr_balance(address)
-        item['balance'] = c + u + x
+        item['balance'] = QEAmount(amount_sat=c + u + x)
         item['held'] = self.wallet.is_frozen_address(address)
         return item
 
diff --git a/electrum/gui/qml/qeinvoicelistmodel.py b/electrum/gui/qml/qeinvoicelistmodel.py
index 52e8d304d..91eec07ad 100644
--- a/electrum/gui/qml/qeinvoicelistmodel.py
+++ b/electrum/gui/qml/qeinvoicelistmodel.py
@@ -33,12 +33,11 @@ class QEAbstractInvoiceListModel(QAbstractListModel):
         invoice = self.invoices[index.row()]
         role_index = role - Qt.UserRole
         value = invoice[self._ROLE_NAMES[role_index]]
-        if isinstance(value, bool) or isinstance(value, list) or isinstance(value, int) or value is None:
+
+        if isinstance(value, (bool, list, int, str, QEAmount)) or value is None:
             return value
         if isinstance(value, Satoshis):
             return value.value
-        if isinstance(value, QEAmount):
-            return value
         return str(value)
 
     def clear(self):
@@ -77,6 +76,12 @@ class QEAbstractInvoiceListModel(QAbstractListModel):
                 break
             i = i + 1
 
+    def get_model_invoice(self, key: str):
+        for invoice in self.invoices:
+            if invoice['key'] == key:
+                return invoice
+        return None
+
     @pyqtSlot(str, int)
     def updateInvoice(self, key, status):
         self._logger.debug('updating invoice for %s to %d' % (key,status))
diff --git a/electrum/gui/qml/qetransactionlistmodel.py b/electrum/gui/qml/qetransactionlistmodel.py
index 22ec81643..3426b0488 100644
--- a/electrum/gui/qml/qetransactionlistmodel.py
+++ b/electrum/gui/qml/qetransactionlistmodel.py
@@ -34,7 +34,7 @@ class QETransactionListModel(QAbstractListModel):
         tx = self.tx_history[index.row()]
         role_index = role - Qt.UserRole
         value = tx[self._ROLE_NAMES[role_index]]
-        if isinstance(value, bool) or isinstance(value, list) or isinstance(value, int) or value is None:
+        if isinstance(value, (bool, list, int, str, QEAmount)) or value is None:
             return value
         if isinstance(value, Satoshis):
             return value.value
@@ -110,7 +110,7 @@ class QETransactionListModel(QAbstractListModel):
                 tx['height'] = info.height
                 tx['confirmations'] = info.conf
                 tx['timestamp'] = info.timestamp
-                tx['date'] = self.format_date_by_section(datetime.fromtimestamp(info.timestamp), tx['section'])
+                tx['date'] = self.format_date_by_section(tx['section'], datetime.fromtimestamp(info.timestamp))
                 index = self.index(i,0)
                 roles = [self._ROLE_RMAP[x] for x in ['height','confirmations','timestamp','date']]
                 self.dataChanged.emit(index, index, roles)
diff --git a/electrum/gui/qml/qewallet.py b/electrum/gui/qml/qewallet.py
index 09e4820ce..a1f2cf3a9 100644
--- a/electrum/gui/qml/qewallet.py
+++ b/electrum/gui/qml/qewallet.py
@@ -322,22 +322,21 @@ class QEWallet(QObject):
             #pass
             ##self.sign_payment_request(addr)
         self._requestModel.add_invoice(self.wallet.get_request(req_key))
-        #return addr
+        return addr
 
     @pyqtSlot(QEAmount, 'QString', int)
     @pyqtSlot(QEAmount, 'QString', int, bool)
     @pyqtSlot(QEAmount, 'QString', int, bool, bool)
     def create_request(self, amount: QEAmount, message: str, expiration: int, is_lightning: bool = False, ignore_gap: bool = False):
-        expiry = expiration #TODO: self.config.get('request_expiry', PR_DEFAULT_EXPIRATION_WHEN_CREATING)
         try:
             if is_lightning:
                 if not self.wallet.lnworker.channels:
                     self.requestCreateError.emit('fatal',_("You need to open a Lightning channel first."))
                     return
                 # TODO maybe show a warning if amount exceeds lnworker.num_sats_can_receive (as in kivy)
-                key = self.wallet.lnworker.add_request(amount.satsInt, message, expiry)
+                key = self.wallet.lnworker.add_request(amount.satsInt, message, expiration)
             else:
-                key = self.create_bitcoin_request(amount.satsInt, message, expiry, ignore_gap)
+                key = self.create_bitcoin_request(amount.satsInt, message, expiration, ignore_gap)
                 if not key:
                     return
                 self._addressModel.init_model()
@@ -356,8 +355,7 @@ class QEWallet(QObject):
 
     @pyqtSlot('QString', result='QVariant')
     def get_request(self, key: str):
-        req = self.wallet.get_request(key)
-        return self._requestModel.invoice_to_model(req)
+        return self._requestModel.get_model_invoice(key)
 
     @pyqtSlot('QString')
     def delete_invoice(self, key: str):
@@ -367,5 +365,4 @@ class QEWallet(QObject):
 
     @pyqtSlot('QString', result='QVariant')
     def get_invoice(self, key: str):
-        invoice = self.wallet.get_invoice(key)
-        return self._invoiceModel.invoice_to_model(invoice)
+        return self._invoiceModel.get_model_invoice(key)