-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomeView.swift
294 lines (262 loc) · 11.8 KB
/
HomeView.swift
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
import SwiftUI
struct HomeView: View {
@StateObject private var locationManager = LocationManager()
@StateObject private var prayerData = PrayerData.shared
@StateObject private var sunnahViewModel = SunnahViewModel()
@StateObject private var quranViewModel = QuranViewModel()
@State private var showSettings = false
@State private var showQiblaView = false
@State private var showQuranReader = false
@State private var showSunnahView = false
@State private var isRefreshing = false
var body: some View {
ZStack {
// Maintain background color during refresh
Color(red: 245/255, green: 245/255, blue: 245/255).ignoresSafeArea()
ScrollView {
ZStack(alignment: .top) {
// This ensures the background color extends to the top during pull
Color(red: 220/255, green: 78/255, blue: 65/255)
.frame(height: 200)
.offset(y: -200)
// Custom refresh control that maintains color
RefreshControl(isRefreshing: $isRefreshing, onRefresh: refreshData)
.background(Color(red: 220/255, green: 78/255, blue: 65/255))
VStack(spacing: 24) {
// Prayer Times Section with Header
PrayerTimesHeaderView(showSettings: $showSettings)
.padding(.bottom, -20) // Overlap with the next section
// Qibla Card View with animation
QiblaCardView(showQiblaView: $showQiblaView)
.padding(.horizontal)
.transition(.scale)
.animation(.spring(response: 0.5, dampingFraction: 0.7), value: isRefreshing)
// Hadith Card View with navigation
Button(action: {
showSunnahView = true
}) {
HadithCardView(viewModel: sunnahViewModel)
.padding(.horizontal)
}
.buttonStyle(PlainButtonStyle())
.transition(.slide)
.animation(.easeInOut(duration: 0.3).delay(0.1), value: isRefreshing)
// Quran Verse Section
QuranVerseCardView(viewModel: quranViewModel, showQuranReader: $showQuranReader)
.padding(.horizontal)
.transition(.slide)
.animation(.easeInOut(duration: 0.3).delay(0.2), value: isRefreshing)
// Community Section
CommunityCardView()
.padding(.horizontal)
.padding(.bottom)
.transition(.slide)
.animation(.easeInOut(duration: 0.3).delay(0.3), value: isRefreshing)
}
}
}
.background(Color(red: 245/255, green: 245/255, blue: 245/255).ignoresSafeArea())
.edgesIgnoringSafeArea(.top) // Allow content to extend under status bar
}
.sheet(isPresented: $showSettings) {
SettingsView()
}
.fullScreenCover(isPresented: $showQiblaView) {
QiblaCompassView()
.environmentObject(locationManager)
}
.fullScreenCover(isPresented: $showQuranReader) {
QuranView(isPresented: $showQuranReader)
}
.fullScreenCover(isPresented: $showSunnahView) {
SunnahView()
.environmentObject(sunnahViewModel)
}
.onAppear {
if let location = locationManager.location {
prayerData.fetchPrayerTimes(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
}
sunnahViewModel.fetchRandomHadith()
quranViewModel.fetchInitialAyah()
}
}
func refreshData() {
// Simulate a network request
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
if let location = locationManager.location {
prayerData.fetchPrayerTimes(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
}
sunnahViewModel.fetchRandomHadith()
quranViewModel.fetchInitialAyah()
isRefreshing = false
}
}
}
// Improved RefreshControl that maintains background color
struct RefreshControl: View {
@Binding var isRefreshing: Bool
let onRefresh: () -> Void
var body: some View {
GeometryReader { geometry in
if geometry.frame(in: .global).minY > 50 {
VStack {
Spacer()
ProgressView()
.progressViewStyle(CircularProgressViewStyle(tint: Color.white.opacity(0.7)))
.scaleEffect(1.5)
Spacer()
}
.frame(width: geometry.size.width, height: 50)
.onAppear {
if !isRefreshing {
isRefreshing = true
onRefresh()
}
}
}
}
.frame(height: isRefreshing ? 50 : 0)
}
}
struct PrayerTimesHeaderView: View {
@Binding var showSettings: Bool
@ObservedObject var prayerData = PrayerData.shared
private var hijriMonthName: String {
return HijriCalendarUtils.getMonthName(from: prayerData.hijriDate)
}
private var currentDay: Int {
let calendar = Calendar.current
return calendar.component(.day, from: Date())
}
private var currentPrayerIndex: Int {
let now = Date()
let calendar = Calendar.current
let currentHour = Double(calendar.component(.hour, from: now))
let currentMinute = Double(calendar.component(.minute, from: now))
let currentTimeDouble = currentHour + currentMinute / 60
let times = prayerData.prayers.compactMap { prayerData.timings[$0] }
for (index, timeString) in times.enumerated() {
if let prayerHour = PrayerData.timeToHours(timeString), prayerHour > currentTimeDouble {
return index
}
}
return 0
}
var body: some View {
VStack(spacing: 0) {
// Top header with month and settings
VStack(spacing: 16) {
HStack {
VStack(alignment: .leading) {
Text(hijriMonthName)
.font(.title)
.fontWeight(.bold)
.foregroundColor(.white)
HStack(spacing: 20) {
ForEach(Calendar.current.weekdaySymbols.prefix(7), id: \.self) { day in
Text(day.prefix(3))
.font(.caption)
.foregroundColor(.white.opacity(0.8))
}
}
}
Spacer()
Button(action: { showSettings.toggle() }) {
Image(systemName: "person.circle.fill")
.resizable()
.frame(width: 36, height: 36)
.foregroundColor(.white)
}
}
// Calendar day selection
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 12) {
ForEach(1...30, id: \.self) { day in
VStack {
Text("\(day)")
.font(.headline)
.foregroundColor(day == currentDay ? .white : .white.opacity(0.7))
}
.frame(width: 40, height: 40)
.background(day == currentDay ? Circle().fill(Color.white.opacity(0.3)) : nil)
}
}
.padding(.vertical, 8)
}
}
.padding(.horizontal)
.padding(.top, 60) // Extra padding for status bar
.padding(.bottom, 20)
.background(Color(red: 220/255, green: 78/255, blue: 65/255))
// Prayer times section
VStack(spacing: 16) {
// All prayer times in a grid
LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())], spacing: 12) {
ForEach(prayerData.prayers.filter { $0 != "Midnight" }, id: \.self) { prayer in
let time = prayerData.timings[prayer] ?? "--:--"
let isNext = prayer == prayerData.prayers[currentPrayerIndex]
HStack {
VStack(alignment: .leading, spacing: 4) {
Text(prayer)
.font(.headline)
.fontWeight(isNext ? .bold : .regular)
.foregroundColor(isNext ? Color(red: 220/255, green: 78/255, blue: 65/255) : .black)
Text(time)
.font(.subheadline)
.foregroundColor(.gray)
}
Spacer()
if isNext {
Text("Next")
.font(.caption)
.padding(.horizontal, 12)
.padding(.vertical, 4)
.background(Color(red: 220/255, green: 78/255, blue: 65/255))
.foregroundColor(.white)
.cornerRadius(8)
}
}
.padding()
.background(
RoundedRectangle(cornerRadius: 16)
.fill(Color.white)
.shadow(color: Color.black.opacity(0.1), radius: 8, x: 0, y: 4)
)
}
}
.padding(.top, 16)
.padding(.horizontal, 8)
.padding(.bottom, 16)
}
.padding(.top, 20)
.padding(.horizontal)
.background(Color(red: 245/255, green: 245/255, blue: 245/255))
.cornerRadius(30, corners: [.topLeft, .topRight])
.offset(y: -30)
}
}
}
// Extension for rounded corners
extension View {
func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View {
clipShape(RoundedCorner(radius: radius, corners: corners))
}
}
struct RoundedCorner: Shape {
var radius: CGFloat = .infinity
var corners: UIRectCorner = .allCorners
func path(in rect: CGRect) -> Path {
let path = UIBezierPath(
roundedRect: rect,
byRoundingCorners: corners,
cornerRadii: CGSize(width: radius, height: radius)
)
return Path(path.cgPath)
}
}
// Preview provider
struct HomeView_Previews: PreviewProvider {
static var previews: some View {
HomeView()
}
}