Skip to content

Instantly share code, notes, and snippets.

View yzhong52's full-sized avatar
⌨️
consider multiple solutions, commit on one, and iterate

Yuchen yzhong52

⌨️
consider multiple solutions, commit on one, and iterate
View GitHub Profile
@yzhong52
yzhong52 / ViewController.swift
Last active February 16, 2020 15:54
Building a Client App From Scratch (ViewController with Client and TableView)
import UIKit
import RxSwift
import SafariServices
class ViewController: UIViewController {
private let disposeBag = DisposeBag()
private let client = NewsClient()
private var articles: [Article] = []
@yzhong52
yzhong52 / NewsClient.swift
Last active February 16, 2020 16:02
Building a Client App From Scratch (NewsClient)
// TODO: update the API key
private let apiKey: String = "c8707b3709a34bbe90837f63c71537ed"
private let path: String = "https://newsapi.org/v2/top-headlines?apiKey=\(apiKey)&country=us"
enum ClientError: Error {
case missingResponseData
}
class NewsClient {
func headlines() -> Single<ArticlesResponse> {
@yzhong52
yzhong52 / ViewController.swift
Last active February 16, 2020 16:06
Building a Client App From Scratch (ViewController with UITableViewDelegate)
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let article = articles[indexPath.row]
present(SFSafariViewController(url: article.url), animated: true, completion: nil)
}
}
@yzhong52
yzhong52 / ViewController.swift
Last active February 16, 2020 16:14
Building a Client App From Scratch (UITableViewDataSource)
extension ViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return articles.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
@yzhong52
yzhong52 / ImagesManager.swift
Created February 16, 2020 19:40
Building a Client App From Scratch - Image download extension
import UIKit
import Alamofire
class ImageManager {
private let session = Alamofire.Session()
static let shared = ImageManager()
func load(url: URLConvertible, completionHandler: @escaping (UIImage) -> Void) -> Void {
@yzhong52
yzhong52 / gist:3f1497d7c58c7b1b0d97236b1e0d39ba
Created February 16, 2020 20:03
Building a Client App From Scratch (UITableViewDataSource v2)
extension ViewController: UITableViewDataSource {
...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! NewsTableViewCell
let article = articles[indexPath.row]
tableViewCell.titleLabel.text = article.title
tableViewCell.contentLabel.text = article.content
@yzhong52
yzhong52 / LoadState.swift
Created February 16, 2020 20:18
Building a Client App From Scratch - LoadState
import Foundation
enum LoadState<T> {
case loading
case updating
case loaded(response: T)
case failed(error: Error)
}
@yzhong52
yzhong52 / NewsManager.swift
Created February 16, 2020 20:21
Building a Client App From Scratch - NewsManager
import Foundation
import RxSwift
import RxCocoa
class NewsManager {
typealias NewsLoadState = LoadState<ArticlesResponse>
enum LoadError: Error {
case unknownError
}
@yzhong52
yzhong52 / ViewController.swift
Created February 16, 2020 20:29
Building a Client App From Scratch - ViewController NewsManager
class ViewController: UIViewController {
private let manager = NewsManager()
private let disposeBag = DisposeBag()
private var articles: [Article] = []
private let titleLabel: UILabel = {
let label = UILabel()
label.text = NSLocalizedString("News", comment: "")
@yzhong52
yzhong52 / NewsTableViewCell.swift
Last active February 16, 2020 22:01
Building a Client App From Scratch (NewsTableViewCell)
import UIKit
class NewsTableViewCell: UITableViewCell {
let titleLabel: UILabel = {
let lable = UILabel()
lable.translatesAutoresizingMaskIntoConstraints = false
lable.numberOfLines = 3
lable.font = UIFont.systemFont(ofSize: 14)
return lable