0

I currently have an app where the user goes through pages of lists to make multiple selections from. (using NavigationLinks)

PROBLEM: The functionality is fine if the user simply makes their selection then moves on, however the issue is when the user goes back THEN forward to a page. I.e. ViewA -> ViewB -> View->A -> ViewB.
Doing this causes ViewB to reinitialize and delete all previous selections on that page, even if ViewA didn't update. Note that using the back button preserves selections as expected.

EXPECTED BEHAVIOR: I want to preserve states through navigation of these pages.

ViewA:

struct YouthEventCheckInView: View {

@StateObject var trackable = TrackableMetricsManager(metricType: TrackableMetricType.Event, isCheckin: true)
@StateObject var event = CustomMetricManager()
@StateObject var checkInViewModel = CheckInViewModel()

@State private var moveToDailyStressorsView = false
@State private var newEvent = false

var body: some View {
    NavigationView {
        ZStack {
            ScrollView {
                VStack(alignment: .leading) {
                    NavigationLink(destination: YouthStressorCheckInView(checkInViewModel: checkInViewModel), isActive: $moveToDailyStressorsView) {
                        EmptyView()
                    }
                    Button {
                        moveToDailyStressorsView = true
                    } label: {
                        HStack {
                            Text("Next")
                    }
        .navigationTitle("Major Life Events")
        .onAppear {
            trackable.observeEvents()
        }
    }
}

ViewB (ViewC is same setup as this one):

struct YouthStressorCheckInView: View {
@StateObject var trackable = TrackableMetricsManager(metricType: TrackableMetricType.Stressor, isCheckin: true)
@StateObject var stressor = CustomMetricManager()

@ObservedObject var checkInViewModel: CheckInViewModel

@State private var moveToCopingStrategiesView = false
@State private var newStressor = false

var body: some View {
    ZStack {

        ScrollView {
            VStack(alignment: .leading) {
                NavigationLink(destination: YouthStrategyCheckInView(checkInViewModel: checkInViewModel), isActive: $moveToCopingStrategiesView) {
                    EmptyView()
                }

                Button( action: {
                    moveToCopingStrategiesView = true
                }, label: {
                    HStack {
                        Text("Next")
                })
            }
    }
    .navigationTitle("Daily Stressors")
    .onAppear {
        trackable.observeStressors()
    }
}

ViewModel for these views:

class ViewCheckInViewModel: ObservableObject {
struct Item: Hashable {
    let name: String
    let color: String
    let image: String
}

@Published var loading = false
@Published var majorLifeEvents: [Item] = []
@Published var dailyStressors: [Item] = []
@Published var copingStrategies: [Item] = []
@Published var date: String = ""


func loadData(withDataStore dataStore: AWSAppSyncDataStore, checkInId: String) {
    self.checkInId = checkInId
    loadDate(withDataStore: dataStore)
    loadMajorLifeEvents(withDataStore: dataStore)
    loadDailyStressors(withDataStore: dataStore)
    loadCopingStrategies(withDataStore: dataStore)
}

private func loadMajorLifeEvents(withDataStore dataStore: AWSAppSyncDataStore) {
    ...
}

private func loadDailyStressors(withDataStore dataStore: AWSAppSyncDataStore) {
    ...
}

private func loadCopingStrategies(withDataStore dataStore: AWSAppSyncDataStore) {
    ...
}

NOTE: Obviously some code is taken out, I left the things that I thought were necessary for this issue

bbrand0n
  • 31
  • 4
  • If you want to preserve states then keep them inside view models outside of view, so being injected in view (independently of creation/re-creation) the state is restored from view model. – Asperi Jul 18 '22 at 19:44
  • @Asperi thanks, so you're saying each page I should have an individual ViewModel? Because they all run off of 1 ViewModel currently. – bbrand0n Jul 19 '22 at 14:59

0 Answers0