From 81d5d1e79b13f6677041256e7416f72662c2f2dc Mon Sep 17 00:00:00 2001 From: Michael Gangolf Date: Sat, 5 Oct 2024 16:23:05 +0200 Subject: [PATCH] feat(ios): add direction to ListView scrolling (#14130) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(ios): add direction to ListView scrolling * Update iphone/Classes/TiUIListView.m Co-authored-by: Hans Knöchel * fix: fix “direction” event attribute default * fix: fix incorrect type * chore: align “unknown” value with Android, update docs --------- Co-authored-by: Hans Knöchel Co-authored-by: Hans Knöchel --- .../ui/widget/listview/TiListView.java | 2 ++ apidoc/Titanium/UI/ListView.yml | 2 +- apidoc/Titanium/UI/TableView.yml | 2 +- iphone/Classes/TiUIListView.m | 18 +++++++++++++++--- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/listview/TiListView.java b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/listview/TiListView.java index 2efb68fd889..a169a10a0cc 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/listview/TiListView.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/listview/TiListView.java @@ -153,6 +153,8 @@ public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) payload.put(TiC.PROPERTY_DIRECTION, "up"); } else if (dy < 0) { payload.put(TiC.PROPERTY_DIRECTION, "down"); + } else { + payload.put(TiC.PROPERTY_DIRECTION, "unknown"); } payload.put(TiC.EVENT_PROPERTY_VELOCITY, 0); if (continuousUpdate) { diff --git a/apidoc/Titanium/UI/ListView.yml b/apidoc/Titanium/UI/ListView.yml index 5cbb58e0f68..b6ac0a46f03 100644 --- a/apidoc/Titanium/UI/ListView.yml +++ b/apidoc/Titanium/UI/ListView.yml @@ -648,7 +648,7 @@ events: ipad: 5.4.0 properties: - name: direction - summary: Direction of the scroll either 'up', or 'down'. + summary: Direction of the scroll either 'up', 'down' or 'unknown'. type: String - name: velocity diff --git a/apidoc/Titanium/UI/TableView.yml b/apidoc/Titanium/UI/TableView.yml index 07837bbb172..6b53a579a52 100644 --- a/apidoc/Titanium/UI/TableView.yml +++ b/apidoc/Titanium/UI/TableView.yml @@ -453,7 +453,7 @@ events: since: { android: "2.1.0", iphone: "2.1.0", ipad: "2.1.0" } properties: - name: direction - summary: Direction of the swipe, either `left` or `right`. + summary: Direction of the swipe, either `left`, `right` or `unknown`. type: String - name: index diff --git a/iphone/Classes/TiUIListView.m b/iphone/Classes/TiUIListView.m index ae821bb0393..6a570920fc3 100644 --- a/iphone/Classes/TiUIListView.m +++ b/iphone/Classes/TiUIListView.m @@ -24,6 +24,7 @@ @interface TiUIListView () @property (nonatomic, readonly) TiUIListViewProxy *listViewProxy; @property (nonatomic, copy, readwrite) NSString *searchString; @property (nonatomic, copy, readwrite) NSString *searchedString; +@property (nonatomic, assign) CGFloat lastContentOffset; @end static TiViewProxy *FindViewProxyWithBindIdContainingPoint(UIView *view, CGPoint point); @@ -79,8 +80,8 @@ @implementation TiUIListView { BOOL isSearched; UIView *dimmingView; BOOL isSearchBarInNavigation; - int lastVisibleItem; - int lastVisibleSection; + NSInteger lastVisibleItem; + NSInteger lastVisibleSection; BOOL forceUpdates; } @@ -2011,6 +2012,15 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView TiUIListSectionProxy *section; CGFloat topSpacing = scrollView.contentOffset.y + scrollView.adjustedContentInset.top; + NSString *direction = @"unknown"; + + if (self.lastContentOffset > scrollView.contentOffset.y) { + direction = @"down"; + } else if (self.lastContentOffset < scrollView.contentOffset.y) { + direction = @"up"; + } + self.lastContentOffset = scrollView.contentOffset.y; + if ([indexPaths count] > 0) { NSIndexPath *indexPath = [self pathForSearchPath:[indexPaths objectAtIndex:0]]; NSUInteger visibleItemCount = [indexPaths count]; @@ -2022,6 +2032,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView [eventArgs setValue:section forKey:@"firstVisibleSection"]; [eventArgs setValue:[section itemAtIndex:[indexPath row]] forKey:@"firstVisibleItem"]; [eventArgs setValue:NUMINTEGER(topSpacing) forKey:@"top"]; + [eventArgs setValue:direction forKey:@"direction"]; if (lastVisibleItem != [indexPath row] || lastVisibleSection != [indexPath section] || forceUpdates) { // only log if the item changes or forced @@ -2038,6 +2049,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView [eventArgs setValue:section forKey:@"firstVisibleSection"]; [eventArgs setValue:NUMINTEGER(-1) forKey:@"firstVisibleItem"]; [eventArgs setValue:NUMINTEGER(topSpacing) forKey:@"top"]; + [eventArgs setValue:direction forKey:@"direction"]; } }); } @@ -2110,7 +2122,7 @@ - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { if ([[self proxy] _hasListeners:@"scrolling"]) { - NSString *direction = nil; + NSString *direction = @"unknown"; if (velocity.y > 0) { direction = @"up";