0

我的应用程序上有一个 NavigationView。从 mainView 我使用 NavigationLink MessagesView 打开,从 MessagesView 内部我使用 NavigationView ChatView 打开。在 ChatView 上,我制作了一个 backButton,但是当我按下 backButton 时,我不知道如何让 MessagesView 重新加载。

我在网上搜索了解决方案,但要么不起作用,要么我不明白。

这是我到目前为止的代码:

struct MessagesView: View {

@ObservedObject var allMessages = GetAllMessages()
@State var readNewThreadMessages = [MessagesDetailes]()
//astea sunt puse pentru a scapa de bugul ca nu iti incarca cateodata View
@ObservedObject var observer = Observer()
@Binding var incepemOnReceive : Bool

var body: some View {
    ScrollView(.vertical, showsIndicators: false) {
        if allMessages.readThreadMessages.count == 0 {
            LoadingView()
                .padding(.top, 50)
        } else {
            
            //mesajele din DB
            ForEach(allMessages.readThreadMessages) { message in
                NavigationLink(destination: ChatView(incepemOnReceive: $incepemOnReceive, other_name: message.other_user, other_object: message.other_object, produs_nume: message.product_name, produs_pret: message.product_price, produs_id: message.product_id, thread_obj: message.threadObj, readThreadMessages: $allMessages.readThreadMessages).onAppear(perform: {
                    self.allMessages.markMessageAsSeen(obiect: message.readStateObj, isRead: message.seen)
                })) {
                    MessagesRow(message: message)
                        .background(message.seen ? Color.clear : Color("ColorSecondary").opacity(0.1))
                }
            }
        }
    }
    .onReceive(self.observer.$enteredForeground){ _ in
        if self.incepemOnReceive {
            self.allMessages.readAllMessages()
            print("read from db")
        }
    }
    .onAppear(perform: {
        self.incepemOnReceive = true
    })
    .navigationTitle(LocalizedStringKey("MessagespNavT"))
    .navigationBarTitleDisplayMode(.inline)
}}

和带有 backButton 的 ChatView

struct ChatView: View {
    @Environment(\.presentationMode) var presentationMode
    @ObservedObject var getChatMessages = GetChatMessages()
    @Binding var incepemOnReceive : Bool
    
    var other_name   : String
    var other_object : PFObject
    var produs_nume  : String
    var produs_pret  : String
    var produs_id    : String
    var thread_obj   : PFObject
    @State var writeMessage = ""
    @State var messageArray = [CurrentChatMessages]()
    @State var showFromDB = true
    
    @Binding var readThreadMessages : [MessagesDetailes]
    @State var size : CGFloat = 35
    //photo picker
    @State var isPresented: Bool = false
    @State var pickerResult: [UIImage] = []
    var config: PHPickerConfiguration {
        var config = PHPickerConfiguration(photoLibrary: PHPhotoLibrary.shared())
        config.filter = .images //videos
        config.selectionLimit = 1
        return config
    }
    
    var body: some View {
        
        VStack{
            // show chat
            
        }.onAppear(perform: {
            self.incepemOnReceive = false
            UITextView.appearance().backgroundColor = UIColor(.gray).withAlphaComponent(0.15)
            self.getChatMessages.getAllChatMessages(threadObj: thread_obj)
            
        })
        .navigationBarItems(leading:
                                Button(action: {
                                    for mesaj in 0..<readThreadMessages.count-1 {
                                        if readThreadMessages[mesaj].threadObj == thread_obj {
                                            readThreadMessages[mesaj].seen = true
                                        }
                                    }
                                    self.incepemOnReceive = true
                                    self.presentationMode.wrappedValue.dismiss()
                                }) {
                                    CustomBackButton()
                                })
    }
}
4

3 回答 3

1

你必须强制你的 ViewModel 更新。这个答案可能对您有所帮助https://stackoverflow.com/a/59873443/13859627

于 2021-02-24T15:01:57.103 回答
0

MessagesView重新加载,您可以在里面添加onAppear

self.allMessages.objectWillChange.send()

这应该足以让您的视图在每次出现时都重新加载,并且如果您愿意,您可以将其置于一个条件内,我不认为您的解决方案是最好的方法。

于 2021-09-20T15:04:48.613 回答
0

我已经重新解决了这个问题。

我已经删除

@ObservedObject var observer = Observer()
@Binding var incepemOnReceive : Bool
 .onReceive(self.observer.$enteredForeground){ _ in ...}

我已经添加 @State var messagesSelected = 0

在 MessagesView 我放了:

.onAppear(perform: {
            if messagesSelected == 0 {
                self.allMessages.readAllMessages()
                print("ASDASDASDASDASDASDASDASDASDASDASDASDASDASDASDASDASDASD")
            }
        })
于 2021-02-24T22:31:50.947 回答