From 229de2594878754edccfc892c691e6090428cf0b Mon Sep 17 00:00:00 2001 From: Chris Brind Date: Thu, 1 Feb 2024 16:38:53 +0000 Subject: [PATCH] prepare / use appropriate previews for the home screen / favorites --- Core/UIViewExtension.swift | 13 +++++++++++++ DuckDuckGo/MainViewController.swift | 26 ++++++++++++++++++++++---- DuckDuckGo/SwipeTabsCoordinator.swift | 2 +- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/Core/UIViewExtension.swift b/Core/UIViewExtension.swift index eb12e52945..d1cea20ad8 100644 --- a/Core/UIViewExtension.swift +++ b/Core/UIViewExtension.swift @@ -73,4 +73,17 @@ extension UIView { view.removeFromSuperview() } } + + @MainActor + public func createImageSnapshot(inBounds bounds: CGRect? = nil) -> UIImage? { + let bounds = bounds ?? self.frame + let size = bounds.size + UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale) + UIGraphicsGetCurrentContext()?.translateBy(x: -bounds.origin.x, y: -bounds.origin.y) + drawHierarchy(in: frame, afterScreenUpdates: true) + let image = UIGraphicsGetImageFromCurrentImageContext() + UIGraphicsEndImageContext() + return image + } + } diff --git a/DuckDuckGo/MainViewController.swift b/DuckDuckGo/MainViewController.swift index 3933ba0bb6..ee8434f136 100644 --- a/DuckDuckGo/MainViewController.swift +++ b/DuckDuckGo/MainViewController.swift @@ -326,16 +326,34 @@ class MainViewController: UIViewController { self?.newTab() } onSwipeStarted: { [weak self] in self?.hideKeyboard() + self?.updatePreviewForCurrentTab() + } + } + + func updatePreviewForCurrentTab() { + assert(Thread.isMainThread) + + if !viewCoordinator.logoContainer.isHidden, + self.tabManager.current()?.link == nil, + let tab = self.tabManager.model.currentTab { - guard let self, - let currentTab = self.tabManager.current(), - currentTab.link != nil else { return } - + // Home screen with logo + if let image = viewCoordinator.logoContainer.createImageSnapshot(inBounds: viewCoordinator.contentContainer.frame) { + previewsSource.update(preview: image, forTab: tab) + } + + } else if let currentTab = self.tabManager.current(), currentTab.link != nil { + // Web view currentTab.preparePreview(completion: { image in guard let image else { return } self.previewsSource.update(preview: image, forTab: currentTab.tabModel) }) + } else if let tab = self.tabManager.model.currentTab { + // Favorites, etc + if let image = viewCoordinator.contentContainer.createImageSnapshot() { + previewsSource.update(preview: image, forTab: tab) + } } } diff --git a/DuckDuckGo/SwipeTabsCoordinator.swift b/DuckDuckGo/SwipeTabsCoordinator.swift index 5daaf2baee..ab3572b8bf 100644 --- a/DuckDuckGo/SwipeTabsCoordinator.swift +++ b/DuckDuckGo/SwipeTabsCoordinator.swift @@ -163,7 +163,7 @@ extension SwipeTabsCoordinator: UICollectionViewDelegate { private func prepareCurrentView() { - if tabsModel.currentTab?.link == nil { + if !coordinator.logoContainer.isHidden { currentView = coordinator.logoContainer } else { currentView = coordinator.contentContainer.subviews.last