NCViewCertificateDetails.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. var delegate: NCViewCertificateDetailsDelegate?
  37. let utilityFileSystem = NCUtilityFileSystem()
  38. @objc public var host: String = ""
  39. public var fileNamePath: String = ""
  40. @objc public var certificateTitle = NSLocalizedString("_certificate_view_", comment: "")
  41. // MARK: - View Life Cycle
  42. override func viewDidLoad() {
  43. super.viewDidLoad()
  44. self.navigationItem.title = certificateTitle
  45. buttonCancel.title = NSLocalizedString("_close_", comment: "")
  46. if fileNamePath.isEmpty {
  47. fileNamePath = utilityFileSystem.directoryCertificates + "/" + host + ".txt"
  48. }
  49. if FileManager.default.fileExists(atPath: fileNamePath) {
  50. do {
  51. let text = try String(contentsOfFile: fileNamePath, encoding: .utf8)
  52. let font = UIFont.systemFont(ofSize: 13)
  53. let attributes = [NSAttributedString.Key.font: font] as [NSAttributedString.Key: Any]
  54. var contentRect = NSString(string: text).boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: 0), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attributes, context: nil)
  55. contentRect = CGRect(x: contentRect.origin.x, y: contentRect.origin.y, width: ceil(contentRect.size.width), height: ceil(contentRect.size.height))
  56. var contentWidth = contentRect.size.width
  57. if contentWidth < view.frame.size.width {
  58. contentWidth = view.frame.size.width
  59. }
  60. var contentHeight = contentRect.size.height
  61. if contentHeight < view.frame.size.height {
  62. contentHeight = view.frame.size.width
  63. }
  64. textView.frame = CGRect(x: 0, y: 0, width: contentWidth, height: contentHeight)
  65. textView.font = font
  66. textView.text = text
  67. scrollView.contentSize = contentRect.size
  68. scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 50, right: 0)
  69. } catch {
  70. print("error")
  71. }
  72. } else {
  73. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  74. self.dismiss(animated: true, completion: nil)
  75. }
  76. }
  77. }
  78. override func viewWillDisappear(_ animated: Bool) {
  79. super.viewWillDisappear(animated)
  80. self.delegate?.viewCertificateDetailsDismiss(host: host)
  81. }
  82. // MARK: ACTION
  83. @IBAction func actionCancel(_ sender: UIBarButtonItem) {
  84. self.dismiss(animated: true, completion: nil)
  85. }
  86. }
  87. // MARK: - UIViewControllerRepresentable
  88. struct NCViewCertificateDetailsRepresentable: UIViewControllerRepresentable {
  89. typealias UIViewControllerType = UINavigationController
  90. var fileNamePath: String
  91. var title: String
  92. func makeUIViewController(context: Context) -> UINavigationController {
  93. let storyboard = UIStoryboard(name: "NCViewCertificateDetails", bundle: nil)
  94. let navigationController = storyboard.instantiateInitialViewController() as? UINavigationController
  95. let viewController = navigationController?.topViewController as? NCViewCertificateDetails
  96. viewController?.fileNamePath = fileNamePath
  97. viewController?.certificateTitle = title
  98. return navigationController!
  99. }
  100. func updateUIViewController(_ uiViewController: UINavigationController, context: Context) { }
  101. }