FeedbackViewController.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import UIKit
  15. class FeedbackViewController: UIViewController {
  16. // TODO: Consider the situations where this instance is initiated once, and used
  17. // multiple times.
  18. var viewDidDisappearCallback: () -> Void = {}
  19. // (TODO) Can we make feedbackName and additionalFormText non-null?
  20. var releaseName: String?
  21. var additionalFormText: String?
  22. var image: UIImage?
  23. @IBOutlet var screenshotUIImageView: UIImageView!
  24. @IBOutlet var additionalFormTextLabel: UILabel!
  25. @IBOutlet var feedbackTextView: UITextView!
  26. @IBOutlet var navigationBar: UINavigationBar!
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. // Do any additional setup after loading the view.
  30. let additionalFormText = additionalFormText
  31. if additionalFormText != nil {
  32. additionalFormTextLabel.text = additionalFormText
  33. }
  34. let image = image
  35. if image != nil {
  36. screenshotUIImageView.image = image
  37. }
  38. }
  39. override func viewWillAppear(_ animated: Bool) {
  40. screenshotUIImageView.image = image
  41. image = nil
  42. }
  43. @IBAction func tappedSend(_ sender: Any) {
  44. guard let releaseName = releaseName else {
  45. // TODO(tundeagboola) throw error or
  46. return
  47. }
  48. ApiService
  49. .createFeedback(releaseName: releaseName,
  50. feedbackText: feedbackTextView.text) { feedbackName, error in
  51. if error != nil {
  52. // TODO(tundeaboola) handle error if create feedback fails
  53. return
  54. }
  55. guard let feedbackName = feedbackName else {
  56. // TODO(tundeaboola) handle error if create feedback fails
  57. return
  58. }
  59. guard let image = self.screenshotUIImageView.image else {
  60. return self.commitFeedback(feedbackName: feedbackName)
  61. }
  62. ApiService.uploadImage(feedbackName: feedbackName, image: image) { error in
  63. if error != nil {
  64. // TODO(tundeaboola) handle error if upload image fails
  65. return
  66. }
  67. self.commitFeedback(feedbackName: feedbackName)
  68. }
  69. }
  70. }
  71. private func commitFeedback(feedbackName: String) {
  72. ApiService.commitFeedback(feedbackName: feedbackName) { error in
  73. if error != nil {
  74. // TODO(tundeaboola) handle error if commit feedback fails
  75. }
  76. self.feedbackSubmitted()
  77. }
  78. }
  79. @IBAction func tappedCancel(_ sender: Any) {
  80. dismiss(animated: true)
  81. }
  82. override func viewDidDisappear(_ animated: Bool) {
  83. viewDidDisappearCallback()
  84. }
  85. func feedbackSubmitted() {
  86. // TODO(tundeagboola) show success toast
  87. dismiss(animated: true)
  88. }
  89. }