Skip to content
Merged
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
8 changes: 3 additions & 5 deletions iBox/Sources/BoxList/BoxListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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? {
Expand Down
17 changes: 12 additions & 5 deletions iBox/Sources/BoxList/BoxListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
45 changes: 45 additions & 0 deletions iBox/Sources/Shared/WebCacheManager.swift
Original file line number Diff line number Diff line change
@@ -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<UUIDWrapper, WebViewController>()

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)
}

}