-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthSessionRepositoryImpl.swift
More file actions
70 lines (61 loc) · 2.01 KB
/
Copy pathAuthSessionRepositoryImpl.swift
File metadata and controls
70 lines (61 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// AuthSessionRepositoryImpl.swift
// DevLogData
//
// Created by 최윤진 on 12/31/25.
//
import Combine
import DevLogDomain
final class AuthSessionRepositoryImpl: AuthSessionRepository {
private enum Key {
static let preferences = "TodoCategory.preferences"
}
private let authService: AuthService
private let todoCategoryService: TodoCategoryService
private let store: MemoryCacheStore
private let provider: AuthSessionStateProvider
init(
authService: AuthService,
todoCategoryService: TodoCategoryService,
store: MemoryCacheStore,
provider: AuthSessionStateProvider
) {
self.authService = authService
self.todoCategoryService = todoCategoryService
self.store = store
self.provider = provider
}
func observeSignedIn() -> AnyPublisher<Bool, Never> {
authService.observeSignedIn()
.removeDuplicates()
.map { [self] isSignedIn in
Future { promise in
Task {
if isSignedIn {
await self.cachePreferencesIfNeeded()
} else {
self.clearPreferencesCache()
}
self.provider.publish(isSignedIn)
promise(.success(isSignedIn))
}
}
}
.switchToLatest()
.eraseToAnyPublisher()
}
}
private extension AuthSessionRepositoryImpl {
func cachePreferencesIfNeeded() async {
if store.value(forKey: Key.preferences) as [TodoCategoryPreferenceResponse]? != nil {
return
}
guard let preferences = try? await todoCategoryService.fetchCategoryPreferences() else {
return
}
store.setValue(preferences, forKey: Key.preferences)
}
func clearPreferencesCache() {
store.setValue(Optional<[TodoCategoryPreferenceResponse]>.none, forKey: Key.preferences)
}
}