Skip to content

Instantly share code, notes, and snippets.

@Codelaby
Last active May 4, 2024 09:05
Show Gist options
  • Save Codelaby/676ee9f0e1cdec93e35a8e577c9f9018 to your computer and use it in GitHub Desktop.
Save Codelaby/676ee9f0e1cdec93e35a8e577c9f9018 to your computer and use it in GitHub Desktop.
Combine styles Swifui
import SwiftUI
enum RowPriceStyle {
case plain
case bordered
case outlined
case twoColors
func isTextColored() -> Bool {
switch self {
case .plain:
return true
case .bordered, .twoColors, .outlined:
return false
}
}
}
struct RowPriceStyleModifier: ViewModifier {
let style: RowPriceStyle
let color: Color
let color2 : Color
init(style: RowPriceStyle, color: Color, color2: Color) {
self.style = style
self.color = color
self.color2 = color2
//self.color2 = color.adjust(saturation: 1, brightness: 1, opacity: 1)
}
func body(content: Content) -> some View {
switch style {
case .plain:
return AnyView(content)
case .bordered:
return AnyView(content
.background(RoundedRectangle(cornerRadius: 16)
.foregroundColor(color2))
.opacity(0.38))
case .outlined:
return AnyView(content
.background(RoundedRectangle(cornerRadius: 16).strokeBorder(color2, lineWidth: 2))
.foregroundColor(.primary))
case .twoColors:
return AnyView(content
.background(RoundedRectangle(cornerRadius: 16)
.foregroundColor(color2))
//.opacity(0.38)
//.overlay(RoundedRectangle(cornerRadius: 16).stroke(color.opacity(0.68), lineWidth: 2))
)
}
}
}
struct RowPriceView: View {
let item: LightPriceModelUI
var rowStyle: RowPriceStyle
init(to item: LightPriceModelUI, rowStyle: RowPriceStyle = .plain) { // Inicializador personalizado
self.item = item
self.rowStyle = rowStyle
}
var body: some View {
HStack(spacing: 16) {
//.padding(.trailing, 8)
Text(item.hour)
.font(.caption)
.foregroundColor(.gray)
Text(String(format: "%.3f €", item.price))
.font(.callout)
//.foregroundColor(rowStyle.isTextColored() ? getColorForPrice(item.typePrice) : .primary)
.foregroundColor(getColorForPrice(item.typePrice))
+ Text(" kWh")
.font(.caption2)
.foregroundColor(.gray)
Spacer()
if let icon = getIconIsTop(item.typePrice) {
Image(systemName: icon)
.foregroundColor(iconColorized(getColorForPrice(item.typePrice)))
.opacity(0.68)
}
Text(String(format: "%.0f", item.diffPercent) + "%")
.font(.caption)
.foregroundColor(.gray)
.frame(minWidth: 40)
}
.padding(.horizontal)
.listRowInsets(EdgeInsets())
.frame(height: 48)
.modifier(RowPriceStyleModifier(style: rowStyle, color: getColorForPrice(item.typePrice), color2: getColorForPrice2(item.typePrice)))
}
private func iconColorized(_ color: Color) -> Color {
switch rowStyle {
case .plain, .outlined:
return color
case .bordered:
return .white
case .twoColors:
return color
}
}
private func getColorForPrice(_ typePrice: TypePrice) -> Color {
switch typePrice {
case .veryCheap, .cheap:
return CustomColor.cheapPrice
case .regular:
return CustomColor.regularPrice
case .expensive, .veryExpensive:
return CustomColor.expensivePrice
}
}
private func getColorForPrice2(_ typePrice: TypePrice) -> Color {
switch typePrice {
case .veryCheap, .cheap:
return CustomColor.cheapPrice2
case .regular:
return CustomColor.regularPrice2
case .expensive, .veryExpensive:
return CustomColor.expensivePrice2
}
}
private func getIconIsTop(_ typePrice: TypePrice) -> String? {
switch typePrice {
case .veryCheap:
return "star.circle.fill"
case .veryExpensive:
return "exclamationmark.circle.fill"
default:
return nil
}
}
}
struct RowPriceView_Previews: PreviewProvider {
static let item = LightPriceModelUI(id: UUID(), created_at: Date(), tariff: .pcb, price: 2.3456, hour: "12h-13h", typePrice: .veryCheap, diffPercent: 0.3)
static var previews: some View {
RowPriceView(to: item, rowStyle: .plain)
.padding()
.preferredColorScheme(.light)
RowPriceView(to: item, rowStyle: .bordered)
.padding()
.preferredColorScheme(.dark)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment