Thursday, July 16, 2015

REST

import Foundation

struct Currency {
    var gbp: Double
    var eur: Double
    var jpy: Double
    var brl: Double
   
    init(currencyDictionary: NSDictionary){
        let currentRates: NSDictionary = currencyDictionary["rates"] as! NSDictionary
   
       
        gbp = currentRates["GBP"] as! Double
        eur = currentRates["EUR"] as! Double
        jpy = currentRates["JPY"] as! Double
        brl = currentRates["BRL"] as! Double


    }
}


//
//  ViewController.swift
//  CurrrencyConverter
//
//  Created by ME on 6/29/15.
//  Copyright (c) 2015 The iOS Code. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
   
    @IBOutlet weak var userInputTextField: UITextField!
   
    var finalResult: NSString!
   
    @IBAction func convertButton(sender: AnyObject) {
       
        disableKeyboard()
        getCurrentCurrencyData()
       
    }
   
    @IBOutlet weak var resultTextView: UITextView!
   
   
    override func viewDidLoad() {
        super.viewDidLoad()
       
        var noteText: String = "This application can convert any USD dollar amount to GBP, EUR, JPY and BRL currency! /n/n Please enter a dollar amount."
       
        self.resultTextView.text = noteText
       
        displayKeyboard()
       
       
    }
   
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
   
    func getCurrentCurrencyData() -> Void{
        let currencyURL = NSURL(string: "http://api.fixer.io/latest?base=USD")
       
       
        let sharedSession = NSURLSession.sharedSession()
        let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(currencyURL!, completionHandler: { (location: NSURL!, response:NSURLResponse!, error: NSError!) -> Void in
           
           
            if(error == nil){
                let dataObject = NSData(contentsOfURL: location)
                let currencyDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject!, options: nil, error: nil) as! NSDictionary
               
//                println(currencyDictionary)
               
               
                let rates = Currency(currencyDictionary: currencyDictionary)
               
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                   
                    var userInputStringValue: String = self.userInputTextField.text
                    var userInputDoubleValue : Double = NSString(string: userInputStringValue).doubleValue
                   
//                    if(userInputDoubleValue = nil){
//                       
//                    }
                   
                    if(userInputDoubleValue > 0){
                       
                        var gbpCurrentValue = rates.gbp * userInputDoubleValue
                        var eurCurrentValue = rates.eur * userInputDoubleValue
                        var jpyCurrentValue = rates.jpy * userInputDoubleValue
                        var brlCurrentValue = rates.brl * userInputDoubleValue
                       
                        var resultString = "UK Pounds - (GBP): " + gbpCurrentValue.description + "\n" +
                            "EU Euro - (Euro): " + eurCurrentValue.description + "\n" +
                            "Japan Yen - (JPY): " + jpyCurrentValue.description + "\n" +
                            "Brazil Reais - (BRL): " + brlCurrentValue.description as NSString
                       
                       
//                        println(resultString)
                       
                        self.resultTextView.text = resultString as String
                    }
                       
                    else{
                        self.resultTextView.text = "Please enter a valid dollar amount which is greater then 0"
                    }
                   
                    //                println(rates.gbp)
                    //                println(rates.eur)
                    //                println(rates.jpy)
                    //                println(rates.brl)
                    //                println(userINputDoubleValue)
                   
                   
                })
               
               
            }
           
            else{
               
                let networkIssueController = UIAlertController(title: "Error", message: "Unable to load data. Connectivity error!", preferredStyle: .Alert)
               
                let okButton = UIAlertAction(title: "OK", style: .Default, handler: nil)
                networkIssueController.addAction(okButton)
               
               
               
//                let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
//                networkIssueController.addAction(cancelButton)
               
                self.presentViewController(networkIssueController, animated: true, completion: { () -> Void in
                    //
                })
               
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    self.resultTextView.text = "Unable to load data due to connectivity error! Please press the CONVERT button again get the conversion rates!"
                })
               
                println(error)
            }
           
        })
        downloadTask.resume()
    }
   
    func displayKeyboard(){
        self.userInputTextField .becomeFirstResponder()
    }
   
    func disableKeyboard(){
        self.userInputTextField .resignFirstResponder()
    }
   
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        self.view .endEditing(true)
    }
   


   
}

No comments:

Post a Comment