Skip to content

Commit 02b7658

Browse files
committed
Implement user-defined save location for memorized words; update entitlements for read-write access
1 parent d9e5da3 commit 02b7658

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

VocabularyApp/ContentView.swift

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import SwiftUI
99
import AppKit
10+
import UniformTypeIdentifiers
1011

1112
struct ContentView: View {
1213
@State private var word: String = "Loading..."
@@ -158,13 +159,25 @@ struct ContentView: View {
158159

159160
func markAsMemorized() {
160161
guard !word.isEmpty, !memorizedWords.contains(word) else { return }
161-
memorizedWords.append(word)
162162

163-
164-
saveMemorizedWords()
165-
loadRandomWord()
163+
if UserDefaults.standard.string(forKey: "memorizedWordsPath") == nil {
164+
promptUserForSaveLocation { selectedURL in
165+
if let selectedURL = selectedURL {
166+
UserDefaults.standard.set(selectedURL.path, forKey: "memorizedWordsPath")
167+
saveMemorizedWords()
168+
loadRandomWord()
169+
} else {
170+
print("User canceled the save location selection.")
171+
}
172+
}
173+
} else {
174+
memorizedWords.append(word)
175+
saveMemorizedWords()
176+
loadRandomWord()
177+
}
166178
}
167179

180+
168181
func saveMemorizedWords() {
169182
let fileURL = getFileURL()
170183
do {
@@ -188,8 +201,28 @@ struct ContentView: View {
188201
}
189202

190203
func getFileURL() -> URL {
191-
let fileManager = FileManager.default
192-
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
193-
return documentsURL.appendingPathComponent("memorized_words.json")
204+
if let savedPath = UserDefaults.standard.string(forKey: "memorizedWordsPath"),
205+
!savedPath.isEmpty {
206+
return URL(fileURLWithPath: savedPath)
207+
} else {
208+
let fileManager = FileManager.default
209+
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
210+
return documentsURL.appendingPathComponent("memorized_words.json")
211+
}
212+
}
213+
214+
func promptUserForSaveLocation(completion: @escaping (URL?) -> Void) {
215+
let savePanel = NSSavePanel()
216+
savePanel.title = "Select Location for Memorized Words"
217+
savePanel.allowedContentTypes = [.json] // Use UTType.json
218+
savePanel.nameFieldStringValue = "memorized_words.json"
219+
220+
savePanel.begin { response in
221+
if response == .OK, let selectedURL = savePanel.url {
222+
completion(selectedURL)
223+
} else {
224+
completion(nil)
225+
}
226+
}
194227
}
195228
}

VocabularyApp/VocabularyApp.entitlements

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<dict>
55
<key>com.apple.security.app-sandbox</key>
66
<true/>
7-
<key>com.apple.security.files.user-selected.read-only</key>
7+
<key>com.apple.security.files.user-selected.read-write</key>
88
<true/>
99
<key>com.apple.security.network.client</key>
1010
<true/>

0 commit comments

Comments
 (0)