Skip to content

Instantly share code, notes, and snippets.

@Thomvis
Thomvis / FunctionView.swift
Created June 7, 2019 08:39
Function components are a thing in React. Here's what they look like in SwiftUI.
func ShieldIcon(ac: Int) -> some View {
ZStack {
Image(systemName: "shield")
.font(.title)
Text("\(ac)")
.font(.caption)
}
}
@Thomvis
Thomvis / SwiftUI-Circular.swift
Last active August 4, 2021 21:41
Rough attempt at creating a container view that lays out its children in a circle #SwiftUI
struct ContentView: View {
@State var count: Int = 3
var body: some View {
return NavigationView {
VStack(spacing: 50) {
HStack {
Button(action: { self.count += 1 }) {
Text("Add")
}
@Thomvis
Thomvis / gist:dac2327ea253ed2c3ed6bd3dc13a95f2
Created June 6, 2019 10:42
Thought it would be nice to create a custom stack implementation. Immediately ran into all kinds of walls.
struct Stack<Item>: View where Item: View {
let items: [Item]
init(@ViewBuilder items: () -> Item) {
fatalError("How to get the views out of the view builder's result (TupleView) into self.items?")
}
var body: some View {
fatalError("How to layout self.items in a Group/ZStack?")
return EmptyView()
type loadableData('e) =
| Loading
| Loaded(list('e));
type loadableDataAction('e) =
| DidLoad(list('e));
type state = {data: loadableData(Api.product)};
/* Action declaration */
// This view overrides safeAreaInsets in order to work around
// (possibly erroneous) behavior of UIKit where safe area insets change
// when a view is transformed. (see http://www.openradar.me/35532074)
//
// Use this view as the direct subview of the view that has the transform applied.
// This view's frame should be equal to the superview's bounds for this to work.
@objc public class TransformIgnoringSafeAreaInsetsView: UIView {
@available(iOS 11.0, *)
override public var safeAreaInsets: UIEdgeInsets {
guard let superview = self.superview, superview.transform != .identity else {
@Thomvis
Thomvis / backpressure.md
Created May 19, 2017 08:32
To the person that asked me a question about backpressure after my talk at UIKonf to which I said "Let's talk about it over lunch" and wasn't able to find...

I didn't cover backpressure because it is quite complex and - from my experience - not a core concept of reactive programming for mobile development. Some implementations do have it (e.g. RxJava), but to my knowledge none of the (big) iOS implementations, such as RxSwift and ReactiveSwift, have support for it. I use RxJava, but have never really needed its backpressure capabilities.

Adding backpressure to my minimal reactive programming implementation would require a layer of indirection that would establish the backchannel needed to allow for backpressure. It could look something like this:

o.subscribe { producer in
  producer.observe {
    // $0 is every element as it gets emitted
    producer.request(1) // makes sure the number of requested events stays 5 (requests are additive)
 }
import Dispatch
public typealias Provider<E> = () -> E
class Container {
{% for type in types.all %}
{% if type|annotated:"singleton" %}
private let {{type.name|lowerFirstWord}}Queue = DispatchQueue(label: "singleton:{{type.name|lowerFirstWord}}")
private var __{{type.name|lowerFirstWord}}: {{type.name}}?
// Generated using Sourcery 0.5.9 — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT
import Dispatch
public typealias Provider<E> = () -> E
class Container {
private let apiServiceQueue = DispatchQueue(label: "singleton:apiService")
import Mimus
{% for type in types.protocols %}
{% if type.annotations.genMock %}
class {{ type.name }}Mock: {{ type.name }}, Mock {
var storage: [RecordedCall] = []
{% for method in type.allMethods %}
func {{ method.name }} {
protocol HeaderViewProtocol {
func setTitle(_ title: String)
func asUIView() -> UIView
}
extension HeaderViewProtocol where Self: UIView {
func asUIView() -> UIView {
return self
}
}