I have these filters in my view and all of them update the FilterViewModel which then takes care of filtering the data. One of these views, SearchAddressView expects a PlacemarkViewModel and not a FilterViewModel because it provides a dropdown list of addresses when user starts typing. There is a lot of code there so I do not want to duplicate this code into my FilterViewModel
However, I need to read @Published var placemark: Placemark from PlacemarkViewModel to FilterViewModel. I'm trying to import PlacemarkViewModel into FilterViewModel and then to use didSet { } to read it's values but its not working.
So the idea is .. while the users searches for an address this updates the PlacemarkViewModel but FilterViewModel needs to get this value also. Any idea on how to achieve this?
struct FiltersView: View {
@ObservedObject var filterViewModel: FilterViewModel
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack {
FilterButtonView(title: LocalizedStringKey(stringLiteral: "category"), systemName: "square.grid.2x2.fill") {
CategoryFilterView(filterViewModel: self.filterViewModel)
}
FilterButtonView(title: LocalizedStringKey(stringLiteral: "location"), systemName: "location.fill") {
SearchAddressView(placemarkViewModel: self.filterViewModel.placemarkViewModel)
}
FilterButtonView(title: LocalizedStringKey(stringLiteral: "sort"), systemName: "arrow.up.arrow.down") {
SortFilterView(filterViewModel: self.filterViewModel)
}
}
}
}
}
FilterViewModel
class FilterViewModel: ObservableObject, LoadProtocol {
@Published var placemarkViewModel: PlacemarkViewModel() {
didSet {
print("ok") // nothing
}
}
}
PlacemarkViewModel
class PlacemarkViewModel: ObservableObject {
let localSearchCompleterService = LocalSearchCompleterService()
let locationManagerService = LocationManagerService()
@Published var addresses: [String] = []
// I need this value in my FilterViewMode;
@Published var placemark: Placemark? = nil
@Published var query = "" {
didSet {
localSearchCompleterService.autocomplete(queryFragment: query) { (addresses: [String]) in
self.addresses = addresses
}
}
}
init(placemark: Placemark? = nil) {
self.placemark = placemark
}
var address: String {
if let placemark = placemark {
return "\(placemark.postalCode) \(placemark.locality), \(placemark.country)"
}
return ""
}
func setPlacemark(address: String) {
locationManagerService.getLocationFromAddress(addressString: address) { (coordinate, error) in
let location: CLLocation = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
self.locationManagerService.getAddressFromLocation(location: location) { (placemark: CLPlacemark?) in
if let placemark = placemark {
self.placemark = Placemark(placemark: placemark)
self.query = placemark.name ?? ""
}
}
}
}
func getAddressFromLocation() {
locationManagerService.getLocation { (location: CLLocation) in
self.locationManagerService.getAddressFromLocation(location: location) { (placemark: CLPlacemark?) in
if let placemark = placemark {
self.placemark = Placemark(placemark: placemark)
self.query = placemark.name ?? ""
}
}
}
}
}