NCEmptyDataSet.swift 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 Foundation
  9. class NCEmptyDataSet: NSObject {
  10. var emptyView: NCEmptyView?
  11. init(view: UIView, image: UIImage?, title: String, description: String, offset: CGFloat) {
  12. super.init()
  13. if let emptyView = UINib(nibName: "NCEmptyView", bundle: nil).instantiate(withOwner: self, options: nil).first as? NCEmptyView {
  14. self.emptyView = emptyView
  15. emptyView.frame = CGRect(x:0, y: 0, width:300, height:300)
  16. emptyView.isHidden = true
  17. emptyView.translatesAutoresizingMaskIntoConstraints = false
  18. emptyView.emptyImage.image = image
  19. emptyView.emptyTtle.text = NSLocalizedString(title, comment: "")
  20. emptyView.emptyDescription.text = NSLocalizedString(description, comment: "")
  21. view.addSubview(emptyView)
  22. let constantY: CGFloat = (view.frame.height - emptyView.frame.height) / 2 - offset
  23. emptyView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
  24. emptyView.topAnchor.constraint(equalTo: view.topAnchor, constant: constantY).isActive = true
  25. }
  26. }
  27. func numberOfItemsInSection(_ numberItems: Int) {
  28. if numberItems == 0 {
  29. DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
  30. self.emptyView?.isHidden = false
  31. }
  32. } else {
  33. DispatchQueue.main.asyncAfter(deadline: .now() + 0.0) {
  34. self.emptyView?.isHidden = true
  35. }
  36. }
  37. }
  38. }
  39. class NCEmptyView: UIView {
  40. @IBOutlet weak var emptyImage: UIImageView!
  41. @IBOutlet weak var emptyTtle: UILabel!
  42. @IBOutlet weak var emptyDescription: UILabel!
  43. }