SwiftUI 使用视图模型模式以两种不同的方式打开视图
SwiftUI Open view in two different ways using view model pattern
我需要打开一个名为 RequestDetails 的视图。
有两种情况可以打开此视图。
提供打开此请求的数据
提供对 Firestore 文档的引用
在这种情况下,我没有这个特定请求的所有详细信息,但我只有对 Firestore 文档的引用。我正在使用此引用在该视图出现后立即进行查询并获取有关此请求的详细信息 RequestDetail(reference: reference)
class RequestViewModel: ObservableObject {
@Published var request: RequestModel?
@Published var requestReference: DocumentReference?
init(request: RequestModel? = nil, requestReference: DocumentReference? = nil) {
self.request = request
self.requestReference = requestReference
}
func loadRequest() {
FirestoreService().fetchDocument(documentReference: self.requestReference) { (request: RequestModel) in
DispatchQueue.main.async {
self.request = request
}
}
}
}
struct RequestDetails: View {
@State var reference: DocumentReference?
@State var request: RequestModel?
@ObservedObject var vm: RequestViewModel
var body: some View {
VStack {
if request != nil {
Text(self.request?.senderFirstName)
}
}.onAppear {
if self.vm.package == nil {
self.vm.loadRequest()
}
}
}
}
struct Home: View {
var request: RequestModel
var reference: DocumentReference
var body: some View {
VStack {
RequestDetail(request: request)
RequestDetail(reference: reference)
}
}
}
问题是我遇到了很多错误,我想知道背后的逻辑是否正确。我是否以正确的方式使用视图模型模式?
以下内容适合遵循 MVVM 概念
struct RequestDetails: View {
@ObservedObject var vm: RequestViewModel
var body: some View {
VStack {
if self.vm.request != nil {
Text(self.vm.request!.senderFirstName)
}
}.onAppear {
if self.vm.request == nil {
self.vm.loadRequest()
}
}
}
}
struct Home: View {
var request: RequestModel
var reference: DocumentReference
var body: some View {
VStack {
RequestDetail(vm: RequestViewModel(request: request))
RequestDetail(vm: RequestViewModel(reference: reference))
}
}
}
我需要打开一个名为 RequestDetails 的视图。
有两种情况可以打开此视图。
提供打开此请求的数据
提供对 Firestore 文档的引用
在这种情况下,我没有这个特定请求的所有详细信息,但我只有对 Firestore 文档的引用。我正在使用此引用在该视图出现后立即进行查询并获取有关此请求的详细信息 RequestDetail(reference: reference)
class RequestViewModel: ObservableObject {
@Published var request: RequestModel?
@Published var requestReference: DocumentReference?
init(request: RequestModel? = nil, requestReference: DocumentReference? = nil) {
self.request = request
self.requestReference = requestReference
}
func loadRequest() {
FirestoreService().fetchDocument(documentReference: self.requestReference) { (request: RequestModel) in
DispatchQueue.main.async {
self.request = request
}
}
}
}
struct RequestDetails: View {
@State var reference: DocumentReference?
@State var request: RequestModel?
@ObservedObject var vm: RequestViewModel
var body: some View {
VStack {
if request != nil {
Text(self.request?.senderFirstName)
}
}.onAppear {
if self.vm.package == nil {
self.vm.loadRequest()
}
}
}
}
struct Home: View {
var request: RequestModel
var reference: DocumentReference
var body: some View {
VStack {
RequestDetail(request: request)
RequestDetail(reference: reference)
}
}
}
问题是我遇到了很多错误,我想知道背后的逻辑是否正确。我是否以正确的方式使用视图模型模式?
以下内容适合遵循 MVVM 概念
struct RequestDetails: View {
@ObservedObject var vm: RequestViewModel
var body: some View {
VStack {
if self.vm.request != nil {
Text(self.vm.request!.senderFirstName)
}
}.onAppear {
if self.vm.request == nil {
self.vm.loadRequest()
}
}
}
}
struct Home: View {
var request: RequestModel
var reference: DocumentReference
var body: some View {
VStack {
RequestDetail(vm: RequestViewModel(request: request))
RequestDetail(vm: RequestViewModel(reference: reference))
}
}
}