NCEmptyDataSet.swift 4.0 KB

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