Refactor, decompose and add previews. Fix part of speech sheet pop over

This commit is contained in:
Madeline
2022-10-08 08:35:14 -04:00
parent aae0b6c87e
commit 5999cc3533
12 changed files with 242 additions and 186 deletions

View File

@@ -44,6 +44,11 @@ class TokiDictionaryViewModel: ObservableObject {
dictionary = []
translatedDictionary = []
if input.isEmpty {
dictionary = fullDictionary
translatedDictionary.append(TokiSubWordListEntry("Dictionary"))
}
let capturePattern = #"(\w+)"#
let captures = self.searchStringForRegex(string: input, regex: capturePattern)
for capture in captures {

View File

@@ -15,8 +15,6 @@ extension String: Identifiable {
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
@State private var tokiInput: String = ""
var body: some View {
TabView {
DictionaryView()
@@ -34,11 +32,6 @@ struct ContentView: View {
Image(systemName: "character.textbox")
Text("Flash Cards")
}
// FlashCardResultsView()
// .tabItem {
// Image(systemName: "number.circle")
// Text("Flash Card Results")
// }
}
}
@@ -47,177 +40,6 @@ struct ContentView: View {
}
}
struct TranslatorView: View {
@ObservedObject var tokiDictViewModel = TokiDictionaryViewModel()
@State private var selectedPartOfSpeech: String?
@State private var tokiInput: String = ""
@State private var translateToTokiPona: Bool = false
var body: some View {
VStack {
Button(action: changeTranslationDirection) {
if translateToTokiPona == true {
LanguageDirectionView(from: "English", to: "Toki Pona", fromColor: .blue, toColor: .cyan)
} else {
LanguageDirectionView(from: "Toki Pona", to: "English", fromColor: .cyan, toColor: .blue)
}
}
TextField("Enter Toki Pona Word or Phrase", text: $tokiInput)
.multilineTextAlignment(.center)
.textInputAutocapitalization(.never)
.disableAutocorrection(true)
.padding(8)
.onSubmit {
tokiDictViewModel.translatePhrase(tokiInput)
}
if tokiInput.count == 0 {
List(tokiDictViewModel.dictionary, id: \.word) { entry in
TokiWordsListEntryView(entry: entry, selectedPartOfSpeech: $selectedPartOfSpeech)
}
//TokiWordsView(tokiDictViewModel: tokiDictViewModel, selectedPartOfSpeech: selectedPartOfSpeech, tokiInput: $tokiInput)
} else {
List(tokiDictViewModel.translatedDictionary, id: \.header) { section in
Section {
ForEach(section.subDictionary, id: \.word) { entry in
TokiWordsListEntryView(entry: entry, selectedPartOfSpeech: $selectedPartOfSpeech)
}
} header: {
Text(section.header)
}
}
// ForEach(tokiDictViewModel.translatedDictionary) { entry in
// TokiSectionedWordsView(tokiDictViewModel: tokiDictViewModel, tokiInput: $tokiInput)
// }
}
}
.onChange(of: tokiInput) { newValue in
tokiDictViewModel.translatePhrase(newValue)
}
}
func changeTranslationDirection() {
translateToTokiPona.toggle()
}
}
struct TokiSectionedWordsView: View {
@ObservedObject var tokiDictViewModel: TokiDictionaryViewModel
@Binding var tokiInput: String
var body: some View {
Section(header: Text(tokiInput)) {
TokiWordsView(tokiDictViewModel: tokiDictViewModel, tokiInput: $tokiInput)
}
}
}
struct TokiWordsListEntryView: View {
@State var entry: TokiDictEntry
@Binding var selectedPartOfSpeech: String?
var body: some View {
VStack(alignment: .leading) {
Text(entry.word)
.font(.title)
ForEach(entry.definitions, id: \.pos) { definition in
HStack(alignment: .top) {
Button(action: {
self.selectedPartOfSpeech = String(definition.pos)
}) {
Text(definition.pos)
.frame(width: 45, height: 22, alignment: .center)
.foregroundColor(.black)
.background(Color(K.posColors[definition.pos]!))
.cornerRadius(5.0)
.padding(4)
}
.buttonStyle(BorderlessButtonStyle())
Text(definition.definition)
.fixedSize(horizontal: false, vertical: true)
.padding(4)
}
}
}
}
}
struct TokiWordsView: View {
@ObservedObject var tokiDictViewModel: TokiDictionaryViewModel
@State var selectedPartOfSpeech: String?
@Binding var tokiInput: String
var body: some View {
List(tokiDictViewModel.dictionary, id: \.word) { entry in
VStack(alignment: .leading) {
Text(entry.word)
.font(.title)
ForEach(entry.definitions, id: \.pos) { definition in
HStack(alignment: .top) {
Button(action: {
self.selectedPartOfSpeech = String(definition.pos)
}) {
Text(definition.pos)
.frame(width: 45, height: 22, alignment: .center)
.foregroundColor(.black)
.background(Color(K.posColors[definition.pos]!))
.cornerRadius(5.0)
.padding(4)
}
.buttonStyle(BorderlessButtonStyle())
Text(definition.definition)
.fixedSize(horizontal: false, vertical: true)
.padding(4)
}
}
}
}
.sheet(item: $selectedPartOfSpeech) { selectedPOS in
PartsOfSpeechView(selectedPartOfSpeech: selectedPOS, partsOfSpeech: tokiDictViewModel.partsOfSpeech)
}
.onChange(of: tokiInput) { newValue in
tokiDictViewModel.translatePhrase(newValue)
}
}
}
struct LanguageDirectionView: View {
private var fromText: String = ""
private var toText: String = ""
private var fromColor: Color = .black
private var toColor: Color = .black
init(from: String, to: String, fromColor: Color, toColor: Color) {
self.fromText = from
self.toText = to
self.fromColor = fromColor
self.toColor = toColor
}
var body: some View {
HStack {
Text(fromText)
.bold()
.multilineTextAlignment(.center)
.foregroundColor(.white)
.frame(width: 100)
.background(fromColor)
.border(fromColor)
.cornerRadius(5)
Image(systemName: "chevron.right")
//rforegroundColor(.black)
Text(toText)
.bold()
.multilineTextAlignment(.center)
.foregroundColor(.white)
.frame(width: 100)
.background(toColor)
.border(toColor)
.cornerRadius(5)
}
.padding(.top, 4)
}
}
struct ContentView_Previews: PreviewProvider {

View File

@@ -157,7 +157,7 @@ struct FlashCardStack: View {
var cardInDatabase = false
for answer in flashCardAnswers {
if answer.word == shuffledDictionary[currentFlashCard].word {
print("word in database: \(answer.word)")
print("word in database: \(answer.word ?? "nil")")
print("tries: \(answer.triesCount)")
print("correct`: \(answer.correctCount)")
cardInDatabase = true

View File

@@ -26,9 +26,19 @@ struct DictionaryView: View {
List(tokiDictViewModel.dictionary, id: \.word) { entry in
TokiWordsListEntryView(entry: entry, selectedPartOfSpeech: $selectedPartOfSpeech)
}
.sheet(item: $selectedPartOfSpeech) { selectedPOS in
PartsOfSpeechView(selectedPartOfSpeech: selectedPOS)
}
.onChange(of: tokiInput) { newValue in
tokiDictViewModel.filterDictionary(newValue)
}
}
}
}
struct DictionaryView_Previews: PreviewProvider {
static var previews: some View {
DictionaryView(selectedPartOfSpeech: "n").previewLayout(.sizeThatFits).environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}

View File

@@ -0,0 +1,53 @@
//
// LanguageDirectionView.swift
// Toki Trainer
//
// Created by maddiefuzz on 10/8/22.
//
import SwiftUI
struct LanguageDirectionView: View {
private var fromText: String = ""
private var toText: String = ""
private var fromColor: Color = .black
private var toColor: Color = .black
init(from: String, to: String, fromColor: Color, toColor: Color) {
self.fromText = from
self.toText = to
self.fromColor = fromColor
self.toColor = toColor
}
var body: some View {
HStack {
Text(fromText)
.bold()
.multilineTextAlignment(.center)
.foregroundColor(.white)
.frame(width: 100)
.background(fromColor)
.border(fromColor)
.cornerRadius(5)
Image(systemName: "chevron.right")
Text(toText)
.bold()
.multilineTextAlignment(.center)
.foregroundColor(.white)
.frame(width: 100)
.background(toColor)
.border(toColor)
.cornerRadius(5)
}
.padding(.top, 4)
}
}
struct LanguageDirectionView_Previews: PreviewProvider {
static var previews: some View {
LanguageDirectionView(from: "English", to: "Toki Pona", fromColor: .blue, toColor: .cyan)
.previewLayout(.fixed(width: 260, height: 40))
}
}

View File

@@ -12,8 +12,6 @@ struct PartsOfSpeechView: View {
@ObservedObject var tokiDictViewModel = TokiDictionaryViewModel()
var partsOfSpeech: [TokiPartOfSpeech]
var body: some View {
VStack {
Text("Parts of Speech")
@@ -42,8 +40,8 @@ struct PartsOfSpeechView: View {
struct PartsOfSpeechView_Previews: PreviewProvider {
static var previews: some View {
PartsOfSpeechView(selectedPartOfSpeech: "sep", partsOfSpeech: [TokiPartOfSpeech(pos: "sep", definition: "test")])
PartsOfSpeechView(selectedPartOfSpeech: "sep")
.preferredColorScheme(.dark)
PartsOfSpeechView(selectedPartOfSpeech: "sep", partsOfSpeech: [TokiPartOfSpeech(pos: "sep", definition: "test")])
PartsOfSpeechView(selectedPartOfSpeech: "sep")
}
}

View File

@@ -0,0 +1,47 @@
//
// TokiWordsListEntryView.swift
// Toki Trainer
//
// Created by maddiefuzz on 10/8/22.
//
import SwiftUI
struct TokiWordsListEntryView: View {
@State var entry: TokiDictEntry
@Binding var selectedPartOfSpeech: String?
var body: some View {
VStack(alignment: .leading) {
Text(entry.word)
.font(.title)
ForEach(entry.definitions, id: \.pos) { definition in
HStack(alignment: .top) {
Button(action: {
self.selectedPartOfSpeech = String(definition.pos)
}) {
Text(definition.pos)
.frame(width: 45, height: 22, alignment: .center)
.foregroundColor(.black)
.background(Color(K.posColors[definition.pos]!))
.cornerRadius(5.0)
.padding(4)
}
.buttonStyle(BorderlessButtonStyle())
Text(definition.definition)
.fixedSize(horizontal: false, vertical: true)
.padding(4)
}
}
}
}
}
struct TokiWordsListEntryView_Previews: PreviewProvider {
static var entry = TokiDictionaryViewModel().dictionary[5]
static var previews: some View {
TokiWordsListEntryView(entry: entry, selectedPartOfSpeech: .constant("n"))
.previewLayout(.sizeThatFits)
}
}

View File

@@ -0,0 +1,68 @@
//
// TranslatorView.swift
// Toki Trainer
//
// Created by maddiefuzz on 10/8/22.
//
import SwiftUI
struct TranslatorView: View {
@ObservedObject var tokiDictViewModel = TokiDictionaryViewModel()
@State private var selectedPartOfSpeech: String?
@State private var tokiInput: String = ""
@State private var translateToTokiPona: Bool = false
var body: some View {
VStack {
Button(action: changeTranslationDirection) {
// TODO: This needs to actually switch how the lookup happens
if translateToTokiPona == true {
LanguageDirectionView(from: "English", to: "Toki Pona", fromColor: .blue, toColor: .cyan)
} else {
LanguageDirectionView(from: "Toki Pona", to: "English", fromColor: .cyan, toColor: .blue)
}
}
TextField("Enter Toki Pona Word or Phrase", text: $tokiInput)
.multilineTextAlignment(.center)
.textInputAutocapitalization(.never)
.disableAutocorrection(true)
.padding(8)
.onSubmit {
tokiDictViewModel.translatePhrase(tokiInput)
}
if tokiInput.count == 0 {
List(tokiDictViewModel.dictionary, id: \.word) { entry in
TokiWordsListEntryView(entry: entry, selectedPartOfSpeech: $selectedPartOfSpeech)
}
} else {
List(tokiDictViewModel.translatedDictionary, id: \.header) { section in
Section {
ForEach(section.subDictionary, id: \.word) { entry in
TokiWordsListEntryView(entry: entry, selectedPartOfSpeech: $selectedPartOfSpeech)
}
} header: {
Text(section.header)
}
}
}
}
.sheet(item: $selectedPartOfSpeech) { selectedPOS in
PartsOfSpeechView(selectedPartOfSpeech: selectedPOS)
}
.onChange(of: tokiInput) { newValue in
tokiDictViewModel.translatePhrase(newValue)
}
}
func changeTranslationDirection() {
translateToTokiPona.toggle()
}
}
struct TranslatorView_Previews: PreviewProvider {
static var previews: some View {
TranslatorView().previewLayout(.sizeThatFits).environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}