MaterialShowcaseSequence.swift 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // MaterialShowCaseSequence.swift
  3. // MaterialShowcase
  4. //
  5. // Created by VB on 16.04.2019.
  6. // Copyright © 2019 Aromajoin. All rights reserved.
  7. //
  8. import Foundation
  9. public class MaterialShowcaseSequence {
  10. var showcaseArray : [MaterialShowcase] = []
  11. var currentCase : Int = 0
  12. var key : String?
  13. public func temp(_ showcase: MaterialShowcase) -> MaterialShowcaseSequence {
  14. showcaseArray.append(showcase)
  15. return self
  16. }
  17. public func start() {
  18. guard !getUserState(key: self.key) else {
  19. return
  20. }
  21. showcaseArray.first?.show(completion: increase)
  22. }
  23. func increase() -> Void {
  24. self.currentCase += 1
  25. }
  26. /// Set user show retry
  27. public func setKey(key : String? = nil) -> MaterialShowcaseSequence {
  28. guard key != nil else {
  29. return self
  30. }
  31. self.key = key
  32. return self
  33. }
  34. /// Remove user state
  35. public func removeUserState(key : String = MaterialKey._default.rawValue) {
  36. UserDefaults.standard.removeObject(forKey: key)
  37. }
  38. /// Remove user state
  39. func getUserState(key : String?) -> Bool {
  40. guard key != nil else {
  41. return false
  42. }
  43. return UserDefaults.standard.bool(forKey: key!)
  44. }
  45. public init() { }
  46. public func showCaseWillDismis() {
  47. guard self.showcaseArray.count > currentCase else {
  48. //last index
  49. guard self.key != nil else {
  50. return
  51. }
  52. UserDefaults.standard.set(true, forKey: key!)
  53. return
  54. }
  55. showcaseArray[currentCase].show(completion: self.increase)
  56. }
  57. }