Skip to content

Commit

Permalink
Make settings view work on iPhone
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinhenderson committed Jul 12, 2024
1 parent 51227fd commit 3c818b8
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "1ef73409b1c134f31d9e6c081ed061acb1296846ec077f77f9b63139c8b5eca7",
"originHash" : "b602d6fc951957ffeb9b75986abca302b915507144de805c999606d8edf9fab0",
"pins" : [
{
"identity" : "sqlite.swift",
Expand Down
195 changes: 106 additions & 89 deletions Echo/Components/Settings/VocabularyOptionsArea.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftData
struct VocabularyOptionsArea: View {
@Environment(Settings.self) var settings: Settings
@EnvironmentObject var editState: EditState

@Query(sort: \Vocabulary.createdAt) var allVocabs: [Vocabulary]

@Environment(\.modelContext) var modelContext
Expand All @@ -20,104 +20,121 @@ struct VocabularyOptionsArea: View {
@State var showCopyAlert = false
@State var copyVocabName = ""

@State var showEditMode = false

var body: some View {
@Bindable var bindableSettings = settings

Form {
Section(content: {
Picker(
String(
localized: "Vocabulary",
comment: "The label that is shown next to the vocab picker"
),
selection: $selectedVocab
) {
ForEach(allVocabs, id: \.self) { vocab in
Text(vocab.name).tag(vocab)

if showEditMode {
EditPage(save: {
showEditMode = false
})
} else {

@Bindable var bindableSettings = settings

Form {
Section(content: {
Picker(
String(
localized: "Vocabulary",
comment: "The label that is shown next to the vocab picker"
),
selection: $selectedVocab
) {
ForEach(allVocabs, id: \.self) { vocab in
Text(vocab.name).tag(vocab)
}
}
}
VStack {
HStack {
Button(action: {
editState.showEditMode = true
}) {
Text("Edit", comment: "Text for edit vocabulary button")
.frame(maxWidth: .infinity)
VStack {
HStack {
Button(action: {

if UIDevice.current.userInterfaceIdiom == .pad {
editState.showEditMode = true

} else {
showEditMode = true
}
}) {
Text("Edit", comment: "Text for edit vocabulary button")
.frame(maxWidth: .infinity)
}
.buttonStyle(.borderedProminent)
.disabled(selectedVocab.systemVocab)


Button(action: {
showCopyAlert = true
copyVocabName = "Copy of '\(selectedVocab.name)'"
}) {
Text("Copy", comment: "Text for copy vocabulary button")
.frame(maxWidth: .infinity)
}
.buttonStyle(.bordered)
.disabled(!selectedVocab.allowCopy)
}
.alert(
String(localized: "Copy Vocabulary", comment: "Alert title for copying vocab"),
isPresented: $showCopyAlert
) {
TextField(String(localized: "Name of the new vocabulary", comment: "TextField label for new vocabulary (hidden"), text: $copyVocabName)
Button(String(localized: "Make copy", comment: "Button text for making copy of a vocabulary")) {
if let newVocab = settings.currentVocab?.copy(copyVocabName) {
newVocab.systemVocab = false
modelContext.insert(newVocab)
selectedVocab = newVocab
}
}
.disabled(selectedVocab.name == copyVocabName)
Button("Cancel", role: .cancel) {
showCopyAlert = false
}
} message: {
Text("Make a copy of the current vocabulary so you can make edits.", comment: "Message for copying vocab alert")
}
.buttonStyle(.borderedProminent)
.disabled(selectedVocab.systemVocab)

Button(action: {
showCopyAlert = true
copyVocabName = "Copy of '\(selectedVocab.name)'"
}) {
Text("Copy", comment: "Text for copy vocabulary button")
.frame(maxWidth: .infinity)
if !selectedVocab.allowCopy {
Text("The copy and edit button are disabled because you cannot copy or edit the spelling vocabulary. Click the spelling and alphabet tab to change spelling options.", comment: "Explanation of why the copy button on the vocab is disabled")
} else if selectedVocab.systemVocab {
Text("The edit button is currently disabled because you are trying to edit a system vocabulary. To edit this vocabulary you must make a copy first.", comment: "Explanation of why the edit button on the vocab is disabled")
}
.buttonStyle(.bordered)
.disabled(!selectedVocab.allowCopy)
}
.alert(
String(localized: "Copy Vocabulary", comment: "Alert title for copying vocab"),
isPresented: $showCopyAlert
}, header: {
Text("Vocabulary", comment: "Header of vocabulary options")
}, footer: {
Text("Select the vocabulary of phrases, words and letters to be used", comment: "Footer of vocabulary options")
})
Section(content: {
Stepper(
value: $bindableSettings.vocabHistory,
in: 1...10,
step: 1
) {
TextField(String(localized: "Name of the new vocabulary", comment: "TextField label for new vocabulary (hidden"), text: $copyVocabName)
Button(String(localized: "Make copy", comment: "Button text for making copy of a vocabulary")) {
if let newVocab = settings.currentVocab?.copy(copyVocabName) {
newVocab.systemVocab = false
modelContext.insert(newVocab)
selectedVocab = newVocab
}
}
.disabled(selectedVocab.name == copyVocabName)
Button("Cancel", role: .cancel) {
showCopyAlert = false
}
} message: {
Text("Make a copy of the current vocabulary so you can make edits.", comment: "Message for copying vocab alert")
}

if !selectedVocab.allowCopy {
Text("The copy and edit button are disabled because you cannot copy or edit the spelling vocabulary. Click the spelling and alphabet tab to change spelling options.", comment: "Explanation of why the copy button on the vocab is disabled")
} else if selectedVocab.systemVocab {
Text("The edit button is currently disabled because you are trying to edit a system vocabulary. To edit this vocabulary you must make a copy first.", comment: "Explanation of why the edit button on the vocab is disabled")
Text(
"Show **\(bindableSettings.vocabHistory)** level of your vocabulary",
comment: "Describe to the user the number of history levels"
)
}
}
}, header: {
Text("Vocabulary", comment: "Header of vocabulary options")
}, footer: {
Text("Select the vocabulary of phrases, words and letters to be used", comment: "Footer of vocabulary options")
})
Section(content: {
Stepper(
value: $bindableSettings.vocabHistory,
in: 1...10,
step: 1
) {
Text(
"Show **\(bindableSettings.vocabHistory)** level of your vocabulary",
comment: "Describe to the user the number of history levels"
)
}
}, header: {
Text("History", comment: "Header for settings about history")
}, footer: {
Text("This is the number of levels of your phrases to show at once.", comment: "Footer for settings about history")
})
}
.navigationTitle(
String(
localized: "Vocabulary",
comment: "The navigation title for the Vocabulary options page"
}, header: {
Text("History", comment: "Header for settings about history")
}, footer: {
Text("This is the number of levels of your phrases to show at once.", comment: "Footer for settings about history")
})
}
.navigationTitle(
String(
localized: "Vocabulary",
comment: "The navigation title for the Vocabulary options page"
)
)
)
.onAppear {
if let unwrapped = settings.currentVocab {
selectedVocab = unwrapped
.onAppear {
if let unwrapped = settings.currentVocab {
selectedVocab = unwrapped
}
}
.onChange(of: selectedVocab) {
settings.currentVocab = selectedVocab
}
}
.onChange(of: selectedVocab) {
settings.currentVocab = selectedVocab
}
}
}
5 changes: 3 additions & 2 deletions Echo/Pages/EditPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import Foundation
import SwiftUI

struct EditPage: View {
@ObservedObject var editState: EditState
var save: () -> Void

@Environment(Settings.self) var settings: Settings

@StateObject var mainCommunicationPageState = MainCommunicationPageState()
Expand Down Expand Up @@ -75,7 +76,7 @@ struct EditPage: View {
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button(String(localized: "Save", comment: "Save button text for saving vocab changes")) {
editState.showEditMode = false
save()
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion Echo/Pages/SettingsPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ struct SettingsPage: View {
var body: some View {
ZStack {
if editState.showEditMode {
EditPage(editState: editState)
EditPage(save: {
editState.showEditMode = false
})
} else if UIDevice.current.userInterfaceIdiom == .pad {
SettingsPagePad(selection: $selection)
} else {
Expand Down

0 comments on commit 3c818b8

Please sign in to comment.