<commit_before>//
//  AddCityViewController.swift
//  OpenWeatherForecast
//
//  Created by <NAME> on 8/28/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import UIKit
import CoreLocation

class AddCityViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, CLLocationManagerDelegate
{
    @IBOutlet var cityNameTextfield: UITextField!
    @IBOutlet var addCityBtn: UIButton!
    @IBOutlet var cityListTableView: UITableView!
    
    var cities: [City] = [];
    
    let pendingOperations = PendingOperations();
    
    var locationManager: CLLocationManager!;
    var locationStatus : NSString = "Not Started";
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    
        self.navigationItem.title = "Open Weather Map";
        
        cityNameTextfield.layer.cornerRadius = 8.0;
        cityNameTextfield.layer.masksToBounds = true;
        cityNameTextfield.layer.borderColor = UIColor.blackColor().CGColor;
        cityNameTextfield.layer.borderWidth = 1.0;
        
        self.registerForNotifications();
        
        //Get location access
        locationManager = CLLocationManager();
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.requestWhenInUseAuthorization();
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func registerForNotifications()
    {
        let notificationCenter = NSNotificationCenter.defaultCenter();
        let mainQueue = NSOperationQueue.mainQueue();
        
        var weatherInfoDownloadCompletionObserver = notificationCenter.addObserverForName(NOTIFICATION_WEATHER_INFO_DOWNLOAD_COMPLETE, object: nil, queue: mainQueue) { (notification) -> Void in
            let cityInfo:Dictionary<String, String!> = notification.userInfo as! Dictionary<String, String!>;
            let requestCityName = cityInfo[REQUEST_KEY_CITY_NAME];
            let cityName = cityInfo[CITY_INFO_KEY_CITY_NAME];
            println("Weather info download complete notification received for city \(cityName)");
            
            //Update city name as per response
            self.updateCityNameAsPerResponse(requestCityName!, actualCityName: cityName!);
            
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.cityListTableView.reloadData();
            })
        }
        
        var weatherInfoDownloadFailureObserver = notificationCenter.addObserverForName(NOTIFICATION_WEATHER_INFO_DOWNLOAD_FAILED, object: nil, queue: mainQueue) { (notification) -> Void in
            let cityInfo:Dictionary<String, String!> = notification.userInfo as! Dictionary<String, String!>;
            let cityName = cityInfo[CITY_INFO_KEY_CITY_NAME];
            println("Weather info download failed notification received for city \(cityName)");
            
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.cityListTableView.reloadData();
            })
        }
    }
    
    func deregisterNotifications()
    {
        NSNotificationCenter.defaultCenter().removeObserver(self);
    }
    
    func updateCityNameAsPerResponse(requestedCityName: String, actualCityName: String)
    {
        //Update only if the names differ
        if(requestedCityName != actualCityName)
        {
            for city in cities
            {
                if(city.name == requestedCityName)
                {
                    city.name = actualCityName;<commit_msg>Showing a toast with error message when weather info API fails<commit_after>//
//  AddCityViewController.swift
//  OpenWeatherForecast
//
//  Created by <NAME> on 8/28/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import UIKit
import CoreLocation

class AddCityViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, CLLocationManagerDelegate
{
    @IBOutlet var cityNameTextfield: UITextField!
    @IBOutlet var addCityBtn: UIButton!
    @IBOutlet var cityListTableView: UITableView!
    
    var cities: [City] = [];
    
    let pendingOperations = PendingOperations();
    
    var locationManager: CLLocationManager!;
    var locationStatus : NSString = "Not Started";
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    
        self.navigationItem.title = "Open Weather Map";
        
        cityNameTextfield.layer.cornerRadius = 8.0;
        cityNameTextfield.layer.masksToBounds = true;
        cityNameTextfield.layer.borderColor = UIColor.blackColor().CGColor;
        cityNameTextfield.layer.borderWidth = 1.0;
        
        self.registerForNotifications();
        
        //Get location access
        locationManager = CLLocationManager();
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.requestWhenInUseAuthorization();
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func registerForNotifications()
    {
        let notificationCenter = NSNotificationCenter.defaultCenter();
        let mainQueue = NSOperationQueue.mainQueue();
        
        var weatherInfoDownloadCompletionObserver = notificationCenter.addObserverForName(NOTIFICATION_WEATHER_INFO_DOWNLOAD_COMPLETE, object: nil, queue: mainQueue) { (notification) -> Void in
            let cityInfo:Dictionary<String, String!> = notification.userInfo as! Dictionary<String, String!>;
            let requestCityName = cityInfo[REQUEST_KEY_CITY_NAME];
            let cityName = cityInfo[CITY_INFO_KEY_CITY_NAME];
            println("Weather info download complete notification received for city \(cityName)");
            
            //Update city name as per response
            self.updateCityNameAsPerResponse(requestCityName!, actualCityName: cityName!);
            
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.cityListTableView.reloadData();
            })
        }
        
        var weatherInfoDownloadFailureObserver = notificationCenter.addObserverForName(NOTIFICATION_WEATHER_INFO_DOWNLOAD_FAILED, object: nil, queue: mainQueue) { (notification) -> Void in
            let cityInfo:Dictionary<String, String!> = notification.userInfo as! Dictionary<String, String!>;
            let cityName = cityInfo[CITY_INFO_KEY_CITY_NAME];
            println("Weather info download failed notification received for city \(cityName)");
            
            let errorMessage = cityInfo[RESPONSE_KEY_MESSAGE];
            
            JLToast.makeText(errorMessage!, duration: JLToastDelay.ShortDelay).show()
            
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.cityListTableView.reloadData();
            })
        }
    }
    
    func deregisterNotifications()
    {
        NSNotificationCenter.defaultCenter().removeObserver(self);
    }
    
    func updateCityNameAsPerResponse(requestedCityName: String, actualCityName: String)
    {
        //Update only if the names differ
        if(requestedCityName != actualCityName)
        {
            for city in cities
            {
                if(city.name == requestedCityName)
                {
                    city.name = actualCityName;
{'fraction_non_alphanumeric': 0.0810740354535975, 'fraction_numerical': 0.003388946819603754, 'mean_word_length': 2.6625298329355607, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4906711', 'n_tokens_mistral': 2025, 'n_tokens_neox': 1958, 'n_words': 441}