Skip to content

Commit 9b1b3ff

Browse files
committed
Initial commit
0 parents  commit 9b1b3ff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+5153
-0
lines changed

.github/workflows/test.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# https://github.com/actions/virtual-environments
2+
3+
name: GitHub Actions
4+
on:
5+
pull_request:
6+
push:
7+
branches:
8+
- main
9+
jobs:
10+
test:
11+
name: Test on macOS
12+
runs-on: macos-10.15
13+
strategy:
14+
matrix:
15+
xcode_version:
16+
- "12.4"
17+
env:
18+
DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode_version }}.app
19+
LEGACY_XCODE_APP: /Applications/Xcode_11.2.1.app
20+
LEGACY_TARGET_OS: 13.2.2
21+
LEGACY_RUNTIME_NAME: com.apple.CoreSimulator.SimRuntime.iOS-13-2
22+
LEGACY_TARGET_DEVICE: iPhone 11 Pro
23+
steps:
24+
- uses: actions/checkout@v2
25+
- name: Show environments
26+
run: |
27+
swift --version
28+
xcodebuild -version
29+
pod --version
30+
31+
- name: Podspec lint
32+
run: pod lib lint Hooks.podspec --allow-warnings --verbose
33+
34+
- name: Prepare legacy OS simulator
35+
run: |
36+
sudo mkdir -p /Library/Developer/CoreSimulator/Profiles/Runtimes
37+
sudo ln -s $LEGACY_XCODE_APP/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS\ $LEGACY_TARGET_OS.simruntime
38+
xcrun simctl list runtimes
39+
xcrun simctl list devices $LEGACY_TARGET_OS
40+
41+
- name: Test for macOS
42+
run: set -o pipefail && xcodebuild build-for-testing test-without-building -scheme Hooks -configuration Release ENABLE_TESTABILITY=YES | xcpretty -c
43+
44+
- name: Test for iOS
45+
run: set -o pipefail && xcodebuild build-for-testing test-without-building -scheme Hooks -configuration Release -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 12 Pro' ENABLE_TESTABILITY=YES | xcpretty -c
46+
47+
- name: Test for tvOS
48+
run: set -o pipefail && xcodebuild build-for-testing test-without-building -scheme Hooks -configuration Release -sdk appletvsimulator -destination 'platform=tvOS Simulator,name=Apple TV' ENABLE_TESTABILITY=YES | xcpretty -c
49+
50+
- name: Build for watchOS
51+
run: set -o pipefail && xcodebuild build -scheme Hooks -configuration Release -sdk watchsimulator -destination 'platform=watchOS Simulator,name=Apple Watch Series 6 - 44mm' ENABLE_TESTABILITY=YES | xcpretty -c
52+
53+
- name: Build BasicExample
54+
run: set -o pipefail && xcodebuild build -scheme BasicExample -configuration Release -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 12 Pro' ENABLE_TESTABILITY=YES | xcpretty -c
55+
56+
- name: Build LegacyExample
57+
run: set -o pipefail && xcodebuild build -scheme LegacyExample -configuration Release -sdk iphonesimulator -destination "platform=iOS Simulator,name=$LEGACY_TARGET_DEVICE,OS=$LEGACY_TARGET_OS" ENABLE_TESTABILITY=YES | xcpretty -c
58+
59+
- name: Build for Package.swift
60+
run: set -o pipefail && rm -rf SwiftUI-Hooks.xcodeproj && xcodebuild build -scheme Hooks -configuration Release -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 12 Pro' ENABLE_TESTABILITY=YES | xcpretty -c

.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
.DS_Store
2+
*/build/*
3+
*.pbxuser
4+
!default.pbxuser
5+
*.mode1v3
6+
!default.mode1v3
7+
*.mode2v3
8+
!default.mode2v3
9+
*.perspectivev3
10+
!default.perspectivev3
11+
xcuserdata
12+
profile
13+
*.moved-aside
14+
DerivedData
15+
.idea/
16+
*.hmap
17+
*.xccheckout
18+
*.xcuserstate
19+
build/
20+
archive/
21+
*.xcframework
22+
23+
## Gem
24+
.bundle
25+
vendor
26+
27+
## npm
28+
node_modules
29+
30+
## Carthage
31+
Carthage/Build
32+
33+
## CocoaPods
34+
Pods/
35+
36+
## SwiftPM
37+
.swiftpm
38+
.build

.swift-format.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"version": 1,
3+
"indentation": {
4+
"spaces": 4
5+
},
6+
"fileScopedDeclarationPrivacy": {
7+
"accessLevel": "private"
8+
},
9+
"indentConditionalCompilationBlocks": true,
10+
"lineBreakAroundMultilineExpressionChainComponents": true,
11+
"lineBreakBeforeControlFlowKeywords": true,
12+
"lineBreakBeforeEachArgument": true,
13+
"lineBreakBeforeEachGenericRequirement": true,
14+
"lineLength": 150,
15+
"maximumBlankLines": 1,
16+
"prioritizeKeepingFunctionOutputTogether": false,
17+
"respectsExistingLineBreaks": true,
18+
"rules": {
19+
"AllPublicDeclarationsHaveDocumentation": true,
20+
"AlwaysUseLowerCamelCase": true,
21+
"AmbiguousTrailingClosureOverload": true,
22+
"BeginDocumentationCommentWithOneLineSummary": false,
23+
"DoNotUseSemicolons": true,
24+
"DontRepeatTypeInStaticProperties": true,
25+
"FileprivateAtFileScope": true,
26+
"FullyIndirectEnum": true,
27+
"GroupNumericLiterals": true,
28+
"IdentifiersMustBeASCII": true,
29+
"NeverForceUnwrap": true,
30+
"NeverUseForceTry": true,
31+
"NeverUseImplicitlyUnwrappedOptionals": true,
32+
"NoAccessLevelOnExtensionDeclaration": false,
33+
"NoBlockComments": true,
34+
"NoCasesWithOnlyFallthrough": true,
35+
"NoEmptyTrailingClosureParentheses": true,
36+
"NoLabelsInCasePatterns": true,
37+
"NoLeadingUnderscores": false,
38+
"NoParensAroundConditions": true,
39+
"NoVoidReturnOnFunctionSignature": true,
40+
"OneCasePerLine": true,
41+
"OneVariableDeclarationPerLine": true,
42+
"OnlyOneTrailingClosureArgument": true,
43+
"OrderedImports": true,
44+
"ReturnVoidInsteadOfEmptyTuple": true,
45+
"UseLetInEveryBoundCaseVariable": true,
46+
"UseShorthandTypeNames": true,
47+
"UseSingleLinePropertyGetter": true,
48+
"UseSynthesizedInitializer": false,
49+
"UseTripleSlashForDocumentationComments": true,
50+
"ValidateDocumentationComments": false
51+
}
52+
}

.version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.0.1
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import Hooks
2+
import SwiftUI
3+
4+
struct Post: Codable {
5+
let id: Int
6+
let title: String
7+
let body: String
8+
}
9+
10+
func useFetchPosts() -> (status: AsyncStatus<[Post], Error>, fetch: () -> Void) {
11+
let url = URL(string: "https://jsonplaceholder.typicode.com/posts").unsafelyUnwrapped
12+
let (status, subscribe) = usePublisherSubscribe {
13+
URLSession.shared.dataTaskPublisher(for: url)
14+
.map(\.data)
15+
.decode(type: [Post].self, decoder: JSONDecoder())
16+
.receive(on: DispatchQueue.main)
17+
}
18+
19+
return (status: status, fetch: subscribe)
20+
}
21+
22+
struct APIRequestPage: HookView {
23+
var hookBody: some View {
24+
let (status, fetch) = useFetchPosts()
25+
26+
return ScrollView {
27+
VStack {
28+
switch status {
29+
case .running:
30+
ProgressView()
31+
32+
case .success(let posts):
33+
postRows(posts)
34+
35+
case .failure(let error):
36+
errorRow(error, retry: fetch)
37+
38+
case .pending:
39+
EmptyView()
40+
}
41+
}
42+
.padding(.vertical, 16)
43+
.padding(.horizontal, 24)
44+
}
45+
.navigationTitle("API Request")
46+
.background(Color(.systemBackground).ignoresSafeArea())
47+
.onAppear(perform: fetch)
48+
}
49+
50+
func postRows(_ posts: [Post]) -> some View {
51+
ForEach(posts, id: \.id) { post in
52+
VStack(alignment: .leading) {
53+
Text(post.title).bold()
54+
Text(post.body).padding(.vertical, 16)
55+
Divider()
56+
}
57+
.frame(maxWidth: .infinity)
58+
}
59+
}
60+
61+
func errorRow(_ error: Error, retry: @escaping () -> Void) -> some View {
62+
VStack {
63+
Text("Error: \(error.localizedDescription)").fixedSize(horizontal: false, vertical: true)
64+
Divider()
65+
Button("Refresh", action: retry)
66+
}
67+
}
68+
}

Examples/Basic/App.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import SwiftUI
2+
3+
@main
4+
struct HooksApp: App {
5+
var body: some Scene {
6+
WindowGroup {
7+
IndexPage()
8+
}
9+
}
10+
}

Examples/Basic/CounterPage.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import Hooks
2+
import SwiftUI
3+
4+
struct CounterPage: HookView {
5+
var hookBody: some View {
6+
let count = useState(0)
7+
let isAutoIncrement = useState(false)
8+
9+
useEffect(.preserved(by: isAutoIncrement.wrappedValue)) {
10+
guard isAutoIncrement.wrappedValue else { return nil }
11+
12+
let timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { _ in
13+
count.wrappedValue += 1
14+
}
15+
16+
return timer.invalidate
17+
}
18+
19+
return VStack(spacing: 50) {
20+
Text(String(format: "%02d", count.wrappedValue))
21+
.lineLimit(1)
22+
.minimumScaleFactor(0.1)
23+
.font(.system(size: 100, weight: .heavy, design: .rounded))
24+
.padding(30)
25+
.frame(width: 200, height: 200)
26+
.background(Color(.secondarySystemBackground))
27+
.shadow(color: Color(.sRGBLinear, white: 0, opacity: 0.2), radius: 3, y: 3)
28+
29+
Stepper(value: count, in: 0...(.max)) { EmptyView() }.fixedSize()
30+
31+
Toggle("Auto +", isOn: isAutoIncrement).fixedSize()
32+
}
33+
.navigationTitle("Counter")
34+
.frame(maxWidth: .infinity, maxHeight: .infinity)
35+
.background(Color(.systemBackground).ignoresSafeArea())
36+
}
37+
}
38+
39+
struct CounterPage_Previews: PreviewProvider {
40+
static var previews: some View {
41+
CounterPage()
42+
}
43+
}

0 commit comments

Comments
 (0)