Skip to content

Instantly share code, notes, and snippets.

View andreasmpet's full-sized avatar

Andreas P. andreasmpet

  • Beining & Bogen
View GitHub Profile
@andreasmpet
andreasmpet / TransformingVariable.swift
Created September 22, 2017 12:29
Wrapper for Variable in RxSwift that allows easy transformation rules for the input values
public class TransformingVariable<Element> : ObserverType, ObservableConvertibleType {
public typealias E = Element
public typealias TransformationBlock = (_ input: E) -> E
private let backingVariable: Variable<E>
private let transformation: TransformationBlock
public init(initialValue: E, transformation: @escaping TransformationBlock) {
self.backingVariable = Variable(initialValue)
self.transformation = transformation
@andreasmpet
andreasmpet / BufferingStackView.swift
Created November 23, 2017 15:10
BufferingStackView
class BufferingStackView: UIStackView {
private var viewBuffer: ViewBuffer = ViewBuffer()
func prepare<T: UIView>(viewCount: Int, config: @escaping (_ index: Int, _ view: T) -> Void) {
let currentViewCount = self.arrangedSubviews.count
let neededViewCount = viewCount
let diff = neededViewCount - currentViewCount
let absDiff = abs(diff)
let shouldDelete = diff < 0
if shouldDelete {
(0..<absDiff).forEach({ _ in
@andreasmpet
andreasmpet / ExampleViewController.swift
Last active April 23, 2018 12:08
ViewControllerLifeCycleEventHandler
import UIKit
struct HideNavBehavior: ViewControllerLifecycleBehavior {
func handle(event: LifecycleEvent) {
switch event.eventType {
case .willAppear:
event.viewController.navigationController?.setNavigationBarHidden(true, animated: false)
case .willDisappear:
event.viewController.navigationController?.setNavigationBarHidden(false, animated: false)
default:
struct TemplatesRequest: PidgeonRequest {
let relativeUrl: String = "/templates"
let httpMethod: PidgeonHTTPMethod = .get
}
struct UploadRequest: PidgeonRequest {
let relativeUrl: String = "/exerciseRoutines"
let httpMethod: PidgeonHTTPMethod = .post
let routine: ExerciseRoutine
@andreasmpet
andreasmpet / Example.swift
Last active August 21, 2021 01:42
Networking layer example
struct ExerciseRoutine: Codable {
let id: String
let templateId: String
let name: String
}
struct ExerciseTemplate: Codable {
let id: String
let name: String
}