Skip to content

Instantly share code, notes, and snippets.

View amfathi's full-sized avatar
🎧

Ahmed Fathi amfathi

🎧
View GitHub Profile
@amfathi
amfathi / RunningTime.swift
Last active April 15, 2021 14:22
A utility function to calculate and print the running time for a code block in milliseconds.
func runningTime(clousure: () -> Void) {
let start = DispatchTime.now()
clousure()
let end = DispatchTime.now()
let runTime = CLongDouble(end.uptimeNanoseconds - start.uptimeNanoseconds) / 1e6
print("runTime: \(runTime) ms")
}
@amfathi
amfathi / UIApplication+SetRootController.swift
Last active April 15, 2021 14:19
[Tutorial] Example on how use `UIView.transition()` to transition views into each others.
import UIKit
extension UIApplication {
static func setRootController(_ viewController: UIViewController) {
guard
let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let window = scene.windows.filter({ $0.isKeyWindow }).first
else { return }
window.rootViewController = viewController
@amfathi
amfathi / UIViewController+fromStoryboard.swift
Last active March 20, 2021 12:21
Utility method to load viewController from storyboard programmatically assuming that the storyboard has the same prefix as the viewController
import UIKit
extension UIViewController {
static func fromStoryboard(_ controllerIdentifier: String? = nil) -> Self? {
let name = String(describing: classForCoder())
let suffix = "ViewController"
let storyboardName = String(name.dropLast(suffix.count))
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
import UIKit
extension UIColor {
public convenience init?(hex: String) {
let r, g, b: CGFloat
if hex.hasPrefix("#") {
let start = hex.index(hex.startIndex, offsetBy: 1)
let hexColor = String(hex[start...])
@amfathi
amfathi / ProgressView.swift
Last active March 20, 2021 12:22
Convenient ProgressView in UIAlertController to block the UI till completion.
import UIKit
class ProgressView {
private lazy var progressView: UIProgressView = {
let progressView = UIProgressView(frame: CGRect(x: 15, y: 20, width: 240, height: 8))
return progressView
}()
private lazy var alert: UIAlertController = {
//
// CardPagingLayout.swift
//
// Created by Ahmed Fathi on 5/29/20.
// Copyright © 2020 Ahmed Fathi. All rights reserved.
//
import UIKit
class CardPagingLayout: UICollectionViewLayout {
// Source https://stackoverflow.com/a/54237526
// MARK: Source Answer
extension UITableView {
//Variable-height UITableView tableHeaderView with autolayout
func layoutTableHeaderView() {
guard let headerView = self.tableHeaderView else { return }
headerView.translatesAutoresizingMaskIntoConstraints = false
@amfathi
amfathi / Optional+Unwrap.swift
Created May 16, 2020 16:19
Handy extension for unwrap optionals
import Foundation
extension Optional where Wrapped == String {
/// Return true if the string value is not nil and not empty string
var exists: Bool {
switch self {
case .none:
return false
case .some(let string):
return !string.isEmpty
@amfathi
amfathi / throttle.swift
Last active April 15, 2021 14:20
[Tutorial] Naive throttling for a block from rapid execution for a specific period of time. Inspired by https://rxmarbles.com/#throttleTime
import Foundation
private var timer: Timer?
func throttle(_ interval: TimeInterval, block: @escaping (() -> Void)) {
timer?.invalidate()
timer = Timer.scheduledTimer(withTimeInterval: interval, repeats: false, block: { _ in
block()
})
}
@amfathi
amfathi / Sieve.swift
Last active April 21, 2020 08:58
Sieve of Eratosthenes to get primes below a specific positive integer.
/// All the primes number below & including 10e7 using Sieve algorithm O(n log log n).
/// https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
func sieve() -> [Int] {
let limit = 10000000 // 10e7
// Create an array assumping all numbers are primes
var isPrime = Array(repeating: true, count: limit)
// Mark trivial cases as not primes
isPrime[0] = false