Skip to content

Commit bc02512

Browse files
committed
feat: 캐시 디렉토리 제거 로직 구현
1 parent a4ad50c commit bc02512

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

DevLog/Presentation/ViewModel/SettingViewModel.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ final class SettingViewModel: Store {
2828
case tapDeleteAuthButton
2929
case tapSignOutButton
3030
case tapRemoveCacheButton
31+
case confirmRemoveCache
3132
}
3233

3334
enum SideEffect {
@@ -70,6 +71,14 @@ final class SettingViewModel: Store {
7071
return [.signOut]
7172
case .tapRemoveCacheButton:
7273
setAlert(&state, isPresented: true, type: .removeCache)
74+
case .confirmRemoveCache:
75+
do {
76+
try clearCacheDirectory()
77+
state.dirSize = dirSizeInBytes()
78+
setAlert(&state, isPresented: false)
79+
} catch {
80+
setAlert(&state, isPresented: true, type: .error)
81+
}
7382
}
7483
return []
7584
}
@@ -107,7 +116,7 @@ private extension SettingViewModel {
107116
func setAlert(
108117
_ state: inout State,
109118
isPresented: Bool,
110-
type: AlertType?
119+
type: AlertType? = nil
111120
) {
112121
switch type {
113122
case .signOut:
@@ -165,4 +174,21 @@ private extension SettingViewModel {
165174
}
166175
return total
167176
}
177+
178+
private func clearCacheDirectory() throws {
179+
let cachesDir = try FileManager.default.url(
180+
for: .cachesDirectory,
181+
in: .userDomainMask,
182+
appropriateFor: nil,
183+
create: true
184+
)
185+
let contents = try FileManager.default.contentsOfDirectory(
186+
at: cachesDir,
187+
includingPropertiesForKeys: nil,
188+
options: [.skipsHiddenFiles]
189+
)
190+
for url in contents {
191+
try FileManager.default.removeItem(at: url)
192+
}
193+
}
168194
}

DevLog/UI/Setting/SettingView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ struct SettingView: View {
178178
viewModel.send(.setAlert(isPresented: false))
179179
}
180180
Button("확인", role: .destructive) {
181-
181+
viewModel.send(.confirmRemoveCache)
182182
}
183183
case .error, .none:
184184
Button("확인", role: .cancel) {

0 commit comments

Comments
 (0)