Skip to content

Commit

Permalink
feat(ios): add direction to ListView scrolling (#14130)
Browse files Browse the repository at this point in the history
* feat(ios): add direction to ListView scrolling

* Update iphone/Classes/TiUIListView.m

Co-authored-by: Hans Knöchel <[email protected]>

* fix: fix “direction” event attribute default

* fix: fix incorrect type

* chore: align “unknown” value with Android, update docs

---------

Co-authored-by: Hans Knöchel <[email protected]>
Co-authored-by: Hans Knöchel <[email protected]>
  • Loading branch information
3 people authored Oct 5, 2024
1 parent b8d1a8f commit 81d5d1e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion apidoc/Titanium/UI/ListView.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion apidoc/Titanium/UI/TableView.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions iphone/Classes/TiUIListView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -79,8 +80,8 @@ @implementation TiUIListView {
BOOL isSearched;
UIView *dimmingView;
BOOL isSearchBarInNavigation;
int lastVisibleItem;
int lastVisibleSection;
NSInteger lastVisibleItem;
NSInteger lastVisibleSection;
BOOL forceUpdates;
}

Expand Down Expand Up @@ -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];
Expand All @@ -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
Expand All @@ -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"];
}
});
}
Expand Down Expand Up @@ -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";
Expand Down

0 comments on commit 81d5d1e

Please sign in to comment.