Skip to content

Commit

Permalink
feat: adds a hint as to what scanning the QR code will do
Browse files Browse the repository at this point in the history
  • Loading branch information
HashMapsData2Value committed Oct 24, 2024
1 parent d8a5f43 commit 0c9bca6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
36 changes: 26 additions & 10 deletions ui/pages/transaction/controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package transaction

import (
"fmt"

"github.com/algorand/go-algorand-sdk/v2/types"
"github.com/algorandfoundation/algourl/encoder"
"github.com/algorandfoundation/hack-tui/api"
Expand All @@ -17,30 +19,31 @@ func (m ViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m *ViewModel) UpdateTxnURLAndQRCode() error {

accountStatus := m.State.Accounts[m.Data.Address].Status

var isOnline bool
m.hint = ""

senderAddress, err := types.DecodeAddress(m.Data.Address)
if err != nil {
return err
}

var isOnline bool
switch accountStatus {
case "Offline":
isOnline = false
case "Online":
isOnline = true
case "Offline":
isOnline = false
case "NotParticipating": // This status means the account can never participate in consensus
m.urlTxn = ""
m.asciiQR = ""
m.hint = fmt.Sprintf("%s is NotParticipating. Cannot register key.", senderAddress)
return nil
}

// Construct Transaction
tx := types.Transaction{}

senderAddress, err := types.DecodeAddress(m.Data.Address)
if err != nil {
return err
}

if !isOnline { // TX take account online
var stateProofPk types.MerkleVerifier
copy(stateProofPk[:], (*m.Data.Key.StateProofKey)[:])
Expand All @@ -61,6 +64,13 @@ func (m *ViewModel) UpdateTxnURLAndQRCode() error {
},
}

// Update hint if no error
defer func() {
if err == nil {
m.hint = fmt.Sprintf("Scan this QR code to take %s Online.", senderAddress)
}
}()

} else { // TX to take account offline
tx = types.Transaction{
Type: types.KeyRegistrationTx,
Expand All @@ -69,6 +79,13 @@ func (m *ViewModel) UpdateTxnURLAndQRCode() error {
Fee: 1000, //TODO: get proper fee
},
}

// Update hint if no error
defer func() {
if err == nil {
m.hint = fmt.Sprintf("Scan this QR code to take %s Offline.", senderAddress)
}
}()
}

// Construct QR Code
Expand All @@ -89,7 +106,6 @@ func (m *ViewModel) UpdateTxnURLAndQRCode() error {

m.urlTxn = kr.String()
m.asciiQR = qrCode

return nil
}

Expand Down
3 changes: 2 additions & 1 deletion ui/pages/transaction/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ type ViewModel struct {
// Components
controls controls.Model

// QR Code and URL
// QR Code, URL and hint text
asciiQR string
urlTxn string
hint string
}

// New creates and instance of the ViewModel with a default controls.Model
Expand Down
16 changes: 12 additions & 4 deletions ui/pages/transaction/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ func (m ViewModel) View() string {
urlStyle := lipgloss.NewStyle().
Width(m.Width - 2)

red := lipgloss.NewStyle().Foreground(lipgloss.Color("9"))
red := lipgloss.NewStyle().
Foreground(lipgloss.Color("9")).
Width(m.Width - 2)

yellow := lipgloss.NewStyle().
Foreground(lipgloss.Color("11")).
Width(m.Width - 2)

var Padding1 = lipgloss.NewStyle().Padding().Render

if m.asciiQR == "" || m.urlTxn == "" {
return lipgloss.JoinVertical(
lipgloss.Left,
m.asciiQR,
m.urlTxn,
"No QR Code or TxnURL available",
"\n",
m.controls.View())
Expand All @@ -33,7 +39,9 @@ func (m ViewModel) View() string {
}
return lipgloss.JoinVertical(
lipgloss.Left,
urlStyle.Render(m.urlTxn),
yellow.Render(m.hint),
Padding1(),
qrStyle.Render(m.asciiQR),
urlStyle.Render(m.urlTxn),
m.controls.View())
}

0 comments on commit 0c9bca6

Please sign in to comment.