MaterialShowcaseController.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // MaterialShowcaseController.swift
  3. // MaterialShowcase
  4. //
  5. // Created by Andrei Tulai on 2017-11-06.
  6. // Copyright © 2017 Aromajoin. All rights reserved.
  7. //
  8. import Foundation
  9. public protocol MaterialShowcaseControllerDelegate: class {
  10. func materialShowcaseController(_ materialShowcaseController: MaterialShowcaseController,
  11. materialShowcaseWillDisappear materialShowcase: MaterialShowcase,
  12. forIndex index: Int)
  13. func materialShowcaseController(_ materialShowcaseController: MaterialShowcaseController,
  14. materialShowcaseDidDisappear materialShowcase: MaterialShowcase,
  15. forIndex index: Int)
  16. }
  17. public extension MaterialShowcaseControllerDelegate {
  18. func materialShowcaseController(_ materialShowcaseController: MaterialShowcaseController,
  19. materialShowcaseWillDisappear materialShowcase: MaterialShowcase,
  20. forIndex index: Int) {
  21. // do nothing
  22. }
  23. func materialShowcaseController(_ materialShowcaseController: MaterialShowcaseController,
  24. materialShowcaseDidDisappear materialShowcase: MaterialShowcase,
  25. forIndex index: Int) {
  26. // do nothing
  27. }
  28. }
  29. public protocol MaterialShowcaseControllerDataSource: class {
  30. func numberOfShowcases(for materialShowcaseController: MaterialShowcaseController) -> Int
  31. func materialShowcaseController(_ materialShowcaseController: MaterialShowcaseController,
  32. showcaseAt index: Int) -> MaterialShowcase?
  33. }
  34. open class MaterialShowcaseController {
  35. public weak var dataSource: MaterialShowcaseControllerDataSource?
  36. public weak var delegate: MaterialShowcaseControllerDelegate?
  37. public var started = false
  38. public var currentIndex = -1
  39. public weak var currentShowcase: MaterialShowcase?
  40. public init() {
  41. }
  42. open func start() {
  43. started = true
  44. nextShowcase()
  45. }
  46. open func stop() {
  47. started = false
  48. currentIndex = -1
  49. currentShowcase?.completeShowcase(animated: true)
  50. }
  51. open func nextShowcase() {
  52. if let currentShowcase = self.currentShowcase {
  53. currentShowcase.completeShowcase(animated: true)
  54. self.currentShowcase = nil
  55. }
  56. let numberOfShowcases = dataSource?.numberOfShowcases(for: self) ?? 0
  57. currentIndex += 1
  58. let showcase = dataSource?.materialShowcaseController(self, showcaseAt: currentIndex)
  59. showcase?.delegate = self
  60. guard currentIndex < numberOfShowcases else {
  61. started = false
  62. currentIndex = -1
  63. return
  64. }
  65. currentShowcase = showcase
  66. showcase?.show(completion: nil)
  67. }
  68. }
  69. extension MaterialShowcaseController: MaterialShowcaseDelegate {
  70. public func showCaseWillDismiss(showcase: MaterialShowcase, didTapTarget: Bool) {
  71. delegate?.materialShowcaseController(self, materialShowcaseWillDisappear: showcase, forIndex: currentIndex)
  72. }
  73. public func showCaseDidDismiss(showcase: MaterialShowcase, didTapTarget: Bool) {
  74. delegate?.materialShowcaseController(self, materialShowcaseDidDisappear: showcase, forIndex: currentIndex)
  75. currentShowcase = nil
  76. if started {
  77. self.nextShowcase()
  78. }
  79. }
  80. }