Skip to content

Instantly share code, notes, and snippets.

View gtokman's full-sized avatar
🏴‍☠️
Focusing

Gary Tokman gtokman

🏴‍☠️
Focusing
View GitHub Profile
import SwiftUI
struct Marquee<Content: View>: View {
@ViewBuilder var content: Content
@State private var containerWidth: CGFloat? = nil
@State private var model: MarqueeModel
private var targetVelocity: Double
private var spacing: CGFloat
init(targetVelocity: Double, spacing: CGFloat = 10, @ViewBuilder content: () -> Content) {
@gtokman
gtokman / RecyclingScrollingLazyView.swift
Created March 9, 2025 15:22
RecyclingScrollingLazyView
import SwiftUI
// https://nilcoalescing.com/blog/
@available(iOS 18.0, *)
struct RecyclingScrollingLazyView<
ID: Hashable, Content: View
>: View {
let rowIDs: [ID]
let rowHeight: CGFloat
@ViewBuilder
//
// YelpApiService.swift
// Foodee
//
// Created by Gary Tokman on 9/21/21.
//
import Combine
import CoreLocation
import Foundation
struct ContentView: View {
let viewModel: ViewModel
@State var businesses = [Business]()
var body: some View {
NavigationView {
ZStack {
if businesses.isEmpty {
ProgressView("Loading...")
}
struct ViewModel {
let apiService: ApiService
let locationService: LocationService // fetch location data
/// Fetch businesses from service for term
func getBusinesses(for term: String) async throws -> [Business] {
return try
await apiService
.businesses(
.search(term: term, location: locationService.current())
/// Fetch API data for `Endpoint`
struct ApiService {
let businesses: (YelpEndpoint) async throws -> [Business]
}
extension ApiService {
/// Live service used in App
static let live = ApiService { endpoint in
let (data, error) = try await URLSession.shared.data(for: endpoint.request)
let decoder = JSONDecoder()
extension YelpEndpoint {
/// Endpoint URL Path
var path: String {
switch self {
case .search:
return "/v3/businesses/search"
case let .detail(id):
return "/v3/businesses/\(id)"
}
}
import CoreLocation
/// Endpoints for Yelp
enum YelpEndpoint {
case search(term: String, location: CLLocation)
case detail(id: String)
}
Task {
let (data, _) = try! await session.data(for: request)
let result = try! JSONDecoder().decode(SearchResult.self, from: data)
self.businesses = result.businesses
}
NavigationView {
List {
ForEach(businesses) { business in
Text(business.name)
}
}
.listStyle(.plain)
.navigationTitle(Text("Boston"))
.refreshable {
// do something