batoulapps / adhan-swift

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

Adhan timings depending on location in SwiftUI #68

Closed IsamZareer closed 2 years ago

IsamZareer commented 2 years ago

I want the application to see the location of the user and adjust the timings for that location (like change coordinates depending on location)

here is my code


import SwiftUI
import Adhan

private let dateFormatter: DateFormatter = {
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .none
    dateFormatter.timeStyle = .medium
    return dateFormatter
}()

struct AthanView: View {
    @State private var times = prayerTimes()

    var body: some View {
            VStack {
            PrayerTimeView(times: $times)
                .navigationBarTitle("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: 24.207500, longitude: 55.744720)
        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)
                }
            }
        }
        .listStyle(InsetGroupedListStyle())
    }

    func formattedPrayerTime(prayer: Prayer, times: PrayerTimes?) -> some View {
        guard let time = times?.time(for: prayer) else {
            return Text("-")
        }

        return Text("\(time, formatter: dateFormatter)").foregroundColor(.red)
    }

    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 SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        AthanView()
    }
}
ipkalid commented 2 years ago

you should use CoreLocation to get the user location and then you change the prayer time depending on that location

you can take look at my app i implement an app taking the user location and i used SwiftUI https://github.com/ipkalid/AthanApp

IsamZareer commented 2 years ago

I took a look at your app but I don't understand how to do this I am new to Xcode and swiftui