Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,5 @@ JLPTVoca/SwiftData.md
JLPTVoca/WordStability.md

.github/.DS_Store

JLPTVoca/.DS_Store
Binary file modified JLPTVoca/.DS_Store
Binary file not shown.
120 changes: 120 additions & 0 deletions JLPTVoca/JLPTVoca.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@
objectVersion = 77;
objects = {

/* Begin PBXContainerItemProxy section */
216132B72E71D3C300DE17B7 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 214FEB582E5E320300C87957 /* Project object */;
proxyType = 1;
remoteGlobalIDString = 214FEB5F2E5E320300C87957;
remoteInfo = JLPTVoca;
};
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
214FEB602E5E320300C87957 /* JLPTVoca.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = JLPTVoca.app; sourceTree = BUILT_PRODUCTS_DIR; };
216132B32E71D3C300DE17B7 /* JLPTVocaUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = JLPTVocaUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

/* Begin PBXFileSystemSynchronizedRootGroup section */
Expand All @@ -16,6 +27,11 @@
path = JLPTVoca;
sourceTree = "<group>";
};
216132B42E71D3C300DE17B7 /* JLPTVocaUITests */ = {
isa = PBXFileSystemSynchronizedRootGroup;
path = JLPTVocaUITests;
sourceTree = "<group>";
};
/* End PBXFileSystemSynchronizedRootGroup section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -26,13 +42,21 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
216132B02E71D3C300DE17B7 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
214FEB572E5E320300C87957 = {
isa = PBXGroup;
children = (
214FEB622E5E320300C87957 /* JLPTVoca */,
216132B42E71D3C300DE17B7 /* JLPTVocaUITests */,
214FEB612E5E320300C87957 /* Products */,
);
sourceTree = "<group>";
Expand All @@ -41,6 +65,7 @@
isa = PBXGroup;
children = (
214FEB602E5E320300C87957 /* JLPTVoca.app */,
216132B32E71D3C300DE17B7 /* JLPTVocaUITests.xctest */,
);
name = Products;
sourceTree = "<group>";
Expand Down Expand Up @@ -70,6 +95,29 @@
productReference = 214FEB602E5E320300C87957 /* JLPTVoca.app */;
productType = "com.apple.product-type.application";
};
216132B22E71D3C300DE17B7 /* JLPTVocaUITests */ = {
isa = PBXNativeTarget;
buildConfigurationList = 216132B92E71D3C300DE17B7 /* Build configuration list for PBXNativeTarget "JLPTVocaUITests" */;
buildPhases = (
216132AF2E71D3C300DE17B7 /* Sources */,
216132B02E71D3C300DE17B7 /* Frameworks */,
216132B12E71D3C300DE17B7 /* Resources */,
);
buildRules = (
);
dependencies = (
216132B82E71D3C300DE17B7 /* PBXTargetDependency */,
);
fileSystemSynchronizedGroups = (
216132B42E71D3C300DE17B7 /* JLPTVocaUITests */,
);
name = JLPTVocaUITests;
packageProductDependencies = (
);
productName = JLPTVocaUITests;
productReference = 216132B32E71D3C300DE17B7 /* JLPTVocaUITests.xctest */;
productType = "com.apple.product-type.bundle.unit-test";
};
/* End PBXNativeTarget section */

/* Begin PBXProject section */
Expand All @@ -83,6 +131,10 @@
214FEB5F2E5E320300C87957 = {
CreatedOnToolsVersion = 16.4;
};
216132B22E71D3C300DE17B7 = {
CreatedOnToolsVersion = 16.4;
TestTargetID = 214FEB5F2E5E320300C87957;
};
};
};
buildConfigurationList = 214FEB5B2E5E320300C87957 /* Build configuration list for PBXProject "JLPTVoca" */;
Expand All @@ -100,6 +152,7 @@
projectRoot = "";
targets = (
214FEB5F2E5E320300C87957 /* JLPTVoca */,
216132B22E71D3C300DE17B7 /* JLPTVocaUITests */,
);
};
/* End PBXProject section */
Expand All @@ -112,6 +165,13 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
216132B12E71D3C300DE17B7 /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
Expand All @@ -122,8 +182,23 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
216132AF2E71D3C300DE17B7 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */

/* Begin PBXTargetDependency section */
216132B82E71D3C300DE17B7 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = 214FEB5F2E5E320300C87957 /* JLPTVoca */;
targetProxy = 216132B72E71D3C300DE17B7 /* PBXContainerItemProxy */;
};
/* End PBXTargetDependency section */

/* Begin XCBuildConfiguration section */
214FEB692E5E320400C87957 /* Debug */ = {
isa = XCBuildConfiguration;
Expand Down Expand Up @@ -302,6 +377,42 @@
};
name = Release;
};
216132BA2E71D3C300DE17B7 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 3HYFT54PK2;
GENERATE_INFOPLIST_FILE = YES;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.JLPTVocaUITests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/JLPTVoca.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/JLPTVoca";
};
name = Debug;
};
216132BB2E71D3C300DE17B7 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 3HYFT54PK2;
GENERATE_INFOPLIST_FILE = YES;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.JLPTVocaUITests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/JLPTVoca.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/JLPTVoca";
};
name = Release;
};
/* End XCBuildConfiguration section */

/* Begin XCConfigurationList section */
Expand All @@ -323,6 +434,15 @@
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
216132B92E71D3C300DE17B7 /* Build configuration list for PBXNativeTarget "JLPTVocaUITests" */ = {
isa = XCConfigurationList;
buildConfigurations = (
216132BA2E71D3C300DE17B7 /* Debug */,
216132BB2E71D3C300DE17B7 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
rootObject = 214FEB582E5E320300C87957 /* Project object */;
Expand Down
11 changes: 2 additions & 9 deletions JLPTVoca/JLPTVoca/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,8 @@ struct ContentView: View {
}
}
.environment(wordManager)
.task(id: words) {
wordManager.setup(
context: context,
words: words
)
.onAppear() {
wordManager.setContext(context: context)
}
}
}

#Preview {
ContentView()
}
14 changes: 14 additions & 0 deletions JLPTVoca/JLPTVoca/Extensions/Logger+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Logger+Extension.swift
// JLPTVoca
//
// Created by Rama on 9/9/25.
//

import Foundation
import OSLog

extension Logger {
private static var subsystem = Bundle.main.bundleIdentifier!
static let word = Logger(subsystem: subsystem, category: "Word")
}
96 changes: 79 additions & 17 deletions JLPTVoca/JLPTVoca/Managers/WordManager.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

//
// WordManager.swift
// JLPTVoca
Expand All @@ -8,48 +7,111 @@

import SwiftUI
import SwiftData
import OSLog

@Observable
final class WordManager {
var allWords: [Word] = []
var wordDeck: [Word] = []
var studyStateDeck: [StudyState] = []

private var context: ModelContext?

func setup(
context: ModelContext,
words: [Word]
) {
func setContext(context: ModelContext) {
self.context = context
self.allWords = words
}

func prepareSession() {
self.wordDeck = allWords
guard context != nil else { return }

let reviewableStates = fetchReviewableStates()
let newWords = fetchNewWords(limit: 20) //TODO: RawVal 수정, 사용자 학습 목표량으로 수정
let newWordStates = createStates(for: newWords)

self.studyStateDeck = (reviewableStates + newWordStates).shuffled()
}

private func fetchData<T: PersistentModel>(with descriptor: FetchDescriptor<T>) -> [T] {
guard let context else { return [] }

do {
return try context.fetch(descriptor)
} catch {
print("@Log - \(error.localizedDescription)")
return []
}
}

private func fetchReviewableStates() -> [StudyState] {
let now = Date()
let predicate = #Predicate<StudyState> { $0.reviewDate <= now }
let descriptor = FetchDescriptor<StudyState>(predicate: predicate)

return fetchData(with: descriptor)
}

func fetchNewWords(limit: Int) -> [Word] {
let predicate = #Predicate<Word> { $0.state == nil }
var descriptor = FetchDescriptor<Word>(predicate: predicate)
descriptor.fetchLimit = limit

return fetchData(with: descriptor)
}

private func createStates(for words: [Word]) -> [StudyState] {
guard let context else { return [] }

return words.map { newWord in
let newState = StudyState(word: newWord)
context.insert(newState)
return newState
}
}

func onCardSwipe(
id: UUID,
direction: CardSwipeDirection
) {
guard let swipedWord = allWords.first(where: { $0.id == id }) else {
guard let swipedState = studyStateDeck.first(where: { $0.word.id == id }) else {
return
}

if direction == .left {
swipedWord.maturityState += 1
swipedState.maturityState = min(swipedState.maturityState + 1, 5)
} else {
swipedWord.maturityState = 2
swipedState.maturityState = 1
}

wordDeck.removeAll { $0.id == id }

if wordDeck.isEmpty {
updateNextReviewDate(for: swipedState)
studyStateDeck.removeAll { $0.word.id == id }
if studyStateDeck.isEmpty {
do {
try context?.save()
} catch {
print("@Log - \(error.localizedDescription)")
}
}
}

func updateNextReviewDate(for state: StudyState) {
let now = Date()
let secondsInDay: TimeInterval = 86400
var interval: TimeInterval = 0

switch state.maturityState {
case 1:
interval = secondsInDay * 1
case 2:
interval = secondsInDay * 3
case 3:
interval = secondsInDay * 7
case 4:
interval = secondsInDay * 14
case 5:
interval = secondsInDay * 30
default:
state.reviewDate = now
return
}

state.reviewDate = now.addingTimeInterval(interval)
}
}
Loading