Skip to content

Instantly share code, notes, and snippets.

View Gagan5278's full-sized avatar

Gagan Vishal Mishra Gagan5278

View GitHub Profile
@Gagan5278
Gagan5278 / Obfuscator.swift
Created April 3, 2023 14:54
Swift Obfuscator
class Obfuscator {
// MARK: - Variables
/// The salt used to obfuscate and reveal the string.
private var salt: String
// MARK: - Initialization
init() {
@Gagan5278
Gagan5278 / BarButton Unit Test
Created February 25, 2023 01:03
iOS Unit Tests: Working with NavigationItems
import XCTest
@testable import WeatherApp
class ViewControllerTest: XCTestCase {
var systemUnderTest: ViewController!
override func setUp() {
super.setUp()
@Gagan5278
Gagan5278 / Logger.swift
Created February 7, 2023 17:37
iOS Simple Logger
class Logger {
private static let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return formatter
}()
static func info(_ messages: Any?..., file: String = #file, function: String = #function, line: Int = #line) {
printMessage(messages, state: "🟢 INFO", file: file, function: function, line: line)
extension UIView {
func addSubviews(_ views: UIView...) {
views.forEach{addSubview($0)}
}
}
##How to Use:
class YourViewController: UIViewController {
@Gagan5278
Gagan5278 / Downsampling.swift
Last active January 26, 2021 14:27
Reducing image size with Downsampling [Using ImageIO]
func downsample(imageAt imageURL: URL, to pointSize: CGSize, scale: CGFloat = UIScreen.main.scale) -> UIImage? {
// Create an CGImageSource that represent an image
let imageSourceOptions = [kCGImageSourceShouldCache: false] as CFDictionary
guard let imageSource = CGImageSourceCreateWithURL(imageURL as CFURL, imageSourceOptions) else {
return nil
}
// Calculate the desired dimension
let maxDimensionInPixels = max(pointSize.width, pointSize.height) * scale
@Gagan5278
Gagan5278 / UIImageView+Cache.swift
Created January 5, 2021 10:50
Cache image from server
extension UIImageView {
/// Loads image from web asynchronosly and caches it, in case you have to load url
/// again, it will be loaded from cache if available
func load(url: URL, placeholder: UIImage?, cache: URLCache? = nil) {
let cache = cache ?? URLCache.shared
let request = URLRequest(url: url)
if let data = cache.cachedResponse(for: request)?.data, let image = UIImage(data: data) {
self.image = image
} else {
self.image = placeholder
@Gagan5278
Gagan5278 / DefaultDecodingValues.swift
Created January 1, 2021 19:05
A Gist to decode SEREVR response if associated keys are not available in each fetch from server.
protocol DefaultSourceValue {
associatedtype Value: Decodable
static var defaultValue: Value {get}
}
enum DefaultValue {} //this will not allow for default init
extension DefaultValue {
@Gagan5278
Gagan5278 / Combine for Network layer
Last active January 26, 2021 14:29
Demonstrate how to use Combine framework for creating Network Layer (Please see https://dummyapi.io/)
//https://dummyapi.io/
import PlaygroundSupport
import Foundation
import Combine
// MARK: - Network Controller
protocol NetworkControllerProtocol: class {
typealias Headers = [String: Any]
func get<T>(type: T.Type,
@Gagan5278
Gagan5278 / gist:3871220b2dc24156132702e342e0fd80
Created August 13, 2020 18:17
Integrating P4MEGRE as diff tool and merge tool with GIT
[merge]
keepBackup = false
tool = p4merge
[mergetool "p4merge"]
cmd = /Applications/p4merge.app/Contents/Resources/launchp4merge "\"$PWD/$BASE\"" "\"$PWD/$REMOTE\"" "\"$PWD/$LOCAL\"" "\"$PWD/$MERGED\""
keepTemporaries = false
trustExitCode = false
keepBackup = false
[diff]
tool = p4merge
@Gagan5278
Gagan5278 / gist:8f53626aa778a2897b66f8f002a66623
Created August 13, 2020 09:38
An awesome code to Create a class instance (without a Class) from a function in Swift
typealias Balance = (add: ((Int) -> Void), get: (() -> Int), print: () -> ()) //Tupple. Tupple is value type but inside tupple we have function with referenc type.
func newBalance() -> Balance {
var balance = 0
return (
add: { newValue in balance += newValue },
get: { return balance },
print: { print("Your balance is \(balance)") }
)
}