GithubPermalinkViewController.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import UIKit
  6. import Foundation
  7. import SwiftyAttributes
  8. @objcMembers class GithubPermalinkViewController: UIViewController, UITextViewDelegate {
  9. @IBOutlet public weak var sourceWithNumbersTextView: UITextView!
  10. @IBOutlet public weak var sourceWithoutNumbersTextView: UITextView!
  11. @IBOutlet public weak var ownerLabel: UILabel!
  12. @IBOutlet public weak var repoLabel: UILabel!
  13. @IBOutlet public weak var fileLabel: UILabel!
  14. @IBOutlet public weak var sourceCodeLeftConstraint: NSLayoutConstraint!
  15. @IBOutlet public weak var lineNumbersRightConstraint: NSLayoutConstraint!
  16. private var url: String?
  17. private var sourceWithLineNumbers = NSAttributedString()
  18. private var sourceWithoutLineNumbers = NSAttributedString()
  19. private var owner = ""
  20. private var repo = ""
  21. private var filePath = ""
  22. private var lineNumberWidth: CGFloat = 0
  23. init(url: String,
  24. sourceWithLineNumbers: NSAttributedString,
  25. sourceWithoutLineNumbers: NSAttributedString,
  26. owner: String,
  27. repo: String,
  28. filePath: String,
  29. lineNumberWidth: CGFloat) {
  30. super.init(nibName: "GithubPermalinkViewController", bundle: nil)
  31. self.url = url
  32. self.sourceWithLineNumbers = sourceWithLineNumbers
  33. self.sourceWithoutLineNumbers = sourceWithoutLineNumbers
  34. self.owner = owner
  35. self.repo = repo
  36. self.filePath = filePath
  37. self.lineNumberWidth = lineNumberWidth
  38. }
  39. required init?(coder: NSCoder) {
  40. super.init(coder: coder)
  41. }
  42. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  43. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  44. }
  45. override func viewDidLoad() {
  46. super.viewDidLoad()
  47. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCAppBranding.themeTextColor()]
  48. self.navigationController?.navigationBar.tintColor = NCAppBranding.themeTextColor()
  49. self.navigationController?.navigationBar.barTintColor = NCAppBranding.themeColor()
  50. self.navigationController?.navigationBar.isTranslucent = false
  51. self.navigationItem.title = NSLocalizedString("Source code", comment: "")
  52. let appearance = UINavigationBarAppearance()
  53. appearance.configureWithOpaqueBackground()
  54. appearance.titleTextAttributes = [.foregroundColor: NCAppBranding.themeTextColor()]
  55. appearance.backgroundColor = NCAppBranding.themeColor()
  56. self.navigationItem.standardAppearance = appearance
  57. self.navigationItem.compactAppearance = appearance
  58. self.navigationItem.scrollEdgeAppearance = appearance
  59. self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(self.cancelButtonPressed))
  60. self.navigationItem.leftBarButtonItem?.tintColor = NCAppBranding.themeTextColor()
  61. let githubButton = UIBarButtonItem(image: UIImage(named: "github")?.withRenderingMode(.alwaysTemplate), style: .plain, target: self, action: #selector(githubButtonPressed))
  62. self.navigationItem.rightBarButtonItem = githubButton
  63. self.navigationItem.rightBarButtonItem?.tintColor = NCAppBranding.themeTextColor()
  64. let font = Font.systemFont(ofSize: 16)
  65. let fontSemibold = Font.systemFont(ofSize: 16, weight: .semibold)
  66. self.sourceWithNumbersTextView.textContainerInset = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
  67. self.sourceWithoutNumbersTextView.textContainerInset = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
  68. self.sourceWithNumbersTextView.layer.cornerRadius = 8
  69. self.sourceWithoutNumbersTextView.layer.cornerRadius = 8
  70. self.sourceWithNumbersTextView.textContainer.lineFragmentPadding = 0
  71. self.sourceWithoutNumbersTextView.textContainer.lineFragmentPadding = 0
  72. self.sourceWithNumbersTextView.attributedText = sourceWithLineNumbers
  73. self.sourceWithoutNumbersTextView.attributedText = sourceWithoutLineNumbers
  74. // Set the delgate to synchronize scrolling
  75. self.sourceWithoutNumbersTextView.delegate = self
  76. // We have to reduce the size of our overlaying view depending on how big the line numbers are
  77. // Take safe-area padding of 10 into account here
  78. self.sourceCodeLeftConstraint.constant = self.lineNumberWidth + 10
  79. var formattedOwner = NSLocalizedString("Owner", comment: "Owner of a repository").attributedString + ": ".attributedString
  80. formattedOwner = formattedOwner.withFont(fontSemibold).withTextColor(.secondaryLabel)
  81. formattedOwner += self.owner.withFont(font)
  82. self.ownerLabel.attributedText = formattedOwner
  83. var formattedRepo = NSLocalizedString("Repo", comment: "Name of a repository").attributedString + ": ".attributedString
  84. formattedRepo = formattedRepo.withFont(fontSemibold).withTextColor(.secondaryLabel)
  85. formattedRepo += self.repo.withFont(font)
  86. self.repoLabel.attributedText = formattedRepo
  87. var formattedPath = NSLocalizedString("File", comment: "Filename of a file").attributedString + ": ".attributedString
  88. formattedPath = formattedPath.withFont(fontSemibold).withTextColor(.secondaryLabel)
  89. formattedPath += self.filePath.withFont(font)
  90. self.fileLabel.attributedText = formattedPath
  91. }
  92. func githubButtonPressed() {
  93. if let url = self.url {
  94. NCUtils.openLinkInBrowser(link: url)
  95. }
  96. }
  97. func cancelButtonPressed() {
  98. self.dismiss(animated: true, completion: nil)
  99. }
  100. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  101. self.sourceWithNumbersTextView.contentOffset = self.sourceWithoutNumbersTextView.contentOffset
  102. }
  103. }