ReferenceGithubView.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import Foundation
  6. @objcMembers class ReferenceGithubView: UIView {
  7. @IBOutlet var contentView: UIView!
  8. @IBOutlet weak var referenceTypeIcon: UIImageView!
  9. @IBOutlet weak var referenceTitle: UILabel!
  10. @IBOutlet weak var referenceBody: UITextView!
  11. @IBOutlet weak var referenceCommentCount: UILabel!
  12. @IBOutlet weak var referenceCommentIcon: UIImageView!
  13. var url: String?
  14. override init(frame: CGRect) {
  15. super.init(frame: frame)
  16. commonInit()
  17. }
  18. required init?(coder aDecoder: NSCoder) {
  19. super.init(coder: aDecoder)
  20. commonInit()
  21. }
  22. func commonInit() {
  23. Bundle.main.loadNibNamed("ReferenceGithubView", owner: self, options: nil)
  24. contentView.frame = self.bounds
  25. contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  26. referenceTitle.text = ""
  27. referenceBody.text = ""
  28. referenceTypeIcon.image = nil
  29. // Remove padding from textView and adjust lineBreakMode
  30. referenceBody.textContainerInset = .zero
  31. referenceBody.textContainer.lineFragmentPadding = .zero
  32. referenceBody.textContainer.lineBreakMode = .byTruncatingTail
  33. referenceBody.textContainer.maximumNumberOfLines = 3
  34. let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap))
  35. contentView.addGestureRecognizer(tap)
  36. self.addSubview(contentView)
  37. }
  38. func handleTap() {
  39. if let url = url {
  40. NCUtils.openLinkInBrowser(link: url)
  41. }
  42. }
  43. func setIcon(for reference: [String: AnyObject]) {
  44. let type = reference["github_type"] as? String
  45. let state = reference["state"] as? String
  46. let stateReason = reference["state_reason"] as? String
  47. var image: UIImage? = UIImage(named: "github-issue-open")?.withTintColor(UIColor.systemGreen)
  48. if type == "issue" {
  49. if state == "closed" {
  50. if stateReason == "not_planned" {
  51. image = UIImage(named: "github-issue-notplanned")?.withTintColor(UIColor.systemGray)
  52. } else {
  53. image = UIImage(named: "github-issue-closed")?.withTintColor(UIColor.systemPurple)
  54. }
  55. }
  56. } else if type == "pull_request" || type == "pr" {
  57. image = UIImage(named: "github-pr-open")?.withTintColor(UIColor.systemGreen)
  58. if state == "open" {
  59. if reference["draft"] as? Bool == true {
  60. image = UIImage(named: "github-pr-draft")?.withTintColor(UIColor.systemGray)
  61. }
  62. } else if state == "closed" {
  63. if reference["merged"] as? Bool == true {
  64. image = UIImage(named: "github-pr-merged")?.withTintColor(UIColor.systemPurple)
  65. } else {
  66. image = UIImage(named: "github-pr-closed")?.withTintColor(UIColor.systemRed)
  67. }
  68. }
  69. }
  70. if image != nil {
  71. referenceTypeIcon.image = image
  72. }
  73. }
  74. func update(for reference: [String: AnyObject], and url: String) {
  75. self.url = url
  76. if let type = reference["github_type"] as? String {
  77. if type == "pr-error" || type == "issue-error" {
  78. referenceTitle.text = NSLocalizedString("GitHub API error", comment: "")
  79. if let bodyDict = reference["body"] as? [String: String],
  80. let body = bodyDict["message"] {
  81. referenceBody.text = body
  82. } else {
  83. referenceBody.text = NSLocalizedString("Unknown error", comment: "")
  84. }
  85. referenceCommentCount.isHidden = true
  86. referenceCommentIcon.isHidden = true
  87. referenceTypeIcon.image = UIImage(named: "github")?.withTintColor(UIColor.systemGray)
  88. return
  89. }
  90. }
  91. setIcon(for: reference)
  92. referenceCommentCount.isHidden = false
  93. referenceCommentIcon.isHidden = false
  94. if let comments = reference["comments"] as? Int {
  95. referenceCommentCount.text = String(comments)
  96. } else {
  97. referenceCommentCount.text = "0"
  98. }
  99. if let title = reference["title"] as? String {
  100. referenceTitle.text = title
  101. }
  102. if let body = reference["body"] as? String {
  103. referenceBody.text = body
  104. } else {
  105. referenceBody.text = NSLocalizedString("No description provided", comment: "")
  106. }
  107. }
  108. }