Skip to content

Commit

Permalink
feat: redesign contentview
Browse files Browse the repository at this point in the history
  • Loading branch information
castdrian committed Feb 26, 2024
1 parent 67d139a commit 7ca2a04
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
47 changes: 37 additions & 10 deletions CoreDex/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,50 @@ let imagePredictor = ImagePredictor()

struct ContentView: View {
@State private var synthesizer: AVSpeechSynthesizer?
@State private var dexnum = 722
@State private var showingImagePicker = false
@State private var inputImage: UIImage?
@State private var pokemonData: GetPokemonByDexNumberQuery.Data.GetPokemonByDexNumber?
@State private var showDexEntryView = false
@State private var isVoiceAvailable = true
@State private var selectedNumber: Int = 722
@State private var isAnimating = false

let numberRange = Array(1...1025)

var body: some View {
NavigationStack {
VStack {
Stepper("PokéMon #\(dexnum)", value: $dexnum, in: 1...1025, step: 1)
VStack(spacing: 20) {
Text("Pokémon #\(selectedNumber)")
.font(.headline)
.padding(.top, 20)

Picker(String(), selection: $selectedNumber) {
ForEach(numberRange, id: \.self) {
Text("#\($0)").tag($0)
}
}
.pickerStyle(WheelPickerStyle())
.frame(height: 150)

Button("Check"){
Button("Check") {
DispatchQueue.global(qos: .userInitiated).async {
getDexEntry()
}
}.padding()
}
.padding(15)
.frame(width: 100, height: 100)
.background(
ZStack {
Circle()
.fill(LinearGradient(gradient: Gradient(colors: [Color.blue.opacity(0.5), Color.blue]), startPoint: .top, endPoint: .bottom)) // Gradient fill
.shadow(color: .gray.opacity(0.5), radius: 10, x: 5, y: 5)

Circle()
.stroke(Color.blue, lineWidth: 2)
}
)
.foregroundColor(Color.white)


Button("Scan (Gen 9 Starters)"){
DispatchQueue.global(qos: .userInitiated).async {
Expand Down Expand Up @@ -68,7 +95,7 @@ struct ContentView: View {

private func getDexEntry() {
DispatchQueue.global(qos: .userInitiated).async {
apolloClient.fetch(query: GetPokemonByDexNumberQuery(number: dexnum)) { result in
apolloClient.fetch(query: GetPokemonByDexNumberQuery(number: selectedNumber)) { result in
guard let data = try? result.get().data else { return }

UINotificationFeedbackGenerator().notificationOccurred(.success)
Expand Down Expand Up @@ -133,10 +160,10 @@ struct ContentView: View {
preferredStyle: .alert
)
alert.addAction(UIAlertAction(title: "Go to Settings", style: .default) { _ in
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url)
}
})
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url)
}
})
keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
}
}
Expand Down
22 changes: 21 additions & 1 deletion CoreDex/Views/DexEntryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ struct DexEntryView: View {
readDexEntry()
}
}
.onDisappear {
stopAudioPlayback()
}
}

private var spriteSection: some View {
Expand Down Expand Up @@ -251,6 +254,7 @@ struct DexEntryView: View {
}
}.resume()
}

private func readDexEntry() {
let audioSession = AVAudioSession()

Expand All @@ -262,12 +266,28 @@ struct DexEntryView: View {
}

let classificationText = getClassification(forNumber: pokemon.num).map { "The \($0)." } ?? ""

let dexEntry = "\(pokemon.species). \(classificationText) \(pokemon.types.count == 2 ? "\(pokemon.types.first!.name) and \(pokemon.types.last!.name) type." : "\(pokemon.types.first!.name) type.") \(((pokemon.preevolutions?.first) != nil) ? "The evolution of \(pokemon.preevolutions!.first!.species)." : "") \(pokemon.flavorTexts.first!.flavor)"

let utterance = AVSpeechUtterance(string: dexEntry)
utterance.voice = AVSpeechSynthesisVoice(identifier: "com.apple.voice.premium.en-US.Zoe")
utterance.rate = 0.4
synthesizer.speak(utterance)
}

private func stopAudioPlayback() {
if synthesizer.isSpeaking {
synthesizer.stopSpeaking(at: .immediate)
}

if let player = audioPlayer, player.isPlaying {
player.stop()
}

do {
try AVAudioSession.sharedInstance().setActive(false)
} catch {
print("Error stopping audio session: \(error)")
}
}
}

0 comments on commit 7ca2a04

Please sign in to comment.