SwiftUI Fundamentals

Feb 28 2023 · Swift 5.7, macOS Venture 13.1, Xcode 14.2

Part 2: Navigation & Data Flow

17. Challenge: StateObject & ObservableObject

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: 16. Observable Objects Next episode: 18. Environment

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.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

OK, you’re nearing the end of the course, but I’ve got one more challenge for you. It’s focused primarily on adding some look and feel options to the FaveFlicks app with an @ObservableObject, but it also requires you to add some new @State properties, a new modal view, and to build a little user interface - things you should know well after viewing the course so far!

class LookAndFeelStore: ObservableObject {
  @Published var currentLookAndFeelInfo = LookAndFeelInfo(accentColor: Color.blue, symbolName: "star")
}
struct LookAndFeelInfo {
  let accentColor: Color
  let symbolName: String
}
@ObservedObject var lookAndFeelStore: LookAndFeelStore
@State private var accentColor = Color.red
@State private var symbolName = ""
.onAppear {
  accentColor = lookAndFeelStore.currentLookAndFeelInfo.accentColor
  symbolName = lookAndFeelStore.currentLookAndFeelInfo.symbolName
}
func updateLookAndFeelInfo() {
  let newLookAndFeelInfo = LookAndFeelInfo(accentColor: accentColor, symbolName: symbolName)
  lookAndFeelStore.currentLookAndFeelInfo = newLookAndFeelInfo
}
@ObservedObject var lookAndFeelStore: LookAndFeelStore
\(lookAndFeelStore.currentLookAndFeelInfo.symbolName)
RatingView(lookAndFeelStore: LookAndFeelStore(), rating: 4.5)
RatingView(lookAndFeelStore: lookAndFeelStore, rating: rating)
@ObservedObject var lookAndFeelStore: LookAndFeelStore
RatingView(lookAndFeelStore: lookAndFeelStore, rating: rating)
@ObservedObject var lookAndFeelStore: LookAndFeelStore
@ObservedObject var lookAndFeelStore = LookAndFeelStore()
  var body: some View {
    NavigationView {
      List {
        ForEach(movieStore.movies, id: \.title) {
          MovieRow(movie: $0, 🟢lookAndFeelStore: lookAndFeelStore)
        }
      }
      .sheet(isPresented: $isPresented) {
        AddMovie(movieStore: movieStore, showModal: $isPresented, lookAndFeelStore: lookAndFeelStore)
      }
      🟢.sheet(isPresented: $isLookAndFeelPresented) {
        LookAndFeelView(lookAndFeelStore: lookAndFeelStore)
      }
      .navigationBarTitle(Text("Fave Flicks"))
      .navigationBarItems(
        leading:
          NavigationLink(destination: UserView(userStore: userStore)) {
            HStack {
              Text(userStore.currentUserInfo.userName)
              Image(systemName: "person.fill")
            }
          },
        trailing:
          🟢HStack {
            🟢Button(action: { isLookAndFeelPresented.toggle() }) {
              Image(systemName: "gear")
            }
            Button(action: { isPresented.toggle() }) {
              Image(systemName: "plus")
            }
          }
      )
    }
    🟢.accentColor(lookAndFeelStore.currentLookAndFeelInfo.accentColor)
  }
}