Skip to content

Commit

Permalink
broke activityViewModel to each activities
Browse files Browse the repository at this point in the history
  • Loading branch information
kaorulego5x committed Nov 30, 2022
1 parent 204d110 commit d3ee6ef
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 229 deletions.
20 changes: 20 additions & 0 deletions Getit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
CD8732A62900090D001AA69C /* LottieView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD8732A52900090D001AA69C /* LottieView.swift */; };
CD8732AA29000AF7001AA69C /* listening.json in Resources */ = {isa = PBXBuildFile; fileRef = CD8732A929000AF7001AA69C /* listening.json */; };
CD8928142937887C00A812AB /* RAMViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD8928132937887C00A812AB /* RAMViewModel.swift */; };
CD892816293791DC00A812AB /* IdiomViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD892815293791DC00A812AB /* IdiomViewModel.swift */; };
CD8928182937962B00A812AB /* ShuffleViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD8928172937962B00A812AB /* ShuffleViewModel.swift */; };
CD89281B293796A100A812AB /* clutterCutter.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD89281A293796A100A812AB /* clutterCutter.swift */; };
CD8C42C428DA052100356824 /* WrappingHStack in Frameworks */ = {isa = PBXBuildFile; productRef = CD8C42C328DA052100356824 /* WrappingHStack */; };
CD9CC33C28BFC7C000224FC7 /* NotoSansJP-Bold.otf in Resources */ = {isa = PBXBuildFile; fileRef = CD9CC33B28BFC7C000224FC7 /* NotoSansJP-Bold.otf */; };
CD9CC33F28BFC8B000224FC7 /* MapView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD9CC33E28BFC8B000224FC7 /* MapView.swift */; };
Expand Down Expand Up @@ -88,6 +91,9 @@
CD8732A52900090D001AA69C /* LottieView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LottieView.swift; sourceTree = "<group>"; };
CD8732A929000AF7001AA69C /* listening.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = listening.json; path = ../../../../../Downloads/listening.json; sourceTree = "<group>"; };
CD8928132937887C00A812AB /* RAMViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RAMViewModel.swift; sourceTree = "<group>"; };
CD892815293791DC00A812AB /* IdiomViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IdiomViewModel.swift; sourceTree = "<group>"; };
CD8928172937962B00A812AB /* ShuffleViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShuffleViewModel.swift; sourceTree = "<group>"; };
CD89281A293796A100A812AB /* clutterCutter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = clutterCutter.swift; sourceTree = "<group>"; };
CD9CC33B28BFC7C000224FC7 /* NotoSansJP-Bold.otf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "NotoSansJP-Bold.otf"; sourceTree = "<group>"; };
CD9CC33E28BFC8B000224FC7 /* MapView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapView.swift; sourceTree = "<group>"; };
CD9CC34728C0CC3800224FC7 /* Phrase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Phrase.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -185,6 +191,14 @@
path = Lottie;
sourceTree = "<group>";
};
CD8928192937969400A812AB /* Utils */ = {
isa = PBXGroup;
children = (
CD89281A293796A100A812AB /* clutterCutter.swift */,
);
path = Utils;
sourceTree = "<group>";
};
CD9CC33D28BFC8A300224FC7 /* MapView */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -262,6 +276,7 @@
CDC9A88D286D810700185C61 /* Getit */ = {
isa = PBXGroup;
children = (
CD8928192937969400A812AB /* Utils */,
CD9CC34C28C0E15F00224FC7 /* Repositories */,
CD9CC34928C0E12600224FC7 /* Lib */,
CD52053D288D75DB00ECA297 /* Resources */,
Expand Down Expand Up @@ -331,7 +346,9 @@
CD23840628D1FF05003FBD74 /* RAMView.swift */,
CD8928132937887C00A812AB /* RAMViewModel.swift */,
CD23840A28D1FF2A003FBD74 /* IdiomView.swift */,
CD892815293791DC00A812AB /* IdiomViewModel.swift */,
CD23840828D1FF22003FBD74 /* ShuffleView.swift */,
CD8928172937962B00A812AB /* ShuffleViewModel.swift */,
CD9CC36428C4A7B300224FC7 /* ResultView.swift */,
CD9CC36628C4AA8B00224FC7 /* ResultViewModel.swift */,
);
Expand Down Expand Up @@ -559,7 +576,10 @@
CD8732A62900090D001AA69C /* LottieView.swift in Sources */,
CDC9A8DC286D8CC400185C61 /* LoadingView.swift in Sources */,
CDC9A8CF286D87B900185C61 /* HomeViewModel.swift in Sources */,
CD89281B293796A100A812AB /* clutterCutter.swift in Sources */,
CD892816293791DC00A812AB /* IdiomViewModel.swift in Sources */,
CD23840728D1FF05003FBD74 /* RAMView.swift in Sources */,
CD8928182937962B00A812AB /* ShuffleViewModel.swift in Sources */,
CD52054D2892B55400ECA297 /* ActivityView.swift in Sources */,
CD23840B28D1FF2A003FBD74 /* IdiomView.swift in Sources */,
CDC9A8EC286D98E500185C61 /* Icon.swift in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions Getit/Pages/Activity/ActivityView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ struct ActivityView: View {
case .ram:
RAMView(session: Binding($vm.currentSession)!, sessionIndex: $vm.sessionIndex, handleNext: vm.handleNext)
case .idiom:
IdiomView(session: session, idiomChoices: vm.idiomChoices, displayIdiomChoices: vm.displayIdiomChoices, selectedChoiceIndex: vm.selectedChoiceIndex, isIdiomChoiceDone: vm.isIdiomChoiceDone, selectChoice: vm.selectChoice, validateIdiomChoice: vm.validateIdiomChoice, handleNext: vm.handleNext)
IdiomView(session: Binding($vm.currentSession)!, handleNext: vm.handleNext)
case .shuffle:
ShuffleView(session: session, blanks: vm.blanks, answers: vm.answers, candidates: vm.candidates, selectedCandidates: vm.selectedCandidates, shuffleStatus: vm.shuffleStatus, selectShuffleCandidate: vm.selectShuffleCandidate, unselectShuffleCandidate: vm.unselectShuffleCandidate, handleNext: vm.handleNext, validateShuffleChoice: vm.validateShuffleChoice)
ShuffleView(session: Binding($vm.currentSession)!, handleNext: vm.handleNext)
}
}
}
Expand Down
149 changes: 0 additions & 149 deletions Getit/Pages/Activity/ActivityViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,6 @@ struct Session: Equatable {
}
}

struct EnPart: Identifiable {
var text: String
var isSpeeched: Bool
var id: UUID
}

struct IdiomChoice {
var text: String
var isCorrect: Bool
}

struct ShuffleCandidate {
var text: String
var position: Int
}

struct ShuffleBlank {
var isBlank: Bool
var answerIndex: Int
var placeHolder: String
}

enum ShuffleStatus: String {
case selecting = "selecting"
case correct = "correct"
case incorrect = "incorrect"
}

let randomChoices: [String] = ["on", "in", "it", "down", "upon", "to", "up"]

class ActivityViewModel: ObservableObject {
Expand All @@ -71,19 +43,6 @@ class ActivityViewModel: ObservableObject {
@Published var currentSession: Session?
@Published var correctNum = 0

// Idiom
@Published var idiomChoices: [IdiomChoice] = []
@Published var displayIdiomChoices: [IdiomChoice] = []
@Published var selectedChoiceIndex: Int = -1
@Published var isIdiomChoiceDone: Bool = false

// Shuffle
@Published var blanks: [ShuffleBlank] = []
@Published var answers: [String] = []
@Published var candidates: [ShuffleCandidate] = []
@Published var selectedCandidates: [ShuffleCandidate] = []
@Published var shuffleStatus: ShuffleStatus = ShuffleStatus.selecting

init(eo: AppViewModel) {
self.eo = eo
}
Expand All @@ -106,69 +65,7 @@ class ActivityViewModel: ObservableObject {

func resetSession() {
self.sessionIndex = 0
self.idiomChoices = []
self.displayIdiomChoices = []
self.selectedChoiceIndex = -1
self.isIdiomChoiceDone = false
self.blanks = []
self.answers = []
self.candidates = []
self.selectedCandidates = []
self.shuffleStatus = ShuffleStatus.selecting

let session = sessions[sessionIndex]
switch(session.sessionType) {
case .ram:
break
case .idiom:
let correctPart = session.phrase.en.components(separatedBy: " ").first(where: { $0.contains("`") })
if let correctPart = correctPart {
let correctMatches = matches(for: "`.*`", in: correctPart)
let correctAnswer = correctMatches[0].replacingOccurrences(of: "`", with: "").lowercased()
self.idiomChoices.append(IdiomChoice(text: correctAnswer, isCorrect: true))
let otherChoices = randomChoices.filter { $0 != correctAnswer }.shuffled()
for i in 0..<4 {
self.idiomChoices.append(IdiomChoice(text: otherChoices[i], isCorrect: false))
}
self.displayIdiomChoices = self.idiomChoices.shuffled()
} else {
print("Idiom not found")
}
case .shuffle:
var answerIndex = 0
for component in session.phrase.en.components(separatedBy: " ") {
let textAndClutter = getTextAndClutter(component)
if (textAndClutter.count == 2) {
self.blanks.append(ShuffleBlank(isBlank: true, answerIndex: answerIndex, placeHolder: ""))
answerIndex += 1
self.blanks.append(ShuffleBlank(isBlank: false, answerIndex: 0, placeHolder: textAndClutter[1]))
self.answers.append(textAndClutter[0])
} else if (textAndClutter[0] == "-") {
self.blanks.append(ShuffleBlank(isBlank: false, answerIndex: 0, placeHolder: "-"))
} else {
self.blanks.append(ShuffleBlank(isBlank: true, answerIndex: answerIndex, placeHolder: ""))
answerIndex += 1
self.answers.append(textAndClutter[0])
}
}
self.candidates = self.answers.shuffled().enumerated().map { (index, answer) in
return ShuffleCandidate(text: answer, position: index)
}
return
}
}

let clutters = ["...", "!?", "!!", "?!", "!", "?", ".", ","]

func getTextAndClutter(_ text: String) -> [String] {
let textLen = text.count
for clutter in clutters {
let clutterLen = clutter.count
if (text.contains(clutter)) {
return [text.substring(to: textLen - clutterLen).lowercased(), text.substring(from: textLen - clutterLen).lowercased()]
}
}
return [text.lowercased()]
}

func fetchUnit() {
Expand Down Expand Up @@ -232,52 +129,6 @@ class ActivityViewModel: ObservableObject {
}
return rams.shuffled() + idioms.shuffled() + shuffles.shuffled()
}

// Idiom
func selectChoice(choice: IdiomChoice) {
if let index = self.idiomChoices.firstIndex(where: {$0.text == choice.text}) {
withAnimation(.easeOut(duration: 0.2)) {
self.selectedChoiceIndex = index
}
} else {
print("Error occurred when selecting idiom choice")
}
}

func validateIdiomChoice() {
withAnimation(.easeOut(duration: 0.2)) {
self.isIdiomChoiceDone = true
}
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(self.selectedChoiceIndex == 0 ? .success : .warning)
}

// Shuffle
func selectShuffleCandidate(candidate: ShuffleCandidate) {
self.selectedCandidates.append(candidate)
}

func unselectShuffleCandidate(candidate: ShuffleCandidate) {
let index = self.selectedCandidates.firstIndex(where: {$0.position == candidate.position})
if let index = index {
self.selectedCandidates.remove(at: index)
}
}

func validateShuffleChoice() {
var isCorrect = true
for (index, candidate) in self.selectedCandidates.enumerated() {
if(candidate.text != self.answers[index]) {
isCorrect = false
break
}
}
withAnimation(.easeOut(duration: 0.2)) {
self.shuffleStatus = (isCorrect ? ShuffleStatus.correct : ShuffleStatus.incorrect)
}
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(isCorrect ? .success : .warning)
}
}

func matches(for regex: String, in text: String) -> [String] {
Expand Down
69 changes: 28 additions & 41 deletions Getit/Pages/Activity/IdiomView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,12 @@ import SwiftUI
import WrappingHStack

struct IdiomView: View {
var session: Session
var baseEnSentence: String
var idiomChoices: [IdiomChoice]
var displayIdiomChoices: [IdiomChoice]
var selectedChoiceIndex: Int
var isIdiomChoiceDone: Bool
var selectChoice: (IdiomChoice) -> Void
let validateIdiomChoice: () -> Void
@Binding var session: Session
let handleNext: () -> Void
@StateObject var vm = IdiomViewModel()

init(session: Session, idiomChoices: [IdiomChoice], displayIdiomChoices: [IdiomChoice], selectedChoiceIndex: Int, isIdiomChoiceDone: Bool, selectChoice: @escaping (IdiomChoice) -> Void, validateIdiomChoice: @escaping () -> Void, handleNext: @escaping () -> Void) {
self.session = session
var enComponents = session.phrase.en.components(separatedBy: " ")
if let blankIndex = enComponents.firstIndex(where: { $0.contains("`")}) {
let correctMatches = matches(for: "`.*`", in: enComponents[blankIndex])
enComponents[blankIndex] = enComponents[blankIndex].replacingOccurrences(of: correctMatches[0], with: "#")
} else {
print("Error occurred while generating en sentence")
}
self.baseEnSentence = enComponents.joined(separator: " ")
self.idiomChoices = idiomChoices
self.displayIdiomChoices = displayIdiomChoices
self.selectedChoiceIndex = selectedChoiceIndex
self.isIdiomChoiceDone = isIdiomChoiceDone
self.selectChoice = selectChoice
self.validateIdiomChoice = validateIdiomChoice
init(session: Binding<Session>, handleNext: @escaping () -> Void) {
self._session = session
self.handleNext = handleNext
}

Expand All @@ -43,28 +23,29 @@ struct IdiomView: View {
HStack(){
Icon(IconName.squarePlus, 15)
.foregroundColor(Color.text)

Text("空欄を埋めよう!")
.smallJaBold()
.foregroundColor(Color.text)

Spacer()
}

Text(session.phrase.ja)
.lgJa()
.foregroundColor(Color.white)

Text(baseEnSentence.replacingOccurrences(of: "#", with: selectedChoiceIndex >= 0 ? self.idiomChoices[self.selectedChoiceIndex].text : "_"))
.getit()
.foregroundColor(Color.white)
.animation(nil)
if let sentence = self.vm.baseEnSentence {
Text(sentence.replacingOccurrences(of: "#", with: self.vm.selectedChoiceIndex >= 0 ? self.vm.idiomChoices[self.vm.selectedChoiceIndex].text : "_"))
.getit()
.foregroundColor(Color.white)
.animation(nil)
}


Spacer()

HStack() {
if(self.isIdiomChoiceDone) {
if(self.selectedChoiceIndex == 0) {
if(self.vm.isIdiomChoiceDone) {
if(self.vm.selectedChoiceIndex == 0) {
HStack(){

Spacer()
Expand All @@ -89,7 +70,7 @@ struct IdiomView: View {
.exLgBold()
.foregroundColor(.orange)
}
Text("正解例: \(self.idiomChoices[0].text)")
Text("正解例: \(self.vm.idiomChoices[0].text)")
.mainBold()
.foregroundColor(.orange)
}
Expand All @@ -100,9 +81,9 @@ struct IdiomView: View {
}
}

WrappingHStack(displayIdiomChoices, alignment: .center) { choice in
WrappingHStack(self.vm.displayIdiomChoices, alignment: .center) { choice in
Button(action: {
self.selectChoice(choice)
self.vm.selectChoice(choice)
}) {
Text(choice.text)
.mainBold()
Expand All @@ -116,18 +97,18 @@ struct IdiomView: View {
.padding(.bottom, 12)
}
.buttonStyle(GrowingButton())
.disabled(self.isIdiomChoiceDone)
.disabled(self.vm.isIdiomChoiceDone)
}

Button(action:{
if(self.isIdiomChoiceDone) {
if(self.vm.isIdiomChoiceDone) {
self.handleNext()
} else {
self.validateIdiomChoice()
self.vm.validateIdiomChoice()
}
}){
HStack(spacing:0){
Text(self.isIdiomChoiceDone ? "次へ進む" : "送信")
Text(self.vm.isIdiomChoiceDone ? "次へ進む" : "送信")
.smallJaBold()
.foregroundColor(.text)
.padding(.bottom, 1)
Expand All @@ -136,13 +117,19 @@ struct IdiomView: View {
.frame(maxWidth:.infinity)
.frame(height: 56)
.background(LinearGradient(gradient: Color.learnGrad, startPoint: .leading, endPoint: .trailing))
.opacity(self.selectedChoiceIndex == -1 ? 0.3 : 1)
.opacity(self.vm.selectedChoiceIndex == -1 ? 0.3 : 1)
.cornerRadius(12)
}
.disabled(self.selectedChoiceIndex == -1)
.disabled(self.vm.selectedChoiceIndex == -1)
.buttonStyle(GrowingButton())
}
.padding(.horizontal, 20)
.onChange(of: session) { newSession in
self.vm.reset(session)
}
.onAppear() {
self.vm.reset(session)
}
}
}

Expand Down
Loading

0 comments on commit d3ee6ef

Please sign in to comment.