-
Notifications
You must be signed in to change notification settings - Fork 57
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
feat: 🎸 [JIRA:HCPSDKFIORIUIKIT-2707] SwiftUI RatingControl Enhancement #794
Changes from 2 commits
5dbc06e
b7471ff
a6bc4f4
5b5e5bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,53 @@ | ||
import FioriThemeManager | ||
import SwiftUI | ||
|
||
public extension RatingControl { | ||
/** | ||
The available styles for the `FUIRatingControl`. | ||
The available styles for the `RatingControl`. | ||
*/ | ||
enum Style { | ||
/** | ||
Editable style. | ||
|
||
Each rating star is a SF Symbol body light style (large scale) with tint color. | ||
Each rating star is large scale with tint color. | ||
This is the default style. | ||
*/ | ||
case editable | ||
|
||
/** | ||
Disabled editable style. | ||
|
||
Each rating star is the same as `Editable` style but with grey color. | ||
Each rating star is the same as `Editable` style but with grey color. User interaction is disabled. | ||
*/ | ||
case editableDisabled | ||
|
||
/** | ||
Standard style. | ||
|
||
This `FUIRatingControl` is read-only. Each rating star is a SF Symbol body light style (small scale). | ||
This `RatingControl` is read-only. Each rating star is in small scale. | ||
*/ | ||
case standard | ||
|
||
/** | ||
Standard large style. | ||
|
||
This `RatingControl` is read-only. Each rating star is in large scale. | ||
*/ | ||
case standardLarge | ||
|
||
/** | ||
Accented read-only style. | ||
|
||
This `FUIRatingControl` is read-only with accented color. Each rating star is the same as in `standard` style. | ||
This `RatingControl` is read-only. Each rating star is in small scale with accented color. | ||
*/ | ||
case accented | ||
|
||
/** | ||
Accented read-only style. | ||
|
||
This `RatingControl` is read-only. Each rating star is in large scale with accented color. | ||
*/ | ||
case accentedLarge | ||
} | ||
|
||
internal static func getAccessibilityLabelString(_ rating: Int, ratingBounds: ClosedRange<Int>) -> String { | ||
|
@@ -45,6 +60,7 @@ extension RatingControlConfiguration { | |
struct RatingItem: Identifiable { | ||
public let id = UUID() | ||
let isOn: Bool | ||
let isHalf: Bool | ||
} | ||
|
||
func ratingItems(_ rating: Int) -> [RatingItem] { | ||
|
@@ -53,7 +69,25 @@ extension RatingControlConfiguration { | |
guard i != self.ratingBounds.upperBound else { | ||
continue | ||
} | ||
items.append(RatingItem(isOn: i < rating)) | ||
items.append(RatingItem(isOn: i < rating, isHalf: false)) | ||
} | ||
return items | ||
} | ||
|
||
func ratingItems(_ averageRating: CGFloat) -> [RatingItem] { | ||
var items: [RatingItem] = [] | ||
for i in self.ratingBounds { | ||
guard i != self.ratingBounds.upperBound else { | ||
continue | ||
} | ||
let diff = averageRating - CGFloat(i) | ||
if diff < 0.3 { | ||
items.append(RatingItem(isOn: false, isHalf: false)) | ||
} else if diff < 0.7 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See FIORITECHE1-8222, web component use 1.0-1.2 -> 1, 1.3-1.7 -> 1.5 and 1.8-1.9 -> 2. So, here, shouldn't diff <= 0.7 or diff < 0.8? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you're right. I'll change it. Actually, I think use half star for 0.25 to 0.75 is more reasonable, since it is a Float. I'll check with the designer. Let's merge this code first and will change later with the changes needed for large text font. |
||
items.append(RatingItem(isOn: false, isHalf: true)) | ||
} else { | ||
items.append(RatingItem(isOn: true, isHalf: false)) | ||
} | ||
} | ||
return items | ||
} | ||
|
@@ -67,10 +101,10 @@ extension RatingControlConfiguration { | |
return .preferredColor(.tintColor) | ||
case .editableDisabled: | ||
return .preferredColor(.quaternaryLabel) | ||
case .standard: | ||
case .standard, .standardLarge: | ||
return .preferredColor(.tertiaryLabel) | ||
case .accented: | ||
return .preferredColor(.mango3) | ||
case .accented, .accentedLarge: | ||
return .preferredColor(.mango8) | ||
} | ||
} | ||
|
||
|
@@ -83,10 +117,10 @@ extension RatingControlConfiguration { | |
return .preferredColor(.tintColor) | ||
case .editableDisabled: | ||
return .preferredColor(.quaternaryLabel) | ||
case .standard: | ||
case .standard, .standardLarge: | ||
return .preferredColor(.tertiaryLabel) | ||
case .accented: | ||
return .preferredColor(.mango4) | ||
case .accented, .accentedLarge: | ||
return .preferredColor(.mango8) | ||
} | ||
} | ||
|
||
|
@@ -112,21 +146,38 @@ extension RatingControlConfiguration { | |
.foregroundColor(self.getOffColor()) | ||
} | ||
|
||
func getHalfImageView() -> some View { | ||
self.getHalfImage() | ||
.resizable() | ||
.scaledToFit() | ||
.frame(width: self.getItemSize().width, height: self.getItemSize().height) | ||
.font(.body) | ||
.fontWeight(.light) | ||
.imageScale(self.getScale()) | ||
.foregroundColor(self.getOnColor()) | ||
} | ||
|
||
func getOnImage() -> Image { | ||
let image: Image = (onImage ?? Image(systemName: "star.fill")) | ||
let image: Image = (onImage ?? FioriIcon.actions.favorite) | ||
.renderingMode(.template) | ||
return image | ||
} | ||
|
||
func getOffImage() -> Image { | ||
let image: Image = (offImage ?? Image(systemName: "star")) | ||
let image: Image = (offImage ?? FioriIcon.actions.unfavorite) | ||
.renderingMode(.template) | ||
return image | ||
} | ||
|
||
func getHalfImage() -> Image { | ||
let image: Image = (halfImage ?? FioriIcon.actions.halfStar) | ||
.renderingMode(.template) | ||
return image | ||
} | ||
|
||
func getScale() -> Image.Scale { | ||
switch self.ratingControlStyle { | ||
case .editable, .editableDisabled: | ||
case .editable, .editableDisabled, .standardLarge, .accentedLarge: | ||
return .large | ||
case .standard, .accented: | ||
return .small | ||
|
@@ -138,7 +189,7 @@ extension RatingControlConfiguration { | |
return itemSize | ||
} | ||
switch self.ratingControlStyle { | ||
case .editable, .editableDisabled: | ||
case .editable, .editableDisabled, .standardLarge, .accentedLarge: | ||
return CGSize(width: 28, height: 28) | ||
case .standard, .accented: | ||
return CGSize(width: 16, height: 16) | ||
|
@@ -150,10 +201,10 @@ extension RatingControlConfiguration { | |
return interItemSpacing | ||
} | ||
switch self.ratingControlStyle { | ||
case .editable, .editableDisabled: | ||
return CGFloat(4) | ||
case .editable, .editableDisabled, .standardLarge, .accentedLarge: | ||
return CGFloat(8) | ||
case .standard, .accented: | ||
return CGFloat(2) | ||
return CGFloat(6) | ||
} | ||
} | ||
|
||
|
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.
I'd like to use the
Bool
variableisLarge
with the UIKitFUIRatingControl
. By default, it is set tofalse
for the read-only state and alwaystrue
for the editable and disabled states. This approach is more simple to me when a smaller size is needed for the editable or disabled states, or for any new states that may be introduced in the future. For us, this means that we do not need to add too many styles, and for the App team, it makes it easy to understand that there are two sizes for each state.I just want to let you know about the difference between your API and mine. However, I am definitely okay with your implementation for these two new styles here for SWiftUI Rating Control.
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.
@JunSong-SH I was originally using the isLarge property. However, later I removed it to reduce the number of properties. I think there are too many properties already.