Skip to content

Priority enum#345

Merged
robertjpayne merged 9 commits into
SnapKit:developfrom
Shehryar:develop
Feb 11, 2017
Merged

Priority enum#345
robertjpayne merged 9 commits into
SnapKit:developfrom
Shehryar:develop

Conversation

@Shehryar

@Shehryar Shehryar commented Oct 4, 2016

Copy link
Copy Markdown
Contributor

This adds an enum for priority based constraints allowing users to write priority as:

.priority(level: .low)

Based on the issue #324

@Shehryar Shehryar mentioned this pull request Oct 4, 2016
3 tasks

@MP0w MP0w left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't approve as I'm not that experienced with the SnapKit codebase but the implementation LGTM, probably someone should decide if the old methods can be removed or not.
CI is failed but it's just the usual random erro 65 😞
I would add few tests to make sure the implementation of Strideable is correct


public init?(rawValue: Float) {
// TODO:
// constrain to 0 <= x <= 1000

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since it's a failable initializer we should return nil for invalid priority (not completely sure that <0 or >1000 is invalid...)

case .low:
return 250.0
case .medium:
return 500.0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like was 501 for macOS?


@available(*, deprecated:3.0, message:"Use priority(_ amount: ConstraintPriorityTarget) instead.")
@discardableResult
public func priorityMedium() -> ConstraintMakerFinalizable {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we have to keep (but change the implementation) those deprecated methods? Otherwise will need to be a Breaking Change version

@robertjpayne

Copy link
Copy Markdown
Member

I agree with most of @MP0w's comments

@Shehryar

Copy link
Copy Markdown
Contributor Author

Will update this PR soon

@Shehryar

Copy link
Copy Markdown
Contributor Author

PR is updated! @robertjpayne @MP0w

@Shehryar

Copy link
Copy Markdown
Contributor Author

still the same exit code 65 error

@Shehryar

Shehryar commented Nov 8, 2016

Copy link
Copy Markdown
Contributor Author

TODO: Need to update this again so the old methods can use the new enum values instead of hardcoded values.

self = .custom(value: rawValue)
}

var macOSMedium: Float {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would make it private or use it inline directly. The naming could be something different ? or is this because other platform don't really have medium priority? (AFAIK)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's mainly because the other platform don't really have a medium priority

@MP0w MP0w left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, also we should change the deprecation message of thew old methods (priorityRequired(), etc) to suggest the new way

@Shehryar

Shehryar commented Dec 21, 2016

Copy link
Copy Markdown
Contributor Author

@robertjpayne please re-review! Should I update the warning messages? Or are those good to go?

@MP0w

MP0w commented Jan 10, 2017

Copy link
Copy Markdown
Contributor

Any update?


public class ConstraintMakerPriortizable: ConstraintMakerFinalizable {

public enum Priority: Strideable, RawRepresentable, Equatable {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, IMHO this enum should not be inside ConstraintMakerPriortizable.
We don't want to write ConstraintMakerPriortizable.Priority right?


@available(*, deprecated:3.0, message:"Use priority(_ amount: ConstraintPriorityTarget) instead.")
@discardableResult
public func priority(level: Priority) -> ConstraintMakerFinalizable {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of the label level.
.priority(.low) feels the natural candidate

@available(*, deprecated:3.0, message:"Use priority(_ amount: ConstraintPriorityTarget) instead.")
@discardableResult
public func priority(level: Priority) -> ConstraintMakerFinalizable {
return self.priority(level: ConstraintMakerPriortizable.Priority(rawValue: level.rawValue)!)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a loop??! (It's late but really looks like)
Also don't see the point of initializing a priority with another priority...

@robertjpayne

Copy link
Copy Markdown
Member

I'll get around to this sorry! I've been insanely busy.

@MP0w

MP0w commented Feb 10, 2017

Copy link
Copy Markdown
Contributor

@robertjpayne Hey! Sorry to push but I feel like this improves the APIs and I'm missing it.
Let me know if I can help with anything to merge this pull request.
I also have a couple of other things that would like to be improved or make it easier to use, will open issues ASAP

@robertjpayne robertjpayne merged commit a7053ad into SnapKit:develop Feb 11, 2017
@robertjpayne

Copy link
Copy Markdown
Member

Hey everyone @MP0w included I've merged this but I've also made some tweaks:

Moved the Priority enum to a ConstraintPriority struct

There's good reasons to make this a struct rather than an enum. For starters much like enums static members on a struct can be used shorthand:

public struct ConstraintPriority {
    public static var required: ConstraintPriority {...}
}
...
public func priority(_ amount: ConstraintPriority)
...
.priority(.required)

Using a struct also allows users to define their own cases e.g.:

extension ConstraintPriority {
    public var almostRequired: ConstraintPriority { return 999.0 }
}

And ConstraintPriority conforms to expressible by float literal so you do not have to use an initialiser to create it from a float literal value.

Left the priority function that accepts a ConstraintPriorityTarget

I've left this API in for niceties to not break people's code. This is still useful because anything can conform to that protocol and be allowed in the priority function.

Thanks again for your contribution! 👍

@robertjpayne

robertjpayne commented Feb 11, 2017

Copy link
Copy Markdown
Member

Also I removed the stride able conformance, I can't see how that is actually useful? Is there a solid use case for that?

EDIT: actually I can see how one may one .required.advance(by: -1)

EDIT2: priority(.required.advance(by: -1) actually produces a compile error so I've again removed the strideable API, If you have to offset a named priority get the raw value and DIY it or add an extra named static case as an extension!

robertjpayne added a commit that referenced this pull request Feb 11, 2017
* make 'layoutConstraints' public again (#382)

* Fix wrong function call within contentCompressionResistanceHorizontalPriority (#387)

* Missing property topMargin on ConstraintMakerExtendable (#393)

This pull request adds the missing property "topMargin" to ConstraintMakerExtendable class.

* Switched main target build settings' "skip install" default settings to YES. (#391)

* Add an isActive API to Constraint

* Priority enum (#345)

* Adds ConstraintPriority enum to ConstraintMakerPrioritizable

* Constraints priority is now more robust

* Adds priority enum function

* Fixes compile error

* Adds failable initializer. Custom macOS medium priority. Adds back deleted methods

* Updates depracation messages

* Improve Priority API's

* Add extra protocol conformances to ConstraintPriority

* Tweak priority API's for offseting

* Tweak priority API's some more and add tests

* Prepare for release
@MP0w

MP0w commented Feb 11, 2017

Copy link
Copy Markdown
Contributor

I see the change to struct, the reason for strideable is that you are able to use for example priority(.low + 1), this improves a lot the usage IMHO.
I tested it back then and it worked

@MP0w

MP0w commented Feb 11, 2017

Copy link
Copy Markdown
Contributor

That was the implementation #324 (comment)
Might need adaption with a struct

@robertjpayne

Copy link
Copy Markdown
Member

@MP0w I couldn't get it to work w/o ambiguous API errors on the struct, I think it worked on the enum because enum cases are slightly different than static members for shorthand mode.

@MP0w

MP0w commented Feb 16, 2017

Copy link
Copy Markdown
Contributor

I will try it out, otherwise would you reconsider using an enum? It can still be extended with static vars that return .custom(value) if the only argument was the extensibility

@MP0w

MP0w commented Feb 16, 2017

Copy link
Copy Markdown
Contributor

@robertjpayne I tested it in a playground and seems to work https://gist.github.com/MP0w/4cd6df5558f8db2729035c0a25203398
Do you remember any specific issue? Should I try to create a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants