Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.61 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // AwsTest
  4. //
  5. // Created by YOUNGSIC KIM on 2019-03-04.
  6. // Copyright © 2019 YOUNGSIC KIM. All rights reserved.
  7. //
  8. import UIKit
  9. import AWSAppSync
  10.  
  11. enum SelectedQuery{
  12. case INSERT
  13. case UPDATE
  14. case SELECT
  15. case DELETE
  16. }
  17.  
  18. class ViewController: UIViewController {
  19.  
  20. @IBOutlet weak var addDataView: UIView! {
  21. didSet {
  22. addDataView.isHidden = true
  23. }
  24. }
  25. @IBOutlet weak var idTextField: UITextField!
  26. @IBOutlet weak var nameTextField: UITextField!
  27. @IBOutlet weak var descriptionTextField: UITextField!
  28.  
  29. @IBOutlet weak var idLabel: UILabel!
  30. @IBOutlet weak var nameLabel: UILabel!
  31. @IBOutlet weak var desctiptionLabel: UILabel!
  32.  
  33. @IBOutlet weak var choiceButton: UILabel!
  34. @IBOutlet weak var selectedTable: UITableView! {
  35. didSet {
  36. selectedTable.isHidden = true
  37. }
  38. }
  39. var selectedQuery: SelectedQuery!
  40. var nameArray:NSMutableArray = NSMutableArray()
  41. var descriptionArray:NSMutableArray = NSMutableArray()
  42.  
  43. var appSyncClient: AWSAppSyncClient?
  44. override func viewDidLoad() {
  45. super.viewDidLoad()
  46. // Do any additional setup after loading the view, typically from a nib.
  47. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  48. appSyncClient = appDelegate.appSyncClient
  49. }
  50.  
  51. // MARK: StoryBoard Actions
  52.  
  53. @IBAction func insertAction() {
  54. showView()
  55. idTextField.isHidden = true
  56. idLabel.isHidden = true
  57. selectedQuery = SelectedQuery.INSERT
  58. }
  59.  
  60. @IBAction func updateAction() {
  61. showView()
  62. selectedQuery = SelectedQuery.UPDATE
  63. }
  64.  
  65. @IBAction func selectAction() {
  66. addDataView.isHidden = true
  67. selectedTable.isHidden = false
  68. selectedQuery = SelectedQuery.SELECT
  69. selectData()
  70. }
  71.  
  72. @IBAction func delectAction() {
  73. showView()
  74. nameLabel.isHidden = true
  75. nameTextField.isHidden = true
  76. desctiptionLabel.isHidden = true
  77. descriptionTextField.isHidden = true
  78. selectedQuery = SelectedQuery.DELETE
  79. }
  80.  
  81. @IBAction func doItNow() {
  82. switch selectedQuery {
  83. case .INSERT?:
  84. if nameTextField.text!.count < 1 || descriptionTextField.text!.count < 1 {
  85. showAlert(messageString: "You have to insert data")
  86. }else {
  87. insertData()
  88. }
  89. break
  90. case .UPDATE?:
  91. if idTextField.text!.count < 1 || nameTextField.text!.count < 1 || descriptionTextField.text!.count < 1 {
  92. showAlert(messageString: "You have to insert data")
  93. }else {
  94. updateData()
  95. }
  96. break
  97. case .DELETE?:
  98. if idTextField.text!.count < 1{
  99. showAlert(messageString: "You have to insert data")
  100. }else {
  101. deleteData()
  102. }
  103. break
  104. case .SELECT?:
  105. selectData()
  106. break
  107. case .none:
  108. break
  109. }
  110. }
  111.  
  112. func showView() {
  113. choiceButton.isHidden = true
  114. selectedTable.isHidden = true
  115. addDataView.isHidden = false
  116. idLabel.isHidden = false
  117. nameLabel.isHidden = false
  118. desctiptionLabel.isHidden = false
  119. idTextField.isHidden = false
  120. idTextField.text = ""
  121. nameTextField.isHidden = false
  122. nameTextField.text = ""
  123. descriptionTextField.isHidden = false
  124. descriptionTextField.text = ""
  125.  
  126. }
  127.  
  128. func showAlert(messageString: String) {
  129. let alertController = UIAlertController(title: "Alert Message", message: messageString, preferredStyle: .alert)
  130. let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default)
  131. alertController.addAction(okAction)
  132. self.present(alertController, animated: true, completion: nil)
  133. }
  134.  
  135. // MARK: AWS APIs
  136.  
  137. func insertData(){
  138. let insertQuery = CreateTodoInput(name: nameTextField.text!, description:descriptionTextField.text!)
  139. appSyncClient?.perform(mutation: CreateTodoMutation(input: insertQuery)) { (result, error) in
  140. self.selectData()
  141. if let error = error as? AWSAppSyncClientError {
  142. print("Error occurred: \(error.localizedDescription )")
  143. }else if let resultError = result?.errors {
  144. print("Error saving the item on server: \(resultError)")
  145. return
  146. }else {
  147. self.showAlert(messageString: "Success Insert Data!! \n Check data in server !")
  148. print("Success Insert Data")
  149. }
  150. }
  151. }
  152.  
  153. func selectData(){
  154. let selectQuery = ListTodosQuery()
  155. // var filter = ModelTodoFilterInput()
  156. // var nameString = ModelStringFilterInput()
  157. // nameString.eq = "Name2"
  158. // filter.name = nameString
  159. // selectQuery.filter = filter
  160. appSyncClient?.fetch(query: selectQuery, cachePolicy: .fetchIgnoringCacheData) {(result, error) in
  161. if error != nil {
  162. print(error?.localizedDescription ?? "")
  163. return
  164. }
  165. result?.data?.listTodos?.items!.forEach {
  166.  
  167. print(($0?.name)! + " " + ($0?.description)!)
  168. self.nameArray.add(($0?.name)!)
  169. self.descriptionArray.add(($0?.description)!)
  170. self.selectedTable.reloadData()
  171. }
  172. }
  173. }
  174.  
  175. func updateData() {
  176. var updateQuery = UpdateTodoInput(id: idTextField.text!)
  177. updateQuery.name = nameTextField.text!
  178. updateQuery.description = descriptionTextField.text!
  179. appSyncClient?.perform(mutation: UpdateTodoMutation(input: updateQuery)) { (result, error) in
  180. if let error = error as? AWSAppSyncClientError {
  181. print("Error occurred: \(error.localizedDescription )")
  182. }else if let resultError = result?.errors {
  183. print("Error saving the item on server: \(resultError)")
  184. return
  185. }else {
  186. self.showAlert(messageString: "Success Update Data!! \n Check data in server !")
  187. print("Success Update Data")
  188. }
  189. }
  190. }
  191.  
  192. func deleteData() {
  193. let deleteQuery = DeleteTodoInput(id: idTextField.text!)
  194. appSyncClient?.perform(mutation: DeleteTodoMutation(input: deleteQuery)) { (result, error) in
  195. if let error = error as? AWSAppSyncClientError {
  196. print("Error occurred: \(error.localizedDescription )")
  197. }else if let resultError = result?.errors {
  198. print("Error saving the item on server: \(resultError)")
  199. return
  200. }else {
  201. self.showAlert(messageString: "Success Delete Data!! \n Check data in server !")
  202. print("Success Delete Data")
  203. }
  204. }
  205. }
  206. }
  207.  
  208. extension ViewController:UITableViewDelegate,UITableViewDataSource {
  209. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  210. if nameArray.count < 1 {
  211. return 1
  212. }else {
  213. return nameArray.count
  214. }
  215. }
  216.  
  217. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  218. let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "tableCell")!
  219. if nameArray.count < 1 {
  220. cell.textLabel?.text = "No data"
  221. }else {
  222. cell.textLabel?.text = nameArray[indexPath.row] as? String
  223. cell.detailTextLabel?.text = descriptionArray[indexPath.row] as? String
  224. }
  225. return cell
  226. }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement