Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 20 additions & 52 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,81 +1,49 @@
# https://github.com/actions/virtual-environments

name: GitHub Actions
name: test

on:
pull_request:
push:
branches:
- main
workflow_dispatch:
branches:
- main

jobs:
test:
name: Test
runs-on: macos-11
runs-on: macos-12
strategy:
matrix:
xcode_version:
- 12.4.0
- 12.5.1
- 13.0.0
- 13.3
env:
DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode_version }}.app
OLD_XCODE_APP: /Applications/Xcode_11.7.app
OLD_TARGET_OS: 13.7
OLD_DEVICE: iPhone 11 Pro
OLD_RUNTIME_NAME: com.apple.CoreSimulator.SimRuntime.iOS-13-7
steps:
- uses: actions/checkout@v2
- name: Show environments
run: |
swift --version
xcodebuild -version
- name: Prepare old OS simulator
run: |
sudo mkdir -p /Library/Developer/CoreSimulator/Profiles/Runtimes
sudo ln -s $OLD_XCODE_APP/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS\ $OLD_TARGET_OS.simruntime
xcrun simctl list runtimes
xcrun simctl list devices $OLD_TARGET_OS
- name: Test for macOS
run: xcodebuild test -scheme Hooks -destination "platform=macOS"
- name: Test for iOS
run: xcodebuild test -scheme Hooks -destination "platform=iOS Simulator,name=iPhone 12 Pro"
- name: Test for tvOS
run: xcodebuild test -scheme Hooks -destination "platform=tvOS Simulator,name=Apple TV"
- name: Build for watchOS
run: WATCHOS=true xcodebuild build -scheme Hooks -destination "platform=watchOS Simulator,name=Apple Watch Series 6 - 44mm"
- name: Test for old iOS
run: xcodebuild test -scheme Hooks -destination "platform=iOS Simulator,name=$OLD_DEVICE,OS=$OLD_TARGET_OS"
- name: UI Test Todo
working-directory: Examples
run: xcodebuild test -scheme Todo-UITests -destination "platform=iOS Simulator,name=$OLD_DEVICE,OS=$OLD_TARGET_OS"
- name: Test library
run: make test-library
- name: Build examples
run: make build-examples

build-examples:
name: Build examples
runs-on: macos-11
strategy:
matrix:
xcode_version:
- 12.5.1
validation:
name: Validation
runs-on: macos-12
env:
DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode_version }}.app
defaults:
run:
working-directory: Examples
DEVELOPER_DIR: /Applications/Xcode_13.3.app
steps:
- uses: actions/checkout@v2
- name: Show environments
- name: Validate lint
run: make lint
- name: Validate format
run: |
swift --version
xcodebuild -version
- name: Build TheMovieDB
run: xcodebuild build -scheme TheMovieDB-MVVM -destination "platform=iOS Simulator,name=iPhone 12 Pro"
- name: Test TheMovieDB
run: xcodebuild test -scheme TheMovieDB-MVVM-Tests -destination "platform=iOS Simulator,name=iPhone 12 Pro"
- name: Build Basic
run: xcodebuild build -scheme BasicUsage -destination "platform=iOS Simulator,name=iPhone 12 Pro"
- name: Build Todo
run: xcodebuild build -scheme Todo -destination "platform=iOS Simulator,name=iPhone 12 Pro"
make format
if [ -n "$(git status --porcelain)" ]; then echo "Make sure that the code is formated by 'make format'."; exit 1; fi
- name: Validate example project
run: |
make proj
if [ -n "$(git status --porcelain)" ]; then echo "Make sure that 'Examples/Examples.xcodeproj' is formated by 'make proj'."; exit 1; fi
16 changes: 9 additions & 7 deletions .swift-format
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"accessLevel": "private"
},
"indentConditionalCompilationBlocks": true,
"lineBreakAroundMultilineExpressionChainComponents": true,
"indentSwitchCaseLabels": false,
"lineBreakAroundMultilineExpressionChainComponents": false,
"lineBreakBeforeControlFlowKeywords": true,
"lineBreakBeforeEachArgument": true,
"lineBreakBeforeEachGenericRequirement": true,
Expand All @@ -16,20 +17,19 @@
"prioritizeKeepingFunctionOutputTogether": false,
"respectsExistingLineBreaks": true,
"rules": {
"NeverForceUnwrap": false,
"AllPublicDeclarationsHaveDocumentation": true,
"AlwaysUseLowerCamelCase": true,
"AmbiguousTrailingClosureOverload": true,
"BeginDocumentationCommentWithOneLineSummary": false,
"DoNotUseSemicolons": true,
"DontRepeatTypeInStaticProperties": true,
"FileprivateAtFileScope": true,
"DontRepeatTypeInStaticProperties": false,
"FileScopedDeclarationPrivacy": true,
"FullyIndirectEnum": true,
"GroupNumericLiterals": true,
"IdentifiersMustBeASCII": true,
"NeverForceUnwrap": true,
"NeverForceUnwrap": false,
"NeverUseForceTry": true,
"NeverUseImplicitlyUnwrappedOptionals": true,
"NeverUseImplicitlyUnwrappedOptionals": false,
"NoAccessLevelOnExtensionDeclaration": false,
"NoBlockComments": true,
"NoCasesWithOnlyFallthrough": true,
Expand All @@ -43,11 +43,13 @@
"OnlyOneTrailingClosureArgument": false,
"OrderedImports": true,
"ReturnVoidInsteadOfEmptyTuple": true,
"UseEarlyExits": false,
"UseLetInEveryBoundCaseVariable": true,
"UseShorthandTypeNames": true,
"UseSingleLinePropertyGetter": true,
"UseSynthesizedInitializer": false,
"UseSynthesizedInitializer": true,
"UseTripleSlashForDocumentationComments": true,
"UseWhereClausesInForLoops": false,
"ValidateDocumentationComments": false
}
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2021 Ryo Aoyama
Copyright (c) 2022 Ryo Aoyama

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
35 changes: 32 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
TOOL = swift run -c release --package-path Tools
PACKAGE = swift package --package-path Tools
SWIFT_FILE_PATHS = Package.swift Sources Tests Examples
TEST_PLATFORM_IOS = iOS Simulator,name=iPhone 13 Pro
TEST_PLATFORM_MACOS = macOS
TEST_PLATFORM_TVOS = tvOS Simulator,name=Apple TV 4K (at 1080p) (2nd generation)
TEST_PLATFORM_WATCHOS = watchOS Simulator,name=Apple Watch Series 7 - 45mm

.PHONY: proj
proj:
$(TOOL) xcodegen -s Examples/project.yml

.PHONY: format
format:
@swift run -c release --package-path tools swift-format -i -r -m format Sources Tests Examples
@swift run -c release --package-path tools xcodegen -s Examples/project.yml
$(TOOL) swift-format format -i -p -r $(SWIFT_FILE_PATHS)

.PHONY: lint
lint:
@swift run -c release --package-path tools swift-format -r -m lint Sources Tests Examples
$(TOOL) swift-format lint -s -p -r $(SWIFT_FILE_PATHS)

.PHONY: test
test: test-library build-examples

.PHONY: test-library
test-library:
for platform in "$(TEST_PLATFORM_IOS)" "$(TEST_PLATFORM_MACOS)" "$(TEST_PLATFORM_TVOS)" "$(TEST_PLATFORM_WATCHOS)"; do \
xcodebuild test -scheme SwiftUI-Hooks -destination platform="$$platform"; \
done
cd Examples \
&& xcodebuild test -scheme Todo-UITests -destination platform="$(TEST_PLATFORM_IOS)" \
&& xcodebuild test -scheme TheMovieDB-MVVM-Tests -destination platform="$(TEST_PLATFORM_IOS)"

.PHONY: build-examples
build-examples:
cd Examples && for scheme in "TheMovieDB-MVVM" "BasicUsage" "Todo" ; do \
xcodebuild build -scheme "$$scheme" -destination platform="$(TEST_PLATFORM_IOS)"; \
done
20 changes: 11 additions & 9 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
// swift-tools-version:5.3
// swift-tools-version:5.6

import Foundation
import PackageDescription

let package = Package(
name: "Hooks",
name: "SwiftUI-Hooks",
platforms: [
.iOS(.v13),
.macOS(.v10_15),
.tvOS(.v13),
.watchOS(.v6)
.watchOS(.v6),
],
products: [
.library(name: "Hooks", targets: ["Hooks"]),
.library(name: "Hooks", targets: ["Hooks"])
],
targets: [
.target(
name: "Hooks"
name: "Hooks",
swiftSettings: [
.unsafeFlags([
"-Xfrontend",
"-enable-actor-data-race-checks",
])
]
),
.testTarget(
name: "HooksTests",
Expand All @@ -25,7 +31,3 @@ let package = Package(
],
swiftLanguageVersions: [.v5]
)

if ProcessInfo.processInfo.environment["WATCHOS"] == "true" {
package.targets.removeAll(where: \.isTest)
}
99 changes: 58 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
<H1 align="center">
SwiftUI Hooks
</H1>

<p align="center">
A SwiftUI implementation of <a href="https://reactjs.org/docs/hooks-intro.html">React Hooks</a>.
</p>
<p align="center">
Enhances reusability of stateful logic and gives state and lifecycle to function view.
</p>

<h1 align="center">SwiftUI Hooks</h1>
<p align="center">A SwiftUI implementation of <a href="https://reactjs.org/docs/hooks-intro.html">React Hooks</a>.</p>
<p align="center">Enhances reusability of stateful logic and gives state and lifecycle to function view.</p>
<p align="center">
<a href="https://developer.apple.com/swift"><img alt="Swift5" src="https://img.shields.io/badge/language-Swift5-orange.svg"></a>
<a href="https://developer.apple.com"><img alt="Platform" src="https://img.shields.io/badge/platform-iOS%20%7C%20macOS%20%7C%20tvOS%20%7C%20watchOS%20%7C-green.svg"></a>
<a href="https://github.com/ra1028/SwiftUI-Hooks/actions"><img alt="GitHub Actions" src="https://github.com/ra1028/SwiftUI-Hooks/workflows/GitHub%20Actions/badge.svg"></a>
<a href="https://github.com/ra1028/SwiftUI-Hooks/actions"><img alt="test" src="https://github.com/ra1028/SwiftUI-Hooks/workflows/test/badge.svg"></a>
<a href="https://github.com/ra1028/SwiftUI-Hooks/releases/latest"><img alt="release" src="https://img.shields.io/github/v/release/ra1028/SwiftUI-Hooks.svg"/></a>
<a href="https://developer.apple.com/swift"><img alt="Swift5" src="https://img.shields.io/badge/language-Swift5-orange.svg"></a>
<a href="https://developer.apple.com"><img alt="Platform" src="https://img.shields.io/badge/platform-iOS%20%7C%20macOS%20%7C%20tvOS%20%7C%20watchOS%20%7C-green.svg"></a>
<a href="LICENSE"><img alt="license" src="https://img.shields.io/badge/license-MIT-black.svg"></a>
</p>

---

- [Introducing Hooks](#introducing-hooks)
- [Introduction](#introduction)
- [Getting Started](#getting-started)
- [Hooks API](#hooks-api)
- [Rules of Hooks](#rules-of-hooks)
- [Building Your Own Hooks](#building-your-own-hooks)
- [How to Test Your Custom Hooks](#how-to-test-your-custom-hooks)
- [Context](#context)
- [Requirements](#requirements)
- [Installation](#installation)
- [License](#license)

---

## Introducing Hooks
## Introduction

```swift
func timer() -> some View {
Expand Down Expand Up @@ -62,6 +55,53 @@ There're already a bunch of documentations on React Hooks, so you can refer to i

---

## Getting Started

### Requirements

| |Minimum Version|
|------:|--------------:|
|Swift |5.6 |
|Xcode |13.3 |
|iOS |13.0 |
|macOS |10.15 |
|tvOS |13.0 |
|watchOS|6.0 |

## Installation

The module name of the package is `Hooks`. Choose one of the instructions below to install and add the following import statement to your source code.

```swift
import Hooks
```

#### [Xcode Package Dependency](https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app)

From Xcode menu: `File` > `Swift Packages` > `Add Package Dependency`

```text
https://github.com/ra1028/SwiftUI-Hooks
```

#### [Swift Package Manager](https://www.swift.org/package-manager)

In your `Package.swift` file, first add the following to the package `dependencies`:

```swift
.package(url: "https://github.com/ra1028/SwiftUI-Hooks"),
```

And then, include "Hooks" as a dependency for your target:

```swift
.target(name: "<target>", dependencies: [
.product(name: "Hooks", package: "SwiftUI-Hooks"),
]),
```

---

## Hooks API

👇 Click to open the description.
Expand Down Expand Up @@ -508,29 +548,6 @@ See also: [React Context](https://reactjs.org/docs/context.html)

---

## Requirements

- Swift 5.3+
- Xcode 12.4.0+
- iOS 13.0+
- macOS 10.15+
- tvOS 13.0+
- watchOS 6.0+

---

## Installation

### [Swift Package Manager for Apple Platforms](https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app)

Xcode menu `File > Swift Packages > Add Package Dependency`.

```text
Repository: https://github.com/ra1028/SwiftUI-Hooks
```

---

## Acknowledgements

- [React Hooks](https://reactjs.org/docs/hooks-intro.html)
Expand Down
Loading