Skip to content

Instantly share code, notes, and snippets.

View Jpunt's full-sized avatar

Jasper Haggenburg Jpunt

View GitHub Profile
var body: some View {
List(tweets.identified(by: \.id)) { tweet in
HStack {
Text(tweet.message)
tweet.imageName.map
{ ViewBuilder.buildEither(first: Image($0) }
?? ViewBuilder.buildEither(second: Text("No image"))
}
}
var body: some View {
List(tweets.identified(by: \.id)) { tweet in
HStack {
Text(tweet.message)
tweet.imageName.map { Image($0) } ?? Image("fallback")
}
}
}
var body: some View {
List(tweets.identified(by: \.id)) { tweet in
HStack {
Text(tweet.message)
tweet.imageName.map { Image($0) }
}
}
}
var body: some View {
List(tweets.identified(by: \.id)) { tweet in
HStack {
Text(tweet.message)
IfLet(tweet.imageName) { imageName in
Image(imageName)
}
}
}
struct IfLet<T, Out: View>: View {
let value: T?
let produce: (T) -> Out
init(_ value: T?, produce: @escaping (T) -> Out) {
self.value = value
self.produce = produce
}
var body: some View {
var body: some View {
Group {
if x {
Text(“Something”)
} else {
Text(“Something else”)
}
}
}
var body: some View {
Group {
Text(“First thing”)
Text(“Second thing”)
Text(“Third thing”)
}
}
import SwiftUI
struct Tweet: Identifiable {
var id: UUID
let message: String
let imageName: String?
init(message: String, imageName: String? = nil) {
self.id = UUID()
self.message = message
@Jpunt
Jpunt / redux.ts
Last active October 4, 2018 11:44
This is how I like to do Redux stuff in TypeScript. Good coverage, with minimal effort and redefining types. What do you think?
/**
* Types
*/
// IState
import rootReducer from "./reducers";
export type IState = {
// Maps the return type of each reducer into its return-type
[K in keyof typeof rootReducer]: ReturnType<typeof rootReducer[K]>
}
@Jpunt
Jpunt / btt-toggle-appearance.json
Created September 25, 2018 12:26
BetterTouchTool: Toggle Mojave appearance on the Touch Bar
{
"BTTWidgetName" : "Toggle appearance",
"BTTTriggerType" : 639,
"BTTTriggerTypeDescription" : "Apple Script Widget",
"BTTTriggerClass" : "BTTTriggerTypeTouchBar",
"BTTPredefinedActionType" : 195,
"BTTPredefinedActionName" : "Run Apple Script (async in background)",
"BTTInlineAppleScript" : "tell application \"System Events\"\r\ttell appearance preferences\r\t\tset dark mode to not dark mode\r\tend tell\rend tell",
"BTTEnabled2" : 1,
"BTTUUID" : "ECC2514E-86E5-4304-AABA-BA7749257FDC",