Skip to content

Commit 2e3495f

Browse files
committed
PasscodeField package
0 parents  commit 2e3495f

File tree

9 files changed

+586
-0
lines changed

9 files changed

+586
-0
lines changed

.github/workflows/main.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Main
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
main:
11+
runs-on: macos-latest
12+
steps:
13+
- uses: fwal/[email protected]
14+
with:
15+
swift-version: "5.6.0"
16+
- uses: actions/checkout@v2
17+
- uses: actions/cache@v2
18+
with:
19+
path: .build
20+
key: ${{ runner.os }}-PasscodeField-${{ hashFiles('**/Package.resolved') }}
21+
restore-keys: ${{ runner.os }}-PasscodeField
22+
- name: Build
23+
run: swift build
24+
- name: Test
25+
run: swift test

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
/*.xcodeproj
5+
xcuserdata/
6+
DerivedData/
7+
.swiftpm/config/registries.json
8+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
9+
.netrc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 hengyu
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Package.swift

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// swift-tools-version: 5.6
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "PasscodeField",
8+
platforms: [
9+
.iOS(.v13),
10+
.tvOS(.v14),
11+
.macCatalyst(.v13),
12+
.macOS(.v11)
13+
],
14+
products: [
15+
.library(name: "PasscodeField", targets: ["PasscodeField"])
16+
],
17+
targets: [
18+
.target(name: "PasscodeField", dependencies: []),
19+
.testTarget(name: "PasscodeFieldTests", dependencies: ["PasscodeField"])
20+
]
21+
)

README.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# PasscodeField
2+
3+
A SwiftUI library which provides a passcode field.
4+
5+
## Features
6+
7+
1. Digit length is customizable;
8+
2. Digit box style is customizable;
9+
3. Support for hide/unhide digits;
10+
4. Available on iOS, tvOS and macOS.
11+
12+
## Core Ideas
13+
14+
To provide the system input keyboard to user when they interacting the `PasscodeField`, we use a system text field as the receiver while making it visually invisible. When user is about to input digits, we first make the text field as the first responder. At this time, the system keyboard will show. Then we listen to the input event, when any input arrives, we validate the input and then update the state of the `PasscodeField` (e.g., set a gray background on the digit box to indicate user they have added one digit). Also, we listen to the text deletion event, and update the `PasscodeField` correspondingly.
15+
16+
## Usages
17+
18+
`PasscodeField` could be installed via [Swift Package Manager](https://www.swift.org/package-manager/). Open Xcode and go to **File** -> **Add Packages...**, search `https://github.com/hengyu/PasscodeField.git`, and add the package as one of your project's dependency.
19+
20+
In the Swift file you want to use, just import the package and then:
21+
22+
```swift
23+
import PasscodeField
24+
25+
// ... some code ...
26+
27+
PasscodeField { digits, action in
28+
if candidatePasscodes.contains(digits.concat) {
29+
withAnimation {
30+
isPresentingPasscode = false
31+
}
32+
action(true)
33+
} else {
34+
withAnimation {
35+
isShowingAlert = true
36+
}
37+
action(false)
38+
}
39+
} label: {
40+
VStack(alignment: .center, spacing: 8) {
41+
Text("Security Number")
42+
.font(.title)
43+
.foregroundColor(Color(.label))
44+
45+
Text("4 digits")
46+
.font(.footnote)
47+
.foregroundColor(Color(.secondaryLabel))
48+
}
49+
}
50+
.alert(
51+
"Wrong Passcode",
52+
isPresented: $isShowingAlert,
53+
confirmationText: "Retry",
54+
confirmationAction: {
55+
isShowingAlert = false
56+
},
57+
cancellationText: "Quit",
58+
cancellationAction: {
59+
isShowingAlert = false
60+
dismissAction()
61+
}
62+
)
63+
```
64+
65+
## Screenshots
66+
67+
<img width=320 src="preview.png"/>
68+
69+
## License
70+
71+
`PasscodeField` is licensed under [MIT License](LICENSE).

0 commit comments

Comments
 (0)