Stack of cards works properly, no animation

This commit is contained in:
Avery Pace 2021-11-06 13:35:56 -04:00
parent e38fdeb324
commit ffa1094722
5 changed files with 110 additions and 16 deletions

View File

@ -16,6 +16,7 @@
7E28111D273302860063DC78 /* toki-dictionary.json in Resources */ = {isa = PBXBuildFile; fileRef = 7E28111B273302860063DC78 /* toki-dictionary.json */; };
7E28112227330DD30063DC78 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E28112127330DD20063DC78 /* Constants.swift */; };
7E71E6ED2735D70C007CFF72 /* FlashCardView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E71E6EC2735D70C007CFF72 /* FlashCardView.swift */; };
7E71E6F12736DAE4007CFF72 /* FlashCardsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E71E6F02736DAE4007CFF72 /* FlashCardsViewModel.swift */; };
7E943A21273211C200E7DDF4 /* Toki_TrainerApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E943A20273211C200E7DDF4 /* Toki_TrainerApp.swift */; };
7E943A23273211C200E7DDF4 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E943A22273211C200E7DDF4 /* ContentView.swift */; };
7E943A25273211C300E7DDF4 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7E943A24273211C300E7DDF4 /* Assets.xcassets */; };
@ -34,6 +35,7 @@
7E28111B273302860063DC78 /* toki-dictionary.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "toki-dictionary.json"; sourceTree = "<group>"; };
7E28112127330DD20063DC78 /* Constants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
7E71E6EC2735D70C007CFF72 /* FlashCardView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlashCardView.swift; sourceTree = "<group>"; };
7E71E6F02736DAE4007CFF72 /* FlashCardsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlashCardsViewModel.swift; sourceTree = "<group>"; };
7E943A1D273211C200E7DDF4 /* Toki Trainer.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Toki Trainer.app"; sourceTree = BUILT_PRODUCTS_DIR; };
7E943A20273211C200E7DDF4 /* Toki_TrainerApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Toki_TrainerApp.swift; sourceTree = "<group>"; };
7E943A22273211C200E7DDF4 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
@ -79,6 +81,7 @@
isa = PBXGroup;
children = (
7E20D6002734466800D75B9A /* TokiDictionaryViewModel.swift */,
7E71E6F02736DAE4007CFF72 /* FlashCardsViewModel.swift */,
);
path = ViewModels;
sourceTree = "<group>";
@ -207,6 +210,7 @@
7E943A2D273211C300E7DDF4 /* Toki_Trainer.xcdatamodeld in Sources */,
7E943A2A273211C300E7DDF4 /* Persistence.swift in Sources */,
7E20D5FF2733AFE700D75B9A /* PartsOfSpeechView.swift in Sources */,
7E71E6F12736DAE4007CFF72 /* FlashCardsViewModel.swift in Sources */,
7E20D6012734466800D75B9A /* TokiDictionaryViewModel.swift in Sources */,
7E2811192733027F0063DC78 /* TokiPartOfSpeech.swift in Sources */,
7E943A23273211C200E7DDF4 /* ContentView.swift in Sources */,

View File

@ -36,5 +36,21 @@
landmarkType = "24">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "D81699D7-BFD5-4D4D-BE35-620044BF33CC"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Toki Trainer/Views/FlashCardView.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "94"
endingLineNumber = "94"
landmarkName = "body"
landmarkType = "24">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>

View File

@ -0,0 +1,25 @@
//
// FlashCardsViewModel.swift
// Toki Trainer
//
// Created by Avery Ada Pace on 11/6/21.
//
import Foundation
class FlashCardsViewModel: ObservableObject {
let jsonLoader = TokiJSONLoader()
private var fullDictionary: [TokiDictEntry] = []
@Published var randomDictionary: [TokiDictEntry] = []
init() {
if let safeDictionary = jsonLoader.loadDictionary() {
fullDictionary = safeDictionary
randomDictionary = safeDictionary
//randomDictionary.shuffle()
}
}
}

View File

@ -7,8 +7,8 @@
import Foundation
class TokiDictionaryViewModel: ObservableObject
{
class TokiDictionaryViewModel: ObservableObject {
let jsonLoader: TokiJSONLoader = TokiJSONLoader()
private var fullDictionary: [TokiDictEntry] = []

View File

@ -9,28 +9,65 @@ import SwiftUI
struct FlashCardView: View {
//@ObservedObject var tokiDictViewModel = TokiDictionaryViewModel()
@ObservedObject var flashCardsViewModel = FlashCardsViewModel()
var body: some View {
VStack {
Spacer()
.frame(height: 100
)
FlashCard(canBeFlipped: true)
Spacer()
FlashCardStack()
//FlashCard(canBeFlipped: true, dictionaryEntry: tokiDictViewModel.dictionary.first!)
FlashCardStack(dictionary: flashCardsViewModel.randomDictionary)
}
}
}
struct FlashCardStack: View {
@State var dictionary: [TokiDictEntry]
@State private var flashCards: [FlashCard] = []
@State private var flashCardsCanBeFlipped: [Bool] = []
var body: some View {
ZStack {
ForEach(0 ..< 8) { item in
FlashCard(canBeFlipped: false)
.offset(x: 0, y: -30 * CGFloat(item))
VStack {
ZStack {
ForEach(flashCards.indices, id: \.self) { i in
if i == 0 {
flashCards[0]
.offset(x: 0, y: -300)
.zIndex(0)
} else if i < 10 {
flashCards[i]
.offset(x: 0, y: 30 * CGFloat(i))
.zIndex(Double(-i))
}
}
}
.onAppear {
initFlashCardsArray()
flashCards[0].setCanBeFlipped(true)
}
Button {
self.popFromDictionary()
} label: {
Text("Next Card")
}
.background(.white)
.animation(.default)
}
}
func initFlashCardsArray() {
flashCards = []
for index in dictionary.indices {
flashCardsCanBeFlipped.append(false)
flashCards.append(FlashCard(canBeFlipped: $flashCardsCanBeFlipped[index], dictionaryEntry: dictionary[index]))
}
}
func popFromDictionary() {
dictionary.removeFirst()
initFlashCardsArray()
}
}
struct FlashCard: View {
@ -38,28 +75,40 @@ struct FlashCard: View {
@State var isFaceDown = false
@State var rotationAngle: Double = 0
@State var canBeFlipped: Bool
@Binding var canBeFlipped: Bool
var dictionaryEntry: TokiDictEntry
var body: some View {
let flipDegrees = isFaceDown ? 0.0 : 180.0
Text("")
.modifier(CardFlipModifier(isFaceDown: isFaceDown, frontText: "linja", backText: "long, very thin, floppy thing, e.g. string, rope, hair, thread, cord, chain"))
.modifier(CardFlipModifier(isFaceDown: isFaceDown, frontText: dictionaryEntry.word, backText: concatenateDefinitions()))
.frame(width: 0.8 * screen.width, height: 200.0)
.font(.title)
.rotation3DEffect(self.isFaceDown ? Angle(degrees: 180) : Angle(degrees: 0), axis: (x: 0.0, y: 10.0, z: 0.0))
.animation(.default)
.onTapGesture {
if canBeFlipped == true {
print("onTapGesture called")
if self.canBeFlipped == true {
self.isFaceDown.toggle()
}
}
}
func concatenateDefinitions() -> String {
var result = String()
for definition in dictionaryEntry.definitions {
result.append(contentsOf: definition.definition)
}
return result
}
func setCanBeFlipped(_ input: Bool) {
canBeFlipped = input
print("setCanBeFlipped called")
self.canBeFlipped.toggle()
}
}