Skip to content

Instantly share code, notes, and snippets.

@z3bi
Created April 1, 2020 21:23
Show Gist options
  • Save z3bi/7651f59bdad966820a8b2d307e5596f9 to your computer and use it in GitHub Desktop.
Save z3bi/7651f59bdad966820a8b2d307e5596f9 to your computer and use it in GitHub Desktop.
Adhan in SwitUI
//
// 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()
}
}
@kamilproo
Copy link

but how I can use user location for coordinations?

@z3bi
Copy link
Author

z3bi commented Apr 7, 2020

but how I can use user location for coordinations?

@kamilproo thats really outside the scope of this example, but there are other online resources that could demonstrate that like https://adrianhall.github.io/swift/2019/11/05/swiftui-location/

@kamilproo
Copy link

yes I done it) Thanks, but I have one more question) your func public func

currentPrayer(at time: Date = Date()) -> Prayer? {
if isha.timeIntervalSince(time) <= 0 {
return .isha
} else if maghrib.timeIntervalSince(time) <= 0 {
return .maghrib
} else if asr.timeIntervalSince(time) <= 0 {
return .asr
} else if dhuhr.timeIntervalSince(time) <= 0 {
return .dhuhr
} else if sunrise.timeIntervalSince(time) <= 0 {
return .sunrise
} else if fajr.timeIntervalSince(time) <= 0 {
return .fajr
}

    return nil
}

how it to use in SwiftUI, I need the program to indicate the current prayer on the main screen and not all five) so that there is a variable variable that displays the prayer in time, if you understand what I mean)

@kamilproo
Copy link

only one name pray display view of current time)

@kamilproo
Copy link

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)

@kamilproo
Copy link

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
       }

}

Copy link

ghost commented Sep 2, 2020

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