Skip to content

Instantly share code, notes, and snippets.

public protocol LayoutItem { // `UIView`, `UILayoutGuide`
var superview: UIView? { get }
}
extension UIView: LayoutItem {}
extension UILayoutGuide: LayoutItem {
public var superview: UIView? { owningView }
}
public struct Alignment {
// Version 1: Using initializer directly
struct ContentView: View {
@State var count = 0
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
var body: some View {
VStack {
Text("container: \(count)")
.padding()
@kean
kean / share.swift
Last active December 9, 2020 22:43
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
extension NSAttributedString {
static func make(html: String, size: CGFloat, color: UIColor) -> NSAttributedString? {
// TODO: Size is configured programatically which means we can
// change it dynamically if needed (aka dynamic type)
struct ContentView: View {
@State private var isShowingDetailView = false
var body: some View {
NavigationView {
NavigationLink(destination: Text("Second View"), isActive: $isShowingDetailView) {
Text("Show Detail")
}
.navigationBarTitle("Navigation")
}
@kean
kean / tbd
Created April 13, 2020 19:07
asd
@kean
kean / Example.swift
Last active November 13, 2020 20:48
FetchedImage
public struct ImageView: View {
@ObservedObject var image: FetchImage
public var body: some View {
ZStack {
Rectangle().fill(Color.gray)
image.view?
.resizable()
.aspectRatio(contentMode: .fill)
}
import Future
import Combine
public extension Future {
var publisher: Combine.Future<Value, Error> {
Combine.Future { fulfill in
self.on(success: {
fulfill(.success($0))
}, failure: {
fulfill(.failure($0))
struct SearchView: View {
@ObservedObject var viewModel: SearchViewModel
var body: some View {
VStack {
TextField("Search", text: $viewModel.query)
List(viewModel.songs) {
Text($0.name)
}
}
@kean
kean / _DynamicProperty.swift
Last active February 23, 2020 22:47
_DynamicProperty
// A speculative _ViewRendererHost implementation which uses Mirror (Swift reflection) to find all
// of the dynamic properties (`DynamicProperty`) associated with a given view (`View`), observe
// the changes to these properties and automatically refresh the view whenever any of these property change.
protocol _DynamicProperty {
var objectWillChange: AnyPublisher<Void, Never> { get }
}
extension ObservedObject: _DynamicProperty {
var objectWillChange: AnyPublisher<Void, Never> {
// The MIT License (MIT)
//
// Copyright (c) 2020 Alexander Grebenyuk (github.com/kean).
import CoreData
final class FetchedEntities<Element: NSManagedObject>: NSObject, NSFetchedResultsControllerDelegate, RandomAccessCollection, ObservableObject {
private let controller: NSFetchedResultsController<Element>
init(context: NSManagedObjectContext, request: NSFetchRequest<Element>) {