Skip to content

Instantly share code, notes, and snippets.

View levochkaa's full-sized avatar
👽

Lev Poznyakov levochkaa

👽
View GitHub Profile
@levochkaa
levochkaa / MapView.swift
Created February 5, 2024 10:41
touchesBegan/touchesEnded for Apple's MKMapView in SwiftUI
import SwiftUI
import MapKit
struct ContentView: View {
@State var region = MKCoordinateRegion()
@State var touching = false
var body: some View {
MapView(
region: $region,
@levochkaa
levochkaa / Observable.swift
Created January 25, 2024 15:24
Examples of View redrawing in SwiftUI
@Observable
class VM {
var counter = 0
}
struct ContentView: View {
@State var vm = VM()
var body: some View {
// doesn't get executed on button tap
@levochkaa
levochkaa / TaskQueue.swift
Created November 3, 2023 10:57
TaskQueue
final class TaskQueue: Sendable {
private actor TaskQueueActor {
private var blocks = [() async -> Void]()
private var currentTask: Task<Void, Never>?
func addBlock(priority: TaskPriority? = nil, block: @escaping @Sendable () async -> Void) {
blocks.append(block)
next(priority: priority)
}
@levochkaa
levochkaa / ContentView.swift
Last active August 28, 2023 10:33
iOS 17, ScrollView, .scrollTargetLayout() modifier example
import SwiftUI
struct ContentView: View {
@State var data: [String] = (0 ..< 25).map { String($0) }
@State var dataID: String?
var dataIDText: String {
dataID.map(String.init(describing:)) ?? "None"
}
@levochkaa
levochkaa / CarouselView.swift
Created August 23, 2023 13:50
CarouselView in SwiftUI
import Combine
import SwiftUI
// https://stackoverflow.com/questions/58896661/swiftui-create-image-slider-with-dots-as-indicators
struct CarouselView<Content>: View where Content: View {
let maxIndex: Int
var content: () -> Content
@State private var offset = CGFloat.zero
@State private var dragging = false
@levochkaa
levochkaa / PublishedAppStorage.swift
Last active October 10, 2023 16:00
@PublishedAppStorage
// PublishedAppStorage.swift
import Combine
import SwiftUI
@propertyWrapper
struct PublishedAppStorage<Value> {
@UserDefault private var storedValue: Value
private var publisher: Publisher?
@levochkaa
levochkaa / View+CustomContextMenu.swift
Last active November 1, 2024 14:03
CustomContextMenu
// View+CustomContextMenu.swift
extension View {
func customContextMenu(
cornerRadius: CGFloat = 0,
_ actions: [UIAction] = [],
didTapPreview: @escaping () -> Void = {},
onAppear: @escaping () -> Void = {},
onDisappear: @escaping () -> Void = {}
) -> some View {
@levochkaa
levochkaa / custom-reviewable-styles.css
Created March 10, 2023 09:58
Custom Reviewable Styles
.review-page .file.root .file .wavy.edge {
background-image: none;
}
.review-page .file.root .file .top.wavy.edge {
margin-top: 1em;
margin-bottom: -0.5em;
}
.review-page .file.root .file .bottom.wavy.edge {
@levochkaa
levochkaa / View+OnVisible.swift
Created February 19, 2023 17:02
To check, when the view is visible
struct VisibleKey: PreferenceKey {
static var defaultValue: Bool = false
static func reduce(value: inout Bool, nextValue: () -> Bool) { }
}
private struct OnVisible: ViewModifier {
@State var action: (() -> Void)?
func body(content: Content) -> some View {
@levochkaa
levochkaa / TaskQueue.swift
Created February 13, 2023 12:42
Blocking Task
// TaskQueue.swift
import Foundation
class TaskQueue {
private actor TaskQueueActor {
private var blocks = [() async -> Void]()
private var currentTask: Task<Void, Never>?
func addBlock(priority: TaskPriority? = nil, block: @escaping () async -> Void) {