Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toast Message Issue #15

Closed
muhammadfarooq012012 opened this issue Mar 6, 2022 · 8 comments · Fixed by #16
Closed

Toast Message Issue #15

muhammadfarooq012012 opened this issue Mar 6, 2022 · 8 comments · Fixed by #16
Assignees
Labels
bug Something isn't working

Comments

@muhammadfarooq012012
Copy link

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

@sanzaru sanzaru self-assigned this Mar 9, 2022
@sanzaru
Copy link
Owner

sanzaru commented Mar 9, 2022

@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.

@Monkey-X-Byte
Copy link

@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)
    }

@sanzaru
Copy link
Owner

sanzaru commented Mar 20, 2022

@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)
        }
    }
}

@Monkey-X-Byte
Copy link

    @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

@sanzaru
Copy link
Owner

sanzaru commented Mar 22, 2022

@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.

@sanzaru sanzaru added the bug Something isn't working label Mar 22, 2022
@Monkey-X-Byte
Copy link

.onChange(of: toastMessage) { newValue in
}

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

@sanzaru
Copy link
Owner

sanzaru commented Mar 25, 2022

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.

@sanzaru
Copy link
Owner

sanzaru commented Mar 26, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants