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() | |
} | |
} |
why not working, countdown asr, I don't understand) help me)))
struct TimerToNext: View {
@binding var times: PrayerTimes?
@State private var currentDate: Date = Date()
@State private var toDate: Date = Date()
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
var body: some View {
VStack {
Text("\(currentDate)")
.onReceive(timer) { input in
self.currentDate = input
}
Text("\(countDownString(for: times!.asr ))")
}
}
func countDownString(for date: Date) -> String {
let calendar = Calendar(identifier: .gregorian)
let components = calendar
.dateComponents([.hour, .minute, .second],
from: currentDate,
to: toDate)
return String(format: "%02d:%02d:%02d",
components.hour ?? 00,
components.minute ?? 00,
components.second ?? 00)
}
var timeFormat: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm:ss"
return formatter
}
func timeString(date: Date) -> String {
let time = timeFormat.string(from: date)
return time
}
}
only one name pray display view of current time)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello countdown not working swiftui, I don't understand, I write many times, not work, maybe you can help
let prayerTimes = PrayerTimes(coordinates: coordinates, date: date, calculationParameters: params)
let current = prayerTimes.currentPrayer()
let next = prayerTimes.nextPrayer()
let countdown = prayerTimes.time(for: next)