ESPullToRefresh+Manager.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // ESPullToRefresh+Manager.swift
  3. //
  4. // Created by egg swift on 16/4/7.
  5. // Copyright (c) 2013-2016 ESPullToRefresh (https://github.com/eggswift/pull-to-refresh)
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. import Foundation
  26. open class ESRefreshDataManager {
  27. static let sharedManager = ESRefreshDataManager.init()
  28. static let lastRefreshKey: String = "com.espulltorefresh.lastRefreshKey"
  29. static let expiredTimeIntervalKey: String = "com.espulltorefresh.expiredTimeIntervalKey"
  30. open var lastRefreshInfo = [String: Date]()
  31. open var expiredTimeIntervalInfo = [String: TimeInterval]()
  32. public required init() {
  33. if let lastRefreshInfo = UserDefaults.standard.dictionary(forKey: ESRefreshDataManager.lastRefreshKey) as? [String: Date] {
  34. self.lastRefreshInfo = lastRefreshInfo
  35. }
  36. if let expiredTimeIntervalInfo = UserDefaults.standard.dictionary(forKey: ESRefreshDataManager.expiredTimeIntervalKey) as? [String: TimeInterval] {
  37. self.expiredTimeIntervalInfo = expiredTimeIntervalInfo
  38. }
  39. }
  40. open func date(forKey key: String) -> Date? {
  41. let date = lastRefreshInfo[key]
  42. return date
  43. }
  44. open func setDate(_ date: Date?, forKey key: String) {
  45. lastRefreshInfo[key] = date
  46. UserDefaults.standard.set(lastRefreshInfo, forKey: ESRefreshDataManager.lastRefreshKey)
  47. UserDefaults.standard.synchronize()
  48. }
  49. open func expiredTimeInterval(forKey key: String) -> TimeInterval? {
  50. let interval = expiredTimeIntervalInfo[key]
  51. return interval
  52. }
  53. open func setExpiredTimeInterval(_ interval: TimeInterval?, forKey key: String) {
  54. expiredTimeIntervalInfo[key] = interval
  55. UserDefaults.standard.set(expiredTimeIntervalInfo, forKey: ESRefreshDataManager.expiredTimeIntervalKey)
  56. UserDefaults.standard.synchronize()
  57. }
  58. open func isExpired(forKey key: String) -> Bool {
  59. guard let date = date(forKey: key) else {
  60. return true
  61. }
  62. guard let interval = expiredTimeInterval(forKey: key) else {
  63. return false
  64. }
  65. if date.timeIntervalSinceNow < -interval {
  66. return true // Expired
  67. }
  68. return false
  69. }
  70. open func isExpired(forKey key: String, block: ((Bool) -> ())?) {
  71. DispatchQueue.global().async {
  72. [weak self] in
  73. let result = self?.isExpired(forKey: key) ?? true
  74. DispatchQueue.main.async(execute: {
  75. block?(result)
  76. })
  77. }
  78. }
  79. public static func clearAll() {
  80. self.clearLastRefreshInfo()
  81. self.clearExpiredTimeIntervalInfo()
  82. }
  83. public static func clearLastRefreshInfo() {
  84. UserDefaults.standard.set(nil, forKey: ESRefreshDataManager.lastRefreshKey)
  85. }
  86. public static func clearExpiredTimeIntervalInfo() {
  87. UserDefaults.standard.set(nil, forKey: ESRefreshDataManager.expiredTimeIntervalKey)
  88. }
  89. }