NCEmptyDataSet.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // NCEmptyDataSet.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 19/10/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. import UIKit
  9. @objc public protocol NCEmptyDataSetDelegate {
  10. @objc optional func emptyDataSetView(_ view: NCEmptyView)
  11. }
  12. class NCEmptyDataSet: NSObject {
  13. var emptyView: NCEmptyView?
  14. var delegate: NCEmptyDataSetDelegate?
  15. var timer: Timer?
  16. var numberItemsForSections: Int = 0
  17. init(view: UIView, offset: CGFloat = 0, delegate: NCEmptyDataSetDelegate?) {
  18. super.init()
  19. if let emptyView = UINib(nibName: "NCEmptyView", bundle: nil).instantiate(withOwner: self, options: nil).first as? NCEmptyView {
  20. self.delegate = delegate
  21. self.emptyView = emptyView
  22. emptyView.isHidden = true
  23. emptyView.translatesAutoresizingMaskIntoConstraints = false
  24. // emptyView.backgroundColor = .red
  25. // emptyView.isHidden = false
  26. emptyView.emptyTitle.sizeToFit()
  27. emptyView.emptyDescription.sizeToFit()
  28. view.addSubview(emptyView)
  29. emptyView.widthAnchor.constraint(equalToConstant: 350).isActive = true
  30. emptyView.heightAnchor.constraint(equalToConstant: 250).isActive = true
  31. if let view = view.superview {
  32. emptyView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
  33. emptyView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: offset).isActive = true
  34. } else {
  35. emptyView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
  36. emptyView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: offset).isActive = true
  37. }
  38. }
  39. }
  40. func numberOfItemsInSection(_ num: Int, section: Int) {
  41. if section == 0 {
  42. numberItemsForSections = num
  43. } else {
  44. numberItemsForSections = numberItemsForSections + num
  45. }
  46. if let emptyView = emptyView {
  47. self.delegate?.emptyDataSetView?(emptyView)
  48. if !(timer?.isValid ?? false) && emptyView.isHidden == true {
  49. timer = Timer.scheduledTimer(timeInterval: 0.3, target: self, selector: #selector(timerHandler(_:)), userInfo: nil, repeats: false)
  50. }
  51. if numberItemsForSections > 0 {
  52. self.emptyView?.isHidden = true
  53. }
  54. }
  55. }
  56. @objc func timerHandler(_ timer: Timer) {
  57. if numberItemsForSections == 0 {
  58. self.emptyView?.isHidden = false
  59. } else {
  60. self.emptyView?.isHidden = true
  61. }
  62. }
  63. }
  64. public class NCEmptyView: UIView {
  65. @IBOutlet weak var emptyImage: UIImageView!
  66. @IBOutlet weak var emptyTitle: UILabel!
  67. @IBOutlet weak var emptyDescription: UILabel!
  68. public override func awakeFromNib() {
  69. super.awakeFromNib()
  70. emptyTitle.textColor = NCBrandColor.shared.label
  71. }
  72. }