Skip to content

Instantly share code, notes, and snippets.

View darrarski's full-sized avatar
:octocat:
🍏🦕

Dariusz Rybicki darrarski

:octocat:
🍏🦕
View GitHub Profile
#!/usr/bin/env bash
# Measure time
# Usage: stopwatch [start|stop|print]
function stopwatch {
case "$1" in
start)
# start new stopwatch
STOPWATCH+=($(date -u +%s))
;;
@darrarski
darrarski / Gravatar.swift
Created August 31, 2023 14:10
Swift wrapper for Gravatar JSON API
import CryptoKit
import Dependencies
import Foundation
import XCTestDynamicOverlay
public struct GravatarJSON: Equatable, Sendable, Codable {
public init(entry: [Entry]) {
self.entry = entry
}
@darrarski
darrarski / ReducerPrinter+SwiftLog.swift
Created August 9, 2023 13:56
ComposableArchitecture + SwiftLog integration
import ComposableArchitecture
import Logging
extension _ReducerPrinter {
/// Logs info about received actions and state changes to swift-log's Logger with provided label.
///
/// Example usage:
/// ```
/// let store = Store(initialState: AppFeature.State()) {
/// AppFeature()._printChanges(.swiftLog(label: "tca"))
@darrarski
darrarski / ExampleApp.swift
Created March 13, 2023 23:47
TCA prerelease 1.0 NavigationLinkStore returningLastNonNilValue issue
import ComposableArchitecture
import SwiftUI
struct Parent: Reducer {
struct State: Equatable {
@PresentationState var child: Child.State?
}
enum Action: Equatable {
case presentChildButtonTapped
@darrarski
darrarski / AsyncThrowingStreamTestReducer.swift
Last active March 5, 2023 10:56
AsyncStream and AsyncThrowingStream testing utility
/// MIT License
///
/// Copyright (c) 2023 Dariusz Rybicki Darrarski
///
/// 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:
@darrarski
darrarski / CurrentValueAsyncSequence.swift
Created January 15, 2023 01:18
[swift-concurrency] Async sequence with current value (similar to Combine's CurrentValueSubject).
import Foundation
@dynamicMemberLookup
public actor CurrentValueAsyncSequence<Value>: AsyncSequence where Value: Sendable {
public typealias Element = Value
public init(_ value: Value) {
self.value = value
}
@darrarski
darrarski / EmojiPicker.swift
Last active September 23, 2024 12:20
SwiftUI emoji picker using UIKit on iOS
import SwiftUI
struct EmojiPickerView: UIViewRepresentable {
@Binding var isFirstResponder: Bool
var onPick: (String) -> Void
var onDelete: () -> Void
func makeUIView(context: Context) -> UIViewType {
UIViewType(view: self)
}
@darrarski
darrarski / Functor.swift
Created August 27, 2022 18:34
Sourcery template for creating functors from functions
import XCTestDynamicOverlay
{% for func in functions %}
public struct {% for text in func.name|split:"(" %}{% if forloop.first %}{{ text|upperFirstLetter }}{% endif %}{% endfor %} {
public var run: ({% for param in func.parameters %}{{ param.typeName }}{% ifnot forloop.last %}, {% endif %}{% endfor %}){% if func.throws %} throws{% endif %} -> {{ func.returnTypeName }}
public func callAsFunction({% if func.parameters.count > 0 %}
{% for param in func.parameters %}
{{ param.asSource }}{% ifnot forloop.last %},{% endif %}
{% endfor %}
@darrarski
darrarski / SwiftUI_tap_gesture_with_location.swift
Last active June 8, 2022 10:15
Get location of a tap in SwiftUI
import SwiftUI
struct ExampleView: View {
var body: some View {
Color.blue
.gesture(
TapGesture(count: 1)
.simultaneously(with: DragGesture(minimumDistance: 0))
.onEnded { value in
if value.first != nil, let location = value.second?.startLocation {
@darrarski
darrarski / SwiftUI_InterfaceOrientationObservingViewModifier.swift
Created February 18, 2022 13:49
SwiftUI view modifier that performs action whenever UIInterfaceOrientation reported by UIViewController changes
import SwiftUI
struct InterfaceOrientationObservingViewModifier: ViewModifier {
let onChange: (UIInterfaceOrientation) -> Void
func body(content: Content) -> some View {
content.background(InterfaceOrientationObservingView(onChange: onChange))
}
}