-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a6bb418
Showing
5 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2021 tokijh. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// swift-tools-version:5.3 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "ViewCondition", | ||
platforms: [ | ||
.iOS(.v13), | ||
.macOS(.v10_15), | ||
.tvOS(.v13), | ||
.watchOS(.v6) | ||
], | ||
products: [ | ||
.library(name: "ViewCondition", targets: ["ViewCondition"]), | ||
], | ||
targets: [ | ||
.target(name: "ViewCondition"), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# ViewCondition | ||
|
||
✨ Super sweet syntactic sugar for SwiftUI.View initializers. | ||
|
||
## At a Glance | ||
|
||
```swift | ||
struct BorderTextView: View { | ||
var color: Color? | ||
|
||
@ViewBuilder | ||
var body: some View { | ||
Text("Hello") | ||
.if(let: color) { | ||
$0.border($1) | ||
} | ||
} | ||
} | ||
``` | ||
|
||
This is equivalent to: | ||
|
||
```swift | ||
struct BorderTextView: View { | ||
var color: Color? | ||
|
||
@ViewBuilder | ||
var body: some View { | ||
let text = Text("Hello") | ||
if let color = color { | ||
text.border(color) // border doesn't allow optional | ||
} else { | ||
text | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Operators | ||
|
||
### if let | ||
|
||
```swift | ||
let color: Color? | ||
Text("Hello") | ||
.if(let: color) { | ||
$0.border($1) | ||
} | ||
``` | ||
|
||
### if | ||
|
||
```swift | ||
let text = "" | ||
Text("Hello") | ||
.if(text == "Hello") { | ||
$0.bold() | ||
} | ||
``` | ||
|
||
### ifNot | ||
|
||
```swift | ||
let text = "" | ||
Text("Hello") | ||
.ifNot(text == "Hello") { | ||
$0.bold() | ||
} | ||
``` | ||
|
||
### then | ||
|
||
```swift | ||
let text = "" | ||
Text("Hello") | ||
.then { | ||
if text == "Hello" { | ||
$0.bold() | ||
} else { | ||
$0.italic() | ||
} | ||
} | ||
``` | ||
|
||
#### ⚠️ Becareful | ||
|
||
**If you don't return view in `else`, the view may not come out.** | ||
|
||
|
||
## Installation | ||
|
||
Using Swift Package Manager: | ||
|
||
```swift | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "MyAwesomeApp", | ||
dependencies: [ | ||
.package(url: "https://github.com/tokijh/ViewCondition.git", from: "1.0.0") | ||
]g | ||
) | ||
``` | ||
|
||
## Special thanks to.. | ||
- [@leedh2004](https://github.com/leedh2004) | ||
|
||
## License | ||
**ViewCondition** is under MIT license. See the [LICENSE](LICENSE) file for more info. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// ViewCondition.swift | ||
// ViewCondition | ||
// | ||
// Created by tokijh on 2021/09/30. | ||
// Copyright © 2021 tokijh. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
extension View { | ||
@ViewBuilder | ||
public func `if`<Content: View>( | ||
_ condition: @autoclosure @escaping () -> Bool, | ||
@ViewBuilder content: (Self) -> Content | ||
) -> some View { | ||
if condition() { | ||
content(self) | ||
} else { | ||
self | ||
} | ||
} | ||
|
||
@ViewBuilder | ||
public func `if`<Value, Content: View>( | ||
`let` value: Value?, | ||
@ViewBuilder content: (_ view: Self, _ value: Value) -> Content | ||
) -> some View { | ||
if let value = value { | ||
content(self, value) | ||
} else { | ||
self | ||
} | ||
} | ||
|
||
@ViewBuilder | ||
public func ifNot<Content: View>( | ||
_ notCondition: @autoclosure @escaping () -> Bool, | ||
@ViewBuilder content: (Self) -> Content | ||
) -> some View { | ||
if notCondition() { | ||
self | ||
} else { | ||
content(self) | ||
} | ||
} | ||
|
||
@ViewBuilder | ||
public func then<Content: View>(@ViewBuilder content: (Self) -> Content) -> some View { | ||
content(self) | ||
} | ||
} |