LogListViewController.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // LogListViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2017/6/2.
  6. // Copyright © 2017年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import QuickLook
  10. class LogListViewController: UIViewController {
  11. fileprivate let qlController = QLPreviewController()
  12. fileprivate var currentFileURLS:[NSURL] = []
  13. @IBOutlet weak var tableView: UITableView!
  14. var logFiles:[O2LogFileInfo] = O2Logger.getLogFiles()
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  17. qlController.delegate = self
  18. qlController.dataSource = self
  19. self.tableView.dataSource = self
  20. self.tableView.delegate = self
  21. // Do any additional setup after loading the view.
  22. }
  23. override func didReceiveMemoryWarning() {
  24. super.didReceiveMemoryWarning()
  25. // Dispose of any resources that can be recreated.
  26. }
  27. @objc fileprivate func qlCloseWindow(){
  28. self.dismiss(animated: true, completion: {
  29. })
  30. }
  31. /*
  32. // MARK: - Navigation
  33. // In a storyboard-based application, you will often want to do a little preparation before navigation
  34. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  35. // Get the new view controller using segue.destinationViewController.
  36. // Pass the selected object to the new view controller.
  37. }
  38. */
  39. }
  40. extension LogListViewController:QLPreviewControllerDataSource,QLPreviewControllerDelegate{
  41. func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
  42. return self.currentFileURLS.count
  43. }
  44. func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
  45. return self.currentFileURLS[index]
  46. }
  47. func previewControllerWillDismiss(_ controller: QLPreviewController) {
  48. }
  49. }
  50. extension LogListViewController:UITableViewDelegate,UITableViewDataSource {
  51. func numberOfSections(in tableView: UITableView) -> Int {
  52. return 1
  53. }
  54. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  55. return logFiles.count
  56. }
  57. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  58. let fileCell = tableView.dequeueReusableCell(withIdentifier: "LogFileCell", for: indexPath) as! LogFileTableViewCell
  59. let logFile = logFiles[indexPath.row]
  60. fileCell.fileNameLabel.text = logFile.friendFileName
  61. fileCell.fileSizeLabel.text = String(logFile.fileSize)
  62. return fileCell
  63. }
  64. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  65. let logFile = logFiles[indexPath.row]
  66. let currentURL = NSURL(fileURLWithPath: logFile.filePath)
  67. self.currentFileURLS.removeAll(keepingCapacity: true)
  68. if QLPreviewController.canPreview(currentURL) {
  69. self.currentFileURLS.append(currentURL)
  70. self.qlController.reloadData()
  71. if #available(iOS 10, *) {
  72. let navVC = ZLNormalNavViewController(rootViewController: self.qlController)
  73. self.qlController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .plain, target: self, action: #selector(self.qlCloseWindow))
  74. self.presentVC(navVC)
  75. }else{
  76. //if #available(iOS 9, *){
  77. let prController = CMSQLViewController()
  78. prController.delegate = self
  79. prController.dataSource = self
  80. self.pushVC(prController)
  81. //}
  82. }
  83. }
  84. }
  85. func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  86. return true
  87. }
  88. func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
  89. let action1 = UITableViewRowAction(style: .destructive, title: "删除") { (action, indexPath) in
  90. self._delete(indexPath)
  91. }
  92. return [action1]
  93. }
  94. private func _delete(_ indexPath:IndexPath){
  95. let logFile = logFiles[indexPath.row]
  96. let currentURL = URL(fileURLWithPath: logFile.filePath)
  97. do {
  98. try FileManager.default.removeItem(at: currentURL)
  99. }catch(let error){
  100. O2Logger.error(error.localizedDescription)
  101. }
  102. logFiles.remove(at: indexPath.row)
  103. self.tableView.reloadData()
  104. }
  105. }