Swiftpack.co - tokijh/ViewCondition as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
tokijh/ViewCondition
✨ Super sweet syntactic sugar for SwiftUI.View initializers.
.package(url: "https://github.com/tokijh/ViewCondition.git", from: "1.0.0")

ViewCondition

✨ Super sweet syntactic sugar for SwiftUI.View initializers.

At a Glance

struct BorderTextView: View {
  var color: Color?

  @ViewBuilder
  var body: some View {
    Text("Hello")
      .if(let: color) {
        $0.border($1)
      }
  }
}

This is equivalent to:

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

let color: Color?
Text("Hello")
  .if(let: color) {
    $0.border($1)
  }

if

let text = ""
Text("Hello")
  .if(text == "Hello") {
    $0.bold()
  }

ifNot

let text = ""
Text("Hello")
  .ifNot(text == "Hello") {
    $0.bold()
  }

then

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:

import PackageDescription

let package = Package(
  name: "MyAwesomeApp",
  dependencies: [
    .package(url: "https://github.com/tokijh/ViewCondition.git", from: "1.0.0")
  ]
)

Special thanks to..

License

ViewCondition is under MIT license. See the LICENSE file for more info.

GitHub

link
Stars: 55
Last commit: 3 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

1.0.0
3 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics