Skip to content

Commit

Permalink
feat: Parity for Ti.UI.ListViewScrollPosition.* / Ti.UI.TableViewScro…
Browse files Browse the repository at this point in the history
…llPosition.* on iOS/Android (#13251)

* feat(android): scrollToItem position top

move constant

clean up

* feat(ios): add parity for „Ti.UI.ListViewScrollPosition.*“ constants

Co-authored-by: Hans Knöchel <hansemannn@users.noreply.github.com>
  • Loading branch information
m1ga and hansemannn committed Jun 26, 2022
1 parent 8dac17b commit 8cea843
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2020 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package ti.modules.titanium.ui;

import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;

import ti.modules.titanium.ui.UIModule;

@Kroll.module(parentModule = UIModule.class)
public class ListViewScrollPositionModule extends KrollModule
{
@Kroll.constant
public static final int TOP = 1;
@Kroll.constant
public static final int BOTTOM = 2;
@Kroll.constant
public static final int MIDDLE = 3;
@Kroll.constant
public static final int NONE = 0;

@Override
public String getApiName()
{
return "Ti.UI.ListViewScrollPosition";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,35 @@
*/
package ti.modules.titanium.ui.widget.listview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import static android.util.TypedValue.COMPLEX_UNIT_DIP;

import android.app.Activity;
import android.view.View;

import androidx.recyclerview.selection.SelectionTracker;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiDimension;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.view.TiUIView;

import android.app.Activity;
import android.view.View;

import androidx.recyclerview.selection.SelectionTracker;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import ti.modules.titanium.ui.UIModule;
import ti.modules.titanium.ui.ListViewScrollPositionModule;
import ti.modules.titanium.ui.widget.TiUIListView;

import static android.util.TypedValue.COMPLEX_UNIT_DIP;

@Kroll.proxy(
creatableInModule = ti.modules.titanium.ui.UIModule.class,
propertyAccessors = {
Expand Down Expand Up @@ -781,6 +786,14 @@ public void scrollToItem(int sectionIndex, int itemIndex, @Kroll.argument(option
{
final TiListView listView = getListView();
final boolean animated = animation == null || animation.optBoolean(TiC.PROPERTY_ANIMATED, true);
final int position = animation != null ? animation.optInt(TiC.PROPERTY_POSITION, 0) : 0;
final RecyclerView.SmoothScroller smoothScrollerToTop =
new LinearSmoothScroller(TiApplication.getAppCurrentActivity())
{
@Override
protected int getVerticalSnapPreference()
{ return LinearSmoothScroller.SNAP_TO_START; }
};

if (listView != null) {
final ListSectionProxy section = getSectionByIndex(sectionIndex);
Expand All @@ -792,9 +805,19 @@ public void scrollToItem(int sectionIndex, int itemIndex, @Kroll.argument(option
final int itemAdapterIndex = listView.getAdapterIndex(item.index);
final Runnable action = () -> {
if (animated) {
listView.getRecyclerView().smoothScrollToPosition(itemAdapterIndex);
if (position == ListViewScrollPositionModule.TOP) {
smoothScrollerToTop.setTargetPosition(itemAdapterIndex);
listView.getRecyclerView().getLayoutManager().startSmoothScroll(smoothScrollerToTop);
} else {
listView.getRecyclerView().smoothScrollToPosition(itemAdapterIndex);
}
} else {
listView.getRecyclerView().scrollToPosition(itemAdapterIndex);
if (position == ListViewScrollPositionModule.TOP) {
((LinearLayoutManager) listView.getRecyclerView().getLayoutManager())
.scrollToPositionWithOffset(itemAdapterIndex, 0);
} else {
listView.getRecyclerView().scrollToPosition(itemAdapterIndex);
}
}
};

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 @@ -1791,7 +1791,7 @@ properties:
- name: position
summary: Specifies what position to scroll the selected cell to.
type: Number
constants: [Titanium.UI.iOS.ListViewScrollPosition.*, Titanium.UI.Windows.ListViewScrollPosition.*]
constants: [Titanium.UI.iOS.ListViewScrollPosition.*, Titanium.UI.ListViewScrollPosition.*]
default: Titanium.UI.iOS.ListViewScrollPosition.NONE

---
Expand Down
36 changes: 36 additions & 0 deletions apidoc/Titanium/UI/ListViewScrollPosition.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
name: Titanium.UI.ListViewScrollPosition
summary: |
A set of constants for the position value that can be used for the `position` property of
<ListViewAnimationProperties> when invoking the ListView's `scrollToItem`, `appendSection`,
`deleteSectionAt`, `insertSectionAt` and `replaceSectionAt` methods.
extends: Titanium.Proxy
platforms: [android, iphone, ipad, macos]
since: {android: "10.2.0", iphone: "5.4.0", ipad: "5.4.0", macos: "9.2.0"}
excludes:
properties: [bubbleParent]
methods: [addEventListener, applyProperties, fireEvent, removeEventListener]
createable: false
properties:
- name: BOTTOM
summary: The list view scrolls the row of interest to the bottom of the visible list view.
type: Number
permission: read-only

- name: MIDDLE
summary: The list view scrolls the row of interest to the middle of the list table view.
type: Number
permission: read-only

- name: NONE
summary: |
The table view scrolls the row of interest to be fully visible with a minimal movement.
If the row is already fully visible, no scrolling occurs. For example, if the row is above the
visible area, the behavior is identical to that specified by `TOP`. This is the default.
type: Number
permission: read-only

- name: TOP
summary: The list view scrolls the row of interest to the top of the visible list view.
type: Number
permission: read-only
23 changes: 23 additions & 0 deletions iphone/Classes/TiUITableViewScrollPositionProxy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2016 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#if defined(USE_TI_UITABLEVIEWSCROLLPOSITION) || defined(USE_TI_UILISTVIEWSCROLLPOSITION)

#import <TitaniumKit/TiProxy.h>

@interface TiUITableViewScrollPositionProxy : TiProxy {

@private
}

@property (nonatomic, readonly) NSNumber *NONE;
@property (nonatomic, readonly) NSNumber *TOP;
@property (nonatomic, readonly) NSNumber *MIDDLE;
@property (nonatomic, readonly) NSNumber *BOTTOM;

@end

#endif
26 changes: 26 additions & 0 deletions iphone/Classes/TiUITableViewScrollPositionProxy.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#if defined(USE_TI_UITABLEVIEWSCROLLPOSITION) || defined(USE_TI_UILISTVIEWSCROLLPOSITION)

#import "TiUITableViewScrollPositionProxy.h"
#import <TitaniumKit/TiBase.h>

@implementation TiUITableViewScrollPositionProxy

- (NSString *)apiName
{
return @"Ti.UI.TableViewScrollPosition";
}

MAKE_SYSTEM_PROP(NONE, UITableViewScrollPositionNone);
MAKE_SYSTEM_PROP(TOP, UITableViewScrollPositionTop);
MAKE_SYSTEM_PROP(MIDDLE, UITableViewScrollPositionMiddle);
MAKE_SYSTEM_PROP(BOTTOM, UITableViewScrollPositionBottom);

@end

#endif
8 changes: 4 additions & 4 deletions iphone/Classes/TiUIiOSTableViewScrollPositionProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ - (NSString *)apiName
return @"Ti.UI.iOS.TableViewScrollPosition";
}

MAKE_SYSTEM_PROP(NONE, UITableViewScrollPositionNone);
MAKE_SYSTEM_PROP(TOP, UITableViewScrollPositionTop);
MAKE_SYSTEM_PROP(MIDDLE, UITableViewScrollPositionMiddle);
MAKE_SYSTEM_PROP(BOTTOM, UITableViewScrollPositionBottom);
MAKE_SYSTEM_PROP_DEPRECATED_REPLACED(NONE, UITableViewScrollPositionNone, @"UI.iOS.TableViewScrollPosition.NONE", @"10.2.0", "UI.TableViewScrollPosition.NONE");
MAKE_SYSTEM_PROP_DEPRECATED_REPLACED(TOP, UITableViewScrollPositionTop, @"UI.iOS.TableViewScrollPosition.NONE", @"10.2.0", "UI.TableViewScrollPosition.TOP");
MAKE_SYSTEM_PROP_DEPRECATED_REPLACED(MIDDLE, UITableViewScrollPositionMiddle, @"UI.iOS.TableViewScrollPosition.NONE", @"10.2.0", "UI.TableViewScrollPosition.MIDDLE");
MAKE_SYSTEM_PROP_DEPRECATED_REPLACED(BOTTOM, UITableViewScrollPositionBottom, @"UI.iOS.TableViewScrollPosition.NONE", @"10.2.0", "UI.TableViewScrollPosition.BOTTOM");

@end

Expand Down
9 changes: 9 additions & 0 deletions iphone/Classes/UIModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#import "TiUIShortcutProxy.h"
#endif

#if defined(USE_TI_UITABLEVIEWSCROLLPOSITION) || defined(USE_TI_UILISTVIEWSCROLLPOSITION)
#import "TiUITableViewScrollPositionProxy.h"
#endif

@interface UIModule : TiModule {

@private
Expand Down Expand Up @@ -305,6 +309,11 @@
@property (nonatomic, readonly) TiUIShortcutProxy *Shortcut;
#endif

#if defined(USE_TI_UITABLEVIEWSCROLLPOSITION) || defined(USE_TI_UILISTVIEWSCROLLPOSITION)
@property (nonatomic, readwrite, assign) TiUITableViewScrollPositionProxy *TableViewScrollPosition;
@property (nonatomic, readwrite, assign) TiUITableViewScrollPositionProxy *ListViewScrollPosition;
#endif

@end

#endif
28 changes: 28 additions & 0 deletions iphone/Classes/UIModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@

@implementation UIModule

#define FORGET_AND_RELEASE(x) \
{ \
[self forgetProxy:x]; \
RELEASE_TO_NIL(x); \
}

- (void)dealloc
{
#ifdef USE_TI_UIIPAD
Expand Down Expand Up @@ -596,6 +602,10 @@ - (void)didReceiveMemoryWarning:(NSNotification *)notification
#endif
#ifdef USE_TI_UICLIPBOARD
RELEASE_TO_NIL(clipboard);
#endif
#if defined(USE_TI_UITABLEVIEWSCROLLPOSITION) || defined(USE_TI_UILISTVIEWSCROLLPOSITION)
FORGET_AND_RELEASE(_TableViewScrollPosition);
FORGET_AND_RELEASE(_ListViewScrollPosition);
#endif
[super didReceiveMemoryWarning:notification];
}
Expand Down Expand Up @@ -830,6 +840,24 @@ - (TiUIShortcutProxy *)Shortcut
}
#endif

#if defined(USE_TI_UITABLEVIEWSCROLLPOSITION) || defined(USE_TI_UILISTVIEWSCROLLPOSITION)
- (TiUITableViewScrollPositionProxy *)TableViewScrollPosition
{
if (_TableViewScrollPosition == nil) {
_TableViewScrollPosition = [[TiUITableViewScrollPositionProxy alloc] _initWithPageContext:[self pageContext]];
}
return _TableViewScrollPosition;
}

- (TiUITableViewScrollPositionProxy *)ListViewScrollPosition
{
if (_ListViewScrollPosition == nil) {
_ListViewScrollPosition = [[TiUITableViewScrollPositionProxy alloc] _initWithPageContext:[self pageContext]];
}
return _ListViewScrollPosition;
}
#endif

@end

#endif
8 changes: 7 additions & 1 deletion iphone/iphone/Titanium.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
3A275F3E1BA881B300EC4912 /* TiUIActivityIndicatorStyleProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = 3A275F3D1BA881B300EC4912 /* TiUIActivityIndicatorStyleProxy.m */; };
3A38F30424D6EBBD00CC6EFB /* TiUtils+Addons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A38F30324D6EBBD00CC6EFB /* TiUtils+Addons.swift */; };
3A3BBAF51D3E2F0F008450DF /* TiAppiOSUserNotificationCenterProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = 3A3BBAF41D3E2F0F008450DF /* TiAppiOSUserNotificationCenterProxy.m */; };
3A527EB327E0F77700A470D6 /* TiUITableViewScrollPositionProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = 3A527EB227E0F77700A470D6 /* TiUITableViewScrollPositionProxy.m */; };
3A5AD7261BB9A6E4005B408B /* TiUIiOSPreviewActionGroupProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = 3A5AD7251BB9A6E4005B408B /* TiUIiOSPreviewActionGroupProxy.m */; };
3A5AD7291BB9BEA8005B408B /* TiUIiOSPreviewContextProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = 3A5AD7281BB9BEA8005B408B /* TiUIiOSPreviewContextProxy.m */; };
3A811CD01C2C21E50023468C /* TiUIiOSBlurViewProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = 3A811CCF1C2C21E50023468C /* TiUIiOSBlurViewProxy.m */; };
Expand Down Expand Up @@ -605,6 +606,8 @@
3A38F30324D6EBBD00CC6EFB /* TiUtils+Addons.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TiUtils+Addons.swift"; sourceTree = "<group>"; };
3A3BBAF31D3E2F0F008450DF /* TiAppiOSUserNotificationCenterProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TiAppiOSUserNotificationCenterProxy.h; sourceTree = "<group>"; };
3A3BBAF41D3E2F0F008450DF /* TiAppiOSUserNotificationCenterProxy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TiAppiOSUserNotificationCenterProxy.m; sourceTree = "<group>"; };
3A527EB027E0F77700A470D6 /* TiUITableViewScrollPositionProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TiUITableViewScrollPositionProxy.h; sourceTree = "<group>"; };
3A527EB227E0F77700A470D6 /* TiUITableViewScrollPositionProxy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TiUITableViewScrollPositionProxy.m; sourceTree = "<group>"; };
3A5AD7241BB9A6E4005B408B /* TiUIiOSPreviewActionGroupProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TiUIiOSPreviewActionGroupProxy.h; sourceTree = "<group>"; };
3A5AD7251BB9A6E4005B408B /* TiUIiOSPreviewActionGroupProxy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TiUIiOSPreviewActionGroupProxy.m; sourceTree = "<group>"; };
3A5AD7271BB9BEA8005B408B /* TiUIiOSPreviewContextProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TiUIiOSPreviewContextProxy.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1278,9 +1281,11 @@
isa = PBXGroup;
children = (
3AA4EC1B232034FE00703A20 /* ListView */,
DB769E00210F0D92004D9338 /* TableView */,
84C3870017417CB800970D26 /* TiSelectedCellbackgroundView.h */,
84C3870117417CB800970D26 /* TiSelectedCellbackgroundView.m */,
DB769E00210F0D92004D9338 /* TableView */,
3A527EB027E0F77700A470D6 /* TiUITableViewScrollPositionProxy.h */,
3A527EB227E0F77700A470D6 /* TiUITableViewScrollPositionProxy.m */,
);
name = "TableView & ListView";
sourceTree = "<group>";
Expand Down Expand Up @@ -2272,6 +2277,7 @@
DA864E3211A2314A00B9CD68 /* TiMediaMusicPlayer.m in Sources */,
823CC8AC1B7F0E4D00D220C7 /* WatchSessionModule.m in Sources */,
DAA905CE11A359F10030B119 /* TiMediaItem.m in Sources */,
3A527EB327E0F77700A470D6 /* TiUITableViewScrollPositionProxy.m in Sources */,
84EB08011A71932D00D35815 /* TiUIiOSSplitWindowProxy.m in Sources */,
242E967711BAD2F20046A0D0 /* AsyncSocket.m in Sources */,
242E967811BAD2F20046A0D0 /* AsyncUdpSocket.m in Sources */,
Expand Down
Loading

0 comments on commit 8cea843

Please sign in to comment.