Skip to content

Commit b038dd6

Browse files
committed
feat: FileManager의 캐시 디렉토리 크기를 보여주고 탭 하면 얼럿이 뜨도록 추가
1 parent d275314 commit b038dd6

3 files changed

Lines changed: 96 additions & 7 deletions

File tree

DevLog/Presentation/ViewModel/SettingViewModel.swift

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Foundation
1010
final class SettingViewModel: Store {
1111
struct State {
1212
var theme = ""
13+
var dirSize: Int64 = 0
1314
var isLoading = false
1415
var showAlert: Bool = false
1516
var alertTitle: String = ""
@@ -23,8 +24,10 @@ final class SettingViewModel: Store {
2324
case setAlert(isPresented: Bool, type: AlertType? = nil)
2425
case setLoading(Bool)
2526
case setTheme(String)
27+
case updateDirSize
2628
case tapDeleteAuthButton
2729
case tapSignOutButton
30+
case tapRemoveCacheButton
2831
}
2932

3033
enum SideEffect {
@@ -33,7 +36,7 @@ final class SettingViewModel: Store {
3336
}
3437

3538
enum AlertType {
36-
case signOut, cancel, error
39+
case signOut, deleteAuth, error, removeCache
3740
}
3841

3942
@Published private(set) var state = State()
@@ -59,10 +62,14 @@ final class SettingViewModel: Store {
5962
state.isLoading = value
6063
case .setTheme(let value):
6164
state.theme = value
65+
case .updateDirSize:
66+
state.dirSize = dirSizeInBytes()
6267
case .tapDeleteAuthButton:
6368
return [.deleteAuth]
6469
case .tapSignOutButton:
6570
return [.signOut]
71+
case .tapRemoveCacheButton:
72+
setAlert(&state, isPresented: true, type: .removeCache)
6673
}
6774
return []
6875
}
@@ -106,17 +113,56 @@ private extension SettingViewModel {
106113
case .signOut:
107114
state.alertTitle = "로그아웃"
108115
state.alertMessage = "로그아웃 하시겠습니까?"
109-
case .cancel:
116+
case .deleteAuth:
110117
state.alertTitle = "정말 탈퇴하시겠습니까?"
111118
state.alertMessage = "회원 탈퇴가 진행되면 모든 데이터가 지워지고 복구할 수 없습니다."
112119
case .error:
113120
state.alertTitle = "오류"
114121
state.alertMessage = "문제가 발생했습니다. 잠시 후 다시 시도해주세요."
122+
case .removeCache:
123+
state.alertTitle = "임시 데이터 삭제"
124+
state.alertMessage = "임시 데이터를 삭제하고 정리합니다.\n계속하시겠습니까?"
115125
case .none:
116126
state.alertTitle = ""
117127
state.alertMessage = ""
118128
}
119129
state.showAlert = isPresented
120130
state.alertType = type
121131
}
132+
133+
func dirSizeInBytes() -> Int64 {
134+
do {
135+
let cachesDir = try FileManager.default.url(
136+
for: .cachesDirectory,
137+
in: .userDomainMask,
138+
appropriateFor: nil,
139+
create: true
140+
)
141+
return directorySize(at: cachesDir)
142+
} catch {
143+
return 0
144+
}
145+
}
146+
147+
private func directorySize(at url: URL) -> Int64 {
148+
guard FileManager.default.fileExists(atPath: url.path) else { return 0 }
149+
guard let enumerator = FileManager.default.enumerator(
150+
at: url,
151+
includingPropertiesForKeys: [.isRegularFileKey, .fileSizeKey],
152+
options: [.skipsHiddenFiles]
153+
) else {
154+
return 0
155+
}
156+
157+
var total: Int64 = 0
158+
for case let fileURL as URL in enumerator {
159+
guard let resourceValues = try? fileURL.resourceValues(forKeys: [.isRegularFileKey, .fileSizeKey]),
160+
resourceValues.isRegularFile == true,
161+
let fileSize = resourceValues.fileSize else {
162+
continue
163+
}
164+
total += Int64(fileSize)
165+
}
166+
return total
167+
}
122168
}

DevLog/Resource/Localizable.xcstrings

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,9 @@
358358
},
359359
"읽지 않음" : {
360360

361+
},
362+
"임시 데이터 삭제" : {
363+
361364
},
362365
"작년" : {
363366

DevLog/UI/Setting/SettingView.swift

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,25 @@ struct SettingView: View {
2727
.foregroundStyle(Color.gray)
2828
}
2929
}
30-
.onAppear {
31-
viewModel.send(.setTheme(theme.localizedName))
32-
}
3330

3431
Button {
3532
router.push(Path.pushNotification)
3633
} label: {
3734
Text("알림")
3835
.foregroundStyle(Color.primary)
3936
}
37+
38+
Button {
39+
viewModel.send(.tapRemoveCacheButton)
40+
} label: {
41+
HStack {
42+
Text("임시 데이터 삭제")
43+
.foregroundStyle(Color.primary)
44+
Spacer()
45+
Text(formatFileSize(bytes: viewModel.state.dirSize))
46+
.foregroundStyle(Color.secondary)
47+
}
48+
}
4049
}
4150

4251
Section {
@@ -91,7 +100,7 @@ struct SettingView: View {
91100
HStack {
92101
Spacer()
93102
Button(role: .destructive, action: {
94-
viewModel.send(.setAlert(isPresented: true, type: .cancel))
103+
viewModel.send(.setAlert(isPresented: true, type: .deleteAuth))
95104
}) {
96105
Text("회원 탈퇴")
97106
.font(.headline)
@@ -137,6 +146,10 @@ struct SettingView: View {
137146
LoadingView()
138147
}
139148
}
149+
.onAppear {
150+
viewModel.send(.setTheme(theme.localizedName))
151+
viewModel.send(.updateDirSize)
152+
}
140153
}
141154

142155
private enum Path: Hashable {
@@ -153,17 +166,44 @@ struct SettingView: View {
153166
Button("확인", role: .destructive) {
154167
viewModel.send(.tapSignOutButton)
155168
}
156-
case .cancel:
169+
case .deleteAuth:
157170
Button("취소", role: .cancel) {
158171
viewModel.send(.setAlert(isPresented: false))
159172
}
160173
Button("탈퇴", role: .destructive) {
161174
viewModel.send(.tapDeleteAuthButton)
162175
}
176+
case .removeCache:
177+
Button("취소", role: .cancel) {
178+
viewModel.send(.setAlert(isPresented: false))
179+
}
180+
Button("확인", role: .destructive) {
181+
182+
}
163183
case .error, .none:
164184
Button("확인", role: .cancel) {
165185
viewModel.send(.setAlert(isPresented: false))
166186
}
167187
}
168188
}
189+
190+
private func formatFileSize(bytes: Int64) -> String {
191+
let units = ["B", "KB", "MB", "GB"]
192+
var value = Double(max(bytes, 0))
193+
var unitIndex = 0
194+
195+
while 1000 <= value && unitIndex < units.count - 1 {
196+
value /= 1024.0
197+
unitIndex += 1
198+
}
199+
200+
let truncated = floor(value * 100.0) / 100.0
201+
let formatter = NumberFormatter()
202+
formatter.minimumIntegerDigits = 1
203+
formatter.minimumFractionDigits = 0
204+
formatter.maximumFractionDigits = 2
205+
206+
let numberString = formatter.string(from: NSNumber(value: truncated)) ?? "0"
207+
return "\(numberString) \(units[unitIndex])"
208+
}
169209
}

0 commit comments

Comments
 (0)