228 lines
7.8 KiB
Swift
228 lines
7.8 KiB
Swift
//
|
|
// ContentView.swift
|
|
// Toki Trainer
|
|
//
|
|
// Created by Avery Ada Pace on 11/2/21.
|
|
//
|
|
|
|
import SwiftUI
|
|
import CoreData
|
|
|
|
extension String: Identifiable {
|
|
public var id: String { self }
|
|
}
|
|
|
|
struct ContentView: View {
|
|
@Environment(\.managedObjectContext) private var viewContext
|
|
|
|
@State private var tokiInput: String = ""
|
|
|
|
var body: some View {
|
|
TabView {
|
|
DictionaryView()
|
|
.tabItem {
|
|
Image(systemName: "book")
|
|
Text("Dictionary")
|
|
}
|
|
TranslatorView()
|
|
.tabItem {
|
|
Image(systemName: "message")
|
|
Text("Phrase Lookup")
|
|
}
|
|
FlashCardLessonsView()
|
|
.tabItem {
|
|
Image(systemName: "character.textbox")
|
|
Text("Flash Cards")
|
|
}
|
|
// FlashCardResultsView()
|
|
// .tabItem {
|
|
// Image(systemName: "number.circle")
|
|
// Text("Flash Card Results")
|
|
// }
|
|
}
|
|
}
|
|
|
|
func openPartsOfSpeechView() {
|
|
print("Button pressed.")
|
|
}
|
|
}
|
|
|
|
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 {
|
|
static var previews: some View {
|
|
ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
|
|
}
|
|
}
|