Skip to content

Instantly share code, notes, and snippets.

// Created by Marcin Krzyzanowski
import Foundation
public protocol JSONEncodable: Encodable { }
public extension JSONEncodable {
func toJSON(using encoder: @autoclosure () -> JSONEncoder = JSONEncoder()) throws -> String {
try String(decoding: encoder().encode(self), as: UTF8.self)
}
@dynamicMemberLookup
struct Template {
var template : String
private var data : [String:String]
var populatedTemplate : String { data.reduce(template) { $0.replacingOccurrences(of: "${\($1.key)}", with: $1.value) } }
init(template: String, data: [String:String] = [:]) {
self.template = template
self.data = data
}
@yHomework
yHomework / FormattedTextField.swift
Created May 4, 2020 00:51 — forked from darrarski/FormattedTextField.swift
SwiftUI FormattedTextField - TextField with custom display/edit formatters
import SwiftUI
public struct FormattedTextField<Formatter: TextFieldFormatter>: View {
public init(_ title: String,
value: Binding<Formatter.Value>,
formatter: Formatter) {
self.title = title
self.value = value
self.formatter = formatter
}
@yHomework
yHomework / FadeView.swift
Created March 24, 2020 12:29 — forked from lpbas/FadeView.swift
Fade a UIView in ios using Gradient (Swift Extension)
///
/// With this extension you can call .fadeView(style: ) on any view to create a fading effect.
/// Very useful when trying to fade a UIScrollView, UITableView or UICollectionView
/// This way also takes care of the "white or black" transparent gradient that happens as described here: https://stackoverflow.com/questions/24882361/ios-white-to-transparent-gradient-layer-is-gray
/// This solution is a swift implementation of the following stackoverflow question in Swift, adding more styles: https://stackoverflow.com/questions/17774761/how-to-create-a-top-fade-effect-using-uiscrollview/25408833#25408833
///
extension UIView {
enum UIViewFadeStyle {
case bottom
@yHomework
yHomework / Debouncer.swift
Created March 24, 2020 12:24 — forked from Ikloo/Debouncer.swift
Deferred funcs calls for Swift
//
// Debouncer.swift
//
// Created by Kirill Budevich on 11/14/19.
// Copyright © 2019 KB. All rights reserved.
//
import Foundation
final public class Debouncer {
@yHomework
yHomework / UIScrollView+UIPageControl.swift
Created March 10, 2020 07:17 — forked from kishikawakatsumi/UIScrollView+UIPageControl.swift
UIScrollView + UIPageControl with RxSwift
import RxSwift
import RxCocoa
fileprivate extension Reactive where Base: UIScrollView {
fileprivate var currentPage: Observable<Int> {
return didEndDecelerating.map({
let pageWidth = self.base.frame.width
let page = floor((self.base.contentOffset.x - pageWidth / 2) / pageWidth) + 1
return Int(page)
})
@yHomework
yHomework / SupportCode.swift
Last active March 9, 2020 09:10 — forked from acchou/SupportCode.swift
Playground support code for using RxSwift or any other framework that sends async events. Article: https://medium.com/@_achou/making-a-playground-using-rxswift-81d8377bd239
import Foundation
public func delay(_ delay: TimeInterval, closure: @escaping ()->()) {
DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
closure()
}
}
#if NOT_IN_PLAYGROUND
@yHomework
yHomework / DeviceCheck.swift
Created March 7, 2020 15:10 — forked from schirrmacher/DeviceCheck.swift
DeviceCheck Example Implementation for Validating Device Authenticity on iOS
import Foundation
import RxSwift
#if !os(watchOS) && !os(tvOS)
import DeviceCheck
#endif
public struct Client {
private static func retrieveDeviceToken() -> Observable<String?> {
//
// PaginationNetworkLogic.swift
//
// Created by Daniel Tartaglia on 4/9/17.
// Copyright © 2019 Daniel Tartaglia. MIT License
//
import RxSwift
struct PaginationUISource {
@yHomework
yHomework / List.swift
Created March 7, 2020 10:01 — forked from kean/List.swift
Proof-of-concept List as Swift Enum
// The MIT License (MIT)
//
// Copyright (c) 2017 Alexander Grebenyuk (github.com/kean).
import Foundation
/// Proof-of-concept List implementation, not optimized in any way.
///
/// Usage:
///