NCEmptyDataSet.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 {
  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 superView: UIView?
  33. private var emptyView: NCEmptyView?
  34. private var timer: Timer?
  35. private var numberItemsForSections: Int = 0
  36. private var delegate: NCEmptyDataSetDelegate?
  37. private var fillBackgroundName: String = ""
  38. private var fillBackgroundView = UIImageView()
  39. public var backgroud: String {
  40. get {
  41. return fillBackgroundName
  42. }
  43. set {
  44. self.fillBackgroundName = newValue
  45. if let image = UIImage(named: newValue) {
  46. fillBackgroundView.image = image
  47. if superView is UICollectionView {
  48. (superView as! UICollectionView).backgroundView = fillBackgroundView
  49. }
  50. } else {
  51. fillBackgroundView.image = nil
  52. if superView is UICollectionView {
  53. (superView as! UICollectionView).backgroundView = nil
  54. }
  55. }
  56. }
  57. }
  58. init(view: UIView, offset: CGFloat = 0, delegate: NCEmptyDataSetDelegate?) {
  59. super.init()
  60. self.superView = view
  61. if let emptyView = UINib(nibName: "NCEmptyView", bundle: nil).instantiate(withOwner: self, options: nil).first as? NCEmptyView {
  62. self.delegate = delegate
  63. self.emptyView = emptyView
  64. emptyView.isHidden = true
  65. emptyView.translatesAutoresizingMaskIntoConstraints = false
  66. // emptyView.backgroundColor = .red
  67. // emptyView.isHidden = false
  68. emptyView.emptyTitle.sizeToFit()
  69. emptyView.emptyDescription.sizeToFit()
  70. view.addSubview(emptyView)
  71. emptyView.widthAnchor.constraint(equalToConstant: 350).isActive = true
  72. emptyView.heightAnchor.constraint(equalToConstant: 250).isActive = true
  73. emptyView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
  74. emptyView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: offset).isActive = true
  75. }
  76. }
  77. func numberOfItemsInSection(_ num: Int, section: Int) {
  78. if section == 0 {
  79. numberItemsForSections = num
  80. } else {
  81. numberItemsForSections = numberItemsForSections + num
  82. }
  83. if let emptyView = emptyView {
  84. self.delegate?.emptyDataSetView(emptyView)
  85. if !(timer?.isValid ?? false) && emptyView.isHidden == true {
  86. timer = Timer.scheduledTimer(timeInterval: 0.3, target: self, selector: #selector(timerHandler(_:)), userInfo: nil, repeats: false)
  87. }
  88. if numberItemsForSections > 0 {
  89. self.emptyView?.isHidden = true
  90. }
  91. }
  92. }
  93. @objc func timerHandler(_ timer: Timer) {
  94. if numberItemsForSections == 0 {
  95. self.emptyView?.isHidden = false
  96. } else {
  97. self.emptyView?.isHidden = true
  98. }
  99. }
  100. }
  101. public class NCEmptyView: UIView {
  102. @IBOutlet weak var emptyImage: UIImageView!
  103. @IBOutlet weak var emptyTitle: UILabel!
  104. @IBOutlet weak var emptyDescription: UILabel!
  105. public override func awakeFromNib() {
  106. super.awakeFromNib()
  107. emptyTitle.textColor = NCBrandColor.shared.label
  108. }
  109. }