sanzaru/SimpleToast

Toast Message Issue

muhammadfarooq012012 opened this issue · 8 comments

The message in the toast does not change if we set the global variable for the toast message.

@muhammadfarooq012012 Could you please provide more information or a short code snippet? I can't reproduce your problem. It's even possible to change the message while the toast is visible, although such things are not recommended.

@State private var toastMessage: String = "xxxx"

.simpleToast(isPresented: $isShowToast, options: SimpleToastOptions(hideAfter: 2)) {
let _ = print(self.toastMessage)

        HStack {
            Image(systemName: "exclamationmark.triangle")
            Text(toastMessage)
        }
        .padding(EdgeInsets(top: 8, leading: 12, bottom: 8, trailing: 12))
        .background(Color.black)
        .foregroundColor(Color.white)
    }

@muhammadfarooq012012
I cannot reproduce the problem. When I create a project with a simple view, a state variable and a textfield, the toast always shows the correct and current value of the variable and even changes while being displayed. In the console the print also shows the current value.

For me it looks more as if your view is not being updated correctly. The toast itself is not modifying the content to display, so it basically only shows what it gets. How do you set the value of your state variable?

I made a recording of my example:

Simulator.Screen.Recording.-.iPhone.13.-.2022-03-20.at.12.10.22.mp4

Here's the code:

import SwiftUI
import SimpleToast

struct ContentView: View {
    @State private var toastMessage = "Some message"
    @State private var showToast = false
    
    var body: some View {
        VStack {
            TextField("", text: $toastMessage)
                .textFieldStyle(RoundedBorderTextFieldStyle())
            
            Button("Toast") { showToast.toggle() }
        }
        .padding()
        .simpleToast(isPresented: $showToast, options: SimpleToastOptions()) {
            let _ = print(self.toastMessage)
            HStack {
                Image(systemName: "exclamationmark.triangle")
                Text(toastMessage)
            }
            .padding(EdgeInsets(top: 8, leading: 12, bottom: 8, trailing: 12))
            .background(Color.black)
            .foregroundColor(Color.white)
        }
    }
}
    @State private var isShowToast: Bool = false
@State private var toastMessage: String = "This is some simple toast message."

var body: some View {
    ZStack {
        VStack {
            Button {
                self.toastMessage = "Hello world!"
                let _ = print(self.toastMessage)
            } label: {
                Text("Change")
            }
            .padding()

            Button {
                self.isShowToast.toggle()
            } label: {
                Text("show")
            }
        }
    }
    .simpleToast(isPresented: $isShowToast, options: SimpleToastOptions(hideAfter: 2)) {
        let _ = print(self.toastMessage)
        HStack {
            Image(systemName: "exclamationmark.triangle")
            Text(toastMessage)
        }
        .padding(EdgeInsets(top: 8, leading: 12, bottom: 8, trailing: 12))
        .background(Color.black)
        .foregroundColor(Color.white)
    }
}

first click change button,then click show button

@Monkey-X-Byte @muhammadfarooq012012 I can confirm the behavior with your code, although I suspect some SwiftUI / Combine publisher quirks to be the problem. If you simply add a Text to your code, the state variable updates perfectly, also inside the toast:

ZStack {
        VStack {
            Text(self.toastMessage)

            Button {
                self.toastMessage = "Hello world!"
                let _ = print(self.toastMessage)

           ...

I will do some more investigation, as this is neither a solution, nor a workaround.

.onChange(of: toastMessage) { newValue in
}

This can solve the problem, but I don't know why.

Yeah, it really has to do with publishing and weird pre caching of views. I will investigate deeper this weekend.

If you call the show before changing the value it works, also removing the if-clause for showing the toast or a simple Text() with the value. Very weird. Hope I can provide a fix, soon.

@muhammadfarooq012012 @Monkey-X-Byte Release 0.6.2 should fix your issue.