This repository has been archived by the owner on May 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive.go
159 lines (136 loc) · 4.18 KB
/
archive.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
package panels
import (
"net/url"
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/jmillerv/analect/internal/models"
"github.com/sirupsen/logrus"
)
func Archive(w fyne.Window) fyne.CanvasObject {
// Load the quotes from the quotes.json file
quotes, err := loadQuotes(w)
if err != nil {
return widget.NewLabel("Error loading quotes")
}
searchBar := widget.NewEntry()
searchBar.SetPlaceHolder("Search quotes")
filteredQuotes := quotes
quoteList := widget.NewList(
func() int {
// return the number of quotes
return len(filteredQuotes.Quotes)
},
func() fyne.CanvasObject {
// return a quote item template
authorLabel := widget.NewLabel("")
authorLabel.Wrapping = fyne.TextWrapWord
// quoteLabel := widget.NewLabel("")
// quoteLabel.Wrapping = fyne.TextTruncate
quoteButton := widget.NewButton("", func() {})
quoteButton.ExtendBaseWidget(quoteButton)
linkLabel := widget.NewHyperlink("", nil)
linkLabel.Wrapping = fyne.TextWrapWord
return container.New(layout.NewGridLayoutWithColumns(3), authorLabel, quoteButton, linkLabel)
},
func(i widget.ListItemID, c fyne.CanvasObject) {
// update the content of a quote item
grid := c.(*fyne.Container)
grid.Objects[0].(*widget.Label).SetText(filteredQuotes.Quotes[i].Author)
quoteButton := grid.Objects[1].(*widget.Button)
quoteButton.SetText(getFirst50Chars(filteredQuotes.Quotes[i].Quote))
quoteButton.SetIcon(theme.MenuExpandIcon())
quoteButton.OnTapped = func() {
dialog := createQuotePopOut(w, filteredQuotes.Quotes[i])
dialog.Show()
}
grid.Objects[2].(*widget.Hyperlink).SetText(filteredQuotes.Quotes[i].Link)
linkLabel := grid.Objects[2].(*widget.Hyperlink)
linkLabel.SetText(filteredQuotes.Quotes[i].Citation)
err := linkLabel.SetURLFromString(filteredQuotes.Quotes[i].Link)
if err != nil {
logrus.WithError(err).Error("unable to set link")
}
},
)
updateList := func() {
filteredQuotes = searchQuotes(quotes.Quotes, searchBar.Text)
quoteList.Refresh()
}
searchBar.OnChanged = func(s string) {
// filter the quotes based on the search term
updateList()
}
archiveContainer := container.NewBorder(searchBar, nil, nil, nil, quoteList)
return archiveContainer
}
func searchQuotes(quotes []models.Quote, searchTerm string) *models.QuoteList {
if searchTerm == "" {
return &models.QuoteList{
Quotes: quotes,
}
}
var filteredQuotes []models.Quote
for _, quote := range quotes {
if strings.Contains(strings.ToLower(quote.Quote), strings.ToLower(searchTerm)) {
filteredQuotes = append(filteredQuotes, quote)
}
}
return &models.QuoteList{
Quotes: filteredQuotes,
}
}
func createQuotePopOut(w fyne.Window, quote models.Quote) fyne.CanvasObject {
var modal *widget.PopUp
quoteHyperlink, err := url.Parse(quote.Link)
if err != nil {
logrus.WithError(err).Error("unable to parse quote link")
}
authorLabel := widget.NewLabel(quote.Author)
quoteLabel := widget.NewLabel(quote.Quote)
quoteLabel.Wrapping = fyne.TextWrapWord
linkLabel := widget.NewHyperlink(quote.Citation, quoteHyperlink)
hideModal := widget.NewButton("Close", func() {
if modal != nil {
modal.Hide()
}
})
// tags
tagCount := len(quote.Tags)
tagsTable := widget.NewTable(
func() (int, int) {
return tagCount, 1 // Number of rows is equal to the number of tags, only 1 column is needed.
},
func() fyne.CanvasObject {
return widget.NewLabel("") // Create a label for each cell in the table.
},
func(tableCell widget.TableCellID, c fyne.CanvasObject) {
tagLabel := c.(*widget.Label)
tagLabel.SetText(quote.Tags[tableCell.Row])
},
)
tableScroll := container.NewScroll(tagsTable)
tableScroll.SetMinSize(fyne.NewSize(100, 100))
// Set a minimum size for the quote label and wrap it with a ScrollContainer
scroll := container.NewScroll(quoteLabel)
scroll.SetMinSize(fyne.NewSize(400, 200))
content := container.NewVBox(
authorLabel,
scroll,
tableScroll,
linkLabel,
hideModal,
)
modal = widget.NewModalPopUp(content, w.Canvas())
modal.Show()
return modal
}
func getFirst50Chars(s string) string {
if len(s) > 25 {
return s[:25]
}
return s
}