Untitled

mail@pastecode.io avatar
unknown
plain_text
20 days ago
3.1 kB
6
Indexable
Never
//
//  SunView.swift
//  MoonPhase
//
//  Created by student on 27/05/2024.
//

import SwiftUI
import CoreLocation
import SwiftAA

struct SunView: View {
    
    @State var hemisphere = Hemisphere.Northern
    @State var height:Double = 245.0
    @State var width:Double = 200.0
    @State var control = 245.0
    @State var sunRise = Date()
    @State var sunSet = Date()
    @State var transit = Date()
    @State var dayLength = String()
    
    @Binding var location : CLLocationCoordinate2D
    init(location: Binding<CLLocationCoordinate2D>) {
        _location = location
    }
    
    @State var date: Date = Date();
    
    func calculateSun(){
        let jd=JulianDay(date) //Konwersja na JulianDay
        let sun = Sun(julianDay:jd)
        var temp = sun.riseTransitSetTimes(for: GeographicCoordinates(CLLocation(latitude: location.latitude, longitude: location.longitude)))
        sunSet = temp.setTime!.date
        sunRise = temp.riseTime!.date
        transit = temp.transitTime!.date
        let delta = sunSet.timeIntervalSince(sunRise)
        let formatter = DateComponentsFormatter()
        formatter.unitsStyle = .abbreviated
        formatter.allowedUnits = [.hour, .minute, .second]
        dayLength = formatter.string(from: delta) ?? "00:00.00"
    }
    
    var body: some View {
        VStack {
            HStack {
                DatePicker(selection: $date, displayedComponents: .date) {
                    Text("Select date")
                }
                Spacer()
                Button("Today") {
                    date = Date()
                }
            }
            Spacer()
            GeometryReader { geometry in
                ZStack {
                    Image("sun").resizable().scaledToFit().onAppear() {
                        let frame = geometry.frame(in: .local)
                        height = frame.height
                        width = frame.width
                        control = width * 0.62
                    }
                }
            }.rotationEffect(.degrees(hemisphere.rawValue))
            Spacer()
            HStack {
                Text("Sunrise")
                Spacer()
                Text(sunRise.timeOnly)
            }.onAppear() {
                calculateSun()
            }
            HStack {
                Text("Transit")
                Spacer()
                Text(transit.timeOnly)
            }.onChange(of: date) {oldDate, newDate in
                calculateSun()
            }
            HStack {
                Text("Sunset")
                Spacer()
                Text(sunSet.timeOnly)
            }.onChange(of: date) {oldDate, newDate in
                calculateSun()
            }
            HStack {
                Text("Day length")
                Spacer()
                Text(dayLength)
            }
        }.onChange(of: location) {oldLocation, newLocation in
            calculateSun()
            if location.latitude < 0 {
                hemisphere = .Southern
            } else {
                hemisphere = .Northern
            }
        }
    }
}

//#Preview {
//    SunView()
//}
Leave a Comment