Skip to content
This repository has been archived by the owner on Jul 17, 2022. It is now read-only.

Commit

Permalink
Refactor viewcontroller
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenichi Saito committed Mar 5, 2016
1 parent c022abc commit 767a83f
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 104 deletions.
43 changes: 25 additions & 18 deletions GoodWeather/Classes/Controller/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit
import Social

class SettingsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
class SettingsViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

Expand All @@ -31,7 +31,14 @@ class SettingsViewController: UIViewController, UITableViewDataSource, UITableVi
// Dispose of any resources that can be recreated.
}

// MARK: - UITableViewDataSource
@IBAction func closeAction(sender: AnyObject) {
self.navigationController?.dismissViewControllerAnimated(true, completion: nil)
}
}

// MARK: - UITableViewDataSource

extension SettingsViewController: UITableViewDataSource {

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellTitle.count
Expand All @@ -47,8 +54,11 @@ class SettingsViewController: UIViewController, UITableViewDataSource, UITableVi
cell.textLabel?.text = cellTitle[indexPath.row]
return cell
}

// MARK: - UITableViewDelegate
}

// MARK: - UITableViewDelegate

extension SettingsViewController: UITableViewDelegate {

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 80
Expand All @@ -63,26 +73,27 @@ class SettingsViewController: UIViewController, UITableViewDataSource, UITableVi
default: return
}
}

// MARK: - Private

}

// MARK: - Private

extension SettingsViewController {
private func units() {
let actionSheet = UIAlertController()

actionSheet.addAction(
UIAlertAction(title: "Celsius", style: .Default, handler: {(action) in
UIAlertAction(title: "Celsius", style: .Default, handler: { action in
print(action.title)
Utility.changeUnitsSetting(true)
})
)
actionSheet.addAction(
UIAlertAction(title: "Fahrenheit", style: .Default, handler: {(action) in
UIAlertAction(title: "Fahrenheit", style: .Default, handler: { action in
print(action.title)
Utility.changeUnitsSetting(false)
})
)
actionSheet.addAction(
UIAlertAction(title: "Cancel", style: .Cancel, handler: {(action) in
UIAlertAction(title: "Cancel", style: .Cancel, handler: { action in
print(action.title)
})
)
Expand All @@ -97,15 +108,15 @@ class SettingsViewController: UIViewController, UITableViewDataSource, UITableVi

for day in days {
actionSheet.addAction(
UIAlertAction(title: "\(day) Days", style: .Default, handler: {(action) in
UIAlertAction(title: "\(day) Days", style: .Default, handler: { action in
print(action.title)
Utility.changeNumberOfDaysSetting(day)
})
)
}

actionSheet.addAction(
UIAlertAction(title: "Cancel", style: .Cancel, handler: {(action) in
UIAlertAction(title: "Cancel", style: .Cancel, handler: { action in
print(action.title)
})
)
Expand All @@ -122,7 +133,7 @@ class SettingsViewController: UIViewController, UITableViewDataSource, UITableVi
}

private func configure() {

tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = .clearColor()
Expand All @@ -142,8 +153,4 @@ class SettingsViewController: UIViewController, UITableViewDataSource, UITableVi
self.view.backgroundColor = .blackColor()
}
}

@IBAction func closeAction(sender: AnyObject) {
self.navigationController?.dismissViewControllerAnimated(true, completion: nil)
}
}
112 changes: 58 additions & 54 deletions GoodWeather/Classes/Controller/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit
import MapKit
import Walhalla

class ViewController: UIViewController, CLLocationManagerDelegate, UITableViewDataSource, UITableViewDelegate {
class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var nameLabel: UILabel!
Expand All @@ -27,7 +27,6 @@ class ViewController: UIViewController, CLLocationManagerDelegate, UITableViewDa

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

configure()
}
Expand All @@ -42,8 +41,27 @@ class ViewController: UIViewController, CLLocationManagerDelegate, UITableViewDa
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

// MARK: - UITableViewDataSource

extension ViewController: UITableViewDataSource {

// MARK: - CLLocationManagerDelegate
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ModelManager.sharedInstance.dailyWeather.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as DailyWeatherTableViewCell
let dailyWeather = ModelManager.sharedInstance.dailyWeather[indexPath.row]
cell.setData(dailyWeather)
return cell
}
}

// MARK: - CLLocationManagerDelegate

extension ViewController: CLLocationManagerDelegate {

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let
Expand All @@ -66,81 +84,57 @@ class ViewController: UIViewController, CLLocationManagerDelegate, UITableViewDa
manager.requestWhenInUseAuthorization()
}
}

// MARK: - UITableViewDataSource

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ModelManager.sharedInstance.dailyWeather.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as DailyWeatherTableViewCell
let dailyWeather = ModelManager.sharedInstance.dailyWeather[indexPath.row]
cell.setData(dailyWeather)
return cell
}

// MARK: - Private

func refreshWeather(lat: Double, lon: Double) {

weak var weakSelf = self
}

// MARK: - Private

extension ViewController {
private func refreshWeather(lat: Double, lon: Double) {

ModelManager.sharedInstance.getDailyWeather(lat, lon: lon, callback: {(error) in
ModelManager.sharedInstance.getDailyWeather(lat, lon: lon, callback: { [weak self] error in
if error == nil {
weakSelf?.tableView.reloadData()
weakSelf?.indicator.stopAnimating()
weakSelf?.refreshControl.endRefreshing()
weakSelf?.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
self?.tableView.reloadData()
self?.indicator.stopAnimating()
self?.refreshControl.endRefreshing()
self?.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
} else {
print(error)

weakSelf?.indicator.stopAnimating()
weakSelf?.refreshControl.endRefreshing()
self?.indicator.stopAnimating()
self?.refreshControl.endRefreshing()

let alert = UIAlertController(title: "Error", message: "Oops! Please try again!!", preferredStyle: .Alert)
let retryAction = UIAlertAction(title: "Try Again!", style: .Default, handler: { action in
weakSelf?.manager.requestLocation()
self?.manager.requestLocation()
})

alert.addAction(retryAction)
weakSelf?.presentViewController(alert, animated: true, completion: nil)
self?.presentViewController(alert, animated: true, completion: nil)

return
}
})

ModelManager.sharedInstance.getWeather(lat, lon: lon, callback: {(error, weather) in
ModelManager.sharedInstance.getWeather(lat, lon: lon, callback: { [weak self] error, weather in
if error == nil {
weakSelf?.nameLabel.text = weather?.name!
weakSelf?.minLabel.text = String(format: "%g°", (weather?.temp_min)!)
weakSelf?.maxLabel.text = String(format: "%g°", (weather?.temp_max)!)
weakSelf?.imageView.image = UIImage(named: (weather?.main)!)?.imageWithRenderingMode(.AlwaysTemplate)
weakSelf?.descriptionLabel.text = weather?.description!
self?.nameLabel.text = weather?.name!
self?.minLabel.text = String(format: "%g°", (Utility.calcKelvin((weather?.temp_min!)!)))
self?.maxLabel.text = String(format: "%g°", (Utility.calcKelvin((weather?.temp_max!)!)))
self?.imageView.image = UIImage(named: (weather?.main)!)?.imageWithRenderingMode(.AlwaysTemplate)
self?.descriptionLabel.text = weather?.description!

Walhalla.performAnimation((weakSelf?.nameLabel)!, duration: 1.0, delay: 0, type: .FadeIn)
Walhalla.performAnimation((weakSelf?.minLabel)!, duration: 1.0, delay: 0, type: .FadeIn)
Walhalla.performAnimation((weakSelf?.maxLabel)!, duration: 1.0, delay: 0, type: .FadeIn)
Walhalla.performAnimation((weakSelf?.imageView)!, duration: 1.0, delay: 0, type: .FadeIn)
Walhalla.performAnimation((weakSelf?.descriptionLabel)!, duration: 1.0, delay: 0, type: .FadeIn)
Walhalla.performAnimation((self?.nameLabel)!, duration: 1.0, delay: 0, type: .FadeIn)
Walhalla.performAnimation((self?.minLabel)!, duration: 1.0, delay: 0, type: .FadeIn)
Walhalla.performAnimation((self?.maxLabel)!, duration: 1.0, delay: 0, type: .FadeIn)
Walhalla.performAnimation((self?.imageView)!, duration: 1.0, delay: 0, type: .FadeIn)
Walhalla.performAnimation((self?.descriptionLabel)!, duration: 1.0, delay: 0, type: .FadeIn)
} else {
print(error)
// show alert
}
})
}

private func update() {

if refreshControl.refreshing {
refreshControl.attributedTitle = NSAttributedString(string: "Loading..")
} else {
indicator.startAnimating()
}

manager.requestLocation()
}

private func configure() {

manager = CLLocationManager()
Expand All @@ -149,7 +143,6 @@ class ViewController: UIViewController, CLLocationManagerDelegate, UITableViewDa
tableViewTopConstrait.constant = UIScreen.mainScreen().bounds.size.height / 2 - 64

tableView.dataSource = self
tableView.delegate = self
tableView.separatorColor = .clearColor()
tableView.register(DailyWeatherTableViewCell.self)

Expand Down Expand Up @@ -181,6 +174,17 @@ class ViewController: UIViewController, CLLocationManagerDelegate, UITableViewDa
backgroundImageView.image = Utility.makeGradient(self.view.frame)
}

func update() {

if refreshControl.refreshing {
refreshControl.attributedTitle = NSAttributedString(string: "Loading..")
} else {
indicator.startAnimating()
}

manager.requestLocation()
}

@IBAction func goSetting(sender: AnyObject) {
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier(settingsViewControllerIdetifier)
vc.modalPresentationStyle = .OverFullScreen
Expand Down
12 changes: 6 additions & 6 deletions GoodWeather/Classes/Model/DailyWeather.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ struct DailyWeather {
weather.icon = icon
weather.humidity = humidity
weather.pressure = pressure
weather.day = Utility.calcKelvin(day)
weather.eve = Utility.calcKelvin(eve)
weather.morn = Utility.calcKelvin(morn)
weather.night = Utility.calcKelvin(night)
weather.min = Utility.calcKelvin(min)
weather.max = Utility.calcKelvin(max)
weather.day = day
weather.eve = eve
weather.morn = morn
weather.night = night
weather.min = min
weather.max = max
weather.main = main
weather.dt = Utility.translateUnixTime(dt)
}
Expand Down
4 changes: 2 additions & 2 deletions GoodWeather/Classes/Model/ModelManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ModelManager {
static let sharedInstance = ModelManager()

func getWeather(lat: Double, lon: Double, callback: ((NSError?, Weather?) -> ())) {
APIClient.sharedInstance.getCurrentWeather(lat, lon: lon, callback: {(error, data) in
APIClient.sharedInstance.getCurrentWeather(lat, lon: lon, callback: { error, data in
if error == nil {
let weather = Weather.parseJSON(data!)
callback(nil, weather)
Expand All @@ -32,7 +32,7 @@ class ModelManager {
func getDailyWeather(lat: Double, lon: Double, callback: ((NSError?) -> ())) {
weak var weakSelf = self

APIClient.sharedInstance.getDailyWeather(lat, lon: lon, callback: {(error, data) in
APIClient.sharedInstance.getDailyWeather(lat, lon: lon, callback: { error, data in
if error == nil {
let dailyWeather = DailyWeather.parseJSON(data!)
weakSelf?.dailyWeather = dailyWeather
Expand Down
6 changes: 3 additions & 3 deletions GoodWeather/Classes/Model/Weather.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ struct Weather {
weather.main = main
weather.description = description
weather.icon = icon
weather.temp = Utility.calcKelvin(temp)
weather.temp = temp
weather.pressure = pressure
weather.humidity = humidity
weather.temp_max = Utility.calcKelvin(temp_max)
weather.temp_min = Utility.calcKelvin(temp_min)
weather.temp_max = temp_max
weather.temp_min = temp_min
weather.name = name
}

Expand Down
16 changes: 8 additions & 8 deletions GoodWeather/Classes/Utility/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class APIClient {
Alamofire.request(.GET, baseCurrentURL, parameters: params)
.responseJSON { response in

// print(response.request)
// print(response.response)
// print(response.data)
// print(response.result.value)
print(response.request)
print(response.response)
print(response.data)
print(response.result.value)

switch response.result {
case .Success:
Expand Down Expand Up @@ -59,10 +59,10 @@ class APIClient {
Alamofire.request(.GET, baseDailyURL, parameters: params)
.responseJSON { response in

// print(response.request)
// print(response.response)
// print(response.data)
// print(response.result.value)
print(response.request)
print(response.response)
print(response.data)
print(response.result.value)

switch response.result {
case .Success:
Expand Down
4 changes: 1 addition & 3 deletions GoodWeather/Classes/Utility/Const.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public let lightPinkColor = Utility.hexColor("#fe80be", alpha: 1).CGColor
public let lightOrangeColor = Utility.hexColor("#fd746c", alpha: 1).CGColor
public let lightYellowColor = Utility.hexColor("#f09819", alpha: 1).CGColor
public let lightPurpleColor = Utility.hexColor("#aa076b", alpha: 1).CGColor
public let lightGrayColor = Utility.hexColor("#232526", alpha: 1).CGColor

public let colors = [
lightBlueColor,
Expand All @@ -33,6 +32,5 @@ public let colors = [
lightPinkColor,
lightOrangeColor,
lightYellowColor,
lightPurpleColor,
lightGrayColor
lightPurpleColor
]
Loading

0 comments on commit 767a83f

Please sign in to comment.