add helper functions for Color/hex conversions

This commit is contained in:
Satindar Dhillon
2022-04-01 09:14:02 -07:00
parent 72438d2d97
commit 90da2e2988
5 changed files with 101 additions and 17 deletions

View File

@ -25,12 +25,12 @@ final class LabelsViewModel: ObservableObject {
.store(in: &subscriptions)
}
func createLabel(dataService: DataService, name: String, color: String, description: String?) {
func createLabel(dataService: DataService, name: String, color: Color, description: String?) {
isLoading = true
dataService.createLabelPublisher(
name: name,
color: color,
color: color.hex ?? "",
description: description
).sink(
receiveCompletion: { [weak self] _ in
@ -48,6 +48,10 @@ final class LabelsViewModel: ObservableObject {
struct LabelsView: View {
@EnvironmentObject var dataService: DataService
@StateObject var viewModel = LabelsViewModel()
@State private var newLabelName = ""
@State private var newLabelColor = Color.clear
let footerText = "Use labels to create curated collections of links."
var body: some View {
@ -69,24 +73,23 @@ struct LabelsView: View {
private var innerBody: some View {
Group {
Section(footer: Text(footerText)) {
TextField("Label Name", text: $newLabelName)
ColorPicker(
newLabelColor == .clear ? "Select Color" : newLabelColor.description,
selection: $newLabelColor
)
Button(
action: {
viewModel.createLabel(
dataService: dataService,
name: "ios-test",
color: "#F9D354",
description: "hardcoded test label"
name: newLabelName,
color: newLabelColor,
description: nil
)
},
label: {
HStack {
Image(systemName: "plus.circle.fill").foregroundColor(.green)
Text("Create a new label")
Spacer()
}
}
label: { Text("Create Label") }
)
.disabled(viewModel.isLoading)
.disabled(viewModel.isLoading || newLabelName.isEmpty || newLabelColor == .clear)
}
if !viewModel.labels.isEmpty {

View File

@ -63,8 +63,10 @@ struct ProfileView: View {
}
Section {
NavigationLink(destination: LabelsView()) {
Text("Labels")
if FeatureFlag.enableLabels {
NavigationLink(destination: LabelsView()) {
Text("Labels")
}
}
NavigationLink(destination: NewsletterEmailsView()) {

View File

@ -0,0 +1,79 @@
import SwiftUI
public extension Color {
/// Inititializes a `Color` from a hex value
/// - Parameter hex: Color hex value. ex: `#FFFFFF`
///
init?(hex: String) {
var hexSanitized = hex.trimmingCharacters(in: .whitespacesAndNewlines)
hexSanitized = hexSanitized.replacingOccurrences(of: "#", with: "")
var rgb: UInt64 = 0
var red: CGFloat = 0.0
var green: CGFloat = 0.0
var blue: CGFloat = 0.0
var alpha: CGFloat = 1.0
let length = hexSanitized.count
guard Scanner(string: hexSanitized).scanHexInt64(&rgb) else { return nil }
if length == 6 {
red = CGFloat((rgb & 0xFF0000) >> 16) / 255.0
green = CGFloat((rgb & 0x00FF00) >> 8) / 255.0
blue = CGFloat(rgb & 0x0000FF) / 255.0
} else if length == 8 {
red = CGFloat((rgb & 0xFF00_0000) >> 24) / 255.0
green = CGFloat((rgb & 0x00FF_0000) >> 16) / 255.0
blue = CGFloat((rgb & 0x0000_FF00) >> 8) / 255.0
alpha = CGFloat(rgb & 0x0000_00FF) / 255.0
} else {
return nil
}
self.init(red: red, green: green, blue: blue, opacity: alpha)
}
var hex: String? {
if let hex = toHex() {
return "#\(hex)"
} else {
return nil
}
}
private func toHex() -> String? {
let uic = UIColor(self)
guard let components = uic.cgColor.components, components.count >= 3 else {
return nil
}
let red = Float(components[0])
let green = Float(components[1])
let blue = Float(components[2])
var alpha = Float(1.0)
if components.count >= 4 {
alpha = Float(components[3])
}
if alpha != Float(1.0) {
return String(
format: "%02lX%02lX%02lX%02lX",
lroundf(red * 255),
lroundf(green * 255),
lroundf(blue * 255),
lroundf(alpha * 255)
)
} else {
return String(
format: "%02lX%02lX%02lX",
lroundf(red * 255),
lroundf(green * 255),
lroundf(blue * 255)
)
}
}
}

View File

@ -14,6 +14,6 @@ public enum FeatureFlag {
public static let enablePushNotifications = false
public static let enableShareButton = false
public static let enableSnooze = false
public static let showFeedItemTags = false
public static let enableLabels = false
public static let useLocalWebView = true
}

View File

@ -156,7 +156,7 @@ public struct GridCard: View {
.onTapGesture { tapHandler() }
// Category Labels
if FeatureFlag.showFeedItemTags {
if FeatureFlag.enableLabels {
ScrollView(.horizontal, showsIndicators: false) {
HStack {
TextChip(text: "label", color: .red)