diff --git a/iBox/Sources/BoxList/BoxListView.swift b/iBox/Sources/BoxList/BoxListView.swift index 85bda57..967479b 100644 --- a/iBox/Sources/BoxList/BoxListView.swift +++ b/iBox/Sources/BoxList/BoxListView.swift @@ -11,7 +11,7 @@ import UIKit import SnapKit protocol BoxListViewDelegate: AnyObject { - func didSelectWeb(at url: URL, withName name: String) + func didSelectWeb(id: UUID, at url: URL, withName name: String) func pushViewController(type: EditType) func pushViewController(url: URL?) func presentEditBookmarkController(at indexPath: IndexPath) @@ -184,10 +184,8 @@ extension BoxListView: UITableViewDelegate { } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - guard let viewModel else { return } - let webUrl = viewModel.boxList[indexPath.section].boxListCellViewModelsWithStatus[indexPath.row].url - let webName = viewModel.boxList[indexPath.section].boxListCellViewModelsWithStatus[indexPath.row].name - delegate?.didSelectWeb(at: webUrl, withName: webName) + guard let cellViewModel = viewModel?.boxList[indexPath.section].boxListCellViewModelsWithStatus[indexPath.row] else { return } + delegate?.didSelectWeb(id: cellViewModel.id, at: cellViewModel.url, withName: cellViewModel.name) } func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { diff --git a/iBox/Sources/BoxList/BoxListViewController.swift b/iBox/Sources/BoxList/BoxListViewController.swift index 4ca8f66..f0823d3 100644 --- a/iBox/Sources/BoxList/BoxListViewController.swift +++ b/iBox/Sources/BoxList/BoxListViewController.swift @@ -126,11 +126,18 @@ extension BoxListViewController: BoxListViewDelegate { } - func didSelectWeb(at url: URL, withName name: String) { - let viewController = WebViewController() - viewController.selectedWebsite = url - viewController.title = name - navigationController?.pushViewController(viewController, animated: true) + func didSelectWeb(id: UUID, at url: URL, withName name: String) { + if let cachedViewController = WebCacheManager.shared.viewControllerForKey(id) { + // 이미 캐시에 존재한다면, 그 인스턴스를 재사용 + navigationController?.pushViewController(cachedViewController, animated: true) + } else { + // 캐시에 없는 경우, 새로운 viewController 인스턴스를 생성하고 캐시에 추가합니다. + let viewController = WebViewController() + viewController.selectedWebsite = url + viewController.title = name + WebCacheManager.shared.cacheData(forKey: id, viewController: viewController) + navigationController?.pushViewController(viewController, animated: true) + } } func pushViewController(type: EditType) { diff --git a/iBox/Sources/Shared/WebCacheManager.swift b/iBox/Sources/Shared/WebCacheManager.swift new file mode 100644 index 0000000..a54e26b --- /dev/null +++ b/iBox/Sources/Shared/WebCacheManager.swift @@ -0,0 +1,45 @@ +// +// WebCacheManager.swift +// iBox +// +// Created by jiyeon on 3/26/24. +// + +import UIKit + +class UUIDWrapper: NSObject { + let uuid: UUID + + init(uuid: UUID) { + self.uuid = uuid + } + + override func isEqual(_ object: Any?) -> Bool { + guard let other = object as? UUIDWrapper else { return false } + return uuid == other.uuid + } + + override var hash: Int { + return uuid.hashValue + } +} + +class WebCacheManager { + + static let shared = WebCacheManager() + + private let cache = NSCache() + + private init() {} + + func cacheData(forKey uuid: UUID, viewController: WebViewController) { + let wrapper = UUIDWrapper(uuid: uuid) + cache.setObject(viewController, forKey: wrapper) + } + + func viewControllerForKey(_ uuid: UUID) -> WebViewController? { + let wrapper = UUIDWrapper(uuid: uuid) + return cache.object(forKey: wrapper) + } + +}