UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Using sheet(item:onDismiss:content:) with item based on ObservedObject?

Forums > SwiftUI

Hey everyone, I'm in the awkward situation where I need to be able to show one of three different sheets based on whether or not an observed (@Published) property on an @ObservedObject is in a certain subset of possible enum cases. This may sound confusing, so let me try to demonstrate with a very reduced example:

class MyObservedClass: ObservableObject {
    // initialisation...

    enum PossibleStates {
        case caseA
        case caseB
        case caseC
        case caseD
        case caseE
    }

    @Published var state: PossibleStates
}

struct Test: View {
    @ObservedObject var myObservedClass: MyObservedClass
    @State private var sheetToShow: SheetToShow?

    var body: some View {
        Rectangle()
            .sheet(item: $sheetToShow) { item in
                switch item {
                // show different sheets based on case
                }
            }
    }

    private enum SheetToShow: Int, Identifiable {
        case showSheet1
        case showSheet2
        case showSheet3

        var id: Int { self.rawValue }
    }
}

I need to be able to set sheetToShow based on conditions like:

if [.caseA, .caseB].contains(myObservedClass.state) {
    sheetToShow = .showSheet1
} else if [.caseC, .caseD].contains(myObservedClass.state) {
    sheetToShow = showSheet2
} else {
    sheetToShow = showSheet3
}

But sheetToShow also needs to be an @State variable I can use in the sheet(item:) call and that can be set to nil when no sheet is to be shown, or a sheet has been dismissed, and sheetToShow needs to update whenever myObservedClass.state changes.

Is there any halfway sane way to achieve this, and have it update correctly (and not just, for example, when the Test view gets initialised, or appears)? 🤔

3      

Put your sheetToShow in the ObservedObject. Make your calculations in your ObservableObject.

I used your first example code before you edited it. But the result should be the same.

import SwiftUI

class MyObservedClass: ObservableObject {

    @Published var state: PossibleStates
    @Published var sheetToShow: SheetToShow?

    init(state: PossibleStates) {
        self.state = state
    }

    enum PossibleStates {
        case caseA
        case caseB
        case caseC
    }

    enum SheetToShow: Int, Identifiable {
        case showSheet1
        case showSheet2
        case showSheet3

        var id: Int { self.rawValue }
    }

    private func calcSheetToShow() {
        if [.caseA, .caseB].contains(self.state) {
            sheetToShow = .showSheet1
        } else if [.caseA, .caseC].contains(self.state) {
            sheetToShow = .showSheet2
        } else {
            sheetToShow = .showSheet3
        }
    }
}

struct Test: View {

    @ObservedObject var myObservedClass: MyObservedClass

    var body: some View {
        Rectangle()
            .sheet(item: $myObservedClass.sheetToShow) { item in
                switch item {
                case .showSheet1:
                    Text("Sheet1")
                case .showSheet2:
                    Text("Sheet2")
                case .showSheet3:
                    Text("Sheet3")
                }

            }
    }

}

4      

And then you would call calcSheetToShow() in the didSet of state?

3      

This should work, yes. No hurting in trying ;)

4      

Here's a different way to do it, where everything is self-contained in PossibleStates. It takes advantage of the fact that enums can also conform to View. I like it because it gets even more of the logic out of your View, cuts down on the number of properties that need to be observed, and eliminates the need for a separate function to calculate which sheet to show.

class MyObservedClass: ObservableObject {
    init() {
        //whatever needs to go here
    }

    enum PossibleStates: String, RawRepresentable, View, Identifiable {
        case caseA = "caseA"
        case caseB = "caseB"
        case caseC = "caseC"
        case caseD = "caseD"
        case caseE = "caseE"

        var id: String {
            self.rawValue
        }

        @ViewBuilder //needed in case the Views returned are of different types
        var body: some View {
            switch self {
            case .caseA, .caseB: Text("Sheet 1")
            case .caseC, .caseD: Text("Sheet 2")
            case .caseE: Text("Sheet 3")
            }
        }
    }

    @Published var state: PossibleStates?
}

struct Test: View {
    @ObservedObject var myObservedClass: MyObservedClass

    var body: some View {
        Rectangle()
            .sheet(item: $myObservedClass.state) { item in
                item
            }
    }
}

5      

Thank you everyone for your great advice! ☺️ @Hatsushira: Not sure how I feel about returning Views from a (view) model but other than that it's very elegant. :) Cheers!

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.