SwiftUI View协议中的EnvironmentObject

时间:2020-10-29 17:12:35

标签: swift view swiftui protocols

我想制作这样的协议:

protocol SubscriptionManagerView: View {
    var subscriptionManager: EnvironmentObject<SubscriptionManager> { get set }
}

例如在这样的视图中使用:

struct Shop: View, SubscriptionManager {
    @EnvironmentObject var subscriptionManager: SubscriptionManager

第一个问题是属性包装器@EnvironmentObject无法使我响应协议SubscriptionManagerView。

最后,我想使用像这样的自定义修饰符:

extension View where Self : SubscriptionManagerView {
    func subscriptionManagerAlert() -> some View {
        self.alert(isPresented: self.$subscriptionManager.stateShowAlert) {
            Alert(title: Text(self.subscriptionManager.transactionStateTitle)
                ...
                  }))
        }
    }
}

有什么想法可以这样工作吗?

1 个答案:

答案 0 :(得分:2)

这里是一个可能方法的演示-想法是将视图修饰符与显式注入的订阅管理器一起使用,因此接口修饰符的契约保证该修饰符的客户端视图将为我们提供所需的对象。

通过Xcode 12 / iOS 14测试。

class SubscriptionManager: ObservableObject {
    @Published var stateShowAlert: Bool = false
    @Published var transactionStateTitle = "Test"
}

struct SubscriptionManagerModifier: ViewModifier {
    @ObservedObject var subscriptionManager: SubscriptionManager
    func body(content: Content) -> some View {
        content
            .alert(isPresented: self.$subscriptionManager.stateShowAlert) {
                Alert(title: Text(self.subscriptionManager.transactionStateTitle))
            }
    }
}

extension View {
    func subscriptionManagerAlert(with subscriptionManager: SubscriptionManager) -> some View {
        self.modifier(SubscriptionManagerModifier(subscriptionManager: subscriptionManager))
    }
}

struct Shop: View {
    
    @EnvironmentObject var subscriptionManager: SubscriptionManager
    
    var body: some View {
        Button("Test") {
            subscriptionManager.stateShowAlert.toggle()
        }
        .subscriptionManagerAlert(with: subscriptionManager)
    }
}

struct Shop_Previews: PreviewProvider {
    static var previews: some View {
        Shop().environmentObject(SubscriptionManager())
    }
}