Skip to content

Instantly share code, notes, and snippets.

@jpomykala
Created September 17, 2017 10:15
Show Gist options
  • Save jpomykala/6c7c8bcf0f984adb862550338a455e11 to your computer and use it in GitHub Desktop.
Save jpomykala/6c7c8bcf0f984adb862550338a455e11 to your computer and use it in GitHub Desktop.
class GoldenHour {
var locationName: String?
var start: Date?
var end: Date?
var latitude: CLLocationDegrees?
var longitude: CLLocationDegrees?
}
class GoldenHourViewModel {
private var goldenHour : GoldenHour
private var delegate: HomeScreenDelegate
init(_ delegate: HomeScreenDelegate) {
self.goldenHour = GoldenHour()
self.delegate = delegate
}
var goldenHourStart: String {
guard let startTime = goldenHour.start else {
return "--:--"
}
guard let endTime = goldenHour.end else {
return "--:--"
}
let fallsBetween = (startTime...endTime).contains(Date())
if(fallsBetween){
return "Now"
}
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
return dateFormatter.string(from: startTime)
}
func calculateGoldenHour(currentLocation: CLLocation) {
let latitude = currentLocation.coordinate.latitude;
let longitude = currentLocation.coordinate.longitude;
let url = "https://api.sunrise-sunset.org/json?lat=\(latitude)&lng=\(longitude)&formatted=0"
Alamofire.request(url).responseJSON { (response) in
guard let json = response.data else {return}
let data = JSON(data: json)
guard let sunSetTimeInString = data["results"]["sunset"].string else {return}
guard let sunSetTime = sunSetTimeInString.dateFromISO8601 else {return}
let goldenHourStart = Calendar.current.date(byAdding: .hour, value: -1, to: sunSetTime)
self.goldenHour.start = goldenHourStart
self.goldenHour.end = sunSetTime
self.delegate.updateView()
}
}
//and more
}
protocol HomeScreenDelegate {
func updateView()
}
class ViewController: UIViewController, HomeScreenDelegate {
@IBOutlet var temperatureLabel: UILabel!
@IBOutlet var notificationsButton: UIButton!
@IBOutlet var locationName: UILabel!
@IBOutlet var coordinatesLabel: UILabel!
@IBOutlet var goldenHourLabel: UILabel!
@IBOutlet var weatherLabel: UILabel!
let locationManager = CLLocationManager()
var goldenHourViewModel : GoldenHourViewModel!
var weatherViewModel: WeatherForecastViewModel!
override func viewDidLoad() {
super.viewDidLoad()
goldenHourViewModel = GoldenHourViewModel(self)
weatherViewModel = WeatherForecastViewModel(self)
locationManager.requestWhenInUseAuthorization()
onLocateMe()
}
@IBAction func onLocateMe() {
let canUseLocation = CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse;
if(canUseLocation) {
guard let currentLocation = locationManager.location else {return};
goldenHourViewModel.calculateGoldenHour(currentLocation: currentLocation)
weatherViewModel.fetchForecast(currentLocation)
}
}
internal func updateView(){
locationName.text = goldenHourViewModel.locationName
coordinatesLabel.text = goldenHourViewModel.coordinates
goldenHourLabel.text = goldenHourViewModel.goldenHourStart
temperatureLabel.text = weatherViewModel.temperature
}
//and some more minor methods
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment