-
Couldn't load subscription status.
- Fork 24
Add ProgressView #82
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
Open
pepix
wants to merge
3
commits into
rakutentech:master
Choose a base branch
from
pepix:progressview
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add ProgressView #82
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
This file contains hidden or 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 hidden or 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
52 changes: 52 additions & 0 deletions
52
Example/AltSwiftUIExample/ExampleViews/ProgressViewExampleView.swift
This file contains hidden or 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 @@ | ||
| // | ||
| // ProgressViewExampleView.swift | ||
| // AltSwiftUIExample | ||
| // | ||
| // Created by Tsuchiya, Hiroma | Hiroma | TID on 2021/05/27. | ||
| // Copyright © 2021 Rakuten Travel. All rights reserved. | ||
| // | ||
|
|
||
| import AltSwiftUI | ||
|
|
||
| struct ProgressViewExampleView: View { | ||
| var viewStore = ViewValues() | ||
| @State private var progressValue: Float = 0.7 | ||
|
|
||
| var body: View { | ||
| VStack() { | ||
| Spacer() | ||
|
|
||
| HStack { | ||
| Spacer() | ||
| ProgressView() | ||
| Spacer() | ||
| ProgressView("Now loading...") | ||
| Spacer() | ||
| } | ||
|
|
||
| Spacer() | ||
|
|
||
| Divider() | ||
|
|
||
| Spacer() | ||
|
|
||
| Button { | ||
| progressValue = Float.random(in: 0..<1) | ||
| } label: { () -> View in | ||
| Text("Set a random progress value") | ||
| } | ||
|
|
||
| Text(String(floor(progressValue * 100)) + "%") | ||
| .padding(.bottom, 16) | ||
|
|
||
| ProgressView(progressValue) | ||
| .frame(maxWidth: .infinity) | ||
|
|
||
| ProgressView("Downloading...", value: progressValue, total: 1.0) | ||
| .frame(maxWidth: .infinity) | ||
|
|
||
| Spacer() | ||
| } | ||
| .frame(maxWidth: .infinity) | ||
| } | ||
| } |
This file contains hidden or 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
120 changes: 120 additions & 0 deletions
120
Sources/AltSwiftUI/Source/Views/ReadOnly/ProgressView.swift
This file contains hidden or 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,120 @@ | ||
| // | ||
| // ProgressView.swift | ||
| // AltSwiftUI | ||
| // | ||
| // Created by Tsuchiya, Hiroma | Hiroma | TID on 2021/05/27. | ||
| // Copyright © 2021 Rakuten Travel. All rights reserved. | ||
| // | ||
|
|
||
| import UIKit | ||
|
|
||
| /// A view that shows the progress towards completion of a task. | ||
| public struct ProgressView: View { | ||
| public var viewStore = ViewValues() | ||
| public var body: View { | ||
| self | ||
| } | ||
|
|
||
| var title: String? | ||
| var label: View? | ||
| var progress: Float? | ||
| var total: Float? | ||
|
|
||
| public init() {} | ||
|
|
||
| public init(_ title: String) { | ||
| self.title = title | ||
| } | ||
|
|
||
| public init(label: View) { | ||
| self.label = label | ||
| } | ||
|
|
||
| public init(_ progress: Float) { | ||
| self.progress = progress | ||
| } | ||
|
|
||
| public init(value: Float?, total: Float? = 1.0) { | ||
| self.progress = (value ?? 0) / (total ?? 1.0) | ||
| } | ||
|
|
||
| public init(_ title: String, value: Float?, total: Float?) { | ||
| self.title = title | ||
| self.progress = (value ?? 0) / (total ?? 1.0) | ||
| } | ||
| } | ||
|
|
||
| extension ProgressView: Renderable { | ||
| public func createView(context: Context) -> UIView { | ||
| if (progress != nil) { | ||
| /// Determinate Progress View | ||
| if (title != nil) { | ||
| return makeDeterminateProgressViewWithTitle(title: title!, context: context) | ||
| } else { | ||
| return makeDeterminateProgressView(context: context) | ||
| } | ||
| } else { | ||
| /// Indeterminate Progress View | ||
| if (title != nil) { | ||
| return makeIndeterminateProgressViewWithTitle(title: title!) | ||
| } else { | ||
| return makeIndeterminateProgressView() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public func updateView(_ view: UIView, context: Context) { | ||
| if let progressView = view as? UIProgressView { | ||
| progressView.setProgress(progress ?? 0, animated: true) | ||
| } else if let uiStackView = view as? UIStackView { | ||
| if let progressView = uiStackView.subviews.last as? UIProgressView { | ||
| progressView.setProgress(progress ?? 0, animated: true) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func makeDeterminateProgressViewWithTitle(title: String, context: Context) -> UIStackView { | ||
| let titleView = UILabel() | ||
| titleView.text = title | ||
| titleView.textColor = .gray | ||
| titleView.font = .systemFont(ofSize: 14) | ||
| let progressView = UIProgressView(progressViewStyle: .default) | ||
| updateView(progressView, context: context) | ||
| let view = UIStackView() | ||
| view.axis = .vertical | ||
| view.alignment = .leading | ||
| view.addArrangedSubview(titleView) | ||
| view.addArrangedSubview(progressView) | ||
| progressView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true | ||
| return view | ||
| } | ||
|
|
||
| func makeDeterminateProgressView(context: Context) -> UIProgressView { | ||
| let view = UIProgressView(progressViewStyle: .default) | ||
| updateView(view, context: context) | ||
| return view | ||
| } | ||
|
|
||
| func makeIndeterminateProgressViewWithTitle(title: String) -> UIStackView { | ||
| let indicatorView = UIActivityIndicatorView(style: .whiteLarge) | ||
| indicatorView.color = .gray | ||
| indicatorView.startAnimating() | ||
| let titleView = UILabel() | ||
| titleView.text = title | ||
| titleView.font = .systemFont(ofSize: 14) | ||
| titleView.textColor = .gray | ||
| let view = UIStackView() | ||
| view.axis = .vertical | ||
| view.alignment = .center | ||
| view.addArrangedSubview(indicatorView) | ||
| view.addArrangedSubview(titleView) | ||
| return view | ||
| } | ||
|
|
||
| func makeIndeterminateProgressView() -> UIActivityIndicatorView { | ||
| let view = UIActivityIndicatorView(style: .whiteLarge) | ||
| view.color = .gray | ||
| view.startAnimating() | ||
| return view | ||
| } | ||
| } | ||
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.
You can move this 4 view creation flows to a separate function each, and call them from this function, in order to make this one cleaner.
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.
Fixed plz check