Skip to content

1Caxz/Weathersama

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Weathersama

Creator Travis GitHub license Code Size Pod Version Platform Download Total
Weathersama is library for access weather data on openweathermap.org. You can find clone apple weather app in WeatersamaDemo project.

This is simple library with class model inside. You can make code short and faster. This library integrated with google geocode. So, its simple to find location and send that to get weather data.

Add to Podfile

Add Weathersama library to your Podfile and install

pod 'Weathersama', '~> 1.1.3'

Install Demo Project

The demo project is a clone of apple weather app. The demo project have weather backgrounds moves like apple weather app. But, that is videos, not animations. Let's contribute to change them with animations.

1. Download the project
2. Open WeathersamaDemo.xcworkspace
2. No need to change appId or googleKey
3. Library already uploaded on CocoaPods repository with version 1.1.2
3. Just change Bundle indentifier & Run

Import Library

You need to import library before use the library in your class.

import Weathersama

Weathersama with Delegete

Add WeathersamaDelegete to your class. You can get data response from openweathermap.org with delegete. Actually, this is the optional technique to handle some conditional on your programs but for other technique, you can use Weathersama with Completion Method for more simple programs.

class ViewController: UIViewController, WeathersamaDelegete {
    fileprivate var weatherSama: Weathersama!
    override func viewDidLoad() {
        super.viewDidLoad()
        weatherSama = Weathersama(appId: "YOUR_APP_ID", temperature: TEMPERATURE_TYPES.Celcius, language: LANGUAGES.French, dataResponse: DATA_RESPONSE.JSON)
        weatherSama.delegete = self

        // You can set nil if use Weathersama library with delegete
        weatherSama.weatherByCityId(cityId: 12345, requestType: .dailyForecast, completion: nil)
    }
}

And add the method delegete for response

public func didStartRequestResponder() {
    print("Request start!")
}

public func didEndRequestResponder(result: Bool, description: String, requestType: REQUEST_TYPE) {
    if result {
        if requestType == .dailyForecast {
            print("response json : \(description)")
        } else if requestType == .Forecast {
            print("response json : \(description)")
        } else if requestType == .Weather {
            print("response json : \(description)")
        }
    } else {
        print("response error: \(description)")
    }
}

Weathersama with Completion Method

You can get the data response from openweathermap.org with class model and without delegete. This is very easy to use data with class model.

class ViewController: UIViewController {
    fileprivate var weatherSama: Weathersama!
    fileprivate var dailyForecastModel: DailyForecastModel!
    override func viewDidLoad() {
        super.viewDidLoad()
        weatherSama = Weathersama(appId: "YOUR_APP_ID", temperature: TEMPERATURE_TYPES.Celcius, language: LANGUAGES.French, dataResponse: DATA_RESPONSE.JSON)

        weatherSama.weatherByCityId(cityId: 12345, requestType: .dailyForecast) { (isSuccess, description, classModel) -> () in
            if isSuccess {
                // you can user response json or class model
                print("response json : \(description)")
                self.dailyForecastModel = classModel as! DailyForecastModel
            } else {
                print("response error : \(description)")
            }
        }
    }
}

Weathersama for Google

Weathersama library integrated with google geocode. So, you can be looking for the location more accurate.

fileprivate var weathersamaForGoogle: WeathersamaForGoogle!
fileprivate var data = [String]()
fileprivate var placeId = [String]()
override func viewDidLoad() {
    super.viewDidLoad()

    weathersamaForGoogle = WeathersamaForGoogle(apiKey: "YOUR_GOOGLE_KEY")
    weathersamaForGoogle.lookingForLocationBy(input: searchBar.text!) { (isSuccess, classModel) in
        if isSuccess {
            for prediction in classModel.predictions {
                self.data.append(prediction.description)
                self.placeId.append(prediction.placeId)
            }
        } else {
            print("Cannot get data fromm google")
        }
    }
}

List of Request Types

public enum REQUEST_TYPE: String {
    case Weather = "weather"
    case Forecast = "forecast"
    case dailyForecast = "forecast/daily"
}

List of Class Model

Every request type have different class model. So, you need to casting the classModel from response such as request type.
Class model for request type Weather

let weatherModel: WeatherModel = WeatherModel()

Class model for request type Forecast

let forecastModel: ForecastModel = ForecastModel()

Class model for request type Daily Forecast

let dailyForecastModel: DailyForecastModel = DailyForecastModel()

List of Temperature Types

public enum TEMPERATURE_TYPES: String {
    case Kelvin = ""
    case Celcius = "&units=metric"
    case Fahrenheit = "&units=imperial"
}

List of Languages Supported

public enum LANGUAGES: String {
    case Ukrainian = "&lang=uk"
    case Italian = "&lang=it"
    case German = "&lang=de"
    case Portuguese = "&lang=pt"
    case English = "&lang=en"
    case Romanian = "&lang=ro"
    case Russian = "&lang=ru"
    case ChineseSimplified = "&lang=zh_cn"
    case Spanish = "&lang=es"
    case French = "&lang=fr"
    case Bulgarian = "&lang=bg"
    case Polish = "&lang=pl"
    case Turkish = "&lang=tr"
    case Catalan = "&lang=ca"
    case Croatian = "&lang=hr"
    case Finnish = "&lang=fi"
    case Dutch = "&lang=nl"
    case Swedish = "&lang=sv"
    case ChineseTraditional = "zh_tw"
}

List of Response Types

public enum DATA_RESPONSE: String {
    case JSON = ""
    case XML = "&mode=xml"
}

MIT License

Copyright (c) 2017 Saiful Irham Wicaksana

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.