Skip to content

Instantly share code, notes, and snippets.

@pnicholls
Created August 3, 2014 00:25
Show Gist options
  • Save pnicholls/42a0769c53e0feb74f6d to your computer and use it in GitHub Desktop.
Save pnicholls/42a0769c53e0feb74f6d to your computer and use it in GitHub Desktop.
//
// FriendsViewController.swift
// Nudge
//
// Created by Peter Nicholls on 2/08/2014.
// Copyright (c) 2014 Peter Nicholls. All rights reserved.
//
import Foundation
import UIKit
class FriendsViewController: UIViewController, NDUserCellDelegate, UITableViewDataSource, UITableViewDelegate {
enum TableViewSection: Int {
case Loading
case NotFriends
case Friends
case Count
}
var user: NDUser
var friends: [NDUser]
var fetchCompleted: Bool
lazy var tableView: UITableView = {
let tableView = UITableView(frame: self.view.frame)
tableView.delegate = self
tableView.dataSource = self
tableView.separatorInset = UIEdgeInsetsMake(0, 60, 0, 0)
tableView.tableFooterView = UIView(frame: CGRectZero)
tableView.registerClass(NDLoadingCell.self, forCellReuseIdentifier: "loadingCell")
tableView.registerClass(NDNoFriendsTableViewCell.self, forCellReuseIdentifier: "notFriendsCell")
tableView.registerClass(NDUserCell.self, forCellReuseIdentifier: "userCell")
tableView.reloadData()
return tableView
}()
init(user: NDUser) {
self.user = user
self.friends = []
self.fetchCompleted = false
super.init(nibName: nil, bundle: nil)
}
// MARK: View lifecycle methods
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Friends"
self.view.backgroundColor = UIColor.whiteColor()
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .Plain, target: nil, action: nil)
self.view.addSubview(tableView)
fetchFriends()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tableView.reloadData()
}
func fetchFriends() {
user.getUserFriendsWithSuccess({ (objects: [AnyObject]!) in
self.friends = objects as [NDUser]
self.fetchCompleted = true
self.tableView.reloadData()
}, failure: { (NSURLSessionDataTask, error: NSError!) in
self.tableView.reloadData()
UIAlertView(title: "Error", message: error.localizedDescription, delegate: nil, cancelButtonTitle: "OK")
})
}
func loadingCell() -> NDLoadingCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("loadingCell") as NDLoadingCell
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
return cell
}
func notFriendsCell() -> NDNoFriendsTableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("notFriendsCell") as NDNoFriendsTableViewCell
return cell
}
func userCell(indexPath: NSIndexPath) -> NDUserCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("userCell", forIndexPath: indexPath) as NDUserCell
cell.user = friends[indexPath.row]
cell.delegate = self
return cell
}
// MARK: NDUserCellDelegate methods
func userCell(cell: NDUserCell!, relationshipButtonTapped sender: AnyObject!, action: NDRelationshipAction) {
cell.showLoadingButton()
let relationship = cell.user.relationship
relationship.action = action
relationship.updateWithSuccess({
cell.refresh()
}, failure: { (NSURLSessionDataTask, error: NSError!) in
cell.refresh()
UIAlertView(title: "Error", message: error.localizedDescription, delegate: nil, cancelButtonTitle: "OK")
})
}
// MARK: UITableViewDelegate methods
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
let user = friends[indexPath.row]
let userViewController = NDUserViewController(user: user, viewMode: .Public, targetResponse: nil)
navigationController.pushViewController(userViewController, animated: true)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
switch indexPath.section {
case TableViewSection.Loading.toRaw(), TableViewSection.NotFriends.toRaw():
return CGRectGetHeight(self.view.frame)
case TableViewSection.Friends.toRaw():
return 50.0
default:
return 0
}
}
// MARK: UITableViewDataSource methods
func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
return TableViewSection.Count.toRaw()
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
switch section {
case TableViewSection.Loading.toRaw():
return !fetchCompleted ? 1 : 0
case TableViewSection.NotFriends.toRaw():
return fetchCompleted && friends.count == 0 ? 1 : 0
case TableViewSection.Friends.toRaw():
return friends.count
default:
return 0
}
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
switch indexPath.section {
case TableViewSection.Loading.toRaw():
return loadingCell()
case TableViewSection.NotFriends.toRaw():
return notFriendsCell()
case TableViewSection.Friends.toRaw():
return userCell(indexPath)
default:
return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment