-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
299 lines (268 loc) · 9.4 KB
/
main.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
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
package main
import (
"fmt"
"gioui.org/io/system"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"image"
"image/color"
"log"
"time"
"gioui.org/app"
"gioui.org/font/gofont"
"gioui.org/layout"
"gioui.org/unit"
"gioui.org/widget/material"
"github.com/hablullah/go-hijri"
"github.com/mnadev/adhango/pkg/calc"
"github.com/mnadev/adhango/pkg/data"
"github.com/mnadev/adhango/pkg/util"
)
type PrayerTimes struct {
Fajr, Sunrise, Dhuhr, Asr, Maghrib, Isha string
TimeUntilNextPrayer time.Duration
}
type DateHolder struct {
Day, Year int
Month string
}
var monthToName = map[int]string{
1: "Muḥarram",
2: "Safar",
3: "Rabi Al-Awwal",
4: "Rabi Al-Thani",
5: "Jumada Al-Ula",
6: "Jumada Al-Thaniyah",
7: "Rajab",
8: "Shaban",
9: "Ramadan",
10: "Shawwal",
11: "Du Al-Qadah",
12: "Du Al-Hijjah",
}
func getCurrentDate() DateHolder {
date, _ := hijri.CreateUmmAlQuraDate(time.Now())
return DateHolder{
Day: int(date.Day),
Month: monthToName[int(date.Month)],
Year: int(date.Year),
}
}
func main() {
go func() {
width := unit.Dp(300)
height := unit.Dp(410)
options := []app.Option{
app.Size(width, height),
app.Title("Athan App"),
app.Decorated(false),
app.MinSize(width, height),
app.MaxSize(width, height),
}
w := app.NewWindow(options...)
if err := loop(w); err != nil {
log.Fatal(err)
}
}()
app.Main()
}
func loop(w *app.Window) error {
th := material.NewTheme(gofont.Collection())
th.TextSize = unit.Sp(14)
th.Fg = color.NRGBA{R: 0x44, G: 0x44, B: 0x44, A: 0xFF}
var ops op.Ops
prayerTimes := getPrayerTimes()
hijriDate := getCurrentDate()
// Add a ticker to update the countdown every second
ticker := time.NewTicker(1 * time.Second)
for {
select {
case e := <-w.Events():
switch e := e.(type) {
case system.FrameEvent:
gtx := layout.NewContext(&ops, e)
layoutUI(gtx, th, prayerTimes, hijriDate)
e.Frame(gtx.Ops)
case system.DestroyEvent:
return e.Err
}
case <-ticker.C:
// Refresh the UI when the ticker ticks
prayerTimes = getPrayerTimes()
w.Invalidate()
}
}
}
func getPrayerTimes() PrayerTimes {
// Replace these coordinates with your location's coordinates
latitude := 29.3117
longitude := 47.4818
// Set the method and parameters
date := data.NewDateComponents(time.Now().In(time.UTC))
params := calc.GetMethodParameters(calc.KUWAIT)
params.Madhab = calc.SHAFI_HANBALI_MALIKI
coords, err := util.NewCoordinates(latitude, longitude)
if err != nil {
fmt.Printf("Error %+v", err)
panic(err)
}
prayerTimes, err := calc.NewPrayerTimes(coords, date, params)
if err != nil {
fmt.Printf("got error %+v", err)
panic(err)
}
err = prayerTimes.SetTimeZone("Asia/Kuwait")
if err != nil {
fmt.Printf("got error %+v", err)
panic(err)
}
prayer := prayerTimes.NextPrayerNow()
var nextPrayer time.Time
if prayer == calc.NO_PRAYER {
temp, _ := calc.NewPrayerTimes(coords, data.NewDateComponents(time.Now().In(time.UTC).AddDate(0, 0, 1)), params)
err := temp.SetTimeZone("Asia/Kuwait")
if err != nil {
fmt.Printf("got error %+v", err)
panic(err)
}
nextPrayer = temp.Fajr
} else {
nextPrayer = prayerTimes.TimeForPrayer(prayer)
}
fmt.Printf("Next prayer is %v\n", prayer)
fmt.Printf("Next prayer at %s\n", nextPrayer.Format("03:04 PM"))
durationUntilNextPrayer := nextPrayer.Sub(time.Now()).Round(time.Second)
return PrayerTimes{
Fajr: prayerTimes.Fajr.Format("03:04 PM"),
Sunrise: prayerTimes.Sunrise.Format("03:04 PM"),
Dhuhr: prayerTimes.Dhuhr.Format("03:04 PM"),
Asr: prayerTimes.Asr.Format("03:04 PM"),
Maghrib: prayerTimes.Maghrib.Format("03:04 PM"),
Isha: prayerTimes.Isha.Format("03:04 PM"),
TimeUntilNextPrayer: durationUntilNextPrayer,
}
}
func layoutUI(gtx layout.Context, th *material.Theme, prayerTimes PrayerTimes, date DateHolder) layout.Dimensions {
inset := layout.Inset{
Top: unit.Dp(10),
Bottom: unit.Dp(10),
Left: unit.Dp(10),
Right: unit.Dp(10),
}
th.TextSize = unit.Sp(18)
//th.Fg = color.NRGBA{R: 0x00, G: 0x00, B: 0x80, A: 0xFF} // New text color
// Set the background color for the entire window
bgColor := color.NRGBA{R: 0x42, G: 0xA5, B: 0xF5, A: 0xFF}
paint.FillShape(gtx.Ops, bgColor, clip.Rect{Max: gtx.Constraints.Max}.Op())
return layout.Center.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layout.Flex{Axis: layout.Vertical, Alignment: layout.Middle}.Layout(gtx, layout.Rigid(func(gtx layout.Context) layout.Dimensions {
// First section
return inset.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layoutDateSection(gtx, th, date)
})
}), layout.Rigid(func(gtx layout.Context) layout.Dimensions {
// Separator
return layoutSeparator(gtx, th)
}), layout.Rigid(func(gtx layout.Context) layout.Dimensions {
// Second section
return inset.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layoutPrayerTimesSection(gtx, th, prayerTimes)
})
}), layout.Rigid(func(gtx layout.Context) layout.Dimensions {
// Separator
return layoutSeparator(gtx, th)
}), layout.Rigid(func(gtx layout.Context) layout.Dimensions {
// Third section
return inset.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layoutCounterSection(gtx, th, prayerTimes)
})
}))
})
}
func layoutCounterSection(gtx layout.Context, th *material.Theme, prayerTimes PrayerTimes) layout.Dimensions {
return layout.Center.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
th.TextSize = unit.Sp(20)
th.Fg = color.NRGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF}
lbl := material.H6(th, fmt.Sprintf("%02d:%02d:%02d", int(prayerTimes.TimeUntilNextPrayer.Hours()), int(prayerTimes.TimeUntilNextPrayer.Minutes())%60, int(prayerTimes.TimeUntilNextPrayer.Seconds())%60))
return layout.Stack{}.Layout(gtx, layout.Expanded(func(gtx layout.Context) layout.Dimensions {
rr := int(unit.Dp(4))
clip.RRect{
Rect: image.Rectangle{
Max: image.Point{
X: lbl.Layout(gtx).Size.X,
Y: lbl.Layout(gtx).Size.Y,
},
},
SE: rr, SW: rr, NW: rr, NE: rr,
}.Op(gtx.Ops).Push(gtx.Ops)
paint.Fill(gtx.Ops, color.NRGBA{R: 0x42, G: 0xA5, B: 0xF5, A: 0xFF})
return layout.Dimensions{}
}), layout.Stacked(func(gtx layout.Context) layout.Dimensions {
return lbl.Layout(gtx)
}))
})
}
func layoutDateSection(gtx layout.Context, th *material.Theme, date DateHolder) layout.Dimensions {
th.TextSize = unit.Sp(18)
th.Fg = color.NRGBA{R: 0x33, G: 0x33, B: 0x33, A: 0xFF}
return layout.Center.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx, layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return material.H3(th, fmt.Sprintf("%02d", date.Day)).Layout(gtx)
}), layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Inset{Left: unit.Dp(16)}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx, layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return material.H4(th, date.Month).Layout(gtx)
}), layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return material.H5(th, fmt.Sprintf("%04dh", date.Year)).Layout(gtx)
}))
})
}))
})
}
func layoutSeparator(gtx layout.Context, th *material.Theme) layout.Dimensions {
return layout.Inset{Top: unit.Dp(8), Bottom: unit.Dp(8)}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
line := image.Rect(0, 0, gtx.Constraints.Max.X, 1)
borderColor := color.NRGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0x40}
paint.FillShape(gtx.Ops, borderColor, clip.Rect(line).Op())
return layout.Dimensions{
Size: image.Point{X: gtx.Constraints.Max.X, Y: 1},
}
})
}
func layoutPrayerTimesSection(gtx layout.Context, th *material.Theme, prayerTimes PrayerTimes) layout.Dimensions {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx, layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layoutPrayerTimeRow(gtx, th, "Fajr", prayerTimes.Fajr)
}), layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layoutPrayerTimeRow(gtx, th, "Sunrise", prayerTimes.Sunrise)
}), layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layoutPrayerTimeRow(gtx, th, "Dhuhr", prayerTimes.Dhuhr)
}), layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layoutPrayerTimeRow(gtx, th, "Asr", prayerTimes.Asr)
}), layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layoutPrayerTimeRow(gtx, th, "Maghrib", prayerTimes.Maghrib)
}), layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layoutPrayerTimeRow(gtx, th, "Isha", prayerTimes.Isha)
}))
}
func layoutPrayerTimeRow(gtx layout.Context, th *material.Theme, prayerName, prayerTime string) layout.Dimensions {
marginY := unit.Dp(6)
marginX := unit.Dp(0)
inset := layout.Inset{
Top: marginY,
Bottom: marginY,
Left: marginX,
Right: marginX,
}
return inset.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx, layout.Rigid(func(gtx layout.Context) layout.Dimensions {
th.TextSize = unit.Sp(18)
return material.Body1(th, prayerName).Layout(gtx)
}), layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
return layout.E.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
th.TextSize = unit.Sp(18)
return material.Body1(th, prayerTime).Layout(gtx)
})
}))
})
}