Skip to content

Instantly share code, notes, and snippets.

Avatar

はるふ ha1f

View GitHub Profile
View Repository.swift
/// リクエストをして、データをキャッシュする
/// ロード中mの再呼び出しは無視する
/// これだと、一回データ取得して、二回目でエラー、みたいなときにつらい
class Repository<T> {
typealias State = RepositoryState<T, Error>
private let _request: Observable<T>
let state = BehaviorRelay<State>(value: .possible)
View CGRect+Lerp.swift
extension CGRect {
/// 線形補間した矩形を返す
/// - parameter rate: 変化割合。0 <= rate <= 1の範囲で指定する。
func lerp(to rect: CGRect, rate: CGFloat) -> CGRect {
let lerp = _lerpCurried(rate)
return CGRect(
x: lerp(origin.x, rect.origin.x),
y: lerp(origin.y, rect.origin.y),
width: lerp(width, rect.width),
height: lerp(height, rect.height)
View DelegateMulticaster.swift
/// delegateなどを配列などに持ちたいときに使う
private final class WeakObject<T: AnyObject> {
weak var object: T?
init(_ object: T?) {
self.object = object
}
}
final class PageViewControllerDelegateMulticaster: NSObject, UIPageViewControllerDelegate {
View CircleImageRenderer.swift
final class CircleImageRenderer {
let image: Observable<UIImage>
init(radius: CGFloat, fillColor: UIColor = .white) {
self.image = Observable.create { observer in
let format = UIGraphicsImageRendererFormat.default()
format.scale = UIScreen.main.scale
let width = radius * 2
let size = CGSize.square(width)
let renderer = UIGraphicsImageRenderer(
View Functions.swift
func const<T, U>(_ value: T) -> (U) -> T {
return { _ in value }
}
func identity<T>() -> (T) -> T {
return { $0 }
}
View WithBlockingView.swift
//
// WithBlockingView.swift
//
extension ObservableConvertibleType {
/// actionがcompleteするまで画面を表示する
func withBlockingView(from parentViewController: UIViewController) -> Observable<Element> {
let viewController = _BlockingLoadingViewController(self)
return viewController.result
.do(onSubscribed: { [weak parentViewController] in
View Comparable+Clamp.swift
//
// Comparable+Clamp.swift
//
import Foundation
extension Comparable {
/// minとmaxの範囲に収める
func clamp(min minValue: Self, max maxValue: Self) -> Self {
return max(min(self, maxValue), minValue)
View UIViewControllerReferenceCycleTests.swift
//
// UIViewControllerReferenceCycleTests.swift
// TargetTests
//
import UIKit
import XCTest
@testable import Target
View YearMonthDay.swift
import Foundation
struct YearMonthDay {
var year: Int
var month: Int
var day: Int
/// Extracts yearMonthDay from Date, using calendar.
static func from(_ date: Date, calendar: Calendar = Calendar(identifier: .gregorian)) -> YearMonthDay {
let components = calendar.dateComponents([.year, .month, .day], from: date)
View WithPrevious.swift
//
// WithPrevious.swift
//
// Created by ha1f on 2018/06/25.
// Copyright © 2018年 ha1f. All rights reserved.
//
import Foundation
import RxCocoa
import RxSwift