Skip to content

Instantly share code, notes, and snippets.

@detrick
Created February 10, 2021 15:25
Show Gist options
  • Select an option

  • Save detrick/828c60797f004f6bcb384f708f2b5c20 to your computer and use it in GitHub Desktop.

Select an option

Save detrick/828c60797f004f6bcb384f708f2b5c20 to your computer and use it in GitHub Desktop.
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
// MARK: - Models
struct Movie: Decodable {
let title: String
let rating: Double
}
struct TVShow: Decodable {
let title: String
let rating: Double
}
// MARK: - API
class API {
func movies(callback: ((Result<[Movie], Error>) -> Void)) {
callback(.success([
Movie(title: "Interstellar", rating: 8.5),
Movie(title: "The Godfather", rating: 9.1),
]))
}
func tvShows(callback: ((Result<[TVShow], Error>) -> Void)) {
callback(.success([
TVShow(title: "The Office", rating: 8.8),
TVShow(title: "Breaking Bad", rating: 9.4),
]))
}
}
// MARK: - UI
class ListViewController: UITableViewController {
enum SortMode {
case ascending // smallest to largest
case descending // largest to smallest
}
var sort: SortMode = .descending
override func viewDidLoad() {
super.viewDidLoad()
addBarButtonItems()
}
func addBarButtonItems() {
navigationItem.leftBarButtonItems = [
UIBarButtonItem(title: "Movies", style: .plain, target: self, action: #selector(showMovies)),
UIBarButtonItem(title: "TV Shows", style: .plain, target: self, action: #selector(showTVShows))
]
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Sort", style: .plain, target: self, action: #selector(sortItems))
}
@objc func showMovies() {
// TODO: Filter out TV Shows
}
@objc func showTVShows() {
// TODO: Filter out Movies
}
@objc func sortItems() {
// TODO: Sort
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "Cell")
cell.textLabel?.text = "Title"
cell.detailTextLabel?.text = "Rating"
let movieIcon = UIImage(systemName: "tv.and.hifispeaker.fill")
let tvIcon = UIImage(systemName: "play.tv")
cell.accessoryView = UIImageView(image: movieIcon)
return cell
}
}
// MARK: - App
let navigation = UINavigationController(rootViewController: ListViewController())
PlaygroundPage.current.liveView = navigation
// 1. Display all movies and tv shows from the API
// 2. Implement filter by movie, filter by tv show
// 3. Sort by rating
// 4. Convert to MVVM
// 5. Add Tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment