Skip to content

Commit

Permalink
Bump 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tokijh committed Sep 30, 2021
0 parents commit a6bb418
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
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
7 changes: 7 additions & 0 deletions LICENSE
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.
19 changes: 19 additions & 0 deletions Package.swift
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"),
]
)
109 changes: 109 additions & 0 deletions README.md
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.
52 changes: 52 additions & 0 deletions Sources/ViewCondition/ViewCondition.swift
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)
}
}

0 comments on commit a6bb418

Please sign in to comment.