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

@@ -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

@@ -0,0 +1,47 @@
//
// PartsOfSpeechView.swift
// Toki Trainer
//
// Created by Avery Ada Pace on 11/4/21.
//
import SwiftUI
struct PartsOfSpeechView: View {
var selectedPartOfSpeech: String? = nil
@ObservedObject var tokiDictViewModel = TokiDictionaryViewModel()
var body: some View {
VStack {
Text("Parts of Speech")
.padding()
VStack(alignment: .leading) {
ForEach(tokiDictViewModel.partsOfSpeech, id: \.pos) { pos in
HStack {
Text(pos.pos)
.frame(width: 45, height: 22, alignment: .center)
.background(Color(K.posColors[pos.pos]!))
.cornerRadius(5.0)
.padding(1)
Text(pos.definition)
Spacer()
}
//.background(.blue)
.background((selectedPartOfSpeech == pos.pos) ? Color(UIColor.systemGray4) : Color(UIColor.systemBackground))
.cornerRadius(5.0)
.padding(2)
}
}
Spacer()
}
}
}
struct PartsOfSpeechView_Previews: PreviewProvider {
static var previews: some View {
PartsOfSpeechView(selectedPartOfSpeech: "sep")
.preferredColorScheme(.dark)
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)
}
}