Let's Build Activity++ - Part 3

Episode #225 | 10 minutes | published on June 23, 2016
Subscribers Only
This week we take our ring views and use them to create a collection view of rings, one for each day in an entire year.

Episode Links

Collection View Cell

class ActivityCell : UICollectionViewCell {

    @IBOutlet weak var summaryRing: SummaryRingView!
    @IBOutlet weak var activityRing: ActivityRingView!
    @IBOutlet weak var exerciseRing: ExerciseRingView!
    @IBOutlet weak var standingRing: StandingRingView!

    var rings: [RingView] {
        return [summaryRing, activityRing, exerciseRing, standingRing]
    }

    func configureForLog(log: ActivityLog) {
        rings.forEach { $0.configureForLog(log) }
    }
}

View Controller

private let reuseIdentifier = "ActivityCell"

class ActivityCollectionViewController: UICollectionViewController {

    var today: NSDate?
    let calendar = NSCalendar.currentCalendar()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        today = NSDate()
    }

    // MARK: UICollectionViewDataSource

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 365
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ActivityCell

        let date = calendar.dateByAddingUnit(.Day, value: -indexPath.row, toDate: today!, options: [])!
        let caloriesBurned = Int(arc4random() % 800)
        let calorieGoal = 500

        let exerciseMin = Int(arc4random() % 45)
        let exerciseGoal = 30

        let standHours = Int(arc4random() % 16)
        let standGoal = 12

        let log = ActivityLog(date: date,
                              caloriesBurned: caloriesBurned,
                              activityGoal: calorieGoal,
                              minutesOfExercise: exerciseMin,
                              exerciseGoal: exerciseGoal,
                              standHours: standHours,
                              standGoal: standGoal)

        cell.configureForLog(log)

        return cell
    }
}
blog comments powered by Disqus