Skip to content

Instantly share code, notes, and snippets.

View strzempa's full-sized avatar

Patryk Strzemiecki strzempa

View GitHub Profile
{
"basics": {
"name": "John Doe",
"label": "Programmer",
"picture": "http://s3.amazonaws.com/37assets/svn/765-default-avatar.png",
"summary": "A summary of John Doe..."
},
"work": [
{
"image": "http://webfeb.in/wp-content/uploads/2016/11/logo-design-for-it-company.jpg",
@strzempa
strzempa / ProgressObservableWebViewController.swift
Last active October 11, 2019 05:22
WKWebViewController with secure progress observation, progressView and activityIndicatorView
import UIKit
import WebKit
open class ProgressObservableWebViewController: UIViewController, WKWebViewProgressObservable {
@IBOutlet weak public var webView: WKWebView!
public var webViewProgressObservation: Any?
private let progressView: UIProgressView = {
let view = UIProgressView(progressViewStyle: .default)
@strzempa
strzempa / ForegroundBackgroundObservable.swift
Created October 11, 2019 05:20
A way to observe .willEnterForegroundNotification and .didEnterBackgroundNotification with no potential memory issues
import UIKit
public protocol EnterForegroundObservable: AnyObject {
var applicationWillEnterForegroundObservation: Any? { get set }
typealias ForegroundBackgroundObservationCompletionHandler = () -> Void
func applicationWillEnterForegroundObservation(completion: ForegroundBackgroundObservationCompletionHandler?)
}
public extension EnterForegroundObservable {
func applicationWillEnterForegroundObservation(completion: ForegroundBackgroundObservationCompletionHandler?) {
public extension String {
func slice(from firstString: String, to lastString: String) -> String? {
return (range(of: firstString)?.upperBound).flatMap { substringFrom in
(range(of: lastString, range: substringFrom..<endIndex)?.lowerBound).map { substringTo in
String(self[substringFrom..<substringTo])
}
}
}
}
import UIKit
public extension UINavigationController {
func popBack(to vc: AnyClass, animated: Bool = true) {
guard let elementFound = (viewControllers.filter { $0.isKind(of: vc) }).first else {
assertionFailure("cannot pop back to \(vc) as it is not in the view hierarchy")
return
}
self.popToViewController(elementFound, animated: animated)
}
import UIKit
public extension UIToolbar {
static func toolbarDone(text: String = "Done", target: Any?, action: Selector?) -> UIToolbar {
let toolbar = UIToolbar()
toolbar.sizeToFit()
let doneButton = UIBarButtonItem(title: text, style: .plain, target: target, action: action)
toolbar.setItems([doneButton], animated: false)
return toolbar
}
import Foundation
public extension UIApplication {
class func tryURL(_ urls: [String]) {
let application = UIApplication.shared
urls.forEach { urlString in
guard let url = URL(string: urlString) else {
Logger.error?.message("Cannot make url from: \(urlString)")
return
}
import Foundation
public extension Bundle {
var releaseVersionNumber: String? {
return infoDictionary?["CFBundleShortVersionString"] as? String
}
var buildVersionNumber: String? {
return infoDictionary?["CFBundleVersion"] as? String
}
}
import UIKit
public protocol UITableViewHeaderFooterViewIdentifiable: UITableViewHeaderFooterView {
static var identifier: String { get }
}
public extension UITableViewHeaderFooterViewIdentifiable where Self: UITableViewHeaderFooterView {
static var identifier: String {
return String(describing: self)
}
import UIKit
public protocol UICollectionViewCellIdentifiable: UICollectionViewCell {
static var identifier: String { get }
}
public extension UICollectionViewCellIdentifiable where Self: UICollectionViewCell {
static var identifier: String {
return String(describing: self)
}