Skip to content

Instantly share code, notes, and snippets.

View SergLam's full-sized avatar
😊
Try to make your code better!

Serhii Liamtsev SergLam

😊
Try to make your code better!
View GitHub Profile
@SergLam
SergLam / CustomNavigationController.swift
Last active February 6, 2024 09:23
Custom screen transition with pan gesture from the center of screen
import UIKit
// Fade animation: https://medium.com/@ludvigeriksson/custom-interactive-uinavigationcontroller-transition-animations-in-swift-4-a4b5e0cefb1e
// Slide animation: https://medium.com/swift2go/simple-custom-uinavigationcontroller-transitions-fdb56a217dd8
class CustomNavigationController: UINavigationController {
private var interactionController: UIPercentDrivenInteractiveTransition?
private var edgeSwipeGestureRecognizer: UIScreenEdgePanGestureRecognizer?
private var panGesturerecognizer: PanDirectionGestureRecognizer?
@SergLam
SergLam / AppVersion.swift
Created November 20, 2019 08:15
Check iOS app version info via App Store request
// Perform URL request
// http://itunes.apple.com/lookup?bundleId=YOUR_APP_BUNDLE_ID
// TXT-file with JSON data will be downloaded
// Parse it, fetch version key-value
// "version":"1.5"
// Compare it with your current app version
let appVersion = Bundle.main.infoDictionary["CFBundleShortVersionString"] as? String
let buildNumber = Bundle.main.infoDictionary["CFBundleVersion"] as? String
@SergLam
SergLam / String+Ext.swift
Last active November 19, 2019 11:49
String+Ext - Validation and ect. things
import var CommonCrypto.CC_MD5_DIGEST_LENGTH
import func CommonCrypto.CC_MD5
import typealias CommonCrypto.CC_LONG
import Foundation
import UIKit
enum UserCredentialError: Error, LocalizedError {
case invalidEmail
@SergLam
SergLam / LayerContainerView.swift
Created November 18, 2019 19:09
UIView + CALayer - right way to handle gradient layer size updates
// More info here
// https://marcosantadev.com/calayer-auto-layout-swift/
class LayerContainerView: UIView {
override public class var layerClass: Swift.AnyClass {
return CAGradientLayer.self
}
override func awakeFromNib() {
@SergLam
SergLam / Codable+Ext.swift
Last active March 17, 2020 06:01
Swift - Codable - magic parsing, ignoring JS types issues
import Foundation
enum CodingError: LocalizedError, Error {
case unableToDecode(key: String, type: String)
var errorDescription: String? {
switch self {
case .unableToDecode(let key, let type):
return Localizable.errorParsingKey(key, type)
@SergLam
SergLam / MoyaProvider+Ext.swift
Created November 16, 2019 13:49
Refresh token handling with Moya
import Alamofire
import Foundation
import Moya
extension MoyaProvider {
convenience init(handleRefreshToken: Bool) {
if handleRefreshToken {
self.init(requestClosure: MoyaProvider.endpointResolver(),
manager: DefaultAlamofireManager.sharedManager,
@SergLam
SergLam / AppDelegate.swift
Last active January 11, 2021 22:21
Realm DataManager - generic operations + migration setup + thread-safe(!!!) read-write async operations
@UIApplicationMain
final class AppDelegate: UIResponder {
override init() {
RealmDAO.shared.configureMigration()
super.init()
}
}
@SergLam
SergLam / Data+Ext.swift
Created October 30, 2019 12:40
Convert device token to string - Swift 5.1
import Foundation
// IMPORTANT: check APS environment (production or sandbox) in your app .entitlements file
extension Data {
func hexString() -> String {
return self.map{ String(format: "%02.2hhx", $0) }.joined()
}
@SergLam
SergLam / AnimationView.swift
Last active October 24, 2019 11:28
Display sequence of images as an animation
import SnapKit
import UIKit
class AnimationView: UIView {
private let duration: Double = 3.0
private var percent: Double = 0.0
private var displayLink: CADisplayLink?
private var start: CFAbsoluteTime = CFAbsoluteTime.zero
@SergLam
SergLam / AppVersionLaunchScreen.sh
Created October 7, 2019 11:19
AppVersion Launch screen
# Output version & build number to the APP_VERSION label on LaunchScreen.storyboard
versionNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${INFOPLIST_FILE}")
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${INFOPLIST_FILE}")
sed -i "" -e "/userLabel=\"APP_VERSION\"/s/text=\"[^\"]*\"/text=\"Version: $versionNumber ($buildNumber)\"/" "$PROJECT_DIR/LaunchScreen.storyboard"