Skip to content

Commit

Permalink
refactored ram
Browse files Browse the repository at this point in the history
  • Loading branch information
kaorulego5x committed Nov 30, 2022
1 parent 2309140 commit 204d110
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 102 deletions.
4 changes: 4 additions & 0 deletions Getit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
CD8732A129000759001AA69C /* Lottie in Frameworks */ = {isa = PBXBuildFile; productRef = CD8732A029000759001AA69C /* Lottie */; };
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 */; };
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 @@ -86,6 +87,7 @@
CD52054C2892B55400ECA297 /* ActivityView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActivityView.swift; sourceTree = "<group>"; };
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>"; };
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 @@ -327,6 +329,7 @@
CD52054C2892B55400ECA297 /* ActivityView.swift */,
CD9CC35728C10D8700224FC7 /* ActivityViewModel.swift */,
CD23840628D1FF05003FBD74 /* RAMView.swift */,
CD8928132937887C00A812AB /* RAMViewModel.swift */,
CD23840A28D1FF2A003FBD74 /* IdiomView.swift */,
CD23840828D1FF22003FBD74 /* ShuffleView.swift */,
CD9CC36428C4A7B300224FC7 /* ResultView.swift */,
Expand Down Expand Up @@ -561,6 +564,7 @@
CD23840B28D1FF2A003FBD74 /* IdiomView.swift in Sources */,
CDC9A8EC286D98E500185C61 /* Icon.swift in Sources */,
CDC9A8F2286DA32100185C61 /* ProgressCard.swift in Sources */,
CD8928142937887C00A812AB /* RAMViewModel.swift in Sources */,
CD9CC36028C2105000224FC7 /* MapRowViewModel.swift in Sources */,
CDC9A8CA286D874F00185C61 /* SplashView.swift in Sources */,
CD9CC35428C0FAB900224FC7 /* UserRepository.swift in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions Getit/AppViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class AppViewModel: ObservableObject {
}

func handleAuth(_ masterData: MasterData) {
if let currentUser = FirebaseManager.shared.auth.currentUser { //アプリを開いたことがある場合
if let currentUser = FirebaseManager.shared.auth.currentUser { //アプリを使ったことがある場合
self.fetchUserData(currentUser.uid, masterData)
} else { //初めてアプリを開いた場合
} else { //初めてアプリを使う場合
FirebaseManager.shared.auth.signInAnonymously() { [weak self] authResult, error in
guard let user = authResult?.user else {
print("Registration failed")
Expand Down
21 changes: 13 additions & 8 deletions Getit/Pages/Activity/ActivityView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ struct ActivityView: View {
.padding(.top, 24)
.padding(.bottom, 30)

if let session = vm.currentSession {
switch(session.sessionType) {
case .ram:
RAMView(session: session, enParts: vm.enParts, sessionIndex: vm.sessionIndex, handleNext: vm.handleNext, handleSpeechInput: vm.handleSpeechInput, isCompleted: $vm.isCompleted)
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)
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)
if let session = vm.sessions[vm.sessionIndex] {
if let _ = vm.currentSession {
switch(session.sessionType) {
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)
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)
}
}
}

Expand All @@ -64,6 +66,9 @@ struct ActivityView: View {
ResultView(eo: self.eo, phraseNum: vm.sessions.count ?? 0, correctNum: vm.correctNum)
}
}
.onChange(of: vm.sessionIndex) { newIndex in
vm.currentSession = vm.sessions[newIndex]
}
.background(Color.bg.ignoresSafeArea())
}
}
Expand Down
79 changes: 9 additions & 70 deletions Getit/Pages/Activity/ActivityViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ enum SessionType: String {
case shuffle = "shuffle"
}

struct Session {
struct Session: Equatable {
var phrase: Phrase
var sessionType: SessionType
static func ==(lhs: Session, rhs: Session) -> Bool {
return lhs.phrase.en == rhs.phrase.en && lhs.phrase.ja == rhs.phrase.ja
}
}

struct EnPart: Identifiable {
Expand Down Expand Up @@ -63,15 +66,11 @@ class ActivityViewModel: ObservableObject {
private var cancellables: [AnyCancellable] = []

@Published var route: ActivityRoute = .loading
@Published var sessions: [Session] = []
@Published var sessionIndex = -1
@Published var currentSession: Session?
var sessions: [Session] = []
@Published var sessionIndex = 0
@Published var correctNum = 0

// RAM
@Published var enParts: [EnPart] = []
@Published var isCompleted: Bool = false

// Idiom
@Published var idiomChoices: [IdiomChoice] = []
@Published var displayIdiomChoices: [IdiomChoice] = []
Expand All @@ -95,7 +94,7 @@ class ActivityViewModel: ObservableObject {
} else {
withAnimation(.easeOut(duration:0.1)) {
self.sessionIndex += 1
self.resetSession()
// self.resetSession()
}
}
}
Expand All @@ -106,9 +105,7 @@ class ActivityViewModel: ObservableObject {
}

func resetSession() {
self.currentSession = sessions[self.sessionIndex]
self.enParts = []
self.isCompleted = false
self.sessionIndex = 0
self.idiomChoices = []
self.displayIdiomChoices = []
self.selectedChoiceIndex = -1
Expand All @@ -122,10 +119,7 @@ class ActivityViewModel: ObservableObject {
let session = sessions[sessionIndex]
switch(session.sessionType) {
case .ram:
self.enParts = session.phrase.en.components(separatedBy: " ").map { component in
return EnPart(text: component.replacingOccurrences(of: "`", with: ""), isSpeeched: false, id: UUID())
}
self.assistSpeech(self.sessionIndex);
break
case .idiom:
let correctPart = session.phrase.en.components(separatedBy: " ").first(where: { $0.contains("`") })
if let correctPart = correctPart {
Expand Down Expand Up @@ -177,15 +171,6 @@ class ActivityViewModel: ObservableObject {
return [text.lowercased()]
}

func assistSpeech(_ idx: Int) {
DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) {
print(idx, self.sessionIndex)
if(idx == self.sessionIndex) {
self.handleSpeechCompleted()
}
}
}

func fetchUnit() {
if let unit = eo.selectedUnit {
phraseRepository
Expand Down Expand Up @@ -248,52 +233,6 @@ class ActivityViewModel: ObservableObject {
return rams.shuffled() + idioms.shuffled() + shuffles.shuffled()
}

// RAM
func handleSpeechInput(_ transcript: String) {
let inputParts = transcript.lowercased().components(separatedBy: " ")
var carry = 0
for (index, part) in enParts.enumerated() {
var a: String = part.text.lowercased()
if(a.contains("!?") || a.contains("?!") || a.contains("!!")) {
a.removeLast()
a.removeLast()
} else if(a.contains(",") || a.contains(".") || a.contains("") || a.contains("!") || a.contains("?")) {
a.removeLast()
}

if(part.isSpeeched) {
continue
} else if(inputParts.contains(a)) {
withAnimation(.easeOut(duration: 0.2)) {
self.enParts[index].isSpeeched = true
}
if(index == enParts.count - 1) {
self.handleSpeechCompleted()
}
if(carry == 1) {
withAnimation(.easeOut(duration: 0.2)) {
self.enParts[index-1].isSpeeched = true
}
}
} else if (carry == 0){
carry = 1
} else {
break
}
}
}

private func handleSpeechCompleted() {
for (index, _) in enParts.enumerated() {
self.enParts[index].isSpeeched = true;
}
withAnimation(.easeOut(duration: 0.2)) {
self.isCompleted = true
}
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.success)
}

// Idiom
func selectChoice(choice: IdiomChoice) {
if let index = self.idiomChoices.firstIndex(where: {$0.text == choice.text}) {
Expand Down
33 changes: 15 additions & 18 deletions Getit/Pages/Activity/RAMView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,17 @@ import SwiftUI
import WrappingHStack

struct RAMView: View {
var session: Session
var enParts: [EnPart]
var sessionIndex: Int
@Binding var session: Session
@Binding var sessionIndex: Int
var handleNext: () -> Void;
var handleSpeechInput: (String) -> Void;
@Binding var isCompleted: Bool
@StateObject var speechRecognizer = SpeechRecognizer()
@StateObject var speaker = Speaker()
@StateObject var vm = RAMViewModel();

init(session: Session, enParts: [EnPart], sessionIndex: Int, handleNext: @escaping () -> Void, handleSpeechInput: @escaping (String) -> Void, isCompleted: Binding<Bool>) {
self.session = session
self.enParts = enParts
self.sessionIndex = sessionIndex
init(session: Binding<Session>, sessionIndex: Binding<Int>, handleNext: @escaping () -> Void) {
self._session = session
self._sessionIndex = sessionIndex
self.handleNext = handleNext
self.handleSpeechInput = handleSpeechInput
self._isCompleted = isCompleted
}

var body: some View {
Expand All @@ -44,7 +39,7 @@ struct RAMView: View {
.lgJa()
.foregroundColor(Color.white)

WrappingHStack(self.enParts) { part in
WrappingHStack(self.vm.enParts) { part in
Text(part.text)
.getit()
.foregroundColor(part.isSpeeched ? Color.white : Color.white.opacity(0.5))
Expand All @@ -56,7 +51,7 @@ struct RAMView: View {
self.handleNext()
}){
HStack(spacing:0){
if(!self.isCompleted) {
if(!self.vm.isCompleted) {
LottieView(name: "listening", loopMode: .loop)
.frame(width: 120, height: 120)
.frame(maxWidth: .infinity)
Expand All @@ -70,25 +65,27 @@ struct RAMView: View {
.frame(maxWidth:.infinity)
.frame(height: 56)
.background(LinearGradient(gradient: Color.learnGrad, startPoint: .leading, endPoint: .trailing))
.opacity(self.isCompleted ? 1 : 0.3)
.opacity(self.vm.isCompleted ? 1 : 0.3)
.cornerRadius(12)
}
.buttonStyle(GrowingButton())
.disabled(!self.isCompleted)
.disabled(!self.vm.isCompleted)
}
.padding(.horizontal, 20)
.onChange(of: speechRecognizer.transcript) { value in
self.handleSpeechInput(speechRecognizer.transcript)
self.vm.handleSpeechInput(speechRecognizer.transcript)
}
.onChange(of: self.isCompleted) { value in
.onChange(of: self.vm.isCompleted) { value in
if(value) {
speechRecognizer.stopTranscribing()
}
}
.onChange(of: self.sessionIndex) { value in
.onChange(of: session) { value in
self.vm.reset(self.session)
speechRecognizer.transcribe()
}
.onAppear() {
self.vm.reset(self.session)
speechRecognizer.transcribe()
}
}
Expand Down
76 changes: 76 additions & 0 deletions Getit/Pages/Activity/RAMViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// RAMViewModel.swift
// Getit
//
// Created by kaorulego5x on 30/11/22.
//

import Foundation
import SwiftUI

class RAMViewModel: ObservableObject {
// let assistSpeech: (Int) -> Void;

// RAM
@Published var enParts: [EnPart] = []
@Published var isCompleted: Bool = false

/*
init(assistSpeech: @escaping (Int) -> Void) {
self.assistSpeech = assistSpeech
}
*/

func reset(_ session: Session) {
self.enParts = session.phrase.en.components(separatedBy: " ").map { component in
return EnPart(text: component.replacingOccurrences(of: "`", with: ""), isSpeeched: false, id: UUID())
}
self.isCompleted = false
// self.assistSpeech(0);
}

func handleSpeechInput(_ transcript: String) {
let inputParts = transcript.lowercased().components(separatedBy: " ")
var carry = 0
for (index, part) in enParts.enumerated() {
var a: String = part.text.lowercased()
if(a.contains("!?") || a.contains("?!") || a.contains("!!")) {
a.removeLast()
a.removeLast()
} else if(a.contains(",") || a.contains(".") || a.contains("") || a.contains("!") || a.contains("?")) {
a.removeLast()
}

if(part.isSpeeched) {
continue
} else if(inputParts.contains(a)) {
withAnimation(.easeOut(duration: 0.2)) {
self.enParts[index].isSpeeched = true
}
if(index == enParts.count - 1) {
self.handleSpeechCompleted()
}
if(carry == 1) {
withAnimation(.easeOut(duration: 0.2)) {
self.enParts[index-1].isSpeeched = true
}
}
} else if (carry == 0){
carry = 1
} else {
break
}
}
}

private func handleSpeechCompleted() {
for (index, _) in enParts.enumerated() {
self.enParts[index].isSpeeched = true;
}
withAnimation(.easeOut(duration: 0.2)) {
self.isCompleted = true
}
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.success)
}
}
4 changes: 0 additions & 4 deletions Getit/Pages/MapView/MapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

import SwiftUI

var wordList = ["Get"]
var activityList = [1, 2, 3, 4]

struct MapView: View {
@EnvironmentObject var eo: AppViewModel
@StateObject var vm: MapViewModel
Expand Down Expand Up @@ -40,7 +37,6 @@ struct MapView: View {
VStack(){
ForEach(vm.masterData.words, id: \.self) { word in
let progress = vm.user.progress.first(where: {$0.word == word.word})

let isFocused = self.focusedWord == word.word
if let progress = progress {
MapRowView(eo: self.eo, progress: progress, word: word, isFocused: isFocused, onToggle: handleToggle)
Expand Down

0 comments on commit 204d110

Please sign in to comment.