Skip to content

Instantly share code, notes, and snippets.

View SpectralDragon's full-sized avatar
🎯
Focusing

Vladislav Prusakov SpectralDragon

🎯
Focusing
View GitHub Profile
@SpectralDragon
SpectralDragon / PreviewContextMenu.swift
Created April 8, 2020 19:22
Simple way to implement preview context menu for SwiftUI
//
// ContentView.swift
// PreviewSwiftUI
//
// Created by v.prusakov on 4/8/20.
// Copyright © 2020 v.prusakov. All rights reserved.
//
import SwiftUI
@SpectralDragon
SpectralDragon / Color+Extensions.swift
Created March 23, 2020 20:03
Get red/blue/green from SwiftUI Color
extension UIColor {
// default implementaion of hex parsing
public convenience init?(hex: String) {
let r, g, b, a: CGFloat
if hex.hasPrefix("#") {
let start = hex.index(hex.startIndex, offsetBy: 1)
let hexColor = String(hex[start...])
if hexColor.count == 8 {
@SpectralDragon
SpectralDragon / iOSToggleStyle.swift
Created February 3, 2020 11:44
iOS-like toggle style for SwiftUI. Because in MacOS we have another style for toggle
//
// iOSToggleStyle.swift
//
// Created by Vladislav Prusakov on 03.02.2020.
// Copyright © 2020 Vladislav Prusakov. All rights reserved.
//
import SwiftUI
struct iOSToggleStyle: ToggleStyle {
@SpectralDragon
SpectralDragon / UIDynamicProviderImage.swift
Created October 31, 2019 11:33
A class for dynamically update image using current trait collections. It's doesn't work for navigation bar and tab bar, because image doesn't update when trait collection did change.
public extension UIImage {
/// Creates a dynamic image that supports displaying a different image asset when dark mode is active.
static func dynamic(
light makeLight: @autoclosure () -> UIImage,
dark makeDark: @autoclosure () -> UIImage
) -> UIImage {
if #available(iOS 13, *) {
return UIDynamicProviderImage(light: makeLight(), dark: makeDark())
} else {
class RangeTree<T> {
private var value: [T]
private(set) var range: Range<Int>
private var leftLeaf: RangeTree<T>?
private var rightLeaf: RangeTree<T>?
convenience init(array: [T]) {
self.init(array: array, range: 0 ..< array.count)
}
@SpectralDragon
SpectralDragon / CompoundProgress.swift
Created July 6, 2018 16:15
Little solution for manage child progresses. Support Adding and Removing children and update totalUnitCount.
final class CompoundProgress: Progress {
private struct Child {
let progress: Progress
let cancellationObservation: NSKeyValueObservation
let completedUnitObservation: NSKeyValueObservation
func invalidate() {
self.cancellationObservation.invalidate()
self.completedUnitObservation.invalidate()