-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
665 lines (582 loc) · 18 KB
/
Copy pathmain.go
File metadata and controls
665 lines (582 loc) · 18 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
package main
import (
"encoding/json"
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"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"
cp "github.com/otiai10/copy"
"image/color"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
lib "tinyappcatalogmanager/lib"
"tinyappcatalogmanager/ui"
)
func getPageAsString(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
type Item struct {
Category string "category"
Subcategory string "subcategory"
Screenshot string "screenshot"
URI string "uri"
Name string "name"
Info string "info"
Size string "size"
Site string "site"
Downloads string "downloads"
Supreme string "supreme"
Sourcecode string "sourcecode"
Shareware string "shareware"
Noinstall string "noinstall"
}
var allItems []Item
func convertLibItemToItem(libItem lib.Item, subcat string) Item {
return Item{
Category: subcat,
Subcategory: libItem.Category,
Screenshot: libItem.Screenshot,
URI: libItem.URI,
Name: libItem.Name,
Info: libItem.Info,
Size: libItem.Size,
Site: libItem.Site,
Downloads: libItem.Downloads,
Supreme: libItem.Supreme,
Sourcecode: libItem.Sourcecode,
Shareware: libItem.Shareware,
Noinstall: libItem.Noinstall,
}
}
func parseCategories(cat string) {
url := "https://tinyapps.org/"
pageString, err := getPageAsString(url + "/" + cat + ".html")
if err != nil {
fmt.Println("Error reading page:", err)
return
}
// Parse page
libItems, err := lib.ParseWebPage(pageString, cat)
if err != nil {
fmt.Println("Error parsing HTML:", err)
return
}
for _, libItem := range libItems {
item := convertLibItemToItem(libItem, cat)
allItems = append(allItems, item)
}
//for _, item := range items {
// fmt.Println("Category:", cat)
// fmt.Println("Subcategory:", item.Category)
// fmt.Println("Screenshot:", item.Screenshot)
// fmt.Println("URI:", item.URI)
// fmt.Println("Name:", item.Name)
// fmt.Println("Info:", item.Info)
// fmt.Println()
//}
}
func saveAllToJson() {
file, err := os.Create("items.json")
if err != nil {
fmt.Println("Error creating file:", err)
return
}
defer file.Close()
encoder := json.NewEncoder(file)
err = encoder.Encode(allItems)
if err != nil {
fmt.Println("Error encoding JSON:", err)
return
}
fmt.Println("Items saved as items.json")
}
func revertFallbackData() {
cp.Copy("data/latest/items.json", "items.json")
cp.Copy("data/latest/items.txt", "items.txt")
cp.Copy("data/latest/screenshots", "../screenshots/")
}
func cleanFiles() {
// cp.Copy("data/latest/items.txt", "items.txt")
os.Remove("items.json")
os.RemoveAll("../screenshots")
os.Create("items.json")
os.Mkdir("../screenshots", os.ModePerm)
}
func readJsonItems(file string) []lib.ItemJsonStruct {
items, err := lib.ReadJSONFromFile(file)
if err != nil {
fmt.Println("Error:", err)
return nil
}
return items
}
func fileExistsAndNotEmpty(filename string) (bool, error) {
fileInfo, err := os.Stat(filename)
if os.IsNotExist(err) {
// File does not exist
return false, nil
} else if err != nil {
// Error occurred while checking file
return false, err
}
// Check if it's a regular file and not a directory
if fileInfo.Mode().IsRegular() && fileInfo.Size() > 0 {
return true, nil
}
// File exists but is empty
return false, nil
}
func buildMenu(win fyne.Window, app fyne.App) *fyne.MainMenu {
nt2 := fyne.NewMenuItem("Update Latest", func() {
progressBar := widget.NewProgressBar()
progressBar.Max = 100
progressLabel := widget.NewLabel("")
updatingLabel := widget.NewLabel("Please wait. Fetching latest catalog from Tinyapps.org")
closeButton := widget.NewButton("Close App", func() {
win.Close()
})
closeButton.Disabled()
progressBarContainer := container.NewVBox(
updatingLabel,
progressBar,
progressLabel,
closeButton,
)
dialog.ShowCustom("Updating catalog", "", progressBarContainer, win)
go func() {
progressBar.SetValue(1)
cleanFiles()
progressBar.SetValue(2)
// fetchApi Start
parsedData, err := lib.ReadAndParseFile("items.txt", "\r\n")
progressBar.SetValue(3)
if err != nil {
fmt.Println("Error:", err)
return
}
for i, line := range parsedData {
progressBar.SetValue(float64(i) / float64(len(parsedData)) * 100)
parseCategories(line[0])
}
progressBar.SetValue(99)
saveAllToJson()
progressBar.SetValue(100)
// fetchApi End
progressBar.SetValue(100)
progressLabel.SetText("Catalog updated successfully. Please close and re-run application")
updatingLabel.SetText("Finished")
closeButton.Enable()
}()
})
nt3 := fyne.NewMenuItem("Revert Fallback Locally", func() {
revertFallbackData()
dialog.ShowInformation("Updating catalog", "Catalog updated from local database and loaded successfully.", win)
})
manageStore := fyne.NewMenu("Manage Catalog", nt2, nt3)
themedef := fyne.NewMenuItem("Tinyapps Default", func() {
app.Settings().SetTheme(ui.TinyappsUi())
//app.Preferences().SetString("theme", "default")
})
thememin := fyne.NewMenuItem("Tinyapps Minimal", func() {
app.Settings().SetTheme(ui.TinyappsUiRecessed())
//app.Preferences().SetString("theme", "min")
})
def := fyne.NewMenuItem("System", func() {
app.Settings().SetTheme(theme.DefaultTheme())
})
themeMenus := fyne.NewMenuItem("Theme", nil)
themeMenus.ChildMenu = fyne.NewMenu(
"",
themedef,
thememin,
def,
)
filterMenus := fyne.NewMenuItem("Filter Apps", nil)
// No filter
filterMenuNoFilterLabel := "✔ No filter (all apps) "
filterMenuNoFilter := fyne.NewMenuItem(filterMenuNoFilterLabel, func() {
app.Preferences().SetString("filter-supreme", "0")
app.Preferences().SetString("filter-shareware", "0")
app.Preferences().SetString("filter-noinstall", "0")
app.Preferences().SetString("filter-run", "0")
if app.Preferences().String("filter-no") == "1" {
app.Preferences().SetString("filter-no", "0")
filterMenuNoFilterLabel = " No filter"
} else {
app.Preferences().SetString("filter-no", "1")
filterMenuNoFilterLabel = "✔ No filter"
}
})
// filter supreme
filterMenuSupremeLabel := " Supreme 🌱 "
filterSupreme := fyne.NewMenuItem(filterMenuSupremeLabel, func() {
app.Preferences().SetString("filter-no", "0")
if app.Preferences().String("filter-supreme") == "1" {
app.Preferences().SetString("filter-supreme", "0")
filterMenuSupremeLabel = " Supreme 🌱"
} else {
app.Preferences().SetString("filter-supreme", "1")
filterMenuSupremeLabel = "✔ Supreme 🌱"
}
})
filterMenuSharewareLabel := " Shareware 💰 "
filterShareware := fyne.NewMenuItem(filterMenuSharewareLabel, func() {
app.Preferences().SetString("filter-no", "0")
if app.Preferences().String("filter-shareware") == "1" {
app.Preferences().SetString("filter-shareware", "0")
filterMenuSharewareLabel = " Shareware 💰"
} else {
app.Preferences().SetString("filter-shareware", "1")
filterMenuSharewareLabel = "✔ Shareware 💰"
}
})
filterMenuNoinstallLabel := " No install â–¶ "
filterNoinstall := fyne.NewMenuItem(filterMenuNoinstallLabel, func() {
app.Preferences().SetString("filter-no", "0")
if app.Preferences().String("filter-noinstall") == "1" {
app.Preferences().SetString("filter-noinstall", "0")
filterMenuNoinstallLabel = " No install â–¶"
} else {
app.Preferences().SetString("filter-noinstall", "1")
filterMenuNoinstallLabel = "✔ No install ▶"
}
})
filterMenuRunLabel := " Source code 📄 "
filterRun := fyne.NewMenuItem(filterMenuRunLabel, func() {
app.Preferences().SetString("filter-no", "0")
if app.Preferences().String("filter-run") == "1" {
app.Preferences().SetString("filter-run", "0")
filterMenuRunLabel = " Source code 📄"
} else {
app.Preferences().SetString("filter-run", "1")
filterMenuRunLabel = "✔ Source code 📄"
}
})
filterMenus.ChildMenu = fyne.NewMenu(
"",
filterMenuNoFilter,
filterSupreme,
filterShareware,
filterNoinstall,
filterRun,
)
settings := fyne.NewMenu("Settings", themeMenus, filterMenus)
website := fyne.NewMenuItem("Open TinyApps Official Site", func() {
u, err := url.Parse("https://tinyapps.org/")
if err != nil {
fmt.Println("error", err)
}
app.OpenURL(u)
})
websiteapp := fyne.NewMenuItem("Open Catalog Manager", func() {
u, err := url.Parse("https://github.com/reactorcoder/tinyappscatalogmanager")
if err != nil {
fmt.Println("error", err)
}
app.OpenURL(u)
})
aboutitem := fyne.NewMenuItem("About Catalog Apps", func() {
// imagePath := filepath.Join("../", selectedApp.Screenshot)
// emptyCanvas = canvas.NewImageFromFile(imagePath)
// emptyCanvas.SetMinSize(fyne.Size{
// Width: 200,
// Height: 200,
// })
textCanvas := canvas.NewText("Developed freely to fetch lists of apps from tinyapps.org as catalog apps manager.\n\n\n\n "+
"Lists of apps are maintained by tinyapps.org. \n\n "+
"Apps catalog manager are available source from GitHub: https://github.com/reactorcoder/tinyappscatalogmanager/tree/main \n\n"+
"2023 \n "+
"Version: 1.0", color.White)
dialog.ShowInformation("About Catalog Apps", textCanvas.Text, win)
})
ab := fyne.NewMenu("About", website, websiteapp, aboutitem)
m := fyne.NewMainMenu(manageStore, settings, ab)
return m
}
var selFilter = widget.NewLabel("")
func main() {
a := app.NewWithID("app.codervio.tinyapps.catalogapp")
myApp := app.New()
myApp.Settings().SetTheme(ui.TinyappsUi())
myApp.Preferences().SetString("filter-no", "1")
myApp.Preferences().SetString("filter-supreme", "0")
myApp.Preferences().SetString("filter-shareware", "0")
myApp.Preferences().SetString("filter-noinstall", "0")
myApp.Preferences().SetString("filter-run", "0")
myWindow := myApp.NewWindow("TinyApps - Apps Catalog Manager")
myWindow.Resize(fyne.NewSize(800, 600))
myWindow.SetMainMenu(buildMenu(myWindow, myApp))
iconPath := "img/favicon-tbn.png"
iconContents, err := os.ReadFile(iconPath)
if err != nil {
} else {
iconRes := fyne.NewStaticResource("tinyapps.org app catalog", iconContents)
myWindow.SetIcon(iconRes)
}
widgetPane := container.New(layout.NewMaxLayout())
selectedAppPane := container.New(layout.NewVBoxLayout())
var emptyCanvas *canvas.Image
var selTab string
leftContent := container.NewMax()
//rightContent := container.NewMax()
exists, err := fileExistsAndNotEmpty("items.json")
if err != nil {
// error
dialog.ShowInformation("Updating catalog data",
"There are error to update catalog data: "+fmt.Sprint(err),
myWindow)
} else if exists {
// skip, exists, not empty
} else {
// not exists/empty
revertFallbackData()
dialog.ShowInformation("Updating catalog data",
"Tinyapps catalog data has been updated from locally. You can run Manage Catalog > Update Latest to get latest updates.",
myWindow)
}
items := readJsonItems("items.json")
uniqueCategories := lib.GetUniqueCategories(items)
tabs := container.NewAppTabs()
//tabsleft := container.NewAppTabs()
//tabsInside := container.NewAppTabs()
//appslists := container.NewHBox()
var itemCategories []string
// Left pane cat lists
for _, category := range uniqueCategories {
cat := category
itemCategories = append(itemCategories, cat)
// Tabs
//uniqueCategories := lib.FilterItemsAndUniqueSubcategories(items, cat)
//tabs := container.NewAppTabs()
//for _, subcat := range uniqueCategories {
//tabsleft.Append(container.NewTabItem(subcat, widget.NewLabel(subcat)))
//}
//tabs.SetTabLocation(container.TabLocationTop)
}
// Category on left pane
listCats := widget.NewList(
func() int {
return len(itemCategories)
},
func() fyne.CanvasObject {
return widget.NewLabel("")
},
func(i widget.ListItemID, obj fyne.CanvasObject) {
obj.(*widget.Label).SetText(itemCategories[i])
},
)
// Event on left click category
listCats.OnSelected = func(id widget.ListItemID) {
selectedItem := itemCategories[id]
selTab = selectedItem
uniqueCategories := lib.FilterItemsAndUniqueSubcategories(items, selectedItem)
tabs.Items = nil
tabs.Append(container.NewTabItem("", widget.NewLabel("")))
for _, subcat := range uniqueCategories {
// Render name inside, make tabs on right
tabs.Append(container.NewTabItem(subcat, widget.NewLabelWithStyle(subcat, fyne.TextAlign(fyne.TextAlignCenter), fyne.TextStyle{
Bold: true,
Italic: false,
Monospace: false,
Symbol: false,
TabWidth: 1,
})))
}
tabs.DisableIndex(0)
//tabs.SetTabLocation(container.TabLocationTop)
widgetPane.RemoveAll()
}
// On clicked right top tab show lists of apps
tabs.OnSelected = func(ti *container.TabItem) {
widgetPane.RemoveAll()
filterNo := myApp.Preferences().String("filter-no") == "1"
filterSupreme := myApp.Preferences().String("filter-supreme") == "1"
filterShareware := myApp.Preferences().String("filter-shareware") == "1"
filterNoinstall := myApp.Preferences().String("filter-noinstall") == "1"
filterSourcecode := myApp.Preferences().String("filter-run") == "1"
appLists := lib.FilterItemsByCategoryAndSubcategoryFiltered(
items,
selTab,
ti.Text,
filterNo,
filterSupreme,
filterShareware,
filterNoinstall,
filterSourcecode)
if filterSupreme || filterShareware || filterNoinstall || filterSourcecode {
activeFilter := ""
if filterSupreme {
activeFilter += "🌱"
}
if filterShareware {
activeFilter += "💰"
}
if filterNoinstall {
activeFilter += "â–¶"
}
if filterSourcecode {
activeFilter += "📄"
}
selFilter.SetText("FILTER ACTIVE: " + activeFilter)
} else {
selFilter.SetText("")
}
listwidget := widget.NewList(
func() int {
return len(appLists)
},
func() fyne.CanvasObject {
return container.NewVBox(
widget.NewLabelWithStyle("", fyne.TextAlign(fyne.TextWrapOff), fyne.TextStyle{
Bold: true,
Italic: false,
Monospace: false,
Symbol: false,
TabWidth: 0,
}),
widget.NewLabelWithStyle("", fyne.TextAlign(fyne.TextAlignLeading), fyne.TextStyle{
Bold: false,
Italic: false,
Monospace: false,
Symbol: false,
TabWidth: 0,
}),
)
},
func(index int, item fyne.CanvasObject) {
listItem := appLists[index]
if vbox, ok := item.(*fyne.Container); ok {
labels := vbox.Objects
if len(labels) >= 2 {
if nameLabel, ok := labels[0].(*widget.Label); ok {
iconsList := ""
if listItem.Supreme != "" {
iconsList += "🌱"
}
if listItem.Shareware != "" {
iconsList += "💰"
}
if listItem.Noinstall != "" {
iconsList += "â–¶"
}
if listItem.Sourcecode != "" {
iconsList += "📄"
}
nameLabel.SetText(fmt.Sprintf("%s %s", listItem.Name, iconsList))
}
if valueLabel, ok := labels[1].(*widget.Label); ok {
valueLabel.SetText(listItem.Info)
valueLabel.Wrapping = fyne.TextWrapOff
}
}
}
})
listwidget.OnSelected = func(id widget.ListItemID) {
selectedApp := appLists[id]
selectedAppPane.RemoveAll()
imagePath := filepath.Join("../", selectedApp.Screenshot)
emptyCanvas = canvas.NewImageFromFile(imagePath)
emptyCanvas.SetMinSize(fyne.Size{
Width: 200,
Height: 200,
})
//selectedAppPane.Add(widget.NewLabel(selectedApp.URI))
selectedAppPane.Add(widget.NewLabel(selectedApp.Name))
selectedAppPane.Add(container.NewMax(emptyCanvas))
selectedAppPane.Add(widget.NewLabel("Size: " + selectedApp.Size))
selectedAppPane.Add(widget.NewButton("Open", func() {
u, err := url.Parse(selectedApp.URI)
if err != nil {
fmt.Println("error", err)
}
a.OpenURL(u)
}))
var dwbtn = widget.NewButton("", nil)
if selectedApp.Downloads != "" {
dwbtn.SetText("Download")
dwbtn.OnTapped = func() {
u, err := url.Parse("https://tinyapps.org" + selectedApp.Downloads)
if err != nil {
fmt.Println("Error parsing URL:", err)
// Optionally, you can display an error message to the user here
return
}
if err := a.OpenURL(u); err != nil {
fmt.Println("Error opening URL:", err)
// Optionally, you can display an error message to the user here
}
}
}
selectedAppPane.Add(dwbtn)
// Imagesearch btn
var imgsrcbtn = widget.NewButton("", nil)
if selectedApp.Screenshot != "" {
imgsrcbtn.SetText("Image Search")
imgsrcbtn.OnTapped = func() {
u, err := url.Parse("https://lens.google.com/uploadbyurl?url=https://tinyapps.org" + selectedApp.Screenshot)
if err != nil {
fmt.Println("Error parsing URL:", err)
// Optionally, you can display an error message to the user here
return
}
if err := a.OpenURL(u); err != nil {
fmt.Println("Error opening URL:", err)
// Optionally, you can display an error message to the user here
}
}
}
selectedAppPane.Add(imgsrcbtn)
// Site btn
var sitebtn = widget.NewButton("", nil)
if selectedApp.Site != "" {
sitebtn.SetText("Site")
sitebtn.OnTapped = func() {
u, err := url.Parse(selectedApp.Site)
if err != nil {
fmt.Println("Error parsing URL:", err)
// Optionally, you can display an error message to the user here
return
}
if err := a.OpenURL(u); err != nil {
fmt.Println("Error opening URL:", err)
// Optionally, you can display an error message to the user here
}
}
}
selectedAppPane.Add(sitebtn)
selectedAppPane.Add(selFilter)
}
widgetPane.Add(listwidget)
}
// Left content
leftpanes := container.NewVSplit(listCats, selectedAppPane)
leftContent.Add(leftpanes)
leftpanes.Offset = 0.6
wpane := container.NewVSplit(tabs, widgetPane)
wpane.Offset = 0.1
ri := container.NewBorder(
container.NewMax(widget.NewSeparator()), nil, nil, nil, wpane,
)
res := container.NewHSplit(leftContent, ri)
res.Offset = 0.1
myWindow.SetContent(res)
myWindow.SetMaster()
myWindow.ShowAndRun()
}