-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[LMN-27] Progress Bar SwfitUI #1716
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1777795
initial progress bar
frugoman eafe153
rebase
frugoman 7ca23fe
swiftui progress bar
frugoman f220de7
better tests
frugoman 227d943
Merge branch 'main' into lmn-27/progress_bar-swfitui
frugoman bc514cf
better rtl support
frugoman 284f986
Merge branch 'lmn-27/progress_bar-swfitui' of github.com:Skyscanner/b…
frugoman 9f6b3ad
snapshots
frugoman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
Backpack-SwiftUI/ProgressBar/Classes/BPKProgressBar.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Backpack - Skyscanner's Design System | ||
* | ||
* Copyright 2018-2022 Skyscanner Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import SwiftUI | ||
|
||
public struct BPKProgressBar: View { | ||
public enum Size { | ||
case small, large | ||
|
||
var height: BPKSpacing { | ||
switch self { | ||
case .small: | ||
return BPKSpacing.md | ||
case .large: | ||
return BPKSpacing.base | ||
} | ||
} | ||
} | ||
|
||
let max: Int | ||
let stepped: Bool | ||
let size: Size | ||
let value: Float | ||
|
||
@Environment(\.layoutDirection) private var layoutDirection | ||
|
||
public init(max: Int, stepped: Bool, size: Size, value: Float) { | ||
self.max = max | ||
self.stepped = stepped | ||
self.size = size | ||
self.value = value | ||
} | ||
|
||
private var progress: Float { | ||
min(Swift.max(0, value), Float(max)) | ||
} | ||
|
||
private var percentage: Float { | ||
progress / Float(max) | ||
} | ||
|
||
public var body: some View { | ||
GeometryReader { proxy in | ||
ZStack { | ||
Capsule() | ||
.foregroundColor(BPKColor.surfaceHighlightColor) | ||
progressBarShape | ||
.foregroundColor(BPKColor.coreAccentColor) | ||
.frame(width: width(for: proxy)) | ||
.offset(x: offset(for: proxy)) | ||
if stepped { | ||
HStack { | ||
Spacer() | ||
ForEach(0..<max - 1, id: \.self) { _ in | ||
Rectangle() | ||
.foregroundColor(BPKColor.surfaceDefaultColor) | ||
.frame(width: 2) | ||
Spacer() | ||
} | ||
} | ||
.frame(width: proxy.size.width, height: proxy.size.height) | ||
} | ||
} | ||
} | ||
.frame(height: size.height) | ||
.animation(.easeInOut, value: value) | ||
} | ||
|
||
@ViewBuilder | ||
private var progressBarShape: some View { | ||
if stepped { | ||
/// iOS 16 adds support for leading and trailing semantics rather than left-right | ||
/// but XCode 15 is needed to use that feature. | ||
/// Update API to leading and trailing when XCode 15 is used in CI | ||
let corners: UIRectCorner = value == Float(max) ? .allCorners | ||
: layoutDirection == .leftToRight ? [.topLeft, .bottomLeft] | ||
: [.topRight, .bottomRight] | ||
let cornerSize = size.height.value / 2 | ||
Rectangle() | ||
.clipShape( | ||
RoundedCustomCornersShape( | ||
radius: cornerSize, | ||
corners: corners | ||
) | ||
) | ||
} else { | ||
Capsule() | ||
} | ||
} | ||
|
||
private func width(for proxy: GeometryProxy) -> CGFloat { | ||
if stepped { | ||
let stepWidth = proxy.size.width / CGFloat(max) | ||
return stepWidth * CGFloat(Int(value)) | ||
} | ||
return proxy.size.width * CGFloat(percentage) | ||
} | ||
|
||
private func offset(for proxy: GeometryProxy) -> CGFloat { | ||
return -proxy.size.width / 2 + width(for: proxy) / 2 | ||
} | ||
} | ||
|
||
struct BPKProgressBar_Previews: PreviewProvider { | ||
static var previews: some View { | ||
VStack { | ||
BPKProgressBar(max: 3, stepped: false, size: .small, value: 1.5) | ||
BPKProgressBar(max: 3, stepped: true, size: .small, value: 1.5) | ||
BPKProgressBar(max: 3, stepped: false, size: .large, value: 1.5) | ||
BPKProgressBar(max: 3, stepped: true, size: .large, value: 1.5) | ||
} | ||
.padding() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Backpack-SwiftUI/ProgressBar | ||
|
||
[![Cocoapods](https://img.shields.io/cocoapods/v/Backpack-SwiftUI.svg?style=flat)](hhttps://cocoapods.org/pods/Backpack-SwiftUI) | ||
[![class reference](https://img.shields.io/badge/Class%20reference-iOS-blue)](https://backpack.github.io/ios/versions/latest/swiftui/Structs/BPKProgressBar.html) | ||
[![view on Github](https://img.shields.io/badge/Source%20code-GitHub-lightgrey)](https://github.com/Skyscanner/backpack-ios/tree/main/Backpack-SwiftUI/ProgressBar) | ||
|
||
## Default | ||
| Day | Night | | ||
| --- | --- | | ||
| <img src="https://raw.githubusercontent.com/Skyscanner/backpack-ios/main/screenshots/iPhone-swiftui_progress-bar___default_lm.png" alt="" width="375" /> |<img src="https://raw.githubusercontent.com/Skyscanner/backpack-ios/main/screenshots/iPhone-swiftui_progress-bar___default_dm.png" alt="" width="375" /> | | ||
|
||
## Usage | ||
|
||
### Small Progress Bar | ||
|
||
```swift | ||
BPKProgressBar(max: 3, stepped: false, size: .small, value: 1.5) | ||
``` | ||
|
||
### Small Stepped Progress Bar | ||
|
||
```swift | ||
BPKProgressBar(max: 3, stepped: true, size: .small, value: 1.5) | ||
``` | ||
|
||
### Large Progress Bar | ||
|
||
```swift | ||
BPKProgressBar(max: 3, stepped: false, size: .large, value: 1.5) | ||
``` | ||
|
||
### Large Stepped Progress Bar | ||
|
||
```swift | ||
BPKProgressBar(max: 3, stepped: true, size: .large, value: 1.5) | ||
``` |
52 changes: 52 additions & 0 deletions
52
Backpack-SwiftUI/Tests/ProgressBar/BPKProgressBarTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Backpack - Skyscanner's Design System | ||
* | ||
* Copyright 2018 Skyscanner Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import XCTest | ||
import SwiftUI | ||
@testable import Backpack_SwiftUI | ||
|
||
class BPKProgressBarTests: XCTestCase { | ||
|
||
func test_small_progress_bar() throws { | ||
assertSnapshot( | ||
BPKProgressBar(max: 3, stepped: false, size: .small, value: 1.5) | ||
.frame(width: 200) | ||
) | ||
} | ||
|
||
func test_large_progress_bar() throws { | ||
assertSnapshot( | ||
BPKProgressBar(max: 3, stepped: false, size: .large, value: 1.5) | ||
.frame(width: 200) | ||
) | ||
} | ||
|
||
func test_small_stepped_progress_bar() throws { | ||
assertSnapshot( | ||
BPKProgressBar(max: 3, stepped: true, size: .small, value: 1.5) | ||
.frame(width: 200) | ||
) | ||
} | ||
|
||
func test_large_stepped_progress_bar() throws { | ||
assertSnapshot( | ||
BPKProgressBar(max: 3, stepped: true, size: .large, value: 1.5) | ||
.frame(width: 200) | ||
) | ||
} | ||
} |
Binary file added
BIN
+1.57 KB
...ressBar/__Snapshots__/BPKProgressBarTests/test_large_progress_bar.dark-mode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.46 KB
...essBar/__Snapshots__/BPKProgressBarTests/test_large_progress_bar.light-mode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.45 KB
...s/ProgressBar/__Snapshots__/BPKProgressBarTests/test_large_progress_bar.rtl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.51 KB
...__Snapshots__/BPKProgressBarTests/test_large_stepped_progress_bar.dark-mode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.55 KB
..._Snapshots__/BPKProgressBarTests/test_large_stepped_progress_bar.light-mode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.62 KB
...ssBar/__Snapshots__/BPKProgressBarTests/test_large_stepped_progress_bar.rtl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+972 Bytes
...ressBar/__Snapshots__/BPKProgressBarTests/test_small_progress_bar.dark-mode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+897 Bytes
...essBar/__Snapshots__/BPKProgressBarTests/test_small_progress_bar.light-mode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+892 Bytes
...s/ProgressBar/__Snapshots__/BPKProgressBarTests/test_small_progress_bar.rtl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+939 Bytes
...__Snapshots__/BPKProgressBarTests/test_small_stepped_progress_bar.dark-mode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+903 Bytes
..._Snapshots__/BPKProgressBarTests/test_small_stepped_progress_bar.light-mode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+954 Bytes
...ssBar/__Snapshots__/BPKProgressBarTests/test_small_stepped_progress_bar.rtl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
Example/Backpack/SwiftUI/Components/ProgressBar/ProgressBarExampleView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
/* | ||
* Backpack - Skyscanner's Design System | ||
* | ||
* Copyright © 2023 Skyscanner Ltd. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import SwiftUI | ||
import Backpack_SwiftUI | ||
|
||
struct ProgressBarExampleView: View { | ||
@State var progress: Float = 1.5 | ||
|
||
var body: some View { | ||
VStack(alignment: .leading) { | ||
BPKText("Small", style: .heading3) | ||
BPKProgressBar(max: 3, stepped: false, size: .small, value: progress) | ||
BPKText("Steped", style: .heading5) | ||
BPKProgressBar(max: 3, stepped: true, size: .small, value: progress) | ||
BPKText("Large", style: .heading3) | ||
BPKProgressBar(max: 3, stepped: false, size: .large, value: progress) | ||
BPKText("Steped", style: .heading5) | ||
BPKProgressBar(max: 3, stepped: true, size: .large, value: progress) | ||
Spacer() | ||
BPKSlider(value: $progress, sliderBounds: 0...3, step: 0.2) | ||
.padding() | ||
} | ||
.padding() | ||
} | ||
} | ||
|
||
struct ProgressBarExampleView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
ProgressBarExampleView() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the stepped RTL snapshot test looks a little broken
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixeddddd