2019-11-03 01:25:55 +01:00
|
|
|
//
|
|
|
|
// TodayViewController.swift
|
|
|
|
// TodayExtension
|
|
|
|
//
|
|
|
|
// Created by Marcos Rodriguez on 11/2/19.
|
|
|
|
// Copyright © 2019 Facebook. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import NotificationCenter
|
|
|
|
|
|
|
|
class TodayViewController: UIViewController, NCWidgetProviding {
|
|
|
|
|
|
|
|
|
|
|
|
@IBOutlet weak var currencyLabel: UILabel!
|
|
|
|
@IBOutlet weak var lastUpdatedDate: UILabel!
|
|
|
|
@IBOutlet weak var priceLabel: UILabel!
|
|
|
|
|
|
|
|
@IBOutlet weak var lastPriceArrowImage: UIImageView!
|
|
|
|
@IBOutlet weak var lastPrice: UILabel!
|
|
|
|
@IBOutlet weak var lastPriceFromLabel: UILabel!
|
|
|
|
private var lastPriceNumber: NSNumber?
|
2020-09-29 06:03:35 +02:00
|
|
|
|
2019-11-03 01:25:55 +01:00
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2020-09-29 06:03:35 +02:00
|
|
|
|
2019-11-03 01:25:55 +01:00
|
|
|
setLastPriceOutletsHidden(isHidden: true)
|
|
|
|
if let lastStoredTodayStore = TodayData.getPriceRateAndLastUpdate() {
|
|
|
|
processRateAndLastUpdate(todayStore: lastStoredTodayStore)
|
|
|
|
} else {
|
|
|
|
setLastPriceOutletsHidden(isHidden: true)
|
|
|
|
}
|
2020-09-29 06:03:35 +02:00
|
|
|
|
|
|
|
if #available(iOSApplicationExtension 13.0, *) {
|
|
|
|
} else{
|
|
|
|
self.lastPriceArrowImage.removeFromSuperview()
|
|
|
|
}
|
2019-11-03 01:25:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func setLastPriceOutletsHidden(isHidden: Bool) {
|
|
|
|
lastPrice.isHidden = isHidden
|
|
|
|
lastPriceFromLabel.isHidden = isHidden
|
2020-09-29 06:03:35 +02:00
|
|
|
lastPriceArrowImage?.isHidden = isHidden
|
2019-11-03 01:25:55 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 14:11:28 +01:00
|
|
|
override func viewWillAppear(_ animated: Bool)
|
|
|
|
{
|
|
|
|
var currentSize: CGSize = self.preferredContentSize
|
|
|
|
currentSize.height = 200.0
|
|
|
|
self.preferredContentSize = currentSize
|
|
|
|
}
|
|
|
|
|
2019-11-03 01:25:55 +01:00
|
|
|
func processRateAndLastUpdate(todayStore: TodayDataStore) {
|
|
|
|
guard let rateString = todayStore.formattedRate, let dateFormatted = todayStore.formattedDate else { return }
|
|
|
|
|
|
|
|
priceLabel.text = rateString
|
|
|
|
lastUpdatedDate.text = dateFormatted
|
|
|
|
}
|
|
|
|
|
|
|
|
func processStoredRateAndLastUpdate(todayStore: TodayDataStore) {
|
|
|
|
guard let lastPriceNumber = todayStore.formattedRate else { return }
|
|
|
|
|
|
|
|
lastPrice.text = lastPriceNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
func processCachedStoredRateAndLastUpdate(new: TodayDataStore, cached: TodayDataStore) {
|
|
|
|
guard let newPriceDoubleValue = new.rateDoubleValue, let cachedPriceNumber = cached.formattedRate, let cachedPriceNumberDoubleValue = cached.rateDoubleValue else { return }
|
|
|
|
|
|
|
|
lastPrice.text = cachedPriceNumber
|
|
|
|
|
|
|
|
|
|
|
|
if newPriceDoubleValue > cachedPriceNumberDoubleValue {
|
2020-03-20 00:39:10 +01:00
|
|
|
if #available(iOSApplicationExtension 13.0, *) {
|
|
|
|
self.setLastPriceOutletsHidden(isHidden: false)
|
|
|
|
self.lastPriceArrowImage.image = UIImage(systemName: "arrow.up")
|
|
|
|
} else {
|
|
|
|
self.setLastPriceOutletsHidden(isHidden: true)
|
|
|
|
self.lastPriceFromLabel.text = "up from"
|
|
|
|
lastPriceFromLabel.isHidden = false
|
|
|
|
// Fallback on earlier versions
|
|
|
|
}
|
2019-11-03 01:25:55 +01:00
|
|
|
} else {
|
2020-03-20 00:39:10 +01:00
|
|
|
if #available(iOSApplicationExtension 13.0, *) {
|
|
|
|
self.setLastPriceOutletsHidden(isHidden: false)
|
|
|
|
self.lastPriceArrowImage.image = UIImage(systemName: "arrow.down")
|
|
|
|
} else {
|
|
|
|
self.setLastPriceOutletsHidden(isHidden: true)
|
|
|
|
lastPriceFromLabel.isHidden = false
|
|
|
|
self.lastPriceFromLabel.text = "down from"
|
|
|
|
// Fallback on earlier versions
|
|
|
|
}
|
2019-11-03 01:25:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
|
|
|
|
// Perform any setup necessary in order to update the view.
|
|
|
|
|
|
|
|
// If an error is encountered, use NCUpdateResult.Failed
|
|
|
|
// If there's no update required, use NCUpdateResult.NoData
|
|
|
|
// If there's an update, use NCUpdateResult.NewData
|
2020-11-02 14:11:28 +01:00
|
|
|
let userPreferredCurrency = TodayAPI.getUserPreferredCurrency();
|
2019-11-03 01:25:55 +01:00
|
|
|
self.currencyLabel.text = userPreferredCurrency
|
2020-11-02 14:11:28 +01:00
|
|
|
TodayAPI.fetchPrice(currency: userPreferredCurrency, completion: { (result, error) in
|
2019-11-03 01:25:55 +01:00
|
|
|
DispatchQueue.main.async { [unowned self] in
|
|
|
|
guard let result = result else {
|
2020-03-21 01:26:28 +01:00
|
|
|
self.lastUpdatedDate.text = error?.localizedDescription
|
2019-11-03 01:25:55 +01:00
|
|
|
completionHandler(.failed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let bpi = result["bpi"] as? Dictionary<String, Any>, let preferredCurrency = bpi[userPreferredCurrency] as? Dictionary<String, Any>, let rateString = preferredCurrency["rate"] as? String,
|
|
|
|
let time = result["time"] as? Dictionary<String, Any>, let lastUpdatedString = time["updatedISO"] as? String
|
|
|
|
else {
|
2020-04-12 05:03:51 +02:00
|
|
|
self.lastUpdatedDate.text = "Obtained unexpected information."
|
|
|
|
completionHandler(.failed)
|
2019-11-03 01:25:55 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let latestRateDataStore = TodayDataStore(rate: rateString, lastUpdate: lastUpdatedString)
|
|
|
|
|
2020-11-02 14:11:28 +01:00
|
|
|
if let lastStoredTodayStore = TodayData.getPriceRateAndLastUpdate(), lastStoredTodayStore.lastUpdate == latestRateDataStore.lastUpdate, rateString == lastStoredTodayStore.rate, TodayAPI.getLastSelectedCurrency() == userPreferredCurrency {
|
2019-11-03 01:25:55 +01:00
|
|
|
if let cached = TodayData.getCachedPriceRateAndLastUpdate() {
|
|
|
|
self.processCachedStoredRateAndLastUpdate(new: lastStoredTodayStore, cached: cached)
|
|
|
|
} else {
|
|
|
|
self.setLastPriceOutletsHidden(isHidden: true)
|
|
|
|
}
|
|
|
|
completionHandler(.noData)
|
|
|
|
} else {
|
|
|
|
self.processRateAndLastUpdate(todayStore: latestRateDataStore)
|
|
|
|
let priceRiceAndLastUpdate = TodayData.getPriceRateAndLastUpdate()
|
|
|
|
|
|
|
|
if let rate = priceRiceAndLastUpdate?.rate, let lastUpdate = priceRiceAndLastUpdate?.lastUpdate {
|
|
|
|
TodayData.saveCachePriceRateAndLastUpdate(rate: rate, lastUpdate: lastUpdate);
|
|
|
|
}
|
|
|
|
|
2020-11-02 14:11:28 +01:00
|
|
|
if let latestRateDataStore = latestRateDataStore.rateDoubleValue, let lastStoredPriceNumber = priceRiceAndLastUpdate?.rateDoubleValue, TodayAPI.getLastSelectedCurrency() == userPreferredCurrency {
|
2019-11-03 01:25:55 +01:00
|
|
|
|
|
|
|
if latestRateDataStore > lastStoredPriceNumber {
|
2020-03-20 00:39:10 +01:00
|
|
|
if #available(iOSApplicationExtension 13.0, *) {
|
|
|
|
self.lastPriceArrowImage.image = UIImage(systemName: "arrow.up")
|
|
|
|
} else {
|
|
|
|
self.lastPriceFromLabel.isHidden = false
|
|
|
|
self.lastPriceFromLabel.text = "up from"
|
|
|
|
}
|
2019-11-03 01:25:55 +01:00
|
|
|
self.setLastPriceOutletsHidden(isHidden: false)
|
|
|
|
} else {
|
2020-03-20 00:39:10 +01:00
|
|
|
if #available(iOSApplicationExtension 13.0, *) {
|
|
|
|
self.lastPriceArrowImage.image = UIImage(systemName: "arrow.down")
|
|
|
|
} else {
|
|
|
|
// Fallback on earlier versions
|
|
|
|
self.lastPriceFromLabel.isHidden = false
|
|
|
|
self.lastPriceFromLabel.text = "down from"
|
|
|
|
}
|
2019-11-03 01:25:55 +01:00
|
|
|
self.setLastPriceOutletsHidden(isHidden: false)
|
|
|
|
}
|
|
|
|
self.lastPrice.text = priceRiceAndLastUpdate?.formattedRate
|
|
|
|
} else {
|
|
|
|
self.setLastPriceOutletsHidden(isHidden: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
TodayData.savePriceRateAndLastUpdate(rate: latestRateDataStore.rate, lastUpdate: latestRateDataStore.lastUpdate)
|
2020-11-02 14:11:28 +01:00
|
|
|
TodayAPI.saveNewSelectedCurrency()
|
2019-11-03 01:25:55 +01:00
|
|
|
completionHandler(.newData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|