Created
April 1, 2020 21:23
-
-
Save z3bi/7651f59bdad966820a8b2d307e5596f9 to your computer and use it in GitHub Desktop.
Adhan in SwitUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// TimeToPray | |
// | |
// Created by Ameir Al-Zoubi on 3/31/20. | |
// Copyright © 2020 ameir. All rights reserved. | |
// | |
import SwiftUI | |
import Adhan | |
private let dateFormatter: DateFormatter = { | |
let dateFormatter = DateFormatter() | |
dateFormatter.dateStyle = .none | |
dateFormatter.timeStyle = .medium | |
return dateFormatter | |
}() | |
struct ContentView: View { | |
@State private var times = prayerTimes() | |
var body: some View { | |
NavigationView { | |
PrayerTimeView(times: $times) | |
.navigationBarTitle(Text("Prayer Times")) | |
} | |
} | |
static func prayerTimes() -> PrayerTimes? { | |
let cal = Calendar(identifier: Calendar.Identifier.gregorian) | |
let date = cal.dateComponents([.year, .month, .day], from: Date()) | |
let coordinates = Coordinates(latitude: 35.78056, longitude: -78.6389) | |
var params = CalculationMethod.moonsightingCommittee.params | |
params.madhab = .hanafi | |
return PrayerTimes(coordinates: coordinates, date: date, calculationParameters: params) | |
} | |
} | |
struct PrayerTimeView: View { | |
@Binding var times: PrayerTimes? | |
let prayers: [Prayer] = [.fajr, .sunrise, .dhuhr, .asr, .maghrib, .isha] | |
var body: some View { | |
List { | |
ForEach(prayers, id: \.self) { prayer in | |
HStack { | |
self.formattedPrayerName(prayer: prayer) | |
self.formattedPrayerTime(prayer: prayer, times: self.times) | |
} | |
} | |
} | |
} | |
func formattedPrayerTime(prayer: Prayer, times: PrayerTimes?) -> some View { | |
guard let time = times?.time(for: prayer) else { | |
return Text("-") | |
} | |
return Text("\(time, formatter: dateFormatter)") | |
} | |
func formattedPrayerName(prayer: Prayer) -> some View { | |
switch prayer { | |
case .fajr: | |
return Text("Fajr") | |
case .sunrise: | |
return Text("Sunrise") | |
case .dhuhr: | |
return Text("Dhuhr") | |
case .asr: | |
return Text("Asr") | |
case .maghrib: | |
return Text("Maghrib") | |
case .isha: | |
return Text("Isha") | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey kamilproo did you find a solution? I am trying to do the same thing. If you could reach me by email in case you did it because I need some help. xmetalz95@gmail.com