Friday, June 9, 2023
HomeiOS developmentios - The right way to replace SwiftUI View in accordance with...

ios – The right way to replace SwiftUI View in accordance with Computed Property in ViewModel utilizing MVVM in Swift?


Context

I’ve a easy Kind utilizing the MVVM structure. Nevertheless, I encountered an issue with this implementation.

Drawback: I’ve a CustomForm which updates a Textual content relying on isValid of FormViewModel outlined in CustomFormObservable. Nevertheless, when getting into a reputation, the Textual content doesn’t change to “Save”. I do perceive that that is because of the truth, that isValid is just not marked as @Revealed, nonetheless, I am unable to determine a workaround with the given structure beneath.


Minimal Reproducible Instance (MRE)

@objc(Entity) public class Entity: NSManagedObject {
    @NSManaged public var title: String
}

protocol CustomFormObservable: ObservableObject {
    var isValid: Bool { get }
}

class FormViewModel: CustomFormObservable {
    @Revealed var entity = Entity(context: ...)

    var isValid: Bool { !entity.title.isEmpty }
}

struct Kind: View {
    @StateObject personal var formVM = FormViewModel()

    var physique: some View {
        CustomForm(formVM: formVM) {
            EditNameSection(entity: formVM.entity)
        }
    }
}

struct CustomForm<CFO: CustomFormObservable>: View {
    @ObservedObject var customFormVM: CFO

    var physique: some View {
        Button(motion: { ... }) { Textual content(customFormVM.isValid ? "Save" : "Lacking") }
    }
}

struct DetailForm: View {
    @ObservedObject var entity: Entity

    var physique: some View {
        TextField("Title", textual content: $entity.title)
    }
}

Query

  • Since I can not mark a ComputedProperty like isValid as @Revealed, how can I clear up this downside?
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments