I have a parentview CreateReportView, it consist of two subviews which are accessed on the basis of navigation. My data is being properly saved and fetched between child view (create report detail view) and parentview (create report view) but when I navigate to NewUploadGalleryView my data vanishes, what could be the potential problem? I even changed my states variable to StateObject and ObservedObject respectively to maintain data consistency. My data remains fine when I perform navigation between create report detail and create report detail view but as soon as I navigate to NewuploadGallery and return back to createReportView to navigate into createReportDetailView my data resets
Here is my CreateReportView,
struct CreateReportView: View {
@Environment(.dismiss) var dismiss
@State private var showingNotifications = false
@State private var isDashboardView = true
@State private var isNotificationView = true
@State var navigateToSelection:Bool = false
@State var navigateToSectionDetail: Bool = false
@State var uploadButton:String?
var isGallery = false
var crTitle = ""
var selectedCat: CatModel? = nil
var selectedSubCat: CatModel? = nil
var aiData: JSON? = nil
@State private var loadingVC: LoadingVC?
@State private var ImageGallary = [CRFileModel]()
@State private var totalToUpload: Int?
@State private var isDraft = false
@State private var isEditSaveReport = false
@State var AILabels: Bool = false
@StateObject private var DashboardviewModel = NewDashBoardViewModel()
@StateObject private var viewModel = ContentViewModel()
@State private var selectedItems: [CRItemModel] = []
@Binding var parentSection: CRSectionModel?
@State private var toHide: Bool = false
@State private var toRecordwithAI: Bool = false
@StateObject private var createReportData = CreateReportData()
var body: some View {
VStack{
NewCustomNavBar(
title: "Create Report",
showBackButton: true,
onBack: {dismiss()},
onProfileTap: {},
showingNotifications: $showingNotifications,
notificationCount: DashboardviewModel.dashBoardModel?.unreadNotificationCount ?? 0,
isDashboardView: isDashboardView,
isNotificationView: isNotificationView
)
List{
VStack(spacing: 20) {
// Media Gallery Section
if isGallery{
NewMediaGallerysView(uploadButton: $uploadButton, minGalleryLimit: selectedSubCat?.galleryMin ?? 0, maxGalleryLimit: selectedSubCat?.galleryMax ?? 20, imageGalleryCount: CRModel.shared.imageGallery.count, selectedSubCat: selectedSubCat)
.onChange(of: uploadButton) { newValue in
if let new = newValue{
print("Button tapped with action: (new)")
print("---------------",CRModel.shared.imageGallery.count)
navigateToSelection = true
uploadButton = nil
}
}
.background(
Group{
NavigationLink(
destination: NewUploadGalleryView(imageGallary: viewModel.imageGallary ?? [CRFileModel](), selectedSubCat: selectedSubCat ?? CatModel(data: JSON()), createReportData: createReportData, parentSection: parentSection).environmentObject(createReportData)
.onDisappear {
// Refresh the count or data in the parent view
viewModel.getData(
selectedSubCat: selectedSubCat,
crTitle: crTitle,
selectedCat: selectedCat,
imageGallary: CRModel.shared.imageGallery
)
print("ON UPLOAD GALLERY DISMISS PARENT SECTION VALUE :(parentSection?.items.first?.textValue)")
print("paraaaaams: (CRModel.shared.getParams())")
},
isActive: $navigateToSelection,
label: { EmptyViewDesign() }
)
.hidden()
}
)
}
if aiData != nil {
if toHide == false {
PrimaryButton(title: "I am happy with it!")
.onTapGesture {
toHide = true
}
SecondaryButton(title: "Re Record with AI")
.onTapGesture {
toRecordwithAI = true
}
}
}
ForEach(viewModel.sections, id: .id) { item in
CreateReportListingView(sectionModel:item, aiData: AILabels)
.onTapGesture {
parentSection = item
navigateToSectionDetail = true
createReportData.items = item.items
}
}
} .listRowSeparator(.hidden) //.padding(.horizontal,20)
// .padding()
.background(Color(.systemBackground))
}.environmentObject(createReportData)
.listStyle(PlainListStyle())
.background(
NavigationLink(destination: CreateReportWithAIView(selectedCat: selectedCat ?? CatModel(data: JSON()), selectedSubCat: selectedSubCat ?? CatModel(data: JSON()), crTitle: crTitle), isActive: $toRecordwithAI){
EmptyViewDesign()
}.hidden()
)
.onAppear {
// for i in parentSection?.items ?? [CRItemModel](){
// print("items values onAppear = (i.textValue)")
//
// }
// if viewModel.createRport == nil {
viewModel.aiData = aiData
if viewModel.sections.isEmpty{
viewModel.getData(selectedSubCat: selectedSubCat, crTitle: crTitle, selectedCat: selectedCat, imageGallary: CRModel.shared.imageGallery) // Fetch data when the view appears
if aiData != nil{
AILabels = true
}
}
// } else {
// viewModel.populateCRModel(crModel: viewModel.createRport)
// }
print("paraaaaams: (CRModel.shared.getParams())")
print("ON CREATE REPORT APPEAR PARENT SECTION VALUE: (parentSection?.items.first?.textValue)")
}
.onDisappear{
for i in parentSection?.items ?? [CRItemModel]() {
print("CR_items values onDisappear = (i.textValue) (i.fieldValue) (i.files)")
print("FILESS ARE HERE: (i.files.contains(where: {$0.isLocalURL()}))")
print("ON CREATE REPORT DISAPPEAR PARENT SECTION VALUE: (parentSection?.items.first?.textValue)")
}
print("ON DISAPPEAR GALLERY COUNT: (CRModel.shared.imageGallery.count)")
}
HStack{
if viewModel.sections.count != 0{
Button(action: {
saveAsDraftReport()
}) {
CreateReportDraftButton(title: "Save as Draft")
}
Button(action: {
//createReport()
}){
let param = CRModel.shared.getParams()
if let params = param.params {
CreateReportCreateButton(title: "Create Report")
.onTapGesture {
print("Ready for create report")
}
}else if let error = param.error {
CreateReportCreateButtonGray(title: "Create Report")
.onTapGesture {
print("Not Ready for create report")
}
.disabled(true)
}
}
}
}.padding(.horizontal, 10)
// }.navigate(to: CreateReportDetailView(itemModel: selectedItems.first, items: selectedItems), when: $navigateToSectionDetail)
}.navigate(to: CreateReportDetailView(parentSection: $parentSection, createReportData: createReportData)
.environmentObject(createReportData),
when: $navigateToSectionDetail)
.navigate(to: NotificationsViews(showingNotifications: $showingNotifications), when: $showingNotifications)
.navigationBarBackButtonHidden()
Here is my CreateReportDetailView
struct CreateReportDetailView: View {
@Environment(.dismiss) var dismiss
@State private var showingNotifications = false
@State private var isDashboardView = true
@State private var isNotificationView = true
@StateObject private var DashboardviewModel = NewDashBoardViewModel()
//@State var items: [CRItemModel]
@Binding var parentSection: CRSectionModel!
@StateObject private var viewModel = ContentViewModel()
@State private var show360: Bool = false
@State private var selectedItem: CRItemModel? = nil
@State private var updatedText: String = "360 View"
@ObservedObject var createReportData: CreateReportData
var body: some View {
VStack {
NewCustomNavBar(
title: "Create Report Detail",
showBackButton: true,
onBack: { CRModel.shared.addModifiedSections()
dismiss() },
onProfileTap: {},
showingNotifications: $showingNotifications,
notificationCount: DashboardviewModel.dashBoardModel?.unreadNotificationCount ?? 0,
isDashboardView: isDashboardView,
isNotificationView: isNotificationView
)
ScrollView {
ForEach(Array(createReportData.items.enumerated()), id: .element.id) { index, item in
switch item.getType().rawValue {
case "TEXT":
let limitString = item.getFieldLimitString()
DescriptionInputView(item: $createReportData.items[index],
title:"(item.title) (item.matricSymbol != nil ? "((item.matricSymbol!))" : "")",
limitString: limitString, characterLimit: item.fieldLimits?.textLimit, mandatory: item.mandatory,
requiredDocument: item.documentRequired, requiredAudio: item.audioRequired,
requiredVideo: item.videoRequired, requiredImage: item.imageRequired, description: item.textValue ?? "", attachment: item.files, parentSection: parentSection
) .....
Here is my NewUploadGalleryView
struct NewUploadGalleryView: View {
@FocusState private var activeTextField: UUID?
@State var imageGallary: [CRFileModel]
@State var selectedSubCat: CatModel
@State private var showingNotifications = false
@State private var isDashboardView = true
@State private var isNotificationView = true
@StateObject private var DashboardviewModel = NewDashBoardViewModel()
@Environment(.dismiss) var dismiss
@State var uploadButton: Bool = false
@State private var showToast = false
@State private var toastMessage = ""
@State private var uploadState: UploadState = .chooseFiles
@State var reload: Bool? = false
@State private var showingFullScreenImage: Bool = false
@State private var selectedImageUrl: String? // Store the URL of the tapped image
@State private var isProcessingTap = false
@State private var deletingItemID: UUID?
@ObservedObject var createReportData: CreateReportData
@StateObject private var viewModel = ContentViewModel()
let parentSection: CRSectionModel?
@EnvironmentObject private var networkMonitorUpdated: NetworkMonitorUpdated
....
6