Skip to content

Instantly share code, notes, and snippets.

View lamprosg's full-sized avatar

Lampros Giampouras lamprosg

  • Athens, Greece
View GitHub Profile
@lamprosg
lamprosg / 1.AsyncAwait.swift
Last active July 2, 2021 09:46
(iOS) Swift 5.5. async await
//https://www.hackingwithswift.com/articles/233/whats-new-in-swift-5-5
// - OLD WAY
func fetchWeatherHistory(completion: @escaping ([Double]) -> Void) {
// Complex networking code here; we'll just send back 100,000 random temperatures
DispatchQueue.global().async {
let results = (1...100_000).map { _ in Double.random(in: -10...30) }
completion(results)
}
@lamprosg
lamprosg / OnboardingCoordinator.swift
Last active February 7, 2021 19:42
(iOS) Onboarding flow
/// The list of the onboarding steps
enum OnboardingStep: String {
case overview
case documentScan
case saveAndLogin
}
/// The onboarding errors
///
/// - failed: Onboarding failed with the described error
@lamprosg
lamprosg / SwiftUIWithVIP.swift
Last active September 20, 2021 10:40
(iOS) - SwiftUI with VIP
import SwiftUI
@main
struct MyMainApp: App {
var body: some Scene {
WindowGroup {
MainView.build()
}
}
}
@lamprosg
lamprosg / CommonAncester.swift
Last active December 26, 2020 14:46
(iOS) Find common ancestor of two UIViews
//Time complexity:O(N), Space complexity: (1)
func findCommonSuper(_ view1:inout UIView, _ view2:inout UIView) -> UIView? {
//Get the level of the 2 views
var level1 = findLevel(view1)
var level2 = findLevel(view2)
/****/
//Find the 2 views for the same level
if level1 > level2 {
@lamprosg
lamprosg / 0.Description.swift
Last active November 28, 2020 17:13
(iOS) SwiftUI reusable views
/*
Content loading is at least three stage process:
- Before the loading starts there is the initial moment.
- The actual process of loading content. We need to present some progress or activity indication in the UI;
- And finally the result, success or failure.
SwiftUI encourages creating small, reusable views, and use composition to create the complete picture.
Each stage of the content loading process will require a view. The container view will compose the result.
*/
@lamprosg
lamprosg / Fingerprinting.swift
Last active July 28, 2023 14:03
(iOS) Fingerprinting, identifying a device
//https://nshipster.com/device-identifiers/
//Besides identifierForVendor..
/*
Locale information is the greatest source of identifying information on Apple platforms.
The combination of your preferred languages, region, calendar, time zone,
and which keyboards you have installed say a lot about who you are
especially if you have less conventional preferences.
*/
@lamprosg
lamprosg / UIImage+Darkness.swift
Created June 24, 2020 10:17
(iOS) Check image darkness
import UIKit
// Call CGImage mechanism from a UIImage
@objc extension UIImage {
var isDark: Bool {
self.cgImage?.isDark ?? false
}
}
@lamprosg
lamprosg / Clean Swift
Last active May 1, 2020 09:47
(iOS) VIP / Clean Swift
VIP / Clean Swift
//https://zonneveld.dev/the-clean-swift-architecture-explained/
-------------------
VC -> Asks the Interactor for an action.
Interactor -> Business logic | Can write unit tests against it.
Will also handle requests and return object to the presenter
Presenter -> Presentation | Parse the object to a view model and give it to the VC to be displayed.
@lamprosg
lamprosg / SomeVC.swift
Last active May 1, 2020 10:01
(iOS) Override loadView()
//https://swiftrocks.com/writing-cleaner-view-code-by-overriding-loadview.html
/// The HasCustomView protocol defines a customView property for UIViewControllers to be used in exchange of the regular view property.
/// In order for this to work, you have to provide a custom view to your UIViewController at the loadView() method.
public protocol HasCustomView {
associatedtype CustomView: UIView
}
extension HasCustomView where Self: UIViewController {
/// The UIViewController's custom view.
@lamprosg
lamprosg / SwiftEventHandler.swift
Last active December 8, 2019 18:14
(iOS) Swift only KVO alternative, event handlers
//Documentation
//https://blog.scottlogic.com/2015/02/05/swift-events.html
//KVO example (not shown in this gist)
//https://blog.scottlogic.com/2015/02/11/swift-kvo-alternatives.html
public class Event<T> {
public typealias EventHandler = T -> ()