-
Notifications
You must be signed in to change notification settings - Fork 10
/
news.go
70 lines (53 loc) · 1.29 KB
/
news.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
package bridge
import (
"github.com/nokka/slashdiablo-launcher/log"
"github.com/nokka/slashdiablo-launcher/news"
"github.com/therecipe/qt/core"
)
// NewsBridge is the connection between QML and the news model.
type NewsBridge struct {
core.QObject
// Dependencies.
newsService news.Service
logger log.Logger
// Properties.
_ bool `property:"loading"`
_ bool `property:"error"`
// Models.
NewsModel *core.QAbstractListModel `property:"items"`
// Slots.
_ func() `slot:"getNews"`
}
// Connect will connect the QML signals to functions in Go.
func (b *NewsBridge) Connect() {
b.ConnectGetNews(b.getNews)
}
func (b *NewsBridge) getNews() {
go func() {
// Tell the GUI that we're fetching data.
b.SetLoading(true)
// Set the news items on the model.
err := b.newsService.SetNewsItems()
// Stop loading when we're done fetching news data.
b.SetLoading(false)
if err != nil {
b.logger.Error(err)
b.SetError(true)
return
}
}()
return
}
// NewNews sets up a news bridge with all dependencies.
func NewNews(ns news.Service, nm *news.Model, logger log.Logger) *NewsBridge {
l := NewNewsBridge(nil)
// Setup dependencies.
l.newsService = ns
l.logger = logger
// Setup model.
l.SetItems(nm)
// Set initial state.
l.SetLoading(false)
l.SetError(false)
return l
}