Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor][#21] caching detail #39

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions DontForget.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
AD3B45872B68C288009529DE /* AnniversaryContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD3B45862B68C287009529DE /* AnniversaryContentView.swift */; };
AD3B458D2B68DE8F009529DE /* ConfirmView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD3B458C2B68DE8F009529DE /* ConfirmView.swift */; };
AD6097C62B7CD9C3005060A6 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = AD6097C52B7CD9C3005060A6 /* GoogleService-Info.plist */; };
AD7637612BA70887007552E1 /* CacheManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD7637602BA70887007552E1 /* CacheManager.swift */; };
AD76D0622B594A5000EFB12E /* UIScreen+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD76D0612B594A5000EFB12E /* UIScreen+Extension.swift */; };
AD76D0642B594D2F00EFB12E /* Color+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD76D0632B594D2F00EFB12E /* Color+Extension.swift */; };
AD7BEBBC2B91E93B00A03753 /* Pretendard-Medium.otf in Resources */ = {isa = PBXBuildFile; fileRef = AD7BEBB32B91E93A00A03753 /* Pretendard-Medium.otf */; };
Expand Down Expand Up @@ -160,6 +161,7 @@
AD3B45862B68C287009529DE /* AnniversaryContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnniversaryContentView.swift; sourceTree = "<group>"; };
AD3B458C2B68DE8F009529DE /* ConfirmView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfirmView.swift; sourceTree = "<group>"; };
AD6097C52B7CD9C3005060A6 /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
AD7637602BA70887007552E1 /* CacheManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CacheManager.swift; sourceTree = "<group>"; };
AD76D0612B594A5000EFB12E /* UIScreen+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIScreen+Extension.swift"; sourceTree = "<group>"; };
AD76D0632B594D2F00EFB12E /* Color+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Color+Extension.swift"; sourceTree = "<group>"; };
AD7BEBB32B91E93A00A03753 /* Pretendard-Medium.otf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "Pretendard-Medium.otf"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -222,6 +224,7 @@
09642C342B649A750015220E /* Data */ = {
isa = PBXGroup;
children = (
AD76375F2BA70845007552E1 /* Cache */,
09642C5F2B67D2D00015220E /* Error */,
09642C392B649B1D0015220E /* Network */,
09642C592B67CA160015220E /* Repository */,
Expand Down Expand Up @@ -498,6 +501,14 @@
path = Detail;
sourceTree = "<group>";
};
AD76375F2BA70845007552E1 /* Cache */ = {
isa = PBXGroup;
children = (
AD7637602BA70887007552E1 /* CacheManager.swift */,
);
path = Cache;
sourceTree = "<group>";
};
AD7BEBB22B91E92F00A03753 /* Pretendard */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -747,6 +758,7 @@
AD147F212B57FF7500561846 /* String+Extension.swift in Sources */,
ADA2CC0E2B6CE4CC001A38D9 /* AnniversariesRepository.swift in Sources */,
09642C612B67D2DC0015220E /* ErrorResponse.swift in Sources */,
AD7637612BA70887007552E1 /* CacheManager.swift in Sources */,
09642C4A2B64A37A0015220E /* AnniversaryService.swift in Sources */,
09642C532B67C76F0015220E /* CreationUseCase.swift in Sources */,
ADAFFDD52B70C8CC00746D3E /* FetchAnniversaryDetailUseCase.swift in Sources */,
Expand Down
35 changes: 35 additions & 0 deletions DontForget/Sources/Data/Cache/CacheManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// CacheManager.swift
// DontForget
//
// Created by 제나 on 3/17/24.
//

import Foundation

final class CacheManager {
static let shared = CacheManager()
private let cacheDetails = NSCache<NSString, AnniversaryDetail>()

init() {
cacheDetails.countLimit = 20
}

func loadDetail(_ anniversaryId: Int) -> AnniversaryDetail? {
let key = NSString(string: "\(anniversaryId)")
if let cached = cacheDetails.object(forKey: key) {
return cached
}
return nil
}

func setDetail(_ detail: AnniversaryDetail) {
let key = NSString(string: "\(detail.dto.anniversaryId)")
cacheDetails.setObject(detail, forKey: key)
}

func removeDetail(_ anniversaryId: Int) {
let key = NSString(string: "\(anniversaryId)")
cacheDetails.removeObject(forKey: key)
}
}
7 changes: 7 additions & 0 deletions DontForget/Sources/Data/DTO/DTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,10 @@ struct AnniversaryDetailDTO: Decodable {
let baseDate: String
let baseType: String
}

class AnniversaryDetail {
let dto: AnniversaryDetailDTO
init(anniversaryDetailDTO: AnniversaryDetailDTO) {
self.dto = anniversaryDetailDTO
}
}
1 change: 0 additions & 1 deletion DontForget/Sources/Data/Network/NetworkMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import Foundation
import Network

@Observable
final class NetworkMonitor: ObservableObject {
static let shared = NetworkMonitor()
private let networkMonitor = NWPathMonitor()
Expand Down
1 change: 0 additions & 1 deletion DontForget/Sources/Data/Service/AnniversaryService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ class AnniversaryService {

func fetchAnniversaryDetail(anniversaryId: Int) async throws -> AnniversaryDetailResponse {
return try await withCheckedThrowingContinuation { continuation in
print("=== DEBUG: fetch detail \(anniversaryId)")
provider.request(.readAnniversary(anniversaryId: anniversaryId)) { result in
switch result {
case let .success(response):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct ConfirmView: View {
ZStack {
Color.black.opacity(0.4)
VStack {
Image(.anniversaryDelete)
Image(.calendarDeleteIcon)
.resizable()
.frame(width: 56, height: 56)
.padding(.top, 32)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ final class CreationViewModel: ViewModelType {
}
.receive(on: DispatchQueue.main)
.sink { completion in
CacheManager.shared.removeDetail(id)
self.dismiss = true
if case let .failure(error) = completion {
self.errorMessage = error.localizedDescription
print("=== \(String(describing: self.errorMessage))")
}
} receiveValue: { response in
self.creationResponse = response
Expand All @@ -130,7 +130,6 @@ final class CreationViewModel: ViewModelType {
)
promise(.success(response))
} catch {
print("=== DEBUG: \(error)")
promise(.failure(error))
self.state = .failed("failed fetchAnniversaryDetail()")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,37 @@ final class DefaultAnniversaryDetailViewModel: ViewModelType {

// MARK: - Method
private func fetchAnniversaryDetail() {
Future<AnniversaryDetailResponse?, Error> { promise in
Task {
do {
let response = try await self.fetchAnniversaryDetailUseCase.execute(
requestValue: .init(
query: AnniversaryDetailQuery(
queryId: self.anniversaryId
if let cachedDetail = CacheManager.shared.loadDetail(self.anniversaryId) {
self.anniversaryDetail = cachedDetail.dto
} else {
Future<AnniversaryDetailResponse?, Error> { promise in
Task {
do {
let response = try await self.fetchAnniversaryDetailUseCase.execute(
requestValue: .init(
query: AnniversaryDetailQuery(
queryId: self.anniversaryId
)
)
)
)
promise(.success(response))
} catch {
print("=== DEBUG: \(error)")
promise(.failure(error))
promise(.success(response))
} catch {
print("=== DEBUG: \(error)")
promise(.failure(error))
}
}
}
}
.receive(on: DispatchQueue.main)
.sink { _ in } receiveValue: { [weak self] response in
if let response = response {
self?.anniversaryDetail = response.anniversaryDetail
.receive(on: DispatchQueue.main)
.sink { _ in } receiveValue: { [weak self] response in
if let response = response {
let detail = response.anniversaryDetail
print("=== DEBUG: fetch detail \(detail.anniversaryId)")
self?.anniversaryDetail = detail
CacheManager.shared.setDetail(AnniversaryDetail(anniversaryDetailDTO: detail))
}
}
.store(in: &cancellables)
}
.store(in: &cancellables)
}

private func deleteAnniversary() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ final class DefaultHomeViewModel: ViewModelType {
.sink { _ in } receiveValue: { [weak self] response in
if let self = self, let response = response {
self.anniversaries = response.anniversaries.sorted(by: { $0.solarDate < $1.solarDate })
print("=== DEBUG: \(self.anniversaries)")
self.state = .success
if !self.anniversaries.isEmpty {
self.fetchFirstAnniversaryDetail()
Expand Down Expand Up @@ -137,7 +136,6 @@ final class DefaultHomeViewModel: ViewModelType {
}
}
let response = try await AnniversaryService.shared.changePushState(status: status)
print("=== DEBUG: changeStatus \(status)")
promise(.success(response))
} catch {
print("=== DEBUG: changeStatus \(error)")
Expand Down