batoulapps / adhan-swift

High precision Islamic prayer time library for Swift
MIT License
185 stars 45 forks source link

i can't get the location for preyer time #57

Closed MAAlsaleh0 closed 3 years ago

MAAlsaleh0 commented 3 years ago

// // ContentView.swift // PrayerTime // // Created by Mohammed Alsaleh on 20/09/1442 AH. //

import SwiftUI import Adhan import CoreLocation

class LocationFecher: NSObject, CLLocationManagerDelegate { let manager = CLLocationManager() var location: CLLocationCoordinate2D? override init() { super.init() manager.delegate = self }

func start() {

    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
    location = locations.first?.coordinate
}

}

struct ContentView: View { @State var Preyers = [ prayer(name: "Fajr", time: "", color: "Fajr"), prayer(name: "Sunrise", time: "", color: "Sunrise"), prayer(name: "Dhuhr", time: "", color: "Dhuhr"), prayer(name: "Asr", time: "", color: "Asr"), prayer(name: "Maghrib", time: "", color: "Maghrib"), prayer(name: "Isha", time: "", color: "Isha")

]

@State var date = ""
@State var crenntSalah = ""
@State var nextSalah = ""
@State var locationmanager = LocationFecher()

@State var cityname = ""

var body: some View {

    ZStack{
        Color("BackgroundColor") .edgesIgnoringSafeArea(.all)
        VStack(spacing: 13.0){
            Text(cityname)
                .font(.system(size: 18))
                .padding()
            Text(crenntSalah)
                .foregroundColor(Color("TimeDD"))
                .fontWeight(.regular)
                .font(.system(size: 30))
                .padding()
            Text(nextSalah)
                .font(.system(size: 16))
            Text(date)
                .font(.system(size: 15))
                .padding()
            ForEach(Preyers, id: \.self){ prayer in
                HStack {
                    Image(systemName: "alarm")
                        .resizable()
                        .frame(width: 25, height: 28)

                    Text(prayer.name)
                        .font(.system(size: 23))
                        .padding(.leading)
                    Spacer()
                    Text(prayer.time)
                        .font(.system(size: 23))
                }.padding()
                .foregroundColor(Color(prayer.color))
            }

        }.foregroundColor(Color("CityTextColor"))
        .padding()

        VStack{
            Spacer()
            HStack(alignment: .bottom){

                Button(action: { print(locationmanager.location?.latitude ?? 0.0 , locationmanager.location?.longitude ?? 0.0) }, label: {
                    Image(systemName: "gearshape.fill")

                        .resizable()
                        .frame(width: 25, height: 28)
                        .foregroundColor(Color("BackgroundColor"))
                        .padding(4.0)
                        .background(Color("CityTextColor"))

                        .clipShape(Rectangle(), style: FillStyle())
                        .cornerRadius(8)
                        .padding(.leading, 28.0)
                })

            Spacer()

            }
            .onAppear(){
                        praytime()
                        locationmanager.start()

            }
        }
    }

}
func praytime()  {
    let cal = Calendar(identifier: Calendar.Identifier.gregorian)
    let date = cal.dateComponents([.year, .month, .day], from: Date())
    let coordinates = Coordinates(latitude: locationmanager.location?.latitude ?? 0.0, longitude: locationmanager.location?.longitude ?? 0.0)
    var params = CalculationMethod.ummAlQura.params

    params.madhab = .shafi
    if let prayers = PrayerTimes(coordinates: coordinates, date: date, calculationParameters: params) {
        let formatter = DateFormatter()
        formatter.timeStyle = .short
        formatter.timeZone = TimeZone.autoupdatingCurrent

        self.Preyers[0].time = formatter.string(from: prayers.fajr)
        self.Preyers[1].time = formatter.string(from: prayers.sunrise)
        self.Preyers[2].time = formatter.string(from: prayers.dhuhr)
        self.Preyers[3].time = formatter.string(from: prayers.asr)
        self.Preyers[4].time = formatter.string(from: prayers.maghrib)
        self.Preyers[5].time = formatter.string(from: prayers.isha)

        self.getdate()
        self.updetelaple(prayerTimes: prayers)
        self.updatelocation()

    }

}
func getdate() {
    let hijriCalendar = Calendar(identifier: .islamicCivil)

    let formatter = DateFormatter()
    formatter.locale = Locale(identifier: "en")
    formatter.calendar = hijriCalendar
    formatter.dateFormat =  "ـــــــــــــــــ  EEEE, MMM dd, yyyy  ـــــــــــــــــ"
    formatter.timeZone = TimeZone.autoupdatingCurrent

    date = "\(formatter.string(from: Date()))"
}
func updetelaple(prayerTimes : PrayerTimes) {

    guard let current = prayerTimes.currentPrayer() else { return }
    crenntSalah = "Time for \(current)"
    guard let next = prayerTimes.nextPrayer() else { return }
    let countdown = prayerTimes.time(for: next)
    let diffTime = countdown.timeIntervalSince(Date())
    let defftimemin = diffTime / 60
    let difftimerem = (defftimemin.truncatingRemainder(dividingBy: 60))
    let defftimehour = (defftimemin / 60)

   let StrSALHa = "\(next) is in \(Int(defftimehour)) hr, \(Int(difftimerem)) min"
   nextSalah = StrSALHa

}
func updatelocation() {
    let location = CLLocation(latitude: locationmanager.location?.latitude ?? 0.0, longitude: locationmanager.location?.longitude ?? 0.0)

    CLGeocoder().reverseGeocodeLocation(location) {
        placemarks, error in
        guard let placemark = placemarks?.first else { return }

        cityname = placemark.country ?? "error"
    }

}

struct ContentView_Previews: PreviewProvider { static var previews: some View { Group { ContentView() .previewDevice(PreviewDevice(rawValue: "iPhone XS")) .previewDisplayName("iPhone XS") } } }

struct prayer: Hashable { var name : String var time : String var color: String }

}

kamilproo commented 3 years ago

your location manager I little wrong) and need use Dispatch get location before canculate prayer time) so check my profile and see my project - PrayerTimes5 this is help you)