/
ui_button_click.go
86 lines (73 loc) · 2.05 KB
/
ui_button_click.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 src
import (
"fmt"
"fyne.io/fyne/dialog"
"github.com/anthonynsimon/bild/transform"
"net/http"
"time"
)
// Проверка подключения к Интернету
func haveConnection() bool {
client := http.Client{
Timeout: 500*time.Millisecond,
}
resp,err := client.Get("https://www.google.com/")
if err != nil {
fmt.Println(err)
return false
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
fmt.Println(resp.StatusCode)
return false
}
return true
}
// Событие нажатия кнопки
func mainButtonClick(s1,s2,tt string) {
q := fmt.Sprintf("%s-%s(%s)",
s1,s2, time.Now().Format("2006-01-02"))
var routes []route
// кэш расписания доступен оффлайн
// подключение в Интернету проверять, если нет в кэше
if inCache(q) {
button2()
routes = routesCache[q]
go buttonBack(time.Second)
} else {
PastCity1,PastCity2 = s1,s2
saveLastCities(s1,s2)
resultBox.Hide()
if s1 == s2 {
dialog.ShowInformation("", "Города должны быть разными", W)
return
}
button1()
// проверка подключения к Интернету
if !haveConnection() {
dialog.ShowInformation("", "Нет подключения к Интернету", W)
buttonBack(0)
return
}
routes = getAllSortedRoutes(s1,s2, time.Now().Format("2006-01-02"), tt)
routesCache[q] = routes
saveCache()
}
if len(routes) > 0 {
// Нарисовать диаграмму и сделать миниатюру с подписью
img := drawDiagram(routes, s1 + " → " + s2)
smallImg := transform.Resize(img, 320, 180, transform.Linear)
СколькоДиаграммСделано++
saveCountOfCompletedDiagrams()
resultImage = img
resultImageWidget.Image = smallImg
resultText.Text = s1 + " — " + s2
resultBox.Show()
} else {
// Если рейсов нет, то миниатюру скрыть
resultBox.Hide()
dialog.ShowInformation("", "Нет рейсов", W)
buttonBack(0)
}
resultBox.Refresh()
}