Skip to content

Instantly share code, notes, and snippets.

View lanserxt's full-sized avatar
🖥️
Developing a dream

Anton Gubarenko lanserxt

🖥️
Developing a dream
View GitHub Profile
@lanserxt
lanserxt / Task.swift
Created May 17, 2023 12:15
UICollection model extension to support extra model
//We have a UICollectionView with Grid layout that shows a collection card.
//Model for a card looks like this:
struct CollectionViewCellModel: Codable {
let id: Int
let image: String
let imageUrl: String
let name: String
let description: String
let stocksAmount: Int
@lanserxt
lanserxt / LoggableButton.swift
Created April 24, 2023 07:05
SwiftUI Button with events logging
import SwiftUI
struct LoggableButton<Label> : View where Label : View {
let eventName: String
let action: () -> Void
let label: () -> Label
var body: some View {
Button {
//Handel event log
@lanserxt
lanserxt / ColorSliderView.swift
Created January 24, 2023 21:23
GradientColorPicker+SwiftUI
struct ColorSliderView: View {
//Picked Color
@State
private var location: CGPoint = CGPoint(x: 0, y: 0) {
didSet {
//Need https://gist.githubusercontent.com/marchinram/3675efc96bf1cc2c02a5/raw/ec95113fa3cf1a6e97361426dc7574d9e14a09c0/UIImage+PixelColor.swift
currentColor = trackImage[Int(location.x), 0] ?? .white
@lanserxt
lanserxt / Firebase+Tools.swift
Created July 6, 2020 13:29
Firebase document decoding to Swift Object
func resolveFirebaseObject<T: Codable>(_ snapshot: DocumentSnapshot) -> T? {
guard snapshot.exists else {return nil}
if let data = try? JSONSerialization.data(withJSONObject: snapshot.data(), options: []) {
do {
return try self.decoder.decode(T.self, from: data)
} catch {
print("Decoding error: \(error)")
}
}
return nil
@lanserxt
lanserxt / FMBlurable.swift
Created January 22, 2020 11:43
FMBlurable (Swift 5 update)
//
// FMBlurable.swift
// FMBlurable
//
// Created by SIMON_NON_ADMIN on 18/09/2015.
// Copyright © 2015 Simon Gladman. All rights reserved.
//
// Thanks to romainmenke (https://twitter.com/romainmenke) for hint on a larger sample...
import UIKit
@lanserxt
lanserxt / QuadricEquationRootsPrint.swift
Last active October 10, 2019 12:02
Quadric equation roots printing method. There were to restrictions on type and ranges so it's a basic example.
func printRoots(a: Double, b: Double, c: Double, showImaginaryRoots: Bool = false) {
let b2 = b * b
let disc = b2 - (4 * a * c)
let discSqrt = sqrt(fabs(disc))
let isImage = disc < 0
let result = b * b - 4.0 * a * c
@lanserxt
lanserxt / Podfile
Created April 20, 2019 05:32
Cocoapods Swift version setup for specific Pod
#insert after all pods declaring (just put at the bottom of a file)
post_install do |installer|
installer.pods_project.targets.each do |target|
if target.name == 'POD_NAME_HERE'
target.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = '4.0'
end
end
end
end
@lanserxt
lanserxt / pi_monte_carlo.swift
Last active March 27, 2019 07:42
Pi approximate calculation by Monte Carlo method. Inspired by https://www.weheartswift.com/random-pi/.
func piCalc(_ totalPoints: Int) -> Double {
var nPointsInside = 0
for _ in 1...totalPoints {
let (x, y) = (drand48() * 2 - 1, drand48() * 2 - 1)
if x * x + y * y <= 1 {
nPointsInside += 1
}
}
return 4.0 * Double(nPointsInside) / Double(totalPoints)
}
@lanserxt
lanserxt / UINavigationController+Class.swift
Created January 15, 2019 11:39
UINavigationController extension to pop to specific class
extension UINavigationController {
func popToLastViewControllerType<T>(_ typeOfVC: T.Type) {
if let vc = viewControllers.last(where: {$0 is T}) {
popToViewController(vc, animated: true)
}
}
func popToFirstViewControllerType<T>(_ typeOfVC: T.Type) {
if let vc = viewControllers.first(where: {$0 is T}) {
popToViewController(vc, animated: true)
@lanserxt
lanserxt / CellBlurAnimator.swift
Last active December 28, 2018 12:54
Blur animator
fileprivate var propertyAnimator: UIViewPropertyAnimator!
fileprivate var blurEffectView: UIVisualEffectView!
fileprivate var backGradientView: UIView!
func addWorkoutBlur() {
if !UIAccessibilityIsReduceTransparencyEnabled() {
if blurEffectView != nil {
blurEffectView.removeFromSuperview()
}
if backGradientView != nil {