// // SecondGameLearnViewController.swift // Guibe // // Created by Max Hunt on 11/06/2019. // Copyright © 2019 8. All rights reserved. // import UIKit import AVKit class SecondGameLearnViewController: UIViewController, AVAudioPlayerDelegate { @IBOutlet weak var textLabel: UILabel! @IBOutlet weak var mainButton: UIButton! @IBOutlet weak var nextBtn: UIButton! @IBOutlet weak var progressBar: UIProgressView! @IBOutlet var vCtrl: UIView! @IBAction func mainBtnPressed(_ sender: Any) { audioPlay(fName: self.fileName) } @IBAction func nextBtnPressed(_ sender: Any) { stepCounter += 1 stepThrough(ptr: stepCounter) } @IBAction func backBtnPressed(_ sender: Any) { self.dismiss(animated: true, completion: nil) } var stepCounter: Int = 0 var fileName: String = "" var player = AVAudioPlayer() let defaults = UserDefaults.standard override func viewDidLoad() { super.viewDidLoad() vCtrl.backgroundColor = UIColor.secondarySystemBackground textLabel.textColor = UIColor.secondaryLabel textLabel.text = "Now we will learn the different haptic patterns for navigating with Guibe" textLabel.accessibilityLabel = "Now we will learn the different haptic patterns for navigationg with ghibe" mainButton.accessibilityLabel = "Play vibration" mainButton.alpha = 0.0 nextBtn.accessibilityLabel = "Next" // Do any additional setup after loading the view. } func stepThrough(ptr:Int) { switch ptr { case 1: beginJourney() self.fileName = "guibe_begin" case 2: arrivedAtDestination() self.fileName = "guibe_arrived" case 3: wrongDirection() self.fileName = "guibe_wrongdirection" case 4: turnRight() let var1 = "guibe_right" let var2 = "guibe_left" if defaults.integer(forKey: "handSide") == 1{ if defaults.integer(forKey: "bigSide") == 1{ self.fileName = var1 } else if defaults.integer(forKey: "bigSide") == 2 { self.fileName = var2 } else { errorMsg() } } else if defaults.integer(forKey: "handSide") == 2 { if defaults.integer(forKey: "bigSide") == 1{ self.fileName = var2 } else if defaults.integer(forKey: "bigSide") == 2 { self.fileName = var1 } else { errorMsg() } } else { errorMsg() } case 5: turnLeft() let var1 = "guibe_left" let var2 = "guibe_right" if defaults.integer(forKey: "handSide") == 1{ if defaults.integer(forKey: "bigSide") == 1{ self.fileName = var1 } else if defaults.integer(forKey: "bigSide") == 2 { self.fileName = var2 } else { errorMsg() } } else if defaults.integer(forKey: "handSide") == 2 { if defaults.integer(forKey: "bigSide") == 1{ self.fileName = var2 } else if defaults.integer(forKey: "bigSide") == 2 { self.fileName = var1 } else { errorMsg() } } else { errorMsg() } case 6: showDone() self.fileName = "guibe_arrived" case 7: nextPage() default: return } } func beginJourney() { UIView.animate(withDuration: 0.5, animations: {self.mainButton.alpha = 1.0}) self.textLabel.fadeTransition(0.5) self.textLabel.text = "This means \n'begin journey'" self.progressBar.setProgress(0.2, animated: true) self.textLabel.accessibilityLabel = "This means begin journey" } func arrivedAtDestination() { self.textLabel.fadeTransition(0.5) self.textLabel.text = "This means \n'arrived at destination'" self.progressBar.setProgress(0.4, animated: true) self.textLabel.accessibilityLabel = "This means arrived at destination" } func wrongDirection() { self.textLabel.fadeTransition(0.5) self.textLabel.text = "This means \n'you're walking in the wrong direction'" self.progressBar.setProgress(0.6, animated: true) self.textLabel.accessibilityLabel = "This means you're walking in the wrong direction" } func turnRight() { self.textLabel.fadeTransition(0.5) self.textLabel.text = "This means \n'turn right'" self.progressBar.setProgress(0.8, animated: true) self.textLabel.accessibilityLabel = "This means turn right" } func turnLeft() { self.textLabel.fadeTransition(0.5) self.textLabel.text = "This means \n'turn left'" self.progressBar.setProgress(1.0, animated: true) self.textLabel.accessibilityLabel = "This means turn left" } func showDone() { self.textLabel.fadeTransition(0.5) self.textLabel.text = "You are all set!" UIView.animate(withDuration: 0.5, animations: {self.mainButton.alpha = 0.0}) self.progressBar.alpha = 0.0 self.textLabel.accessibilityLabel = "You are all set" } func nextPage() { self.dismiss(animated: true, completion: nil) } func audioPlay (fName: String) { let path = Bundle.main.path(forResource: fName, ofType : "mp3")! let url = URL(fileURLWithPath : path) do { player = try AVAudioPlayer(contentsOf: url) player.delegate = self player.play() } catch { debugPrint("Error in playing sound file \(fName).mp3") } } func errorMsg() { let alert = UIAlertController(title: "Playback Error", message: "Error in either keyValues or filename", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in switch action.style{ case .default: self.dismiss(animated: true, completion: nil) case .cancel: return case .destructive: return }})) self.present(alert, animated: true, completion: nil) } }