Skip to content

Instantly share code, notes, and snippets.

View daltonclaybrook's full-sized avatar

Dalton Claybrook daltonclaybrook

View GitHub Profile
import Foundation
private var formatters = [String: DateFormatter]()
extension DateFormatter {
static func sharedFormatter(withFormat format: String,
calendar: Calendar = .current,
locale: Locale = .current,
timeZone: TimeZone = .current) -> DateFormatter {
if let formatter = formatters[format] {
import UIKit
extension UIStoryboardSegue {
// MARK: View Controller Helpers
func prepare<T: UIViewController>(forId id: String, viewControllerBlock: (T) -> Void) {
guard self.identifier == id, let viewController = destination as? T else { return }
viewControllerBlock(viewController)
}
@daltonclaybrook
daltonclaybrook / SayTag.swift
Last active September 13, 2017 16:58
Protocol for creating XML tags and representing them as strings
import Foundation
struct SayTag {
static let indefiniteLoop = 0
enum Voice: String {
case man, woman, alice
}
let message: String
@daltonclaybrook
daltonclaybrook / RxSwitchFetcher.swift
Last active September 16, 2017 07:50
When a UISwitch changes to "on," a network request is performed, then the switch is flipped back off again.
import Moya
import RxCocoa
import RxSwift
import UIKit
func bindSwitchToTeamFetch(_ aSwitch: UISwitch) {
let teamsRequest = observeAllTeams()
aSwitch
.rx
.isOn // an Observable mirror of UISwitch.isOn
extension APIError {
var message: String {
switch reasonCode {
case 300:
return "error_code_send_failed".localized(in: .swipeKit)
case 301:
return "error_code_validate_failed".localized(in: .swipeKit)
default:
return "error_generic".localized(in: .swipeKit)
}
//
// UIViewController+Additions.h
// SFSCategoryKit
//
// Created by Dalton Claybrook on 4/6/15.
// Copyright (c) 2015 Dalton Claybrook. All rights reserved.
//
#import <UIKit/UIKit.h>
extension UIImage {
func scaleFactorFor(maxSize: CGSize) -> CGFloat {
let imageSize = self.size
guard imageSize.width > maxSize.width || imageSize.height > maxSize.height else {
// image is already smaller than max
return 1.0
}
let widthScaleFactor = maxSize.width / imageSize.width
let heightScaleFactor = maxSize.height / imageSize.height
class ViewController: UIViewController {
@IBAction func refreshButtonPressed(_ sender: Any) {
self.fetchUsers()
}
func fetchUsers() {
self.loadingView.show(in: self.view)
self.apiProvider
.request(.users)
.asObservable()
struct ViewModel {
// MARK: Public
var users: Observable<[User]> {
return self.usersVariable.asObservable()
}
var isLoading: Observable<Bool> {
return self.isLoadingVariable.asObservable()
}
var error: Observable<Error> {
private func bindUsers(with viewModel: ViewModel) {
viewModel.users
.bind(to: self.tableView.rx.items(cellIdentifier: UserCellReuseID, cellType: UserCell.self)) { _, user, cell in
cell.configure(with: user)
}
.disposed(by: self.disposeBag)
}
private func bindRefreshButton(with viewModel: ViewModel) {
let tapObservable = self.refreshButton