Skip to content

Commit 741075a

Browse files
committed
Write New Types article + fix Documentation entry point & more
1 parent e02f39c commit 741075a

File tree

4 files changed

+101
-15
lines changed

4 files changed

+101
-15
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Learn how you can make the most of HandySwift by reading the guides inside the d
2222

2323
## Showcase
2424

25-
I extracted this library from these Indie apps (rate them with 5 stars to support me!):
25+
I extracted most of this library from these Indie apps (rate them with 5 stars to support me!):
2626

2727
<table>
2828
<tr>

Sources/HandySwift/HandySwift.docc/Essentials/Extensions.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ func downloadPuzzle(from url: URL) async throws -> Puzzle {
309309
- ``Swift/Double/duration()``
310310

311311

312-
[SCTranslator]: https://apps.apple.com/app/apple-store/id6476773066?pt=549314&ct=github.com&mt=8
313-
[CrossCraft]: https://apps.apple.com/app/apple-store/id6472669260?pt=549314&ct=github.com&mt=8
314-
[FocusBeats]: https://apps.apple.com/app/apple-store/id6477829138?pt=549314&ct=github.com&mt=8
315-
[Guided Guest Mode]: https://apps.apple.com/app/apple-store/id6479207869?pt=549314&ct=github.com&mt=8
316-
[Posters]: https://apps.apple.com/app/apple-store/id6478062053?pt=549314&ct=github.com&mt=8
312+
[SCTranslator]: https://apps.apple.com/app/apple-store/id6476773066?pt=549314&ct=swiftpackageindex.com&mt=8
313+
[CrossCraft]: https://apps.apple.com/app/apple-store/id6472669260?pt=549314&ct=swiftpackageindex.com&mt=8
314+
[FocusBeats]: https://apps.apple.com/app/apple-store/id6477829138?pt=549314&ct=swiftpackageindex.com&mt=8
315+
[Guided Guest Mode]: https://apps.apple.com/app/apple-store/id6479207869?pt=549314&ct=swiftpackageindex.com&mt=8
316+
[Posters]: https://apps.apple.com/app/apple-store/id6478062053?pt=549314&ct=swiftpackageindex.com&mt=8

Sources/HandySwift/HandySwift.docc/Essentials/New Types.md

+94-8
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,101 @@ In the [Topics](#topics) section below you can find a list of all new types & fu
1313

1414
To get you started quickly, here are the ones I use in nearly all of my apps with a practical usage example for each:
1515

16-
### TODO
16+
### Gregorian Day & Time
1717

18-
TODO
18+
You want to construct a `Date` from year, month, and day? Easy:
19+
20+
```swift
21+
GregorianDay(year: 1960, month: 11, day: 01).startOfDay() // => Date
22+
```
23+
24+
You have a `Date` and want to store just the day part of the date, not the time? Just use ``GregorianDay`` in your model:
25+
26+
```swift
27+
struct User {
28+
let birthday: GregorianDay
29+
}
30+
31+
let selectedDate = // coming from DatePicker
32+
let timCook = User(birthday: GregorianDay(date: selectedDate))
33+
print(timCook.birthday.iso8601Formatted) // => "1960-11-01"
34+
```
35+
36+
You just want today's date without time?
37+
38+
```swift
39+
GregorianDay.today
40+
```
41+
42+
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?
62+
63+
```swift
64+
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .milliseconds(250)) {
65+
// your code
66+
}
67+
```
68+
69+
HandySwift introduces a shorter version that's easier to remember:
70+
71+
```swift
72+
delay(by: .milliseconds(250)) {
73+
// your code
74+
}
75+
```
76+
77+
It also supports different Quality of Service classes like `DispatchQueue` (default is main queue):
78+
79+
```swift
80+
delay(by: .milliseconds(250), qosClass: .background) {
81+
// your code
82+
}
83+
```
84+
85+
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+
@State private var 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.
19111
20112
## Topics
21113

@@ -39,9 +131,3 @@ TODO
39131
- ``delay(by:qosClass:_:)-8iw4f``
40132
- ``delay(by:qosClass:_:)-yedf``
41133
- ``HandyRegex``
42-
43-
[SCTranslator]: https://apps.apple.com/app/apple-store/id6476773066?pt=549314&ct=github.com&mt=8
44-
[CrossCraft]: https://apps.apple.com/app/apple-store/id6472669260?pt=549314&ct=github.com&mt=8
45-
[FocusBeats]: https://apps.apple.com/app/apple-store/id6477829138?pt=549314&ct=github.com&mt=8
46-
[Guided Guest Mode]: https://apps.apple.com/app/apple-store/id6479207869?pt=549314&ct=github.com&mt=8
47-
[Posters]: https://apps.apple.com/app/apple-store/id6478062053?pt=549314&ct=github.com&mt=8

Sources/HandySwift/HandySwift.docc/HandySwift.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# HandySwift
1+
# ``HandySwift``
22

33
Handy Swift features that didn't make it into the Swift standard library.
44

0 commit comments

Comments
 (0)