Skip to content

Instantly share code, notes, and snippets.

View levochkaa's full-sized avatar
👽

Lev Poznyakov levochkaa

👽
View GitHub Profile
@levochkaa
levochkaa / CarouselView.swift
Created September 14, 2022 15:54
Easy implementaion of a Snap Carousel in SwiftUI
import SwiftUI
struct CarouselView: View {
@State private var currentCardIndex = 2
@GestureState private var dragOffset: CGFloat = 0
@State private var sampleArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
var body: some View {
HStack(spacing: 0) {
@levochkaa
levochkaa / Logger.swift
Created January 14, 2023 22:05
Best Logger in Swift ever (and the easiest to use)
// Logger.swift
import Foundation
import os.log
// swiftlint:disable line_length
func log(_ messages: Any..., file: String = #fileID, function: String = #function, line: Int = #line) {
// actually impossible cases
guard let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String else {
return print("Unable to get App Name")
@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) {
@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 / 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+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 / 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 / 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 / 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 / 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)
}