Giter Site home page Giter Site logo

support selector about shorthandalert HOT 3 OPEN

mironal avatar mironal commented on May 23, 2024
support selector

from shorthandalert.

Comments (3)

kervich avatar kervich commented on May 23, 2024 1

I personally find the cancel() call a bit confusing when reading this code - as we act on a UIAlertController, I first resolve it to be a call that cancels the creation/presentation of the controller, it takes some thinking to realise it is called to create a 'Cancel' button. I think i'd be more comfortable seeing code like

UIAlertController(title: "Title", message: nil, preferredStyle: .actionSheet)
      .defaultAction("Some", selector: #selector(doSomething))
	  .otherAction("more", selector: #selector(doMore))
      .cancelAction("Nevermind")
      .present(in: self)

now this is more readable - notice that I omitted the target parameter, ad I find that this one can have a default value of either self or, even better, whatever is the object you present() it on. I guess in most cases the selectors will be defined in the calling class unless it's not a viewController or some other usual UI-class, so they're be mostly called on that VC (or its' parent in the cain of command) - an option of not passing the same thing everywhere is a plus. You can pass whatever object you need to call the selector on anyways. Same default of self could be implied on present(). One thing that needs to be taken care of here though - regardless of whether the object to call the selector on (or present in for that matter) is specified explicitly or implicated by a default value, some check, preferably compile-time, needs to be done to figure out if calling that selector is possible (same goes to present(on))

from shorthandalert.

mironal avatar mironal commented on May 23, 2024

@kervich
Thanks for your comments. And this opinion of code readability is constructive for me because Iā€™m a not English speaker. šŸ˜ƒ

And now, I understand your comments and a code you provided is very clear and readable. But I thought it was a bit redundant. šŸ¤”

I do not want to write the same word repeatedly as much as possible, like "Action".

However, the idea of omitting target is excellent; I will refer to it. šŸ‘ šŸ‘

Iā€™d like to rewrite a bit of code and comment here again based on your opinion.

Although story changes; how do you feel about the readability of already published the extension of UIAlertController and AlertBuilder? Is this also confusing?

Such as

AlertBuilder()
    .confirm(title: nil, message: "Some message")
    .approve()
    .present(in: self)

// or 

UIAlertController(title: "Alert title", message: nil, preferredStyle: .alert)
    .ok()
    .cancel()
    .present(in: self)

from shorthandalert.

mironal avatar mironal commented on May 23, 2024

The second proposal šŸ™‹ā€ā™‚ļø

I did not change the method name because I cherished the consistency with already published method names.

UIAlertController(title: "Title", message: "Some message.", preferredStyle: .actionSheet)
             // implicit self ver.
            .default("DefaultAction", whenSelect: #selector(pushDefaultAction(_:)))
            // or specific self ver.
            .default("DefaultAction", with: otherTarget, whenSelect: #selector(pushDefaultAction(_:))) 
            // implicit self ver.
            .cancel(whenSelect: #selector(pushCancel(_:))) 
            // or specific self ver.
            .cancel("Custom Cancel", with: otherTarget, whenSelect: #selector(pushCancel(_:)))
            .present(in: self)

The detail implementation of UIAlertController extension

extension UIAlertController {

    private func perform(to target: NSObjectProtocol?, selector: Selector, with action: UIAlertAction) {
        guard let target: NSObjectProtocol = target ?? self.presentingViewController, target.responds(to: selector) else {
            fatalError("error")
        }
        target.perform(selector, with: action, with: textFields)
    }

    public func `default`(_ title: String, with target: NSObjectProtocol? = nil, whenSelect selector: Selector) -> Self {
        return `default`(title) { self.perform(to: target, selector: selector, with: $0) }
    }

    public func destructive(_ title: String, with target: NSObjectProtocol? = nil, whenSelect selector: Selector) -> Self {
        return destructive(title) { self.perform(to: target, selector: selector, with: $0) }
    }

    public func cancel(_ title: String = "Cancel", with target: NSObjectProtocol? = nil, whenSelect selector: Selector) -> Self {
        return cancel(title) { self.perform(to: target, selector: selector, with: $0) }
    }
}

from shorthandalert.

Related Issues (6)

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.