Your Second iOS & SwiftUI App

Nov 4 2021 · Swift 5.5, iOS 15, Xcode 13

Part 3: Managing Rows

24. Environment Values

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 23. Environment Next episode: 25. Sections

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 24. Environment Values

EnvironmentValues

Transcript: 24. Environment Values

As you can see, environment objects are a really nice SwiftUI feature, for keeping the code that connects views as simple as possible.

But the environment is not just a place for model objects! An environment is a storage container for potentially any information that multiple views need to function.

One piece of potentially handy information: isPresented. This property lets us know if a sheet, for example, is presented. And its friend, dismiss, allows us to programmatically dismiss a sheet.

Views can access isPresented, dismiss, and a wealth of other useful data, through the EnvironmentValues structure. Go check out the documentation for it – there’s a lot there!

You may recall that our NewBookView doesn’t go away, even after we’ve tapped the “Add to Library” button.

We’ll use Environment Values to fix that up. The way you access Environment Values involves the Environment property wrapper, and a set of parentheses.

  @EnvironmentObject var library: Library
  @Environment

  var body: some View {

Unlike all the other wrappers you’ve used in this course, this one takes an argument. It needs a key path to an EnvironmentKey. “backslash–dot–dismiss” is what we need.

@Environment(\.dismiss)

That gets you access to the environment’s dismissAction, but you still need to tell the compiler how you’re going to refer to it, within NewBookView. And you do that by supplying a variable – which can have any name, but might as well be dismiss.

@Environment(\.dimiss) var dismiss

Because a KeyPath provides complete type information, you don’t need to supply a type for this variable.

It’s an instance of a DismissAction. Which is a structure, but it has a special feature that allows us to call it as a function. So! After adding the new book, call dismiss!

            library.addNewBook(book, image: image)
            dismiss()
          }

That should do it! But let’s do one more thing in this New Book View.

We probably don’t want to let folks add books with no title or author to their library. So, let’s disable the “Add to Library” button sometimes.

            presentationMode.wrappedValue.dismiss()
          }
          .disabled(

          )
        }

Before the book’s title or author have been filled out, the button isn’t really ready to be used. Let’s start off by putting those into an array…

          .disabled(
            [book.title, book.author]
          )

And use its “contains” method, with String’s “isEmpty” property.

[book.title, book.author].contains(where: \.isEmpty)

That’s saying, “disable the button if either the title, or author string, is empty”. And as you can see from the grayed-out button, it’s working! Build and run to try your Add New Book button.

…you can watch the “Add to Library” button become active as you fill out the information! You can also see that the simulator is not running super smooth right now.

Once the button is active, and you tap on it… the sheet gets dismissed! Good stuff.