Skip to content

Instantly share code, notes, and snippets.

//
// ContentView.swift
// search-test
//
// Created by Alex on 5/15/24.
//
import SwiftUI
import SwiftUI
actor SomeManager {
private var tasks: [String: DataTask] = [:]
private func data(for request: URLRequest) async throws -> Data {
let requestKey = request.urlRequest?.url?.absoluteString ?? ""
let task = tasks[requestKey] ?? DataTask(task: Task {
try await self._data(for: request, key: requestKey)
})
let subscriptionID = UUID()
task.subscriptions.insert(subscriptionID)
CREATE TABLE ZSTATSRECORDVALUE ( Z_PK INTEGER PRIMARY KEY, Z_ENT INTEGER, Z_OPT INTEGER, ZSTATSRECORD INTEGER, ZBESTVIEWSPERDAYCOUNT INTEGER, ZPOSTSCOUNT INTEGER, ZVIEWSCOUNT INTEGER, ZVISITORSCOUNT INTEGER, ZINSIGHTYEAR INTEGER, ZMOSTPOPULARDAYOFWEEK INTEGER, ZMOSTPOPULARDAYOFWEEKPERCENTAGE INTEGER, ZMOSTPOPULARHOUR INTEGER, ZMOSTPOPULARHOURPERCENTAGE INTEGER, ZTOTALCOMMENTSCOUNT INTEGER, ZTOTALIMAGESCOUNT INTEGER, ZTOTALLIKESCOUNT INTEGER, ZTOTALPOSTSCOUNT INTEGER, ZTOTALWORDSCOUNT INTEGER, ZCLICKSCOUNT INTEGER, ZPARENT INTEGER, ZVIEWSCOUNT1 INTEGER, ZDOWNLOADCOUNT INTEGER, ZCOUNT INTEGER, ZTYPE INTEGER, ZTYPE1 INTEGER, ZCOMMENTSCOUNT INTEGER, ZLIKESCOUNT INTEGER, ZPOSTID INTEGER, ZVIEWSCOUNT2 INTEGER, ZOTHERCOUNT INTEGER, ZTOTALCOUNT INTEGER, ZFOLLOWERSCOUNT INTEGER, ZVIEWSCOUNT3 INTEGER, ZPARENT1 INTEGER, ZVIEWSCOUNT4 INTEGER, ZCURRENTSTREAKLENGTH INTEGER, ZLONGESTSTREAKLENGTH INTEGER, ZPOSTCOUNT INTEGER, ZSTREAKINSIGHT INTEGER, ZTYPE2 INTEGER, ZVIEWSCOUNT5 INTEGER, ZPARENT2 INTEGER, ZCOMMENTSCOUNT1 INTEG
@kean
kean / ScrollViewPrefetcher.swift
Created February 18, 2023 15:28
ScrollViewPrefetcher
// The MIT License (MIT)
//
// Copyright (c) 2021 Alexander Grebenyuk (github.com/kean).
import SwiftUI
public protocol ScrollViewPrefetcherDelegate: AnyObject {
/// Returns all valid indices for the collection.
func getAllIndicesForPrefetcher(_ prefetcher: ScrollViewPrefetcher) -> Range<Int>
func prefetcher(_ prefetcher: ScrollViewPrefetcher, prefetchItemsAt indices: [Int])
// The MIT License (MIT)
//
// Copyright (c) 2020–2022 Alexander Grebenyuk (github.com/kean).
import Pulse
import Foundation
import ObjectiveC.runtime
#if DEBUG
extension Experimental {
@kean
kean / color-convert.swift
Last active December 25, 2022 14:58
Converts UIColor init to a color literal
import Foundation
/// Input:
///
/// public static let purple = UIColor(red: 74/255, green: 21/255, blue: 153/255, alpha: 1.0)
///
/// Output:
///
/// #4A1599 (74, 21, 153)
/// public static let purple = #colorLiteral(red: 0.2901960784, green: 0.0823529412, blue: 0.6, alpha: 1)
@kean
kean / Formatting.swift
Created October 1, 2022 00:20
Formatting
// The MIT License (MIT)
//
// Copyright (c) 2020 Alexander Grebenyuk (github.com/kean).
import Foundation
#if !os(macOS)
import UIKit
#else
import AppKit
#endif
import UIKit
// Almost pseudocode, but should be possible to write in proper Swift
// MARK: High-Level APIs
// Decodes the document. By default, ignores errors and warnings.
func decode(data: Data, isStrict: Bool = false) -> throws OpenAPI.Document {
let parser = JSONDecoder().decoder(DocumentParser.self, from: data)
// Or maybe be even more granular: "strict", "ignoreWarnings", "ignoreAll"?
@kean
kean / cURLDescription.swift
Last active April 28, 2022 09:55
test-octokit 2
extension URLRequest {
public func cURLDescription() -> String {
guard let url = url, let method = httpMethod else {
return "$ curl command generation failed"
}
var components = ["curl -v"]
components.append("-X \(method)")
for header in allHTTPHeaderFields ?? [:] {
let escapedValue = header.value.replacingOccurrences(of: "\"", with: "\\\"")
components.append("-H \"\(header.key): \(escapedValue)\"")
@kean
kean / EnvironmentPicker.swift
Created November 21, 2021 03:06
Code from "Designing an API Client in Swift"
enum APIEnvironment: String, CaseIterable {
case dev
case test
case stage
case prod
}
struct ContentView: View {
@State private var selection: APIEnvironment? = env