You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Works also with `.yesterday` and `.tomorrow`. For more, just call:
43
+
44
+
```swift
45
+
let todayNextWeek = GregorianDay.today.advanced(by: 7)
46
+
```
47
+
48
+
> Note: `GregorianDay` conforms to all the protocols you would expect, such as `Codable`, `Hashable`, and `Comparable`. For encoding/decoding, it uses the ISO format as in "2014-07-13".
49
+
50
+
``GregorianTimeOfDay`` is the counterpart:
51
+
52
+
```swift
53
+
let iPhoneAnnounceTime =GregorianTimeOfDay(hour: 09, minute: 41)
54
+
let anHourFromNow = GregorianTimeOfDay.now.advanced(by: .hours(1))
55
+
56
+
let date = iPhoneAnnounceTime.date(day: GregorianDay.today) // => Date
57
+
```
58
+
59
+
### Delay & Debounce
60
+
61
+
Have you ever wanted to delay some code and found this API annoying to remember & type out?
While delaying is great for one-off tasks, sometimes there's fast input that causes performance or scalability issues. For example, a user might type fast in a search field. It's common practice to delay updating the search results and additionally cancelling any older inputs once the user makes a new one. This practice is called "Debouncing". And it's easy with HandySwift:
86
+
87
+
```swift
88
+
@Stateprivatevar searchText =""
89
+
let debouncer =Debouncer()
90
+
91
+
var body: some View {
92
+
List(filteredItems) { item in
93
+
Text(item.title)
94
+
}
95
+
.searchable(text: self.$searchText)
96
+
.onChange(of: self.searchText) { newValue in
97
+
self.debouncer.delay(for: .milliseconds(500)) {
98
+
// Perform search operation with the updated search text after 500 milliseconds of user inactivity
99
+
self.performSearch(with: newValue)
100
+
}
101
+
}
102
+
.onDisappear {
103
+
debouncer.cancelAll()
104
+
}
105
+
}
106
+
```
107
+
108
+
Note that the ``Debouncer`` was stored in a property so ``Debouncer/cancelAll()`` could be called on disappear for cleanup. But the ``Debouncer/delay(for:id:operation:)-83bbm`` is where the magic happens – and you don't have to deal with the details!
109
+
110
+
> Note: If you need multiple debouncing operations in one view, you don't need multiple debouncers. Just pass an `id` to the delay function.
0 commit comments