Skip to content

Instantly share code, notes, and snippets.

View wingchi's full-sized avatar

S. Wingchi Wong wingchi

  • Affirm
  • Austin, TX
View GitHub Profile
@wingchi
wingchi / ViewController.swift
Last active May 5, 2018 22:40
Using grid layout logic in view controller
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return gridViewModel.cellSize(for: collectionView.bounds, at: indexPath)
}
@wingchi
wingchi / GridViewModel.swift
Created May 5, 2018 22:31
Grid View Model with all item layout logic
import UIKit
class GridViewModel {
static var itemMargin: CGFloat = 8.0
var data: [Int] = (1...10).map { $0 }
func cellSize(for frame: CGRect, at indexPath: IndexPath) -> CGSize {
let height = (frame.height / 5) - 2 * GridViewModel.itemMargin
let fullWidth = frame.width
let halfWidth = (frame.width / 2) - (GridViewModel.itemMargin / 2)
@wingchi
wingchi / GridViewModelTests.swift
Created May 5, 2018 22:13
Unit Tests for Grid Cell sizing 6 - 10
class GridViewModelTests: XCTestCase {
let testFrame = CGRect(x: 0, y: 0, width: 72, height: 130)
let expectedFullWidth: CGFloat = 72 // 72
let expectedHalfWidth = (72 / 2) - (GridViewModel.itemMargin / 2) //32
let expectedHeight = (130 / 5) - 2 * GridViewModel.itemMargin //10
// ... tests one through five
func testSixItemCellSize() {
let expectedCellSizes = [
@wingchi
wingchi / GridViewModel.swift
Created May 5, 2018 22:08
Logic for full width cell sizing
func cellSize(for frame: CGRect, at indexPath: IndexPath) -> CGSize {
let height = (frame.height / 5) - 2 * GridViewModel.itemMargin
let fullWidth = frame.width - 2 * GridViewModel.itemMargin
return CGSize(width: fullWidth, height: height)
}
@wingchi
wingchi / GridViewModelTests.swift
Created May 5, 2018 21:52
Unit Tests for Grid Cell sizing 1-5
class GridViewModelTests: XCTestCase {
let testFrame = CGRect(x: 0, y: 0, width: 72, height: 130)
let expectedFullWidth: CGFloat = 72 // 72
let expectedHeight = (130 / 5) - 2 * GridViewModel.itemMargin //10
func testOneItemCellSize() {
let expectedCellSize = CGSize(width: expectedFullWidth, height: expectedHeight)
let viewModel = GridViewModel()
viewModel.data = [1]
let row = 0
@wingchi
wingchi / GridViewModel.swift
Last active May 5, 2018 21:54
Grid View Model with basic method signature
import UIKit
class GridViewModel {
static var itemMargin: CGFloat = 8.0
var data: [Int] = (1...10).map { $0 }
func cellSize(for frame: CGRect, at indexPath: IndexPath) -> CGSize {
return CGSize(width: 0, height: 0)
}
}
@wingchi
wingchi / ViewController.swift
Last active May 5, 2018 22:40
View Controller with Collection View boilerplate
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var collectionView: UICollectionView!
private let gridViewModel = GridViewModel()
override func viewDidLoad() {
super.viewDidLoad()
$oneString = array("", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine");
$tenString = array("", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "one hundred");
$teenString = array("", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen");
for ($count = 100; $count > 0; --$count) {
$next = $count - 1;
$count_text = (!($count>10 && $count<20) ? $tenString[$count/10] . (($count%10 != 0 && $count>9) ? " " : "") . $oneString[$count%10] : $teenString[$count-10]);
$next_text = (!($next>10 && $next<20) ? $tenString[$next/10] . (($next%10 != 0 && $next > 9) ? " " : "") . $oneString[$next%10] : $teenString[$next-10]);
print (ucFirst($count_text). " bottles of beer on the wall, " . $count_text . " bottles of beer! Take one down, and pass it around; " . $next_text . " bottles of beer on the wall!