From 4d64eb93df8cb41aa336fceb19d4523b9e73ab86 Mon Sep 17 00:00:00 2001 From: Leandro Alonso Date: Wed, 30 Mar 2022 14:42:45 -0300 Subject: [PATCH] add: after a diff is applied, update the scroll view if needed --- .../BlogDashboardViewController.swift | 5 +++++ .../ViewModel/BlogDashboardViewModel.swift | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/WordPress/Classes/ViewRelated/Blog/Blog Dashboard/BlogDashboardViewController.swift b/WordPress/Classes/ViewRelated/Blog/Blog Dashboard/BlogDashboardViewController.swift index 9326a17f7086..79018c6f4221 100644 --- a/WordPress/Classes/ViewRelated/Blog/Blog Dashboard/BlogDashboardViewController.swift +++ b/WordPress/Classes/ViewRelated/Blog/Blog Dashboard/BlogDashboardViewController.swift @@ -21,6 +21,11 @@ final class BlogDashboardViewController: UIViewController { return collectionView }() + /// The "My Site" main scroll view + var mySiteScrollView: UIScrollView? { + return view.superview?.superview as? UIScrollView + } + @objc init(blog: Blog) { self.blog = blog super.init(nibName: nil, bundle: nil) diff --git a/WordPress/Classes/ViewRelated/Blog/Blog Dashboard/ViewModel/BlogDashboardViewModel.swift b/WordPress/Classes/ViewRelated/Blog/Blog Dashboard/ViewModel/BlogDashboardViewModel.swift index fe2cdb0cf16c..390f8af0301f 100644 --- a/WordPress/Classes/ViewRelated/Blog/Blog Dashboard/ViewModel/BlogDashboardViewModel.swift +++ b/WordPress/Classes/ViewRelated/Blog/Blog Dashboard/ViewModel/BlogDashboardViewModel.swift @@ -81,7 +81,22 @@ class BlogDashboardViewModel { private extension BlogDashboardViewModel { func apply(snapshot: DashboardSnapshot) { - dataSource?.apply(snapshot, animatingDifferences: false) + let scrollView = viewController?.mySiteScrollView + let position = scrollView?.contentOffset + + dataSource?.apply(snapshot, animatingDifferences: false) { [weak self] in + guard let scrollView = scrollView, let position = position else { + return + } + + self?.scroll(scrollView, to: position) + } + } + + func scroll(_ scrollView: UIScrollView, to position: CGPoint) { + if position.y > 0 { + scrollView.setContentOffset(position, animated: false) + } } }