0

I make my iOS app islamic prayer, so I need to View countdown time next prayer.

this is code from Adhan project:

so I have 5 prayer for every day, and I need countdown time between every prayer.

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")
    }
}

}

Kamilpro
  • 45
  • 8
  • In short, You have the date-time for the next prayer. And you need to show a count-down. Right? – Frankenstein May 14 '20 at 14:55
  • Yes I have all time for all 5 prayer per day, and I need count down timer between prayers – Kamilpro May 14 '20 at 15:15
  • Do you mean count down timer until the next prayer or time difference between prayer, I'm not getting you? How can there be a countdown timer between prayers? Can you count down between 6:00 - 10:00? – Frankenstein May 14 '20 at 15:19
  • I mean count down timer until the next prayer – Kamilpro May 14 '20 at 16:03
  • https://stackoverflow.com/a/61891905/12478830 – MMG May 21 '20 at 14:06
  • Hey, I have a question for you. How are you selecting the calculation method for prayer? because almost every country uses different calculation methods. BTW, I am also working on a prayer time app. – Anas Ikhlas Oct 28 '22 at 12:40

2 Answers2

0

I use something like this before in viewdidload in releasedate prayer time and add target for @objc func update time func will automatically will updated if time changed every second ( timeInterval ) in scheduledTimer

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let releaseDateString = "7:07:43" // <- prayer time
    let releaseDateFormatter = DateFormatter()
    releaseDateFormatter.dateFormat = "HH:mm:ss"
    releaseDate = releaseDateFormatter.date(from: releaseDateString)! as NSDate

    countdownTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
}

@objc func updateTime() {

    let currentDate = Date()
    let calendar = Calendar.current

    let diffDateComponents = calendar.dateComponents([.hour, .minute, .second], from: currentDate, to: releaseDate! as Date)

    DispatchQueue.global().sync
    {
        hours.text = "\(diffDateComponents.hour ?? 0)"
        min.text = "\(diffDateComponents.minute ?? 0)"
        sec.text = "\(diffDateComponents.second ?? 0)"

    }

you can use SwiftDate also it's awesome library

Nawafmu
  • 1
  • 3
0

You could use this function:

func timeUntilNextPrayer(_ nextPrayer: Date) {
    Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in
        let difference = Calendar.current.dateComponents([.hour, .minute, .second], from: Date(), to: nextPrayer)
        print(difference)
        if nextPrayer == Date() {
            timer.invalidate()
        }
    }
}

Call this function by passing the next prayer time as parameter. Example:

timeUntilNextPrayer(Date().addingTimeInterval(50))
Frankenstein
  • 15,732
  • 4
  • 22
  • 47
  • thx I will try, but if I will can't do it, can give me your mail I ll send your my project, maybe you watch it – Kamilpro May 14 '20 at 17:01
  • not working I don't understand why) so this is link https://gist.github.com/z3bi/7651f59bdad966820a8b2d307e5596f9. maybe someone help me do count down time for every prayers per day) – Kamilpro May 15 '20 at 05:46