Blame view

Pods/Toaster/Sources/Toast.swift 3.75 KB
d774f0637   Trịnh Văn Quân   fisrt comit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
  /*
   * Toast.swift
   *
   *            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
   *                    Version 2, December 2004
   *
   * Copyright (C) 2013-2015 Su Yeol Jeon
   *
   * Everyone is permitted to copy and distribute verbatim or modified
   * copies of this license document, and changing it is allowed as long
   * as the name is changed.
   *
   *            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
   *   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
   *
   *  0. You just DO WHAT THE FUCK YOU WANT TO.
   *
   */
  
  import UIKit
  
  public struct Delay {
    public static let short: TimeInterval = 2.0
    public static let long: TimeInterval = 3.5
  }
  
  open class Toast: Operation {
  
    // MARK: Properties
  
    public var text: String? {
      get { return self.view.text }
      set { self.view.text = newValue }
    }
  
    public var delay: TimeInterval
    public var duration: TimeInterval
  
    private var _executing = false
    override open var isExecuting: Bool {
      get {
        return self._executing
      }
      set {
        self.willChangeValue(forKey: "isExecuting")
        self._executing = newValue
        self.didChangeValue(forKey: "isExecuting")
      }
    }
  
    private var _finished = false
    override open var isFinished: Bool {
      get {
        return self._finished
      }
      set {
        self.willChangeValue(forKey: "isFinished")
        self._finished = newValue
        self.didChangeValue(forKey: "isFinished")
      }
    }
  
  
    // MARK: UI
  
    public var view: ToastView = ToastView()
  
  
    // MARK: Initializing
  
    public init(text: String?, delay: TimeInterval = 0, duration: TimeInterval = Delay.short) {
      self.delay = delay
      self.duration = duration
      super.init()
      self.text = text
    }
  
  
    // MARK: Factory (Deprecated)
  
    @available(*, deprecated, message: "Use 'init(text:)' instead.")
    public class func makeText(_ text: String) -> Toast {
      return Toast(text: text)
    }
  
    @available(*, deprecated, message: "Use 'init(text:duration:)' instead.")
    public class func makeText(_ text: String, duration: TimeInterval) -> Toast {
      return Toast(text: text, duration: duration)
    }
  
    @available(*, deprecated, message: "Use 'init(text:delay:duration:)' instead.")
    public class func makeText(_ text: String?, delay: TimeInterval, duration: TimeInterval) -> Toast {
      return Toast(text: text, delay: delay, duration: duration)
    }
  
  
    // MARK: Showing
  
    public func show() {
      ToastCenter.default.add(self)
    }
  
  
    // MARK: Cancelling
  
    open override func cancel() {
      super.cancel()
      self.finish()
      self.view.removeFromSuperview()
    }
  
  
    // MARK: Operation Subclassing
  
    override open func start() {
      guard Thread.isMainThread else {
        DispatchQueue.main.async { [weak self] in
          self?.start()
        }
        return
      }
      super.start()
    }
  
    override open func main() {
      self.isExecuting = true
  
      DispatchQueue.main.async {
        self.view.setNeedsLayout()
        self.view.alpha = 0
        ToastWindow.shared.addSubview(self.view)
  
        UIView.animate(
          withDuration: 0.5,
          delay: self.delay,
          options: .beginFromCurrentState,
          animations: {
            self.view.alpha = 1
          },
          completion: { completed in
            UIView.animate(
              withDuration: self.duration,
              animations: {
                self.view.alpha = 1.0001
              },
              completion: { completed in
                self.finish()
                UIView.animate(
                  withDuration: 0.5,
                  animations: {
                    self.view.alpha = 0
                  },
                  completion: { completed in
                    self.view.removeFromSuperview()
                  }
                )
              }
            )
          }
        )
      }
    }
  
    open func finish() {
      self.isExecuting = false
      self.isFinished = true
    }
  
  }