-
-
Save anupamchugh/b665c3c7256c875caff33e7c45b41521 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
import SwiftUI | |
struct ContentView: View { | |
@State var timeRemaining = 10 | |
@State var timer = Timer.publish (every: 1, on: .main, in: .common).autoconnect() | |
@State var emojiStatus = EmojiSearch.searching | |
var emojiObjects = [EmojiModel(emoji: "💻", emojiName: "laptop"), | |
EmojiModel(emoji: "😎", emojiName: "glasses"), | |
EmojiModel(emoji: "📚", emojiName: "book"), | |
EmojiModel(emoji: "✍️", emojiName: "pen"), | |
EmojiModel(emoji: "📲", emojiName: "iPod") | |
] | |
@State var currentLevel = 0 | |
@State var showNext = false | |
var body: some View { | |
ZStack { | |
if showNext || emojiStatus == .found{ | |
Button(action: { | |
if self.currentLevel == self.emojiObjects.count - 1{ | |
self.emojiStatus = .gameOver | |
} | |
else{ | |
self.currentLevel = self.currentLevel + 1 | |
self.timeRemaining = 10 | |
self.emojiStatus = .searching | |
self.showNext = false | |
self.instantiateTimer() | |
} | |
}) { | |
Text("NEXT") | |
.padding() | |
.background(Color.green) | |
.foregroundColor(Color.white) | |
.cornerRadius(6) | |
} | |
} | |
else{ | |
CustomCameraRepresentable(emojiString: emojiObjects[currentLevel].emojiName, emojiFound: $emojiStatus) | |
} | |
VStack(alignment: .leading, spacing: 16){ | |
Spacer() | |
if self.emojiStatus == .gameOver{ | |
Button(action: { | |
self.currentLevel = 0 | |
self.timeRemaining = 10 | |
self.emojiStatus = .searching | |
self.showNext = false | |
self.instantiateTimer() | |
}) { | |
Text("GAME OVER. TAP to RETRY") | |
.padding() | |
.background(Color.green) | |
.foregroundColor(Color.white) | |
.cornerRadius(3) | |
} | |
} | |
else{ | |
if self.emojiStatus == .searching{ | |
Text("\(timeRemaining)") | |
.font(.system(size:50, design: .rounded)) | |
.fontWeight(.bold) | |
.foregroundColor(.yellow) | |
.onReceive(timer) { _ in | |
if self.emojiStatus == .found{ | |
self.cancelTimer() | |
self.timeRemaining = 10 | |
} | |
else { | |
if self.timeRemaining > 0 { | |
self.timeRemaining -= 1 | |
} | |
else{ | |
self.emojiStatus = .notFound | |
self.showNext = true | |
} | |
} | |
} | |
} | |
emojiResultText() | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment