-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscreens.go
217 lines (179 loc) · 7.69 KB
/
screens.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
package main
import (
"errors"
"fmt"
"log"
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
widgetx "fyne.io/x/fyne/widget"
"github.com/lostdusty/gobalt/v2"
)
func showFirstRunScreen() fyne.CanvasObject {
welcomeText := widget.NewRichTextFromMarkdown("# Welcome!\n\nLooks like it's the first time you're launching this app, so we need to do a quick setup before you can download.\n\nCobalt doesn't provide a public api anymore, so you need to host your own instance or use someone's instance, with their permission.")
welcomeText.Wrapping = fyne.TextWrapWord
welcomeSep := canvas.NewLine(theme.Color(theme.ColorNamePrimary))
welcomeSep.StrokeWidth = 1.5
welcomeTextNext := widget.NewLabel("To proceed, type or select an instance below. Click on the button on the right to refresh the instance list.")
welcomeInstanceSelector := widgetx.NewCompletionEntry(nil)
welcomeEntryApiKey := widget.NewPasswordEntry()
welcomeEntryApiKey.PlaceHolder = "Something like this: 123e4567-e89b-12d3-a456-426655440000"
welcomeEntryApiKey.ActionItem = &widget.Button{
Icon: theme.HelpIcon(),
OnTapped: func() {
dialog.ShowInformation("Help - Api Keys", "You might need an api key if you wanna use certain instances.\nAsk for the instance owner for one.\nThis field can be blank if a key is not needed.", coalWindow)
},
}
revealApiKeyBtn := widget.NewButtonWithIcon("", theme.VisibilityIcon(), func() {
welcomeEntryApiKey.Password = !welcomeEntryApiKey.Password
welcomeEntryApiKey.Refresh()
})
formFieldApiKey := container.NewBorder(nil, nil, nil, revealApiKeyBtn, welcomeEntryApiKey)
finishSetupBtn := widget.NewButtonWithIcon("Proceed", theme.NavigateNextIcon(), func() {
popupVerifyInstance := dialog.NewCustomWithoutButtons("Testing this instance", loadingContent("Checking instance..."), coalWindow)
popupVerifyInstance.Show()
if !strings.HasPrefix(welcomeInstanceSelector.Text, "https") {
log.Println("triggered, new string: " + "https://" + welcomeInstanceSelector.Text)
welcomeInstanceSelector.Text = "https://" + welcomeInstanceSelector.Text
welcomeInstanceSelector.Refresh()
}
config := gobalt.CreateDefaultSettings()
config.Url = "https://www.youtube.com/watch?v=aQvGIIdgFDM"
gobalt.CobaltApi = welcomeInstanceSelector.Text
gobalt.ApiKey = welcomeEntryApiKey.Text
//log.Println(welcomeInstanceSelector.Text, welcomeEntryApiKey.Text)
_, err := gobalt.Run(config)
if err != nil {
//log.Println(err)
popupVerifyInstance.Hide()
log.Printf("Err: %v | Instance: %v | API-Key: %v | Gobalt config: %v", err, gobalt.CobaltApi, gobalt.ApiKey, config)
if err.Error() == "error.api.youtube.login" {
dialog.ShowConfirm("Something is wrong", "This instance is working, but it can't download YouTube videos.\nDo you want to use this instance anyway?", func(b bool) {
if b {
coalApp.Preferences().SetString("instance", gobalt.CobaltApi)
coalApp.Preferences().SetString("api-key", gobalt.ApiKey)
coalApp.Preferences().SetBool("first-run", true)
coalWindow.SetContent(showMainScreen())
}
}, coalWindow)
return
}
dialog.ShowError(fmt.Errorf("unable to use selected instance, did you set an api key?\n(Details: %v)", err), coalWindow)
return
}
coalApp.Preferences().SetString("instance", gobalt.CobaltApi)
coalApp.Preferences().SetString("api-key", gobalt.ApiKey)
coalApp.Preferences().SetBool("first-run", true)
coalWindow.SetContent(showMainScreen())
popupVerifyInstance.Hide()
})
finishSetupBtn.Disable()
finishSetupBtn.Importance = widget.SuccessImportance
finishSetupBtn.IconPlacement = widget.ButtonIconTrailingText
welcomeInstanceSelector.ActionItem = &widget.Button{
Icon: theme.ViewRefreshIcon(),
OnTapped: func() {
log.Println("refreshing instances...")
popupRefreshing := widget.NewModalPopUp(loadingContent("Refreshing instance list..."), coalWindow.Canvas())
popupRefreshing.Show()
instances, err := refreshInstances()
if err != nil {
popupRefreshing.Hide()
dialog.ShowError(errors.New("unable to refresh instances.\nverify your internet connection"), coalWindow)
}
welcomeInstanceSelector.SetOptions(instances)
popupRefreshing.Hide()
log.Println("done refreshing, instances:", instances)
},
}
welcomeInstanceSelector.PlaceHolder = "api.cobalt.tools"
welcomeInstanceSelector.Validator = func(s string) error {
if s == "" {
finishSetupBtn.Disable()
return errors.New("please set one instance to proceed")
}
finishSetupBtn.Enable()
return nil
}
welcomeInstanceSelector.OnChanged = func(s string) {
if len(welcomeInstanceSelector.Options) > 1 {
welcomeInstanceSelector.ShowCompletion()
}
}
formInstanceInfo := widget.NewForm(&widget.FormItem{
Text: "Instance URL",
Widget: welcomeInstanceSelector,
HintText: "Click on the refresh button to get a list of public instances.",
}, &widget.FormItem{
Text: "Instance API Key",
Widget: formFieldApiKey,
HintText: "Click on the \"?\" button for help",
})
welcomeLayout := container.NewBorder(nil, finishSetupBtn, nil, nil, container.NewVBox(welcomeText, welcomeSep, welcomeTextNext, formInstanceInfo))
return welcomeLayout
}
func showMainScreen() fyne.CanvasObject {
//downloadOptions := gobalt.CreateDefaultSettings()
btnConfig := widget.NewButtonWithIcon("", theme.SettingsIcon(), func() {
coalWindow.SetContent(showConfigScreen())
})
btnDownloadQueue := widget.NewButtonWithIcon("", theme.ListIcon(), func() {
//queue screen
})
headerTitleApp := widget.NewLabelWithStyle("coal", fyne.TextAlignCenter, fyne.TextStyle{Bold: true})
headerContainer := container.NewBorder(nil, nil, btnConfig, btnDownloadQueue, headerTitleApp)
container.NewThemeOverride(headerContainer, themeNoBg{})
cardProgress := widget.NewProgressBar()
cardTitle := widget.NewRichTextFromMarkdown("## download.title")
cardSubtitle := widget.NewLabel("download.description")
cardImg := &canvas.Image{Resource: theme.FyneLogo(), FillMode: canvas.ImageFillContain}
layoutCard := container.NewGridWithColumns(2, cardImg, container.NewVBox(cardTitle, cardSubtitle, cardProgress))
downloadEntry := widget.NewEntry()
downloadEntry.PlaceHolder = "https://www.youtube.com/watch?v=Q7M5v8UAZAI"
btnPasteFromClip := widget.NewButtonWithIcon("", theme.ContentPasteIcon(), func() {
downloadEntry.SetText(regexPaste.FindString(coalWindow.Clipboard().Content()))
})
//btnPasteFromClip.Importance = widget.HighImportance
downloadEntry.ActionItem = btnPasteFromClip
btnDownload := widget.NewButtonWithIcon("", theme.DownloadIcon(), nil)
btnDownload.Importance = widget.SuccessImportance
btnDownload.Disable()
downloadEntry.Validator = func(s string) error {
if regexPaste.MatchString(s) {
btnDownload.Enable()
return nil
}
btnDownload.Disable()
return fmt.Errorf("invalid url")
}
downloadInputLayout := container.NewBorder(nil, nil, nil, btnDownload, downloadEntry)
return container.NewVBox(headerContainer, layout.NewSpacer(), layoutCard, downloadInputLayout, layout.NewSpacer())
}
func showConfigScreen() fyne.CanvasObject {
return widgetLoading()
}
func refreshInstances() ([]string, error) {
getInstances, err := gobalt.GetCobaltInstances()
if err != nil {
return nil, err
}
var instancesList []string
for _, v := range getInstances {
instancesList = append(instancesList, "https://"+v.API)
}
return instancesList, nil
}
func widgetLoading() fyne.CanvasObject {
load, _ := widgetx.NewAnimatedGifFromResource(resourceLoadingGif)
load.SetMinSize(fyne.NewSquareSize(16))
load.Start()
return load
}
func loadingContent(text string) fyne.CanvasObject {
return container.NewHBox(widgetLoading(), widget.NewLabel(text))
}