forked from fyne-io/apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
86 lines (67 loc) · 1.44 KB
/
data.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
package main
import (
"encoding/json"
"io"
"net/http"
"os"
"path/filepath"
"sort"
"time"
"fyne.io/fyne/v2"
)
const keyInstallPrefix = "installed."
type App struct {
ID, Name, Icon string
Developer, Summary string
URL, Website string
Screenshots []AppScreenshot
Date time.Time
Version string
Source AppSource
Requires string
}
type AppScreenshot struct {
Image, Type string
}
type AppSource struct {
Git, Package string
}
type AppList []App
func installedVersion(a App) string {
return fyne.CurrentApp().Preferences().String(keyInstallPrefix + a.ID)
}
func markInstalled(a App) {
ver := a.Version
if ver == "" {
ver = "latest"
}
fyne.CurrentApp().Preferences().SetString(keyInstallPrefix+a.ID, ver)
}
func parseAppList(reader io.Reader) (AppList, error) {
decode := json.NewDecoder(reader)
appList := AppList{}
err := decode.Decode(&appList)
if err != nil {
return nil, err
}
appList = appList.filterCompatible()
sort.Slice(appList, func(a, b int) bool {
return appList[a].Name < appList[b].Name
})
return appList, nil
}
func loadAppListFromWeb() (io.ReadCloser, error) {
res, err := http.Get("https://apps.fyne.io/api/v1/list.json")
if err != nil {
return nil, err
}
return res.Body, nil
}
// TODO make actual cache read()
func loadAppListFromCache() (io.ReadCloser, error) {
res, err := os.Open(filepath.Join("testdata", "list.json"))
if err != nil {
return nil, err
}
return res, nil
}