Skip to content

Instantly share code, notes, and snippets.

View finestructure's full-sized avatar

Sven A. Schmidt finestructure

View GitHub Profile
@mugbug
mugbug / AssertEquals+Diff.swift
Created June 10, 2021 20:29
XCTestCase wrapper for asserting equatable structs with formatted error diff message
import XCTest
// Credits: https://github.com/pointfreeco/swift-composable-architecture
//swiftlint:disable empty_string force_cast force_unwrapping unused_closure_parameter function_body_length
class MyCustomTestCase: XCTestCase {
func assertEqual<T: Equatable>(
expected: T,
actual: T
) {
@matthewtonkin
matthewtonkin / NSApplication+OpeanAtLogin.m
Created September 21, 2020 00:06
NSApplication Open at Login extension
@implementation NSApplication (OpenAtLogin)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (BOOL)openAtLogin
{
LSSharedFileListItemRef loginItem = [self loginItem];
BOOL result = loginItem ? YES : NO;
clean:
@# just delete specific directories instead of .builds so we don't
@# have to re-fetch dependencies
@rm -f .build/debug
@rm -rf .build/x86_64-apple-macosx/
@rm -rf .build/x86_64-unknown-linux/
@rm -rf .build/x86_64-unknown-linux-gnu/
macos-spm-4.2: clean
@echo
import SwiftUI
// Note: There are some issues with using these modifiers inside of ButtonStyles on macOS. Please see https://twitter.com/noahsark769/status/1288256379640139776?s=20 for more info.
struct ConditionalContent<TrueContent: View, FalseContent: View>: View {
let value: Bool
let trueContent: () -> TrueContent
let falseContent: () -> FalseContent
@ViewBuilder var body: some View {
@chriseidhof
chriseidhof / boilerplate.swift
Last active January 3, 2024 05:54
QuickMacApp
// Run any SwiftUI view as a Mac app.
import Cocoa
import SwiftUI
NSApplication.shared.run {
VStack {
Text("Hello, World")
.padding()
.background(Capsule().fill(Color.blue))
@AliSoftware
AliSoftware / Demo.swift
Last active October 31, 2023 12:25
NestableCodingKey: Nice way to define nested coding keys for properties
struct Contact: Decodable, CustomStringConvertible {
var id: String
@NestedKey
var firstname: String
@NestedKey
var lastname: String
@NestedKey
var address: String
enum CodingKeys: String, NestableCodingKey {
@AliSoftware
AliSoftware / CustomMatcher.swift
Last active January 5, 2020 21:40
Add custom pattern matching to make your switch statements magical
struct CustomMatcher<Value> {
let closure: (Value) -> Bool
static func ~= (caseValue: CustomMatcher<Value>, switchValue: Value) -> Bool {
caseValue.closure(switchValue)
}
static func ~= (caseValue: Value, switchValue: CustomMatcher<Value>) -> Bool {
switchValue.closure(caseValue)
}
}
extension Publisher {
func cancellable<Id: Hashable>(id: Id) -> AnyPublisher<Output, Failure> {
return Deferred { () -> PassthroughSubject<Output, Failure> in
cancellables[id]?.cancel()
let subject = PassthroughSubject<Output, Failure>()
cancellables[id] = self.subscribe(subject)
return subject
}
.eraseToAnyPublisher()
@cdmcmahon
cdmcmahon / PointFreeStateManagement.swift
Last active November 6, 2019 23:51
In the Point-Free series on application architecture and state management, they define a series of ways to compose reducers. Sometimes, however, it seems that the signature of reducers can complicate the signature of these different compositions. I found creating a typealias for reducers separated the concern of understanding the concept of redu…
// In the Point-Free series on application architecture and state management,
// they define a series of ways to compose reducers. Sometimes, however, it seems
// that the signature of reducers can complicate the signature of these different
// compositions. I found creating a typealias for reducers separated the concern of
// understanding the concept of reducers and understanding their higher order
// constructions, especially when starting to write my own.
// Adding a typealias for Reducer can make certain parts a bit more readable
typealias Reducer<Value, Action> = (inout Value, Action) -> Void
@scottmatthewman
scottmatthewman / AdaptsToSoftwareKeyboard.swift
Last active April 19, 2024 12:56
An example of using Combine to automatically adapt a SwiftUI scrollable view to accommodate an iOS onscreen keyboard
import SwiftUI
import Combine
struct AdaptsToSoftwareKeyboard: ViewModifier {
@State var currentHeight: CGFloat = 0
func body(content: Content) -> some View {
content
.padding(.bottom, currentHeight)
.edgesIgnoringSafeArea(.bottom)