Skip to content

Instantly share code, notes, and snippets.

Avatar

Krzysztof Zabłocki krzysztofzablocki

View GitHub Profile
@krzysztofzablocki
krzysztofzablocki / gist:338eddf527a351de825bd62cf2a1de28
Last active May 12, 2022
Use Sourcery to Generate bash script that will rewrite your source code to add final to all classes that have no inheritance
View gist:338eddf527a351de825bd62cf2a1de28
#!/usr/bin/env bash
<% for type in types.classes { -%>
<%_ if type.attributes["final"] != nil || type.attributes["open"] != nil || types.based[type.name].isEmpty == false { continue } -%>
<%_ _%>git grep -lz 'class <%= type.name %>' | xargs -0 perl -i'' -pE "s/class <%= type.name %>(?=\s|:)/final class <%= type.name %>/g"
<% } %>
// Run with Sourcery on your codebase and then execute generated code via bash :)
@krzysztofzablocki
krzysztofzablocki / debugDiffing.swift
Created Aug 18, 2021
Higher order reducer for TCA that enables better debugging
View debugDiffing.swift
import ComposableArchitecture
import Difference
import Foundation
/// A container for storing action filters.
///
/// The logic behind having this rather than a normal closure is that it allows us to namespace and gather action filters together in a consistent manner.
/// - Note: You should be adding extensions in your modules and exposing common filters you might want to use to focus your debugging work, e.g.
/// ```swift
/// extension ActionFilter where Action == AppAction {
@krzysztofzablocki
krzysztofzablocki / NetworkResponseConvertible.swift
Last active Aug 22, 2021
A good way to deal with mapping network data to your local models in a consistent way, This is how @noremac standardised it in Times codebase
View NetworkResponseConvertible.swift
import Combine
import Foundation
/// This protocols allows you to declare your type as having a distinct network
/// representation.
///
/// Rather than writing and maintaining a custom `Decodable` implementation for
/// your type, declare a brand new struct that exactly matches the expected
/// network response, and then write an initializer for your actual type that
/// accepts a `NetworkResponse`.
@krzysztofzablocki
krzysztofzablocki / Versionable.swift
Created May 26, 2021
Extension of http://merowing.info/2020/06/adding-support-for-versioning-and-migration-to-your-codable-models./ that supports a situation when you shipped app without versioning and want to add it now
View Versionable.swift
import Foundation
public protocol VersionType: CaseIterable, Codable, Comparable, RawRepresentable {}
public extension VersionType where RawValue: Comparable {
static func < (a: Self, b: Self) -> Bool {
return a.rawValue < b.rawValue
}
}
View CloudKitAccountStatus.swift
import CloudKit
import Combine
/// Fetches the user's CloudKit Account status.
///
/// - Parameter container: The container to check the status in.
///
/// - Returns: A deferred future that resolves to the user's CloudKit Account status.
func getAccountStatus(for container: CKContainer) -> AnyPublisher<CKAccountStatus, Error> {
Deferred {
@krzysztofzablocki
krzysztofzablocki / mac-list-bug.swift
Last active Apr 5, 2021
Why isn't last delete refreshing the list? Mac example
View mac-list-bug.swift
@main
struct ListBugApp: App {
var body: some Scene {
WindowGroup {
NavigationView { // comment out navigation view and list works
ContentView()
Text("Detail")
}
}
}
View SharedMutableState.swift
// Copyright (c) 2021 Manuel Maly
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
@krzysztofzablocki
krzysztofzablocki / process.sh
Created Jan 18, 2021
3rd party tooling processing script
View process.sh
#!/bin/zsh
cd "$(dirname "$0")/.."
if [[ -n "$CI" ]] || [[ $1 == "--fail-on-errors" ]] ; then
FAIL_ON_ERRORS=true
echo "Running in --fail-on-errors mode"
ERROR_START=""
COLOR_END=""
INFO_START=""
@krzysztofzablocki
krzysztofzablocki / FindFinalClasses.swift
Last active Aug 22, 2021
Swift makes classes final if possible
View FindFinalClasses.swift
#!/usr/bin/env bash
<% for type in types.classes { -%>
<%_ if type.attributes["final"] != nil || type.attributes["open"] != nil || types.based[type.name]?.isEmpty == false { continue } -%>
<%_ _%>git grep -lz 'class <%= type.name %>' | xargs -0 perl -i'' -pE "s/class <%= type.name %>(?=\s|:)/final class <%= type.name %>/g"
<% } %>
View something.swift
button.onTouchUpInside(context: self) { context in
// doStuff
}