RoomFileBrowserViewController.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // RoomFileBrowserViewController.swift
  3. // DemoApp
  4. //
  5. // Created by CY zhao on 2023/7/4.
  6. //
  7. import Foundation
  8. import UIKit
  9. @objcMembers public class RoomFileBrowserViewController: UITableViewController {
  10. var modelArray: [RoomFileBroswerModel] = []
  11. var documentController: UIDocumentInteractionController? = nil
  12. var bathPath: String
  13. public init(bathPath: String) {
  14. self.bathPath = bathPath
  15. super.init(nibName: nil, bundle: nil)
  16. }
  17. required init?(coder: NSCoder) {
  18. fatalError("init(coder:) has not been implemented")
  19. }
  20. public override func viewDidLoad() {
  21. super.viewDidLoad()
  22. self.initUI()
  23. self.loadData()
  24. }
  25. func initUI() {
  26. self.tableView.rowHeight = RoomFileBroswerCellHeight
  27. self.tableView.register(RoomFileBroswerCell.self, forCellReuseIdentifier: "RoomFileBroswerCell")
  28. self.navigationItem.title = self.bathPath
  29. self.navigationController?.setNavigationBarHidden(false, animated: false)
  30. let backBtnItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
  31. self.navigationItem.backBarButtonItem = backBtnItem
  32. }
  33. func loadData() {
  34. guard let fileList = try? FileManager.default.contentsOfDirectory(atPath: self.bathPath) else {
  35. return
  36. }
  37. let sortedList = fileList.sorted { file1, file2 in
  38. return file1.localizedStandardCompare(file2) == .orderedAscending
  39. }
  40. for fileName in sortedList {
  41. let path = URL(fileURLWithPath: self.bathPath).appendingPathComponent(fileName).path
  42. let model = RoomFileBroswerModel(title: fileName, path: path )
  43. self.modelArray.append(model)
  44. }
  45. }
  46. // MARK: Table view data source
  47. public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  48. return self.modelArray.count
  49. }
  50. public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  51. let model = self.modelArray[indexPath.row]
  52. let cell = tableView.dequeueReusableCell(withIdentifier: "RoomFileBroswerCell") as! RoomFileBroswerCell
  53. cell.updateUI(model: model)
  54. return cell
  55. }
  56. public override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  57. let model = self.modelArray[indexPath.row]
  58. var isDirectory: ObjCBool = false
  59. FileManager.default.fileExists(atPath: model.path, isDirectory: &isDirectory)
  60. if isDirectory.boolValue {
  61. let vc = RoomFileBrowserViewController(bathPath: self.bathPath)
  62. vc.bathPath = model.path
  63. self.navigationController?.pushViewController(vc, animated: true)
  64. } else {
  65. self.handleFile(model: model)
  66. }
  67. }
  68. func handleFile(model: RoomFileBroswerModel) {
  69. self.documentController = UIDocumentInteractionController(url: URL(fileURLWithPath: model.path))
  70. self.documentController?.presentOpenInMenu(from: CGRectZero, in: self.view, animated: true)
  71. }
  72. }