NCEmptyDataSet.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // 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 NCEmptyDataSetDelegate: AnyObject {
  25. func emptyDataSetView(_ view: NCEmptyView)
  26. }
  27. // optional func
  28. public extension NCEmptyDataSetDelegate {
  29. func emptyDataSetView(_ view: NCEmptyView) {}
  30. }
  31. class NCEmptyDataSet: NSObject {
  32. private var emptyView: NCEmptyView?
  33. private var timer: Timer?
  34. private var numberItemsForSections: Int = 0
  35. private weak var delegate: NCEmptyDataSetDelegate?
  36. private var fillBackgroundName: String = ""
  37. private var fillBackgroundView = UIImageView()
  38. private var centerXAnchor: NSLayoutConstraint?
  39. private var centerYAnchor: NSLayoutConstraint?
  40. init(view: UIView, offset: CGFloat = 0, delegate: NCEmptyDataSetDelegate?) {
  41. super.init()
  42. guard let emptyView = NCEmptyView.fromNib().instantiate(withOwner: self, options: nil).first as? NCEmptyView else { return }
  43. self.delegate = delegate
  44. self.emptyView = emptyView
  45. emptyView.isHidden = true
  46. emptyView.translatesAutoresizingMaskIntoConstraints = false
  47. view.addSubview(emptyView)
  48. emptyView.widthAnchor.constraint(equalToConstant: 350).isActive = true
  49. emptyView.heightAnchor.constraint(equalToConstant: 250).isActive = true
  50. if let view = view.superview {
  51. centerXAnchor = emptyView.centerXAnchor.constraint(equalTo: view.centerXAnchor)
  52. centerYAnchor = emptyView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: offset)
  53. } else {
  54. centerXAnchor = emptyView.centerXAnchor.constraint(equalTo: view.centerXAnchor)
  55. centerYAnchor = emptyView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: offset)
  56. }
  57. centerXAnchor?.isActive = true
  58. centerYAnchor?.isActive = true
  59. }
  60. func setOffset(_ offset: CGFloat) {
  61. centerYAnchor?.constant = offset
  62. }
  63. func numberOfItemsInSection(_ num: Int, section: Int) {
  64. if section == 0 {
  65. numberItemsForSections = num
  66. } else {
  67. numberItemsForSections += num
  68. }
  69. if let emptyView = emptyView {
  70. self.delegate?.emptyDataSetView(emptyView)
  71. if !(timer?.isValid ?? false) && emptyView.isHidden == true {
  72. timer = Timer.scheduledTimer(timeInterval: 0.3, target: self, selector: #selector(timerHandler(_:)), userInfo: nil, repeats: false)
  73. }
  74. if numberItemsForSections > 0 {
  75. self.emptyView?.isHidden = true
  76. }
  77. }
  78. }
  79. @objc func timerHandler(_ timer: Timer) {
  80. if numberItemsForSections == 0 {
  81. self.emptyView?.isHidden = false
  82. } else {
  83. self.emptyView?.isHidden = true
  84. }
  85. }
  86. }
  87. public class NCEmptyView: UIView {
  88. @IBOutlet weak var emptyImage: UIImageView!
  89. @IBOutlet weak var emptyTitle: UILabel!
  90. @IBOutlet weak var emptyDescription: UILabel!
  91. static func fromNib() -> UINib {
  92. return UINib(nibName: "NCEmptyView", bundle: nil)
  93. }
  94. public override func awakeFromNib() {
  95. super.awakeFromNib()
  96. emptyTitle.textColor = .label
  97. }
  98. }