-
Notifications
You must be signed in to change notification settings - Fork 46
/
start_page.go
237 lines (207 loc) · 7.29 KB
/
start_page.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package page
import (
"os"
"gioui.org/layout"
"gioui.org/text"
"github.com/planetdecred/dcrlibwallet"
"github.com/planetdecred/godcr/ui/decredmaterial"
"github.com/planetdecred/godcr/ui/load"
"github.com/planetdecred/godcr/ui/modal"
"github.com/planetdecred/godcr/ui/page/wallets"
"github.com/planetdecred/godcr/ui/values"
)
const StartPageID = "start_page"
type startPage struct {
*load.Load
createButton decredmaterial.Button
restoreButton decredmaterial.Button
loading bool
}
func NewStartPage(l *load.Load) load.Page {
sp := &startPage{
Load: l,
loading: true,
createButton: l.Theme.Button("Create a new wallet"),
restoreButton: l.Theme.Button("Restore an existing wallet"),
}
return sp
}
// ID is a unique string that identifies the page and may be used
// to differentiate this page from other pages.
// Part of the load.Page interface.
func (sp *startPage) ID() string {
return StartPageID
}
// OnNavigatedTo is called when the page is about to be displayed and
// may be used to initialize page features that are only relevant when
// the page is displayed.
// Part of the load.Page interface.
func (sp *startPage) OnNavigatedTo() {
sp.WL.MultiWallet = sp.WL.Wallet.GetMultiWallet()
// refresh theme now that config is available
sp.RefreshTheme()
if sp.WL.MultiWallet.LoadedWalletsCount() > 0 {
if sp.WL.MultiWallet.IsStartupSecuritySet() {
sp.unlock()
} else {
go sp.openWallets("")
}
} else {
sp.loading = false
}
}
func (sp *startPage) unlock() {
modal.NewPasswordModal(sp.Load).
Title("Unlock with password").
Hint("Startup password").
NegativeButton("Exit", func() {
sp.WL.MultiWallet.Shutdown()
os.Exit(0)
}).
PositiveButton("Unlock", func(password string, m *modal.PasswordModal) bool {
go func() {
err := sp.openWallets(password)
if err != nil {
m.SetError(translateErr(err))
m.SetLoading(false)
return
}
m.Dismiss()
}()
return false
}).Show()
}
func (sp *startPage) openWallets(password string) error {
err := sp.WL.MultiWallet.OpenWallets([]byte(password))
if err != nil {
log.Info("Error opening wallet:", err)
// show err dialog
return err
}
sp.ChangeWindowPage(NewMainPage(sp.Load), false)
return nil
}
// HandleUserInteractions is called just before Layout() to determine
// if any user interaction recently occurred on the page and may be
// used to update the page's UI components shortly before they are
// displayed.
// Part of the load.Page interface.
func (sp *startPage) HandleUserInteractions() {
for sp.createButton.Clicked() {
modal.NewCreatePasswordModal(sp.Load).
Title("Create new wallet").
PasswordCreated(func(_, password string, m *modal.CreatePasswordModal) bool {
go func() {
_, err := sp.WL.MultiWallet.CreateNewWallet("mywallet", password, dcrlibwallet.PassphraseTypePass)
if err != nil {
m.SetError(err.Error())
m.SetLoading(false)
return
}
m.Dismiss()
sp.ChangeWindowPage(NewMainPage(sp.Load), false)
}()
return false
}).Show()
}
for sp.restoreButton.Clicked() {
afterRestore := func() {
sp.ChangeWindowPage(NewMainPage(sp.Load), false)
}
sp.ChangeWindowPage(wallets.NewRestorePage(sp.Load, afterRestore), true)
}
}
// OnNavigatedFrom is called when the page is about to be removed from
// the displayed window. This method should ideally be used to disable
// features that are irrelevant when the page is NOT displayed.
// NOTE: The page may be re-displayed on the app's window, in which case
// OnNavigatedTo() will be called again. This method should not destroy UI
// components unless they'll be recreated in the OnNavigatedTo() method.
// Part of the load.Page interface.
func (sp *startPage) OnNavigatedFrom() {}
// Layout draws the page UI components into the provided layout context
// to be eventually drawn on screen.
// Part of the load.Page interface.
func (sp *startPage) Layout(gtx layout.Context) layout.Dimensions {
gtx.Constraints.Min = gtx.Constraints.Max // use maximum height & width
return layout.Stack{Alignment: layout.N}.Layout(gtx,
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return sp.loadingSection(gtx)
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
if sp.loading {
return layout.Dimensions{}
}
return sp.buttonSection(gtx)
}),
)
}),
)
}
func (sp *startPage) loadingSection(gtx layout.Context) layout.Dimensions {
gtx.Constraints.Min.X = gtx.Constraints.Max.X // use maximum width
if sp.loading {
gtx.Constraints.Min.Y = gtx.Constraints.Max.Y
} else {
gtx.Constraints.Min.Y = (gtx.Constraints.Max.Y * 75) / 100 // use 75% of view height
}
return layout.Stack{Alignment: layout.Center}.Layout(gtx,
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
return layout.Flex{Alignment: layout.Middle, Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Center.Layout(gtx, func(gtx C) D {
return sp.Theme.Icons.DecredSymbolIcon.LayoutSize(gtx, values.MarginPadding150)
})
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
netType := sp.WL.Wallet.Net
if sp.WL.Wallet.Net == dcrlibwallet.Testnet3 {
netType = "Testnet"
}
nType := sp.Theme.Label(values.TextSize20, netType)
nType.Font.Weight = text.Medium
return nType.Layout(gtx)
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
if sp.loading {
loadStatus := sp.Theme.Label(values.TextSize20, "Loading")
if sp.WL.MultiWallet.LoadedWalletsCount() > 0 {
loadStatus.Text = "Opening wallets"
}
return layout.Inset{Top: values.MarginPadding24}.Layout(gtx, loadStatus.Layout)
}
welcomeText := sp.Theme.Label(values.TextSize24, "Welcome to Decred Wallet, a secure & open-source mobile wallet.")
return layout.Inset{Top: values.MarginPadding24}.Layout(gtx, welcomeText.Layout)
}),
)
}),
)
}
func (sp *startPage) buttonSection(gtx layout.Context) layout.Dimensions {
gtx.Constraints.Min.X = gtx.Constraints.Max.X // use maximum width
gtx.Constraints.Min.Y = (gtx.Constraints.Max.Y * 25) / 100 // use 25% of view height
return layout.Stack{Alignment: layout.S}.Layout(gtx,
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
return layout.Flex{Alignment: layout.Middle, Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
gtx.Constraints.Max.X = gtx.Px(values.AppWidth) // set button with to app width
gtx.Constraints.Min.X = gtx.Constraints.Max.X
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Inset{Top: values.MarginPadding24, Left: values.MarginPadding24, Right: values.MarginPadding24}.Layout(gtx, func(gtx C) D {
return sp.createButton.Layout(gtx)
})
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Inset{Top: values.MarginPadding24, Bottom: values.MarginPadding24, Left: values.MarginPadding24, Right: values.MarginPadding24}.Layout(gtx, func(gtx C) D {
return sp.restoreButton.Layout(gtx)
})
}),
)
}),
)
}),
)
}