//
// [Link]
// Assignment 3
//
// Created by Tee Wen Seng on 7/14/19.
// Copyright © 2019 Tee Wen Seng. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
@IBOutlet weak var poemTextView: UITextView!
@IBOutlet weak var dismissKeyboardButton: UIButton!
@IBOutlet weak var rhymeTextField: UITextField!
@IBOutlet weak var selectedTextLabel: UILabel!
@IBOutlet weak var checkWordsButton: UIButton!
@IBOutlet weak var wordsLabel: UILabel!
var selectedWord:String!
var filteredWords:Array<String> = []
var words:Array<String> = []
var activeTextField: UITextField!
var activeTextView: UITextView!
override func viewDidLoad() {
[Link]()
//Initialize the data from property list
setupData()
//Styling text view
[Link] = self
[Link] = [Link]
[Link] = 2.0
[Link] = 10.0
//Set TextView placeholder
[Link] = "Please type your poem here..."
[Link] = [Link]
//Styling text field
[Link] = [Link]
[Link] = 1.0
[Link] = 5.0
//Styling button
[Link] = 5.0
[Link] = 1.0
[Link] = [Link]
//Button to dismiss the keyboard
let btnImage = UIImage(named: "keyboard")
[Link](btnImage, for: [Link])
}
//Function to initialize data
func setupData(){
if let filePath = [Link](forResource: "[Link]", ofType:
"plist"){
if let plistData = [Link](atPath: filePath){
do {
let plistObject = try
[Link](from: plistData,
options: [Link](), format: nil)
words = plistObject as! [String]
} catch {
print("Error serializing data from property list")
}
}else {
print("Error reading data from property list file")
}
} else {
print("Property list file does not exist")
}
}
//Function for action when button pressed
@IBAction func buttonPressed(_ sender: UIButton) {
//Trimming the input
let rhyme =
[Link]!.trimmingCharacters(in: .whitespacesAndNewlines)
[Link] = true;
[Link] = true;
//Action for empty input
if [Link]{
actionForEmptyInput();
}
//Action for input
else {
//Filter the rhyme words
filteredWords = findRhymeWords(words, rhyme)
//Action for rhyme words found
if [Link] {
actionForNoRhymes(rhyme)
}
//Action for empty rhyme words
else {
//performSegue is used instead of directly move to next view
controller
//to trigger other actions based on condition
performSegue(withIdentifier: "toRhymeWords", sender: self)
}
}
}
//Function for preparing the next view
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let secondVC = [Link] as! SecondViewController
[Link] = filteredWords
}
//Function to fliter the rhyme words
func findRhymeWords(_ words:Array<String>,_ rhyme:String) -> Array<String>{
let filteredWords = [Link]({(word: String) -> Bool in
let stringMatch = [Link]().hasSuffix([Link]())
return stringMatch
})
return filteredWords
}
//Function for action when no rhyme words is found
func actionForNoRhymes(_ rhyme:String){
let noRhymeWarning = "There is no word rhymes with '\(rhyme)'"
//Trigger alert
let alertController = UIAlertController(
title: "Invalid Word",
message: noRhymeWarning,
preferredStyle: .alert
)
let okAction = UIAlertAction(
title: "OK",
style: .default,
handler: nil
)
[Link](okAction)
[Link](alertController, animated: true, completion: nil)
}
//Function for empty input
func actionForEmptyInput(){
//Trigger alert
let alertController = UIAlertController(
title: "Empty Input",
message: "Please type the words you want to search for rhyme",
preferredStyle: .alert
)
let okAction = UIAlertAction(
title: "OK",
style: .default,
handler: nil
)
[Link](okAction)
[Link](alertController, animated: true, completion: nil)
}
//Hide placeholder when user pressed the TextView
func textViewDidBeginEditing(_ textView: UITextView) {
if [Link] == [Link] {
[Link] = nil
[Link] = [Link]
}
activeTextView = textView
}
//Show placeholder if the TextView is emptied
func textViewDidEndEditing(_ textView: UITextView) {
if [Link] {
[Link] = "Please type your poem here..."
[Link] = [Link]
}
}
//Function to dismiss the keyboard
@IBAction func dismissKeyboard(_ sender: UIButton) {
[Link](true)
}
}
//To set the padding within the TextView
class UITextViewPadding : UITextView {
required init?(coder aDecoder: NSCoder) {
[Link](coder: aDecoder)
textContainerInset = UIEdgeInsets(top: 15, left: 10, bottom: 15, right: 10)
}
}