I have a view that shows a sheet for filtering the items in a list. The view has this var:
struct JobsTab: View {
@State private var jobFilter: JobFilter = JobFilter()
var filter: some View {
Button {
self.showFilter = true
} label: {
Image(systemName: "line.horizontal.3.decrease.circle")
.renderingMode(.original)
}
.sheet(isPresented: $showFilter) {
FilterView($jobFilter, categoriesViewModel, jobsViewModel)
}
}
However, in the sheet, I'm trying the following and I can't make the view dismissed when clicking on the DONE button, only on the CANCEL button:
struct FilterView: View {
@Environment(\.presentationMode) var presentationMode
@ObservedObject var categoriesViewModel: CategoriesViewModel
@ObservedObject var jobsViewModel: JobsViewModel
let filterViewModel: FilterViewModel
@Binding var jobFilter: JobFilter
@State private var draft: JobFilter
@State var searchText = ""
init(_ jobFilter: Binding<JobFilter>, _ categoriesViewModel: CategoriesViewModel, _ jobsViewModel: JobsViewModel) {
_jobFilter = jobFilter
_draft = State(wrappedValue: jobFilter.wrappedValue)
self.categoriesViewModel = categoriesViewModel
self.jobsViewModel = jobsViewModel
self.filterViewModel = FilterViewModel()
}
...
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button("FilterView.Button.Cancel.Text".capitalizedLocalization) {
presentationMode.wrappedValue.dismiss()
}
}
ToolbarItem(placement: .navigationBarTrailing) {
Button("FilterView.Button.Done.Text".capitalizedLocalization) {
let request = Job.defaultRequest()
request.predicate = filterViewModel.buildPredicate(withJobFilterDraft: self.draft)
request.sortDescriptors = [NSSortDescriptor(key: #keyPath(Job.publicationDate), ascending: false)]
jobsViewModel.filteredJobsFetchRequest = request
self.jobFilter = self.draft
presentationMode.wrappedValue.dismiss()
}
}
}
I have also tried with a @Binding
like Paul says here but there's no luck.
Is there any workaround, or am I doing something wrong?
Thanks in advance!
EDIT: I've posted the properties of both views, because I think the problem comes from the the line in FilterView
self.jobFilter = self.draft
.
What I'm trying to do here is create a filter view, and the aforementioned line will be executed when the user presses the DONE button: I want to assign my binding jobFilter
in the JobsTab
the value of the FilterView
source of truth (which is a @State
) and probably, since I'm updating the binding jobFilter
the FilterView
is being shown again even though the $showFilter
is false
? I don't know to be honest.
EDIT2: I have also tried `` if #available(iOS 15.0, *) { let _ = Self._printChanges() } else { // Fallback on earlier versions }
in both `FilterView` and its called `JobTabs` and in both, I get the same result: unchanged