Skip to content

Instantly share code, notes, and snippets.

@mjhassan
mjhassan / classWithHooks.js
Created October 27, 2020 03:04
Hook usage in class
function withHook(Component) {
return function WrappedComponent(props) {
const useReducer = React.useReducer(reducer, initialState);
return <Component {...props} useReducer={useReducer}/>
}
}
@mjhassan
mjhassan / UIColorExtensions.swift
Created January 14, 2020 03:12
Convert any color format to UIColor
import UIKit
extension UIColor {
convenience init(r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat = 1.0) {
self.init(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: a)
}
convenience init(red: Int, green: Int, blue: Int, a: Int = 0xFF) {
assert(red >= 0 && red <= 255, "Invalid red component")
assert(green >= 0 && green <= 255, "Invalid green component")
@mjhassan
mjhassan / .gitignore
Created January 13, 2020 11:42
Generic git ignore file for Xcode, Swift, ObjC & MacOSX
# Created by https://www.gitignore.io/api/osx,swift,objective-c,xcode
# Edit at https://www.gitignore.io/?templates=osx,swift,objective-c,xcode
### Objective-C ###
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
## Build generated
@mjhassan
mjhassan / StringSlicingExtesions.swift
Created October 26, 2019 13:36
String sclicing make easy
extension String {
public subscript (range: PartialRangeFrom<Int>) -> Substring {
return self[index(startIndex, offsetBy: range.lowerBound)..<endIndex]
}
public subscript (range: PartialRangeUpTo<Int>) -> Substring {
return self[startIndex..<index(endIndex, offsetBy: range.upperBound)]
}
public subscript (range: PartialRangeThrough<Int>) -> Substring {
URLCache.shared = {
let cacheDirectory = (NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0] as String).appendingFormat("/\(Bundle.main.bundleIdentifier ?? "cache")/" )
return URLCache(memoryCapacity: /*your_desired_memory_cache_size*/,
diskCapacity: /*your_desired_disk_cache_size*/,
diskPath: cacheDirectory)
}()
@mjhassan
mjhassan / AppDelegate.swift
Created September 3, 2019 04:48
way to config shared URLCache from AppDelegate
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let diskCacheSize = 500*1024*1024 // 500MB
@mjhassan
mjhassan / URLCacheExtension.swift
Last active September 3, 2019 04:48
Shared url cache configuration
extension URLCache {
static func configSharedCache(directory: String? = Bundle.main.bundleIdentifier, memory: Int = 0, disk: Int = 0) {
URLCache.shared = {
let cacheDirectory = (NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0] as String).appendingFormat("/\(directory ?? "cache")/" )
return URLCache(memoryCapacity: memory, diskCapacity: disk, diskPath: cacheDirectory)
}()
}
}
@mjhassan
mjhassan / UIAsyncImageView-URLCache.swift
Created September 3, 2019 04:44
Simple custom image view to load image asynchronously, uses URLCache
class UIAsyncImageView: UIImageView {
// Associated Object property; implementation omitted.
final private var imageUrl: URL?
func loadImage(from urlString: String, placeholder: UIImage? = nil) {
setImage(placeholder)
guard let url = URL(string: urlString) else {
return
}
@mjhassan
mjhassan / UIAsyncImageView-NSCache.swift
Created September 3, 2019 04:42
Simple custom image view to load image asynchronously
class UIAsyncImageView: UIImageView {
static let imageCache = NSCache<AnyObject, AnyObject>()
// Associated Object property; implementation omitted.
final private var imageUrl: URL?
func loadImage(from urlString: String, placeholder: UIImage? = nil) {
setImage(placeholder)
guard let url = URL(string: urlString) else {
@mjhassan
mjhassan / APIService-URLCache.swift
Created September 3, 2019 04:41
Simple service class template for URLCache example
class APIService: ServiceProtocol {
func get( url: URL, callback: @escaping (_ data: Data?, _ error: Error?) -> Void ) {
let request = URLRequest(url: url, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 30)
URLSession.shared.dataTask(with: request) { (data, _, error) in
callback(data, error)
}.resume()
}
}