Skip to content

Commit

Permalink
feat(all): add forceUpdates propterty to listView to improve scroll…
Browse files Browse the repository at this point in the history
…ing event (#13830)

Co-authored-by: Chris Barber <chris@cb1inc.com>
  • Loading branch information
2 people authored and hansemannn committed Aug 24, 2023
1 parent 56bd00c commit f954ae7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
Expand Up @@ -562,6 +562,11 @@ private void processProperty(String name, Object value)
if (listView != null) {
listView.setContinousUpdate(TiConvert.toBoolean(value, false));
}
} else if (name.equals("forceUpdates")) {
final TiListView listView = getListView();
if (listView != null) {
listView.setForceUpdates(TiConvert.toBoolean(value, false));
}
}
}

Expand Down
Expand Up @@ -56,6 +56,7 @@ public class TiListView extends TiSwipeRefreshLayout implements OnSearchChangeLi
private SelectionTracker tracker = null;
private boolean isScrolling = false;
private boolean continuousUpdate = false;
private boolean forceUpdate = false;
private int lastScrollDeltaY;
private int scrollOffsetX = 0;
private int scrollOffsetY = 0;
Expand Down Expand Up @@ -157,7 +158,8 @@ public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy)
if (continuousUpdate) {
if (lastVisibleItem != TiConvert.toInt(payload.get(TiC.PROPERTY_FIRST_VISIBLE_ITEM_INDEX))
|| lastVisibleSection
!= TiConvert.toInt(payload.get(TiC.PROPERTY_FIRST_VISIBLE_SECTION_INDEX))) {
!= TiConvert.toInt(payload.get(TiC.PROPERTY_FIRST_VISIBLE_SECTION_INDEX))
|| forceUpdate) {
proxy.fireSyncEvent(TiC.EVENT_SCROLLING, payload);
lastVisibleItem = TiConvert.toInt(payload.get(TiC.PROPERTY_FIRST_VISIBLE_ITEM_INDEX));
lastVisibleSection = TiConvert.toInt(payload.get(TiC.PROPERTY_FIRST_VISIBLE_SECTION_INDEX));
Expand Down Expand Up @@ -282,6 +284,7 @@ public boolean inSelectionHotspot(@NonNull MotionEvent e)
final boolean allowsMultipleSelection
= properties.optBoolean(TiC.PROPERTY_ALLOWS_MULTIPLE_SELECTION_DURING_EDITING, false);
continuousUpdate = properties.optBoolean(TiC.PROPERTY_CONTINUOUS_UPDATE, false);
forceUpdate = properties.optBoolean("forceUpdates", false);

if (properties.optBoolean(TiC.PROPERTY_FIXED_SIZE, false)) {
this.recyclerView.setHasFixedSize(true);
Expand Down Expand Up @@ -398,6 +401,8 @@ public KrollDict generateScrollPayload()
payload.put(TiC.PROPERTY_FIRST_VISIBLE_SECTION, null);
payload.put(TiC.PROPERTY_FIRST_VISIBLE_SECTION_INDEX, -1);
}

payload.put(TiC.PROPERTY_TOP, recyclerView.computeVerticalScrollOffset());
}

// Define visible item count.
Expand Down Expand Up @@ -817,6 +822,11 @@ public void setContinousUpdate(boolean value)
continuousUpdate = value;
}

public void setForceUpdates(boolean value)
{
forceUpdate = value;
}

public void update()
{
this.update(false);
Expand Down
12 changes: 11 additions & 1 deletion apidoc/Titanium/UI/ListView.yml
Expand Up @@ -388,7 +388,7 @@ events:
summary: Fired when a list row has started moving.
description: |
This event can be used to change the UI once a new drag-and-drop interaction starts.
The event properties are available in Titanium SDK 12.0.0+
The event properties are available in Titanium SDK 12.0.0+
since: "11.1.0"
platforms: [android, iphone, ipad, macos]
properties:
Expand Down Expand Up @@ -959,6 +959,16 @@ properties:
platforms: [android, iphone, ipad, macos]
since: 11.1.0

- name: forceUpdates
summary: Optimize the `continuousUpdate` scrolling event.
description: |
If `continuousUpdate` is enabled and you set `forceUpdates` to `true` it will fire the scrolling](Titanium.UI.ListView.scrolling) all the time.
Otherwise it will only update when a new item is scrolled in (default).
type: Boolean
default: false
platforms: [android, iphone, ipad, macos]
since: 12.3.0

- name: keepSectionsInSearch
summary: Determines if the section information is displayed in the search results when using the `searchText` property.
description: |
Expand Down
10 changes: 8 additions & 2 deletions iphone/Classes/TiUIListView.m
Expand Up @@ -81,6 +81,7 @@ @implementation TiUIListView {
BOOL isSearchBarInNavigation;
int lastVisibleItem;
int lastVisibleSection;
BOOL forceUpdates;
}

#ifdef TI_USE_AUTOLAYOUT
Expand All @@ -101,6 +102,7 @@ - (id)init
_defaultItemTemplate = [[NSNumber numberWithUnsignedInteger:UITableViewCellStyleDefault] retain];
_defaultSeparatorInsets = UIEdgeInsetsZero;
_dimsBackgroundDuringPresentation = YES;
forceUpdates = NO;
}
return self;
}
Expand Down Expand Up @@ -207,6 +209,7 @@ - (UITableView *)tableView
if (_tableView == nil) {
UITableViewStyle style = [TiUtils intValue:[self.proxy valueForKey:@"style"] def:UITableViewStylePlain];
BOOL requiresEditingToMove = [TiUtils boolValue:[self.proxy valueForKey:@"requiresEditingToMove"] def:YES];
forceUpdates = [TiUtils boolValue:[self.proxy valueForKey:@"forceUpdates"] def:NO];

_tableView = [[UITableView alloc] initWithFrame:self.bounds style:style];
_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
Expand Down Expand Up @@ -2002,6 +2005,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView
NSArray *indexPaths = [[self tableView] indexPathsForVisibleRows];
NSMutableDictionary *eventArgs = [NSMutableDictionary dictionary];
TiUIListSectionProxy *section;
CGFloat topSpacing = scrollView.contentOffset.y + scrollView.adjustedContentInset.top;

if ([indexPaths count] > 0) {
NSIndexPath *indexPath = [self pathForSearchPath:[indexPaths objectAtIndex:0]];
Expand All @@ -2013,9 +2017,10 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView
[eventArgs setValue:NUMINTEGER([indexPath section]) forKey:@"firstVisibleSectionIndex"];
[eventArgs setValue:section forKey:@"firstVisibleSection"];
[eventArgs setValue:[section itemAtIndex:[indexPath row]] forKey:@"firstVisibleItem"];
[eventArgs setValue:NUMINTEGER(topSpacing) forKey:@"top"];

if (lastVisibleItem != [indexPath row] || lastVisibleSection != [indexPath section]) {
// only log if the item changes
if (lastVisibleItem != [indexPath row] || lastVisibleSection != [indexPath section] || forceUpdates) {
// only log if the item changes or forced
[self.proxy fireEvent:@"scrolling" withObject:eventArgs propagate:NO];
lastVisibleItem = [indexPath row];
lastVisibleSection = [indexPath section];
Expand All @@ -2028,6 +2033,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView
[eventArgs setValue:NUMINTEGER(0) forKey:@"firstVisibleSectionIndex"];
[eventArgs setValue:section forKey:@"firstVisibleSection"];
[eventArgs setValue:NUMINTEGER(-1) forKey:@"firstVisibleItem"];
[eventArgs setValue:NUMINTEGER(topSpacing) forKey:@"top"];
}
});
}
Expand Down

0 comments on commit f954ae7

Please sign in to comment.