Home > OS >  Generate random text from JSON file when button is pressed - SwiftUI
Generate random text from JSON file when button is pressed - SwiftUI

Time:10-24

I want to create random text generator from json file which are called by button. At this moment I have random text generator, but to refresh that I need to go back on another view and open same view.

How can I make it refresh by button? I have tried creating this by function, but every time I was getting lot of errors...

ContentView code (except hacking swift code that allows me to run this code)

struct Quote: Codable {
        
        var text: String
        var author: String
        
        var shareMessage: String {
            return "\"\(text)\" - \(author)"
        }
    }
        
        struct PytaniaNormalne : View {
            @State private var quote : Quote?
            var body: some View {
                VStack {
                    if let quote = quote {
                        VStack {
                            VStack {
                                Text(quote.text)
                                    .font(.system(.title3))
                                    .foregroundColor(.white)
                                Text(quote.author)
                                    .font(.system(.title3))
                                    .foregroundColor(.white)
                            }
                        }.frame(width: 240)
                            .background(RoundedRectangle(cornerRadius: 7.0).fill(Color.blue))
                    }
                }.onAppear {
                    let quotes = Bundle.main.decode([Quote].self, from: "quotes.json")
                    quote = quotes.randomElement()
                    
                }
            }
        }

JSON FILE

[
    {
        "text": "Pytanie 1",
        "author": "tekst"
    },
    {
        "text": "Pytanie 2",
        "author": "tekst"
    },
    {
        "text": "Pytanie 3",
        "author": "teskt"
    },
    {
        "text": "Pytanie 4",
        "author": "tekst"
    },
    {
        "text": "Pytanie 5",
        "author": "author"
    },
    {
        "text": "Pytanie 6",
        "author": "author"
    },
    {
        "text": "Pytanie 7",
        "author": "author"
    }
]

CodePudding user response:

As you can see, I've added the button below the author Text. Every time you tap it, setRandomQuote will be invoked, setting a random quote to your @State var, which subsequently will reload the view.

struct PytaniaNormalne : View {
    @State private var quote : Quote?
    var body: some View {
        VStack {
            if let quote = quote {
                VStack {
                    VStack {
                        Text(quote.text)
                            .font(.system(.title3))
                            .foregroundColor(.white)
                        Text(quote.author)
                            .font(.system(.title3))
                            .foregroundColor(.white)
                        
                        Button(action: setRandomQuote) {
                            Text("Refresh")
                                .font(.system(.title3))
                                .foregroundColor(.white)
                                .padding()
                                .background {
                                    RoundedRectangle(cornerRadius: 8)
                                        .foregroundColor(.blue)
                                }
                        }

                    }
                }.frame(width: 240)
                    .background(RoundedRectangle(cornerRadius: 7.0).fill(Color.blue))
            }
        }.onAppear {
            setRandomQuote()
        }
    }
    
    private func setRandomQuote() {
        let quotes = Bundle.main.decode([Quote].self, from: "quotes.json")
        quote = quotes.randomElement()
    }
}
  • Related