NCEmptyDataSet.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 fillBackgroundView: NCFillBackgroundView?
  15. var delegate: NCEmptyDataSetDelegate?
  16. var timer: Timer?
  17. var numberItemsForSections: Int = 0
  18. init(view: UIView, offset: CGFloat = 0, delegate: NCEmptyDataSetDelegate?) {
  19. super.init()
  20. if let fillBackgroundView = UINib(nibName: "NCFillBackgroundView", bundle: nil).instantiate(withOwner: self, options: nil).first as? NCFillBackgroundView {
  21. self.fillBackgroundView = fillBackgroundView
  22. fillBackgroundView.isHidden = true
  23. fillBackgroundView.translatesAutoresizingMaskIntoConstraints = false
  24. view.addSubview(fillBackgroundView)
  25. fillBackgroundView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
  26. fillBackgroundView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
  27. fillBackgroundView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
  28. fillBackgroundView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
  29. }
  30. if let emptyView = UINib(nibName: "NCEmptyView", bundle: nil).instantiate(withOwner: self, options: nil).first as? NCEmptyView {
  31. self.delegate = delegate
  32. self.emptyView = emptyView
  33. emptyView.isHidden = true
  34. emptyView.translatesAutoresizingMaskIntoConstraints = false
  35. // emptyView.backgroundColor = .red
  36. // emptyView.isHidden = false
  37. emptyView.emptyTitle.sizeToFit()
  38. emptyView.emptyDescription.sizeToFit()
  39. view.addSubview(emptyView)
  40. emptyView.widthAnchor.constraint(equalToConstant: 350).isActive = true
  41. emptyView.heightAnchor.constraint(equalToConstant: 250).isActive = true
  42. emptyView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
  43. emptyView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: offset).isActive = true
  44. }
  45. }
  46. func numberOfItemsInSection(_ num: Int, section: Int) {
  47. if section == 0 {
  48. numberItemsForSections = num
  49. } else {
  50. numberItemsForSections = numberItemsForSections + num
  51. }
  52. if let emptyView = emptyView {
  53. self.delegate?.emptyDataSetView?(emptyView)
  54. if !(timer?.isValid ?? false) && emptyView.isHidden == true {
  55. timer = Timer.scheduledTimer(timeInterval: 0.3, target: self, selector: #selector(timerHandler(_:)), userInfo: nil, repeats: false)
  56. }
  57. if numberItemsForSections > 0 {
  58. self.emptyView?.isHidden = true
  59. }
  60. }
  61. }
  62. @objc func timerHandler(_ timer: Timer) {
  63. if numberItemsForSections == 0 {
  64. self.emptyView?.isHidden = false
  65. } else {
  66. self.emptyView?.isHidden = true
  67. }
  68. }
  69. }
  70. public class NCEmptyView: UIView {
  71. @IBOutlet weak var emptyImage: UIImageView!
  72. @IBOutlet weak var emptyTitle: UILabel!
  73. @IBOutlet weak var emptyDescription: UILabel!
  74. public override func awakeFromNib() {
  75. super.awakeFromNib()
  76. emptyTitle.textColor = NCBrandColor.shared.label
  77. }
  78. }
  79. public class NCFillBackgroundView: UIView {
  80. @IBOutlet weak var fillBackground: UIImageView!
  81. public override func awakeFromNib() {
  82. super.awakeFromNib()
  83. }
  84. }