Created
April 19, 2024 23:10
-
-
Save apatronl/aa0039e8c9061b65dd53004939e8cfb1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ContentView: View { | |
private let viewModel = HobbyViewModel() | |
@State private var showAlert: Bool = false | |
@State private var randomHobby: Hobby? | |
var body: some View { | |
Button("Random hobby") { | |
randomHobby = viewModel.randomHobby() | |
showAlert = true | |
} | |
.font(.title) | |
.padding() | |
.alert( | |
"My favorite hobby is...", | |
isPresented: $showAlert, | |
presenting: randomHobby, | |
actions: { _ in }, | |
message: { hobby in | |
Text("\(hobby.name) \(hobby.emoji)") | |
} | |
) | |
} | |
} | |
struct HobbyViewModel { | |
private let hobbies = [ | |
Hobby(emoji: "🎾", name: "playing tennis"), | |
Hobby(emoji: "🎥", name: "watching YouTube videos"), | |
Hobby(emoji: "📖", name: "reading books"), | |
Hobby(emoji: "☕️", name: "going to coffee shops"), | |
Hobby(emoji: "🏃🏽♀️", name: "running"), | |
Hobby(emoji: "✈️", name: "traveling"), | |
] | |
func randomHobby() -> Hobby { | |
return hobbies.randomElement()! | |
} | |
} | |
struct Hobby: Identifiable { | |
let id: UUID = UUID() | |
let emoji: String | |
let name: String | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment