batoulapps / adhan-swift

High precision Islamic prayer time library for Swift
MIT License
182 stars 42 forks source link

Changed: How to fix current prayer time? #75

Closed Babyyoda777 closed 2 years ago

Babyyoda777 commented 2 years ago

Is there a method to control jamat times for each prayer. I am trying to replicate the prayer timing from here: http://harrowmosque.org.uk

z3bi commented 2 years ago

@Babyyoda777 this library does not handle jamat/iqamah times as that is really going to be different per masjid and not something that can be calculated.

Babyyoda777 commented 2 years ago

Okay understood, jazakallah. Then what about notifications. I cannot figure out how to make it work. Here is my implementation:` let cal = Calendar(identifier: Calendar.Identifier.gregorian) let coordinates = Coordinates(latitude: 51.5806, longitude: 0.3420) let size = UIScreen.main.bounds

@State  var sunrize : String
@State  var fajr : String
@State var dhuhr : String
@State var asr : String
@State var maghrib : String
@State var isha : String
@State var currentPrayer : String

@StateObject var feedViewModel = FeedViewModel()

func calculateTime()->Void{
    let date = cal.dateComponents([.year, .month, .day], from: Date())

    var params = CalculationMethod.moonsightingCommittee.params
    params.madhab = .hanafi
    params.adjustments.fajr = 5/2
    params.adjustments.sunrise = 2
    params.adjustments.dhuhr = 3
    params.adjustments.maghrib = -1
    params.adjustments.asr = 3
    params.adjustments.isha = 3
    if let prayers = PrayerTimes(coordinates: coordinates, date: date, calculationParameters: params) {
        let formatter = DateFormatter()
        formatter.timeStyle = .short
        formatter.timeZone = TimeZone(identifier: "Europe/London")!

        self.sunrize = formatter.string(from: prayers.sunrise)
        self.fajr = formatter.string(from: prayers.fajr)
        self.dhuhr = formatter.string(from: prayers.dhuhr)
        self.asr = formatter.string(from: prayers.asr)
        self.maghrib = formatter.string(from: prayers.maghrib)
        self.isha = formatter.string(from: prayers.isha)

    }
}`

This is what I have when trying to call a notification but it just says "prayer not found"

let center = UNUserNotificationCenter.current() let content = UNMutableNotificationContent() content.title = "prayer time" content.body = "Time to pray" content.categoryIdentifier = "alarm" content.sound = UNNotificationSound.default let trigger = UNCalendarNotificationTrigger(dateMatching: prayers.fajr, repeats: false) let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger) center.add(request)

z3bi commented 2 years ago

that's really outside of the scope of this library as well, it's not even clear in this code sample how the notification code gets access to the prayers struct.

Babyyoda777 commented 2 years ago

I want to also use the current prayer time feature but I am at loss at how to implement. This is what im trying so far: image I get the following error

z3bi commented 2 years ago

prayers.currentPrayer returns a Prayer value, not a date. You would need to instead use prayers.time(for: prayers.currentPrayer) to get the Date associated for that Prayer.

Babyyoda777 commented 2 years ago

Is this what you mean? sorry im still a bit new to image swift.

z3bi commented 2 years ago

Yes but you will need to unwrap the optional prayer since it can be nil sometimes.

if let currentPrayer = prayers.currentPrayer {
    self.current = formatter.string(from: prayers.time(for: currentPrayer))
} else {
    // if there's no current prayer then it means its between midnight and fajr.
    // It's up to you to decide how you want to handle this situation
}
Babyyoda777 commented 2 years ago

Hmm, it still gives me a circular reference error :/ as well as Trailing closure passed to parameter of type 'Date' that does not accept a closure

Babyyoda777 commented 2 years ago

Screenshot 2022-08-23 at 16 03 05

z3bi commented 2 years ago

Sorry, currentPrayer is a function. Try this.

if let currentPrayer = prayers.currentPrayer() {
    self.current = formatter.string(from: prayers.time(for: currentPrayer))
} else {
    // if there's no current prayer then it means its between midnight and fajr.
    // It's up to you to decide how you want to handle this situation
}
Babyyoda777 commented 2 years ago

THANK YOU SO MUCH, YOU HAVE MADE MY DAY. Jazakallah

Babyyoda777 commented 2 years ago

Sorry for reopening but I this doesn't seem to work no matter what I try :/ Screenshot 2022-08-24 at 15 12 31