Updated add project screen to use the theme colour if none selected

This commit is contained in:
breadone 2024-06-24 14:16:48 +12:00
parent 71806c9ca4
commit a4302ef65d
No known key found for this signature in database
3 changed files with 37 additions and 20 deletions

View File

@ -18,6 +18,8 @@ struct MainView: View {
@State private var searchText = "" @State private var searchText = ""
@AppStorage(Preferences.themeColour) var themeColor = Color.blue.toHex()!
var body: some View { var body: some View {
NavigationSplitView { NavigationSplitView {
List { List {
@ -52,7 +54,13 @@ struct MainView: View {
private func addItem(name: String, color: String) { private func addItem(name: String, color: String) {
withAnimation { withAnimation {
let newProject = OCProject(name: name, color: color) var newProject: OCProject
if color == Color.clear.toHex()! {
newProject = OCProject(name: name, color: themeColor)
} else {
newProject = OCProject(name: name, color: color)
}
modelContext.insert(newProject) modelContext.insert(newProject)
} }
} }

View File

@ -10,28 +10,41 @@ import SwiftUI
struct NewProjectForm: View { struct NewProjectForm: View {
@State private var newProjectName = "" @State private var newProjectName = ""
@State private var chosenColor: Color = .blue @State private var chosenColor = Color.clear
@Environment(\.dismiss) var dismiss @Environment(\.dismiss) var dismiss
let completionHandler: (String, String) -> Void let completionHandler: (String, String) -> Void
let availColours: [String] = {
var c = Constants.colors.map { $0.toHex()! }
c.insert("Theme", at: 0)
return c
}()
var body: some View { var body: some View {
NavigationView { NavigationView {
Form { Form {
TextField("Project Name", text: $newProjectName) TextField("Project Name", text: $newProjectName)
Section(header: Text("Project Colour")) { Section {
HStack { HStack {
ForEach(Constants.colors, id: \.self) { clr in ForEach(Constants.colors, id: \.self) { color in
ColorChooserCircle(color: clr) if chosenColor == color {
.padding(.vertical) ZStack {
.onTapGesture { Circle()
self.chosenColor = clr .foregroundStyle(color)
Image(systemName: "checkmark")
} }
.onTapGesture { chooseColour(color) }
} else {
Circle()
.foregroundStyle(color)
.onTapGesture { chooseColour(color) }
}
} }
} }
} } header: { Text("Project Colour") } footer: { Text("Leave unselected to use theme colour in settings" )}
} }
.navigationTitle("Add New Project").foregroundStyle(chosenColor) .navigationTitle("Add New Project")
.navigationBarTitleDisplayMode(.inline) .navigationBarTitleDisplayMode(.inline)
.toolbar { .toolbar {
ToolbarItem(placement: .topBarLeading) { ToolbarItem(placement: .topBarLeading) {
@ -47,16 +60,13 @@ struct NewProjectForm: View {
} }
} }
} }
}
struct ColorChooserCircle: View {
var color: Color
var body: some View { private func chooseColour(_ color: Color) {
Circle() if color == chosenColor {
.strokeBorder(Color.gray,lineWidth: 2) chosenColor = .clear
.background(Circle().foregroundColor(color)) } else {
.frame(minHeight: 25) chosenColor = color
}
} }
} }

View File

@ -9,5 +9,4 @@ import SwiftUI
public struct Constants { public struct Constants {
static let colors: [Color] = [.red, .orange, .yellow, .green, .blue, .indigo] static let colors: [Color] = [.red, .orange, .yellow, .green, .blue, .indigo]
static let colorText = ["None", "Red", "Orange", "Yellow", "Green", "Blue", "Indigo"]
} }