Skip to content

Instantly share code, notes, and snippets.

View pofat's full-sized avatar

Pofat pofat

View GitHub Profile
@pofat
pofat / LaunchTracker.swift
Created February 22, 2023 02:10
Get permain and check if it's prewarm
/// iOS 15+
func isPrewarm() -> Bool {
if let prewawrmEnv = ProcessInfo.processInfo.environment["ActivePrewarm"],
prewawrmEnv == "1" {
return true
} else {
return false
}
}
@pofat
pofat / asyncstream.swift
Created August 16, 2022 08:24
Delegate with AsyncStream
protocol FooDelegate: AnyObject {
func didReceive(value: Int)
func didOpen()
func didClose()
}
class Foo {
weak var delegate: FooDelegate?
func open() {
@pofat
pofat / saerv.swift
Created August 15, 2022 09:52
TCA state-action-env-reducer-view code snippet
import ComposableArchitecture
import SwiftUI
struct <#Name#>State: Equatable {}
enum <#Name#>Action: Equatable {}
struct <#Name#>Environment {}
extension <#Name#>Environment {
@pofat
pofat / crash.swift
Created July 24, 2022 21:36
Code that crash Swift compiler (before 5.8)
protocol P {
associatedtype A
associatedtype B
}
protocol Q: P {}
extension Q where A == Int {
typealias B = Int
}
struct Collapsable<Content: View>: View {
@State var collapsed: Bool = true
let content: () -> Content
init(@ViewBuilder content: @escapin () -> Content) {
self.content = content
}
var body: some View {
VStack {
@pofat
pofat / filter_keyPath.swift
Created May 22, 2022 16:21
Filter with KeyPath
struct Foo {
var bar: Int
var overThreshold: Bool {
bar > 5
}
}
let fooArray: [Foo] = ...
let filtered = fooArray.filter(\.overThreshold)
@pofat
pofat / demangler.swift
Created April 10, 2022 21:57
Swift demangler
import Foundation
guard CommandLine.arguments.count > 1 else {
print("Please specify object file/executable")
exit(EXIT_FAILURE)
}
var nm = Process()
nm.launchPath = "/usr/bin/nm"
nm.arguments = [CommandLine.arguments[1]]
@pofat
pofat / operator.swift
Created January 12, 2022 11:11
[Effect<Output, Failure>] -> Effect<[Output], Failure>
extension Effect {
// Zip with other effects
func zip<Others: Collection>(with others: Others)
-> Effect<[Output], Failure>
where Others.Element == Effect<Output, Failure> {
let first = map { [$0] }
.eraseToEffect()
return others
.reduce(first) { zipped, next in
@pofat
pofat / DelegatePublisher.swift
Last active August 9, 2022 04:42
How to handle delegate in a reactive way? Turn a delegate into Publisher.
import Combine
import CombineExt // Check this implementation: https://github.com/CombineCommunity/CombineExt/blob/main/Sources/Operators/Create.swift
import CoreBluetooth
// Client to generate target delegate publisher
// Learned from https://github.com/pointfreeco/composable-core-location/blob/main/Sources/ComposableCoreLocation/Live.swift
struct PeripheralClient {
enum Action: Equatable {
case didUptateName
@pofat
pofat / Kind.swift
Created March 12, 2021 15:49
Swift Struct Metadata
// Kind of type
public enum Kind {
case `struct`
case `enum`
case optional
case opaque
case tuple
case function
case existential
case metatype