Skip to content

Commit 9dc2714

Browse files
committed
Add scroll visibility type and tests.
1 parent 98b44bc commit 9dc2714

4 files changed

Lines changed: 81 additions & 23 deletions

File tree

WordPress/Classes/ViewRelated/Jetpack/Branding/Banner/JPScrollViewDelegate.swift

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,7 @@ extension JPScrollViewDelegate {
1616
}
1717

1818
func processJetpackBannerVisibility(_ scrollView: UIScrollView) {
19-
let shouldHideJetpackBanner = Self.shouldHideJetpackBanner(
20-
contentHeight: scrollView.contentSize.height,
21-
frameHeight: scrollView.frame.height,
22-
verticalContentOffset: scrollView.contentOffset.y + scrollView.adjustedContentInset.top
23-
)
24-
25-
scrollViewTranslationPublisher.send(shouldHideJetpackBanner)
26-
}
27-
28-
static func shouldHideJetpackBanner(
29-
contentHeight: CGFloat,
30-
frameHeight: CGFloat,
31-
verticalContentOffset: CGFloat
32-
) -> Bool {
33-
/// The scrollable content isn't any larger than its frame, so don't hide the banner if the view is bounced.
34-
if contentHeight <= frameHeight {
35-
return false
36-
/// Don't hide the banner until the view has scrolled down some. Currently the height of the banner itself.
37-
} else if verticalContentOffset <= JetpackBannerView.minimumHeight {
38-
return false
39-
}
40-
41-
return true
19+
let shouldHide = JetpackBannerScrollVisibility.shouldHide(scrollView)
20+
scrollViewTranslationPublisher.send(shouldHide)
4221
}
4322
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Foundation
2+
import UIKit
3+
4+
struct JetpackBannerScrollVisibility {
5+
static func shouldHide(_ scrollView: UIScrollView) -> Bool {
6+
return Self.shouldHide(
7+
contentHeight: scrollView.contentSize.height,
8+
frameHeight: scrollView.frame.height,
9+
verticalContentOffset: scrollView.contentOffset.y + scrollView.adjustedContentInset.top
10+
)
11+
}
12+
13+
static func shouldHide(
14+
contentHeight: CGFloat,
15+
frameHeight: CGFloat,
16+
verticalContentOffset: CGFloat
17+
) -> Bool {
18+
/// The scrollable content isn't any larger than its frame, so don't hide the banner if the view is bounced.
19+
if contentHeight <= frameHeight {
20+
return false
21+
/// Don't hide the banner until the view has scrolled down some. Currently the height of the banner itself.
22+
} else if verticalContentOffset <= JetpackBannerView.minimumHeight {
23+
return false
24+
}
25+
26+
return true
27+
}
28+
}

WordPress/WordPress.xcodeproj/project.pbxproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,6 +2291,9 @@
22912291
C396C80B280F2401006FE7AC /* SiteDesignTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C396C80A280F2401006FE7AC /* SiteDesignTests.swift */; };
22922292
C3C21EB928385EC8002296E2 /* RemoteSiteDesigns.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3C21EB828385EC8002296E2 /* RemoteSiteDesigns.swift */; };
22932293
C3C21EBA28385EC8002296E2 /* RemoteSiteDesigns.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3C21EB828385EC8002296E2 /* RemoteSiteDesigns.swift */; };
2294+
C3C2F84628AC8BC700937E45 /* JetpackBannerScrollVisibilityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3C2F84528AC8BC700937E45 /* JetpackBannerScrollVisibilityTests.swift */; };
2295+
C3C2F84828AC8EBF00937E45 /* JetpackBannerScrollVisibility.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3C2F84728AC8EBF00937E45 /* JetpackBannerScrollVisibility.swift */; };
2296+
C3C2F84928AC8EBF00937E45 /* JetpackBannerScrollVisibility.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3C2F84728AC8EBF00937E45 /* JetpackBannerScrollVisibility.swift */; };
22942297
C3C39B0726F50D3900B1238D /* WordPressSupportSourceTag+Editor.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3C39B0626F50D3900B1238D /* WordPressSupportSourceTag+Editor.swift */; };
22952298
C3C39B0826F50D3900B1238D /* WordPressSupportSourceTag+Editor.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3C39B0626F50D3900B1238D /* WordPressSupportSourceTag+Editor.swift */; };
22962299
C3C70C562835C5BB00DD2546 /* SiteDesignSectionLoaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3C70C552835C5BB00DD2546 /* SiteDesignSectionLoaderTests.swift */; };
@@ -7193,6 +7196,8 @@
71937196
C396C80A280F2401006FE7AC /* SiteDesignTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SiteDesignTests.swift; sourceTree = "<group>"; };
71947197
C3ABE791263099F7009BD402 /* WordPress 121.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "WordPress 121.xcdatamodel"; sourceTree = "<group>"; };
71957198
C3C21EB828385EC8002296E2 /* RemoteSiteDesigns.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteSiteDesigns.swift; sourceTree = "<group>"; };
7199+
C3C2F84528AC8BC700937E45 /* JetpackBannerScrollVisibilityTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JetpackBannerScrollVisibilityTests.swift; sourceTree = "<group>"; };
7200+
C3C2F84728AC8EBF00937E45 /* JetpackBannerScrollVisibility.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JetpackBannerScrollVisibility.swift; sourceTree = "<group>"; };
71967201
C3C39B0626F50D3900B1238D /* WordPressSupportSourceTag+Editor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "WordPressSupportSourceTag+Editor.swift"; sourceTree = "<group>"; };
71977202
C3C70C552835C5BB00DD2546 /* SiteDesignSectionLoaderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SiteDesignSectionLoaderTests.swift; sourceTree = "<group>"; };
71987203
C3DA0EDF2807062600DA3250 /* SiteCreationNameTracksEventTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SiteCreationNameTracksEventTests.swift; sourceTree = "<group>"; };
@@ -10174,6 +10179,7 @@
1017410179
isa = PBXGroup;
1017510180
children = (
1017610181
B0CD27CE286F8858009500BF /* JetpackBannerView.swift */,
10182+
C3C2F84728AC8EBF00937E45 /* JetpackBannerScrollVisibility.swift */,
1017710183
C3835558288B02B00062E402 /* JetpackBannerWrapperViewController.swift */,
1017810184
3FAE0651287C8FC500F46508 /* JPScrollViewDelegate.swift */,
1017910185
);
@@ -13925,6 +13931,7 @@
1392513931
BE20F5E11B2F738E0020694C /* ViewRelated */ = {
1392613932
isa = PBXGroup;
1392713933
children = (
13934+
C3C2F84428AC8B9E00937E45 /* Jetpack */,
1392813935
F44FB6C92878957E0001E3CE /* Mention */,
1392913936
8B69F0E2255C2BC0006B1CEF /* Activity */,
1393013937
8BD36E042395CC2F00EFFF1C /* Aztec */,
@@ -14064,6 +14071,14 @@
1406414071
path = Extensions;
1406514072
sourceTree = "<group>";
1406614073
};
14074+
C3C2F84428AC8B9E00937E45 /* Jetpack */ = {
14075+
isa = PBXGroup;
14076+
children = (
14077+
C3C2F84528AC8BC700937E45 /* JetpackBannerScrollVisibilityTests.swift */,
14078+
);
14079+
path = Jetpack;
14080+
sourceTree = "<group>";
14081+
};
1406714082
C3E42AAD27F4D2CF00546706 /* Menus */ = {
1406814083
isa = PBXGroup;
1406914084
children = (
@@ -18586,6 +18601,7 @@
1858618601
8217380B1FE05EE600BEC94C /* BlogSettings+DateAndTimeFormat.swift in Sources */,
1858718602
57047A4F22A961BC00B461DF /* PostSearchHeader.swift in Sources */,
1858818603
088B89891DA6F93B000E8DEF /* ReaderPostCardContentLabel.swift in Sources */,
18604+
C3C2F84828AC8EBF00937E45 /* JetpackBannerScrollVisibility.swift in Sources */,
1858918605
F181EDE526B2AC7200C61241 /* BackgroundTasksCoordinator.swift in Sources */,
1859018606
8B93412F257029F60097D0AC /* FilterChipButton.swift in Sources */,
1859118607
40A71C68220E1952002E3D25 /* StatsRecordValue+CoreDataClass.swift in Sources */,
@@ -20397,6 +20413,7 @@
2039720413
FF1B11E7238FE27A0038B93E /* GutenbergGalleryUploadProcessorTests.swift in Sources */,
2039820414
F4D9AF51288AE23500803D40 /* SuggestionTableViewTests.swift in Sources */,
2039920415
8BC12F72231FEBA1004DDA72 /* PostCoordinatorTests.swift in Sources */,
20416+
C3C2F84628AC8BC700937E45 /* JetpackBannerScrollVisibilityTests.swift in Sources */,
2040020417
D8B6BEB7203E11F2007C8A19 /* Bundle+LoadFromNib.swift in Sources */,
2040120418
8B2D4F5527ECE376009B085C /* BlogDashboardPostsParserTests.swift in Sources */,
2040220419
4A266B91282B13A70089CF3D /* CoreDataTestCase.swift in Sources */,
@@ -21474,6 +21491,7 @@
2147421491
FABB235A2602FC2C00C8785C /* ReaderCardsStreamViewController.swift in Sources */,
2147521492
FABB235B2602FC2C00C8785C /* DeleteSiteViewController.swift in Sources */,
2147621493
FABB235C2602FC2C00C8785C /* WPAnalyticsTrackerWPCom.m in Sources */,
21494+
C3C2F84928AC8EBF00937E45 /* JetpackBannerScrollVisibility.swift in Sources */,
2147721495
FABB235D2602FC2C00C8785C /* AztecAttachmentViewController.swift in Sources */,
2147821496
FABB235F2602FC2C00C8785C /* LoginEpilogueBlogCell.swift in Sources */,
2147921497
FABB23602602FC2C00C8785C /* PostServiceRemoteFactory.swift in Sources */,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import XCTest
2+
3+
@testable import WordPress
4+
5+
class JetpackBannerScrollVisibilityTests: XCTestCase {
6+
7+
/// A scroll view without enough content to scroll, so it "bounces"
8+
func testBannerIsNotHiddenWhenScrollViewBounces() {
9+
// When
10+
let hidden = JetpackBannerScrollVisibility.shouldHide(
11+
contentHeight: 400,
12+
frameHeight: 400,
13+
verticalContentOffset: 200
14+
)
15+
16+
// Then
17+
XCTAssertFalse(hidden)
18+
}
19+
20+
/// A scroll view with enough content to scroll and has been scrolled past the minimum height of a Jetpack Banner
21+
func testBannerIsHiddenWhenScrolledDown() {
22+
// When
23+
let hidden = JetpackBannerScrollVisibility.shouldHide(
24+
contentHeight: 600,
25+
frameHeight: 400,
26+
verticalContentOffset: JetpackBannerView.minimumHeight + 1
27+
)
28+
29+
// Then
30+
XCTAssertTrue(hidden)
31+
}
32+
33+
}

0 commit comments

Comments
 (0)