NCEmptyDataSet.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. if let emptyView = UINib(nibName: "NCEmptyView", bundle: nil).instantiate(withOwner: self, options: nil).first as? NCEmptyView {
  43. self.delegate = delegate
  44. self.emptyView = emptyView
  45. emptyView.isHidden = true
  46. emptyView.translatesAutoresizingMaskIntoConstraints = false
  47. // emptyView.backgroundColor = .red
  48. // emptyView.isHidden = false
  49. emptyView.emptyTitle.sizeToFit()
  50. emptyView.emptyDescription.sizeToFit()
  51. view.addSubview(emptyView)
  52. emptyView.widthAnchor.constraint(equalToConstant: 350).isActive = true
  53. emptyView.heightAnchor.constraint(equalToConstant: 250).isActive = true
  54. if let view = view.superview {
  55. centerXAnchor = emptyView.centerXAnchor.constraint(equalTo: view.centerXAnchor)
  56. centerYAnchor = emptyView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: offset)
  57. } else {
  58. centerXAnchor = emptyView.centerXAnchor.constraint(equalTo: view.centerXAnchor)
  59. centerYAnchor = emptyView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: offset)
  60. }
  61. centerXAnchor?.isActive = true
  62. centerYAnchor?.isActive = true
  63. }
  64. }
  65. func setOffset(_ offset: CGFloat) {
  66. centerYAnchor?.constant = offset
  67. }
  68. func numberOfItemsInSection(_ num: Int, section: Int) {
  69. if section == 0 {
  70. numberItemsForSections = num
  71. } else {
  72. numberItemsForSections += num
  73. }
  74. if let emptyView = emptyView {
  75. self.delegate?.emptyDataSetView(emptyView)
  76. if !(timer?.isValid ?? false) && emptyView.isHidden == true {
  77. timer = Timer.scheduledTimer(timeInterval: 0.3, target: self, selector: #selector(timerHandler(_:)), userInfo: nil, repeats: false)
  78. }
  79. if numberItemsForSections > 0 {
  80. self.emptyView?.isHidden = true
  81. }
  82. }
  83. }
  84. @objc func timerHandler(_ timer: Timer) {
  85. if numberItemsForSections == 0 {
  86. self.emptyView?.isHidden = false
  87. } else {
  88. self.emptyView?.isHidden = true
  89. }
  90. }
  91. }
  92. public class NCEmptyView: UIView {
  93. @IBOutlet weak var emptyImage: UIImageView!
  94. @IBOutlet weak var emptyTitle: UILabel!
  95. @IBOutlet weak var emptyDescription: UILabel!
  96. public override func awakeFromNib() {
  97. super.awakeFromNib()
  98. emptyTitle.textColor = .label
  99. }
  100. }