Skip to content

Commit

Permalink
prepare / use appropriate previews for the home screen / favorites
Browse files Browse the repository at this point in the history
  • Loading branch information
brindy committed Feb 1, 2024
1 parent 103dcd3 commit 229de25
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
13 changes: 13 additions & 0 deletions Core/UIViewExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
26 changes: 22 additions & 4 deletions DuckDuckGo/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/SwipeTabsCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 229de25

Please sign in to comment.