NCViewCertificateDetails.swift 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. public protocol NCViewCertificateDetailsDelegate {
  25. func viewCertificateDetailsDismiss()
  26. }
  27. // optional func
  28. public extension NCViewCertificateDetailsDelegate {
  29. func viewCertificateDetailsDismiss() {}
  30. }
  31. class NCViewCertificateDetails: UIViewController {
  32. @IBOutlet weak var buttonCancel: UIBarButtonItem!
  33. @IBOutlet weak var scrollView: UIScrollView!
  34. @IBOutlet weak var textView: UITextView!
  35. public var delegate: NCViewCertificateDetailsDelegate?
  36. // MARK: - View Life Cycle
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. self.navigationItem.title = NSLocalizedString("_certificate_details_", comment: "")
  40. buttonCancel.title = NSLocalizedString("_close_", comment: "")
  41. let directoryCertificate = CCUtility.getDirectoryCerificates()!
  42. let certificatePath = directoryCertificate + "/" + NCGlobal.shared.certificateTmpV2 + ".txt"
  43. if FileManager.default.fileExists(atPath: certificatePath) {
  44. do {
  45. let text = try String(contentsOfFile: certificatePath, encoding: .utf8)
  46. let font = UIFont.systemFont(ofSize: 13)
  47. let attributes = [NSAttributedString.Key.font: font] as [NSAttributedString.Key : Any]
  48. var contentRect = NSString(string: text).boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: 0), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attributes, context: nil)
  49. contentRect = CGRect(x: contentRect.origin.x, y: contentRect.origin.y, width: ceil(contentRect.size.width), height: ceil(contentRect.size.height))
  50. var contentWidth = contentRect.size.width
  51. if contentWidth < view.frame.size.width {
  52. contentWidth = view.frame.size.width
  53. }
  54. var contentHeight = contentRect.size.height
  55. if contentHeight < view.frame.size.height {
  56. contentHeight = view.frame.size.width
  57. }
  58. textView.frame = CGRect(x: 0, y: 0, width: contentWidth, height: contentHeight)
  59. textView.font = font
  60. textView.text = text
  61. scrollView.contentSize = contentRect.size
  62. scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 50, right: 0)
  63. } catch {
  64. print("error")
  65. }
  66. } else {
  67. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  68. self.dismiss(animated: true, completion: nil)
  69. }
  70. }
  71. }
  72. override func viewWillDisappear(_ animated: Bool) {
  73. super.viewWillDisappear(animated)
  74. self.delegate?.viewCertificateDetailsDismiss()
  75. }
  76. // MARK: ACTION
  77. @IBAction func actionCancel(_ sender: UIBarButtonItem) {
  78. self.dismiss(animated: true, completion: nil)
  79. }
  80. }