import SwiftUI

//// MASK - 先定义两个Model 继承 ObservableObject
class WorkModel: ObservableObject {
    
    @Published var name = "name"
    
    @Published var count = 1
}

class UserModel: ObservableObject {
    
    @Published var nickname = "nickname"
    
    @Published var header = "http://www.baidu.com"
}

class WrapperModel: ObservableObject{
    
    @ObservedObject var workModel:WorkModel = WorkModel()
        
    @ObservedObject var userModel:UserModel = UserModel()
}

struct TestBinding: View {
    @ObservedObject var wrapperModel:WrapperModel = WrapperModel()

    var body: some View {
        VStack{
            Text("work.count \(wrapperModel.workModel.count)")

            Text("work.name \(wrapperModel.workModel.name)")

            Text("user.nickname \(wrapperModel.userModel.nickname)")

            Text("work.header \(wrapperModel.userModel.header)")
            
            Button(action: {
               wrapperModel.userModel.nickname = "Renew"

               wrapperModel.userModel.header = "http://..."

               wrapperModel.workModel.name = "work name"
                
               wrapperModel.workModel.count += 1
                wrapperModel.objectWillChange.send()

            }, label: {
                Text("更新数据")
            })
        }

     }
}

struct TestBinding_Previews: PreviewProvider {
    static var previews: some View {
        TestBinding()
            .frame(width: 400, height: 300)
    }
}
