Skip to content

Instantly share code, notes, and snippets.

View m25lazi's full-sized avatar

Mohammed Lazim m25lazi

View GitHub Profile
@m25lazi
m25lazi / ReadEnvironmentVariable.swift
Created January 7, 2020 15:18
Snippet for reading environment variables.
var shouldLogTextAnalyzer = false
if ProcessInfo.processInfo.environment["text_analyzer_log"] == "verbose" {
shouldLogTextAnalyzer = true
}
if shouldLogTextAnalyzer {
/// Actual logger code
}
@m25lazi
m25lazi / ReadEnvironmentVariable.swift
Created January 7, 2020 15:18
Snippet for reading environment variables.
var shouldLogTextAnalyzer = false
if ProcessInfo.processInfo.environment["text_analyzer_log"] == "verbose" {
shouldLogTextAnalyzer = true
}
if shouldLogTextAnalyzer {
/// Actual logger code
}
@m25lazi
m25lazi / VerticalScrollableStackViewController.swift
Created March 21, 2019 05:13
Sample implementation of vertically scrollable stack view
import UIKit
class VerticalScrollableStackViewController: UIViewController {
let scrollView: UIScrollView = UIScrollView(frame: .zero)
let stackView: UIStackView = UIStackView(frame: .zero)
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .gray
// Pure function
func sum(_ num1: Int, _ num2: int) -> Int {
return num1 + num2
}
// Impure function
var accumulated = 0
func add(_ num: Int) -> Int {
accumulated = num + accumulated
/// Mock Subclass of ImageDownloader
/// Fetch API will return what you were passed while init.
internal class MockImageDownloader: ImageFetcher {
typealias MockResponse = (UIImage?, Error?)
/// Mocked response. Pass this as part of init and this will be the response of fetch API
let mockedResponse: MockResponse
init(url: URL, response: MockResponse) {
/// Interface that fetches image from a source.
/// Image can be fetched from a web service or even from a local source.
internal protocol ImageFetcher {
func fetch(then completion: @escaping (UIImage?, Error?) -> Void)
}
/// ImageFetcher implementation to fetch image from web service
internal class ImageDownloader: ImageFetcher {
let url: URL
init(url: URL) {
/// Interface that fetches image from a source.
/// Image can be fetched from a web service or even from a local source.
internal protocol ImageFetcher {
func fetch(then completion: @escaping (UIImage?, Error?) -> Void)
}
/// User Model
public class User {
// Stores the
public var name: String
/// Mock Subclass of ImageDownloader
/// Fetch API will return what you were passed while init.
internal class MockImageDownloader: ImageDownloader {
typealias MockResponse = (UIImage?, Error?)
/// Mocked response. Pass this as part of init and this will be the response of fetch API
let mockedResponse: MockResponse
init(url: URL, response: MockResponse) {
/// Injecting fetcher to the fetch API, so that we get control of downloading outside of the User class
func fetchProfileImage(fetcher: ImageDownloader, then completion: @escaping (Bool)-> Void) {
fetcher.fetch { (image, error) in
if let fetchError = error {
print("Failed to fetch Image with error - \(fetchError)")
completion(false)
} else if let fetchedImage = image {
print("Image fetched")
self.iProfileImage = fetchedImage
completion(true)
@m25lazi
m25lazi / User-1.swift
Last active September 15, 2018 07:18
import Foundation
import UIKit
internal class ImageDownloader {
let url: URL
init(url: URL) {
self.url = url
}
func fetch(then completion: @escaping (UIImage?, Error?) -> Void) {