NCViewCertificateDetails.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // NCViewCertificateDetails.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 01/06/21.
  6. // Copyright © 2021 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import SwiftUI
  25. public protocol NCViewCertificateDetailsDelegate: AnyObject {
  26. func viewCertificateDetailsDismiss(host: String)
  27. }
  28. // optional func
  29. public extension NCViewCertificateDetailsDelegate {
  30. func viewCertificateDetailsDismiss(host: String) {}
  31. }
  32. class NCViewCertificateDetails: UIViewController {
  33. @IBOutlet weak var buttonCancel: UIBarButtonItem!
  34. @IBOutlet weak var scrollView: UIScrollView!
  35. @IBOutlet weak var textView: UITextView!
  36. public var delegate: NCViewCertificateDetailsDelegate?
  37. @objc public var host: String = ""
  38. public var fileNamePath: String = ""
  39. @objc public var certificateTitle = NSLocalizedString("_certificate_view_", comment: "")
  40. // MARK: - View Life Cycle
  41. override func viewDidLoad() {
  42. super.viewDidLoad()
  43. self.navigationItem.title = certificateTitle
  44. buttonCancel.title = NSLocalizedString("_close_", comment: "")
  45. if fileNamePath.isEmpty {
  46. fileNamePath = NCUtilityFileSystem.shared.directoryCertificates + "/" + host + ".txt"
  47. }
  48. if FileManager.default.fileExists(atPath: fileNamePath) {
  49. do {
  50. let text = try String(contentsOfFile: fileNamePath, encoding: .utf8)
  51. let font = UIFont.systemFont(ofSize: 13)
  52. let attributes = [NSAttributedString.Key.font: font] as [NSAttributedString.Key: Any]
  53. var contentRect = NSString(string: text).boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: 0), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attributes, context: nil)
  54. contentRect = CGRect(x: contentRect.origin.x, y: contentRect.origin.y, width: ceil(contentRect.size.width), height: ceil(contentRect.size.height))
  55. var contentWidth = contentRect.size.width
  56. if contentWidth < view.frame.size.width {
  57. contentWidth = view.frame.size.width
  58. }
  59. var contentHeight = contentRect.size.height
  60. if contentHeight < view.frame.size.height {
  61. contentHeight = view.frame.size.width
  62. }
  63. textView.frame = CGRect(x: 0, y: 0, width: contentWidth, height: contentHeight)
  64. textView.font = font
  65. textView.text = text
  66. scrollView.contentSize = contentRect.size
  67. scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 50, right: 0)
  68. } catch {
  69. print("error")
  70. }
  71. } else {
  72. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  73. self.dismiss(animated: true, completion: nil)
  74. }
  75. }
  76. }
  77. override func viewWillDisappear(_ animated: Bool) {
  78. super.viewWillDisappear(animated)
  79. self.delegate?.viewCertificateDetailsDismiss(host: host)
  80. }
  81. // MARK: ACTION
  82. @IBAction func actionCancel(_ sender: UIBarButtonItem) {
  83. self.dismiss(animated: true, completion: nil)
  84. }
  85. }
  86. // MARK: - UIViewControllerRepresentable
  87. struct NCViewCertificateDetailsRepresentable: UIViewControllerRepresentable {
  88. typealias UIViewControllerType = UINavigationController
  89. var fileNamePath: String
  90. var title: String
  91. func makeUIViewController(context: Context) -> UINavigationController {
  92. let storyboard = UIStoryboard(name: "NCViewCertificateDetails", bundle: nil)
  93. let navigationController = storyboard.instantiateInitialViewController() as? UINavigationController
  94. let viewController = navigationController?.topViewController as? NCViewCertificateDetails
  95. viewController?.fileNamePath = fileNamePath
  96. viewController?.certificateTitle = title
  97. return navigationController!
  98. }
  99. func updateUIViewController(_ uiViewController: UINavigationController, context: Context) { }
  100. }