Skip to content

Instantly share code, notes, and snippets.

@Jimmy-Prime
Jimmy-Prime / FloatingLabelTextField.swift
Created November 2, 2020 09:40
SwiftUI floating label textfield
// Because Label's size don't match, transition looks weird.
import SwiftUI
struct FloatingLabelTextField: View {
@Namespace private var namespace
let placeholder: String
@Binding var text: String
@State private var isTextFieldFirstResponder: Bool = false
@Jimmy-Prime
Jimmy-Prime / ThreeColumnNavigation.swift
Last active June 29, 2020 08:43
SwiftUI three column navigation example - Xcode Version 12.0 beta (12A6159)
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
Sidebar()
Text("Select a mailbox")
Text("Select a mail")
}
}
import UIKit
class InputToolbar: UIView {
private let separator = UIView()
private let button = UIButton(type: .system)
private let textField = UITextField()
private let sendButton = UIButton(type: .system)
func initialize() {
backgroundColor = .systemGreen
@Jimmy-Prime
Jimmy-Prime / InfinitePagingScrollViewController.swift
Last active May 14, 2020 09:43
Implementation of infinite scroll
import UIKit
protocol ScrollingContent: Comparable {}
protocol ScrollingContentProvider {
associatedtype Content: ScrollingContent
func previous(of content: Content) -> Content
func next(of content: Content) -> Content
}
import UIKit
class ViewController: UIViewController {
var yearView: UIView?
var currentYear: Int = 2020 {
didSet {
createYearView()
}
}
@Jimmy-Prime
Jimmy-Prime / JSON.swift
Last active May 25, 2020 07:48
JSON Codable
import Foundation
enum JSON {
case null
case bool(Bool)
case int64(Int64)
case double(Double)
case string(String)
indirect case array([JSON])
indirect case dictionary([String: JSON])
import UIKit
class ViewController: UIViewController {
private var customInput = false
private let textInput = UITextField()
private var leadingBarButtonGroups: [UIBarButtonItemGroup] = []
private var trailingBarButtonGroups: [UIBarButtonItemGroup] = []
override func viewDidLoad() {
super.viewDidLoad()
import Foundation
class SessionDelegate: NSObject, URLSessionDelegate {
func urlSession(
_ session: URLSession,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void
) {
guard challenge.protectionSpace.host != URL.sns.host else {
completionHandler(.performDefaultHandling, nil)
@Jimmy-Prime
Jimmy-Prime / HeterogeneousListDecoding.swift
Last active January 16, 2020 09:01
Decoding heterogeneous array with Swift Decodable
import Foundation
enum TypeKey: CodingKey {
case type
}
protocol TypeFamily: Decodable {
associatedtype Member: Decodable
var type: Member.Type { get }
}
@Jimmy-Prime
Jimmy-Prime / Zeroes.swift
Created July 9, 2019 06:56
Generate zero array
import Foundation
protocol Zeroes {
static var zero: Self { get }
}
extension Int: Zeroes {}
extension Int8: Zeroes {}
extension Int16: Zeroes {}
extension Int32: Zeroes {}