Chapter 14
Data Sharing with Combine and Environment Objects

In the previous chapter, you learned how to layout a form using the Form component. However, the form is not yet functional. Regardless of the options selected, the list view doesn't change to reflect the user's preferences. In this chapter, we will continue developing the settings screen and make the app fully functional by updating the restaurant list based on the user's preferences.

In the upcoming sections, we will cover the following topics:

  1. How to use enum to better organize our code
  2. How to store the user's preference permanently using UserDefaults
  3. How to share data using Combine and @Environment

If you haven't completed the exercise from the previous chapter, I encourage you to spend some time on it. However, if you can't wait to read this chapter, you can download the project from the following link: https://www.appcoda.com/resources/swiftui7/SwiftUIForm.zip.

Refactoring the Code with Enum

Currently, we store the three options of the display order in an array. While this approach works, there is a better way to improve the code.

An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.

- Apple's official documentation (https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html)

Since this group of fixed values is related to the display order, we can use an enum to hold them, with each case assigned an integer value, like this:

enum DisplayOrderType: Int, CaseIterable {
    case alphabetical = 0
    case favoriteFirst = 1
    case checkInFirst = 2

    init(type: Int) {
        switch type {
        case 0: self = .alphabetical
        case 1: self = .favoriteFirst
        case 2: self = .checkInFirst
        default: self = .alphabetical
        }
    }

    var text: String {
        switch self {
        case .alphabetical: return "Alphabetical"
        case .favoriteFirst: return "Show Favorite First"
        case .checkInFirst: return "Show Check-in First"
        }
    }
}

What makes enum great is that we can work with these values in a type-safe way within our code. Additionally, enum in Swift is a first-class type in its own right. This means that we can create instance methods to provide additional functionality related to the values. Later on, we will add a function for handling the filtering. For now, let's create a new Swift file named SettingStore.swift to store the enum. You can right-click SwiftUIForm in the project navigator and choose New File from Template... to create the file.

Figure 1. Creating a new Swift file
Figure 1. Creating a new Swift file

After creating SettingStore.swift, insert the code snippet above in the file. Next, go back to SettingView.swift. We will update the code to use the DisplayOrder enumeration instead of the displayOrders array.

First, delete this line of code from SettingView:

To access the full content and the complete source code, please get your copy at https://www.appcoda.com/swiftui.

results matching ""

    No results matching ""