I have this inside my View:
Image(uiImage: viewModel.emojiImage)
Button(action: {
viewModel.updatesRandomEmojiImage()
}, label: {
Text("RANDOM EMOJI")
}).buttonStyle(FilledButton())
and these methods in my ViewModel:
@State var emojiImage: UIImage = UIImage()
func updatesRandomEmojiImage() {
guard let url = URL(string: randomEmojiUrl()) else { return }
downloadEmojiImage(fromUrl: url)
}
func randomEmojiUrl() -> String {
repository.emojis().randomElement()?.imageUrl ?? ""
}
private func downloadEmojiImage(fromUrl url: URL) {
if let data = try? Data(contentsOf: url) {
self.emojiImage = UIImage(data: data)!
}
}
I'm trying to make that the Image content changes whenever I click on the "RANDOM EMOJI" button. How can I do that?