IntroQuestionViewHelper.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2018-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. class IntroQuestionViewHelper: NSObject, IntroQuestionDelegate {
  22. private var parent: IDCreationPageViewController
  23. private var onAnswer: ((Any?, Answer) -> Void)?
  24. private var alert: IntroQuestionView?
  25. private var confirm: IntroQuestionView?
  26. public enum Answer {
  27. case ok
  28. case no
  29. case yes
  30. }
  31. init(parent: IDCreationPageViewController, onAnswer: ((Any?, Answer) -> Void)?) {
  32. self.parent = parent
  33. self.onAnswer = onAnswer
  34. }
  35. func showAlert(_ text: String, title: String? = nil) {
  36. if self.alert == nil {
  37. self.alert = NibUtil.loadViewFromNib(withName: "IntroQuestionView") as? IntroQuestionView
  38. self.alert?.showOnlyOkButton = true
  39. self.alert?.delegate = self
  40. self.alert?.frame = RectUtil.rect(self.alert!.frame, centerIn: self.parent.view.frame, round: true)
  41. self.parent.view.addSubview(self.alert!)
  42. }
  43. if let title = title {
  44. self.alert?.title = title
  45. }
  46. self.alert?.questionLabel.text = text
  47. self.alert?.questionLabel.sizeToFit()
  48. self.parent.showMessageView(self.alert)
  49. }
  50. func showConfirm(_ text: String, noButtonLabel: String? = nil, yesButtonLabel: String? = nil) {
  51. if self.confirm == nil {
  52. self.confirm = NibUtil.loadViewFromNib(withName: "IntroQuestionView") as? IntroQuestionView
  53. self.confirm?.delegate = self
  54. self.confirm?.frame = RectUtil.rect(self.confirm!.frame, centerIn: self.parent.view.frame, round: true)
  55. self.parent.view.addSubview(self.confirm!)
  56. }
  57. self.confirm?.questionLabel.text = text
  58. if let no = noButtonLabel {
  59. self.confirm?.noButton.setTitle(no, for: .normal)
  60. }
  61. if let yes = yesButtonLabel {
  62. self.confirm?.yesButton.setTitle(yes, for: .normal)
  63. }
  64. self.parent.showMessageView(self.confirm)
  65. }
  66. private func answer(_ sender: IntroQuestionView, answer: Answer) {
  67. self.parent.hideMessageView(sender)
  68. if let onAnswer = self.onAnswer {
  69. onAnswer(sender, answer)
  70. }
  71. }
  72. //MARK: - IntroQuestionViewDelegate
  73. @objc func selectedOk(_ sender: IntroQuestionView) {
  74. answer(sender, answer: .ok)
  75. }
  76. @objc func selectedYes(_ sender: IntroQuestionView) {
  77. answer(sender, answer: .yes)
  78. }
  79. @objc func selectedNo(_ sender: IntroQuestionView) {
  80. answer(sender, answer: .no)
  81. }
  82. }