Skip to content

Instantly share code, notes, and snippets.

@wotjd
wotjd / LoopPlayerView.swift
Created September 18, 2024 18:07
an implementation of UIKit bridged LoopPlayerView
import SwiftUI
import AVKit
// NOTE: be careful when use as it has state problem.
struct LoopPlayerView: View {
let player: AVQueuePlayer
let playerItem: AVPlayerItem
let configuration: PlayerView.Configuration
@wotjd
wotjd / FigmaBackgroundBlur.swift
Created September 9, 2024 11:29
implementation of figma's background blur
import SwiftUI
struct VisualEffectView: UIViewRepresentable {
let visualEffectViewConfiguration: (UIVisualEffectView) -> Void
init(
visualEffectViewConfiguration: @escaping (UIVisualEffectView) -> Void = { _ in }
) {
self.visualEffectViewConfiguration = visualEffectViewConfiguration
}
@wotjd
wotjd / Carousel.swift
Last active August 28, 2024 07:57
implementation of infinite scroll carousel using SwiftUI
@wotjd
wotjd / TaskManagingActor.swift
Created June 25, 2024 18:10
results using previous task without creating new task
actor MyActor {
var currentTask: Task<Void, Error>?
@discardableResult
func run<Value>(
_ action: @Sendable (_ actor: isolated MyActor) async throws -> Value
) async rethrows -> Value {
try await action(self)
}
}
@wotjd
wotjd / MoyaProvider+Extension.swift
Created April 1, 2024 16:39
Moya async request with handling cancellation
import Foundation
import Moya
struct AsyncRequestError: Error {
// TODO: add Description
}
extension MoyaProvider {
// method #1 - using AsyncThrowingStream
func request(_ target: Target) async throws -> Response {
protocol AProtocol {
func callAsFunction()
}
class AClass: AProtocol {
func callAsFunction() { print("hi there") }
}
let a: any AProtocol = AClass()
a() // prints "hi there"
@wotjd
wotjd / View+UIKitSheet.swift
Last active September 20, 2024 07:50
Present UIKit based sheet as simple as SwiftUI
import SwiftUI
#Preview {
struct SampleView: View {
@State var presentsSheet = false
var body: some View {
Button { presentsSheet.toggle() } label: { Text("Toggle Sheet") }
.uiKitSheet(
isPresented: $presentsSheet,
import Combine
extension Publisher {
func makeHot() -> AnyPublisher<Output, Failure> {
let subject = PassthroughSubject<Void, Never>()
let publisher = map(Result.success)
.catch { Just(.failure($0)) }
.prefix(untilOutputFrom: subject)
.makeConnectable()
let connection = publisher.connect()
@wotjd
wotjd / compositional_waterfall_layout.swift
Created October 26, 2022 11:37
a simple implementation of waterfall layout using compositional layout
import UIKit
extension UICollectionViewCompositionalLayout {
static func waterfall(
columnCount: Int,
interItemSpacing: CGFloat = 0,
lineSpacing: CGFloat = 0,
itemCountProvider: @escaping (Int) -> Int,
itemSizeProvider: @escaping (IndexPath) -> CGSize,
sectionConfigurator: @escaping (inout NSCollectionLayoutSection) -> Void = { _ in },
import Foundation
import Combine
// shareWhileConnected using Combine Proof of Concept
extension Publisher {
func shareWhileConnected() -> Publishers.ShareWhileConnected<Self, PassthroughSubject<Output, Failure>> {
.init(upstream: self)
}
}