Created
February 2, 2015 05:08
-
-
Save cooper/68c3621b8c99c5184500 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// InfoCell.swift | |
// FeedReader | |
// | |
// Created by Mitchell Cooper on 2/1/15. | |
// Copyright (c) 2015 Mitchell Cooper. All rights reserved. | |
// | |
import UIKit | |
class InfoCell: UITableViewCell { | |
@IBOutlet var unreadBox: UIView! | |
@IBOutlet var totalBox: UIView! | |
@IBOutlet var savedBox: UIView! | |
// info for the buttons, as returned by delegate methods | |
typealias ButtonInfo = (title: String, articles: [Article]) | |
var totalInfo: ButtonInfo? | |
var unreadInfo: ButtonInfo? | |
var savedInfo: ButtonInfo? | |
// upon setting the delegate, get the info we need | |
private weak var _dataSource: InfoCellDataSource? | |
var dataSource: InfoCellDataSource? { | |
get { | |
return _dataSource | |
} | |
set { | |
_dataSource = newValue | |
totalInfo = _dataSource?.infoCellTotalArticles (self) | |
unreadInfo = _dataSource?.infoCellUnreadArticles(self) | |
savedInfo = _dataSource?.infoCellSavedArticles (self) | |
updateArticleCounts() | |
} | |
} | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
// add gesture recognizers | |
for box in [unreadBox, totalBox, savedBox] { | |
let tap = UITapGestureRecognizer(target: self, action: "handleTap:") | |
box.addGestureRecognizer(tap) | |
} | |
} | |
// button box view map to button info | |
// this is a lazy var because it will only be computed after | |
// the info properties are present | |
lazy var boxToInfoMap: [UIView: ButtonInfo!] = { | |
return [ | |
self.unreadBox: self.unreadInfo, | |
self.totalBox: self.totalInfo, | |
self.savedBox: self.savedInfo | |
] | |
}() | |
// callback for all gesture recognizers | |
func handleTap(sender: UITapGestureRecognizer) { | |
let box = sender.view! | |
updateBackground(box) | |
pushCollection(boxToInfoMap[box]!) | |
} | |
// make the background the cell selected background color | |
// after half a second, return to transparent background | |
func updateBackground(box: UIView) { | |
box.backgroundColor = Colors.cellSelectedBackgroundColor | |
after(0.5) { box.backgroundColor = nil } | |
} | |
// update the article counts in the buttons | |
func updateArticleCounts() { | |
for (box, info) in boxToInfoMap { | |
(box.subviews.first as? UILabel)?.text = "\(info.articles.count)" | |
} | |
} | |
// push a collection of articles with a generic article collection | |
func pushCollection(info: ButtonInfo) { | |
let collection = GenericArticleCollection(title: info.title, articles: info.articles) | |
let articleVC = ArticleListVC(collection: collection) | |
rss.navigationController.pushViewController(articleVC, animated: true) | |
} | |
} | |
// protocol for article collection data source | |
// defined as a class protocol so that data source can be weak | |
protocol InfoCellDataSource: class { | |
func infoCellTotalArticles (cell: InfoCell) -> InfoCell.ButtonInfo | |
func infoCellUnreadArticles(cell: InfoCell) -> InfoCell.ButtonInfo | |
func infoCellSavedArticles (cell: InfoCell) -> InfoCell.ButtonInfo | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment