Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-20286] iOS/Android: Expose Ti.UI.View.getViewById method #8677

Merged
merged 7 commits into from
Jan 5, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,32 @@ public void removeAllChildren()
}
}
}

/**
* Returns the view by the given ID.
* @module.api
*/
@Kroll.method
public TiViewProxy getViewById(String arg)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String id // same name as docs

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work on Android? id is a type on iOS. Will change it here, but primarily followed other method argument lists.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

{
if (children != null) {
for (TiViewProxy child : children) {
if (child.children != null && child.children.size() > 0) {
TiViewProxy parentChild = child.getViewById(arg);
if (parentChild != null) {
return parentChild;
}
}

if (child.getProperty(TiC.PROPERTY_ID) && child.getProperty(TiC.PROPERTY_ID).equals(arg)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Builds failing, getProperty should be hasProperty

if (child.hasProperty(TiC.PROPERTY_ID) && child.getProperty(TiC.PROPERTY_ID).equals(id)) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But hasProperty returns a boolean and getProperty the property itself. Please go ahead and edit it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but you can only perform an && operation on a Boolean in Java. So either:

if (child.hasProperty(TiC.PROPERTY_ID) && child.getProperty(TiC.PROPERTY_ID).equals(id)) {

// OR

if (child.getProperty(TiC.PROPERTY_ID) != null && child.getProperty(TiC.PROPERTY_ID).equals(id)) {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that's how it was before all that. You can edit it on Github, so we can ensure it doesn't cause problems again.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, I think you might have missed my change to hasProperty here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, changed it. But the original implementation was like the second suggestion above already (except the string-casting), that's why I wondered. Sorry for the confusion, let's finish this 🙂

return child;
}
}
}

return null;
}

public void handleRemove(TiViewProxy child)
{
if (children != null) {
Expand Down
14 changes: 14 additions & 0 deletions apidoc/Titanium/UI/View.yml
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,20 @@ methods:
is not a view, an exception will be raised.
type: Titanium.UI.View

- name: getViewById
summary: Returns the matching view of a given view ID.
returns:
type: Titanium.UI.View
parameters:
- name: id
summary: |
The ID of the view that should be returned. Use the `id` property in your views to
enable it for indexing in this method.
type: String
optional: false
since: 6.1.0
platforms: [iphone, ipad, android]

properties:
- name: accessibilityHidden
summary: Whether the view should be "hidden" from (i.e., ignored by) the accessibility service.
Expand Down
6 changes: 6 additions & 0 deletions iphone/Classes/TiViewProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ enum
*/
-(void)hide:(id)arg;

/**
Returns the view by the given ID.
@param arg The ID of the view to receive.
*/
-(id)getViewById:(id)arg;

/**
Tells the view proxy to run animation on its view.
@param arg An animation object.
Expand Down
20 changes: 20 additions & 0 deletions iphone/Classes/TiViewProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,26 @@ -(void)hide:(id)arg
}, NO);
}

-(id)getViewById:(id)arg
{
ENSURE_SINGLE_ARG(arg, NSString);

for (TiViewProxy *child in [self children]) {
if ([[child children] count] > 0) {
TiViewProxy *parentChild = [child getViewById:arg];
if (parentChild) {
return parentChild;
}
}

if ([[child valueForKey:@"id"] isEqualToString:arg]) {
return child;
}
}

return nil;
}

-(void)animate:(id)arg
{
TiAnimation * newAnimation = [TiAnimation animationFromArg:arg context:[self executionContext] create:NO];
Expand Down