Coder Social home page Coder Social logo

triforkswiftextensions's People

Contributors

dependabot[bot] avatar jhntrifork avatar kimdv avatar ktvtrifork avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

triforkswiftextensions's Issues

Globally queued UIAlertViewControllers

Requested (and implemented) by @ktvtrifork :

//
//  GlobalDialog.swift
//  Created by Kasper Tvede on 18/02/2019.
//  Copyright ยฉ 2019 Trifork a/s. All rights reserved.
//

// inspiration drawn from https://github.com/agilityvision/FFGlobalAlertController
//  && https://github.com/lostatseajoshua/Alerts/blob/master/Alerts/AlertCoordinator.swift
// and further see https://stackoverflow.com/a/25428409
import Foundation
import UIKit

// Declare a global var to produce a unique address as the assoc object handle for the alert window variable
private var alertWindowObjectHandle: UInt8 = 0
// Declare a global var to produce a unique address as the assoc object handle for the dialog id variable
private var dialogIdObjectHandle: UInt8 = 1


//static variables for functionality; kept private as this is quite edgy already.
private var numOfGlobalDialogsShowing: Int = 0

private var dialogIdSet: Set<Int> = Set()


// MARK: - 
extension UIAlertController {
    
    /// This creates a property on a ui alert controller via objective C.
    private var alertWindow: UIWindow? {
        get {
            return objc_getAssociatedObject(self, &alertWindowObjectHandle) as? UIWindow
        }
        set {
            objc_setAssociatedObject(self, &alertWindowObjectHandle, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    /// This creates a property on a ui alert controller via objective C.
    private var dialogId: Int? {
        get {
            return objc_getAssociatedObject(self, &dialogIdObjectHandle) as? Int
        }
        set {
            objc_setAssociatedObject(self, &dialogIdObjectHandle, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    //can safely be called from any thread, and anywhere. multiple calls will just stack up.
    func showGlobally(animated: Bool = true) {
        DispatchQueue.main.async {
            numOfGlobalDialogsShowing += 1
            let window = UIWindow(frame: UIScreen.main.bounds)
            let rootController = UIViewController()
            window.rootViewController = rootController
            //above all else
            window.windowLevel = UIWindow.Level.alert + 1
            self.alertWindow = window
            window.makeKeyAndVisible()
            rootController.present(self, animated: animated)
        }
    }
    
    
    /// Only shows the igen dialog iff there are no global dialogs there already.
    ///
    /// - Parameter animated: true if the dialog should be animated in place
    func showGloballyIfNoGlobalIsPresented(animated: Bool = true) {
        if numOfGlobalDialogsShowing > 0 {
            return
        }
        showGlobally(animated: animated)
    }
    
    /// a more advanced editon; where we simply map each dialog to a given id, and iff that id is presented then skips displaying it (akk preventing dual
    ///   displaing these dialogs).
    ///
    /// - Parameters:
    ///   - id: the unique id, (so if mulitple dialogs contain this, then only the first one will be shown, prevents displaying multiple dialogs)
    ///   - animated: whenever the appearence should be animated
    func showGloballyWithId(id: Int, animated: Bool = true) {
        //force it though the main thread, thus ensuring no data raceing, since 1 thing can at max execute at the mainthread.
        DispatchQueue.main.async {
            if dialogIdSet.contains(id) {
                return
            }
            dialogIdSet.insert(id)
            self.dialogId = id
            self.showGlobally(animated: animated)
        }
    }
    //clean up when the dialog gets closed.
    override open func viewDidDisappear(_ animated: Bool) {
        super.viewDidAppear(animated)
        //cleanup
        numOfGlobalDialogsShowing -= 1
        if let id = dialogId {
            dialogIdSet.remove(id)
        }
        //dismiss all visible things.
        alertWindow?.isHidden = true
        //and make it possible to garbage collect
        alertWindow = nil
    }
    
    
}

CGRect midPoint

public var midPoint: CGPoint {
    return CGPoint(x: self.midX, y: self.midY)
}

Swiftlint to make style cohesive

A simple potentially very permissive, set of swiftlint rules to for example disallow force trys and to make some of the styling more cohesive.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.