Skip to content

Commit

Permalink
Merge pull request tidev#5109 from pec1985/timob-7673
Browse files Browse the repository at this point in the history
[TIMOB-7673] "replaceAt", "insertAt", and "getAt" view-like UI components
  • Loading branch information
vishalduggal committed Apr 17, 2014
2 parents bbc42e2 + fefcbb8 commit 0d80790
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public abstract class TiViewProxy extends KrollProxy implements Handler.Callback
private static final int MSG_FINISH_LAYOUT = MSG_FIRST_ID + 112;
private static final int MSG_UPDATE_LAYOUT = MSG_FIRST_ID + 113;
private static final int MSG_QUEUED_ANIMATE = MSG_FIRST_ID + 114;
private static final int MSG_HIDE_KEYBOARD = MSG_FIRST_ID + 115;
private static final int MSG_INSERT_VIEW_AT = MSG_FIRST_ID + 115;
private static final int MSG_HIDE_KEYBOARD = MSG_FIRST_ID + 116;

protected static final int MSG_LAST_ID = MSG_FIRST_ID + 999;

Expand Down Expand Up @@ -329,6 +330,10 @@ public boolean handleMessage(Message msg)
handleUpdateLayout((HashMap) msg.obj);
return true;
}
case MSG_INSERT_VIEW_AT : {
handleInsertAt((HashMap) msg.obj);
return true;
}
}
return super.handleMessage(msg);
}
Expand Down Expand Up @@ -560,18 +565,100 @@ public void add(TiViewProxy child)
//TODO zOrder
}

public void handleAdd(TiViewProxy child)
@Kroll.method
public void replaceAt(Object params)
{
children.add(child);
if (!(params instanceof HashMap)) {
Log.e(TAG, "Argument for replaceAt must be a dictionary");
return;
}
@SuppressWarnings("rawtypes")
HashMap options = (HashMap) params;
Integer position = -1;
if(options.containsKey("position")) {
position = (Integer) options.get("position");
}
if(children != null && children.size() > position) {
TiViewProxy childToRemove = children.get(position);
insertAt(params);
remove(childToRemove);
}
}


/**
* Adds a child to this view proxy in the specified position. This is useful for "vertical" and
* "horizontal" layouts.
* @param params A Dictionary containing a TiViewProxy for the view and an int for the position
* @module.api
*/
@Kroll.method
public void insertAt(Object params)
{
if (!(params instanceof HashMap)) {
Log.e(TAG, "Argument for insertAt must be a dictionary");
return;
}
@SuppressWarnings("rawtypes")
HashMap options = (HashMap) params;

if (children == null) {
children = new ArrayList<TiViewProxy>();
}


if (view != null) {
if (TiApplication.isUIThread()) {
handleInsertAt(options);
return;
}
getMainHandler().obtainMessage(MSG_INSERT_VIEW_AT, options).sendToTarget();
} else {
handleInsertAt(options);
}
}

private void handleInsertAt(@SuppressWarnings("rawtypes") HashMap options)
{
TiViewProxy child = null;
Integer position = -1;
if(options.containsKey("view")) {
child = (TiViewProxy) options.get("view");
}
if(options.containsKey("position")) {
position = (Integer) options.get("position");
}
if(child == null) {
Log.e(TAG, "insertAt must be contain a view");
return;
}
if(position < 0 || position > children.size()) {
position = children.size();
}

children.add(position, child);
child.parent = new WeakReference<TiViewProxy>(this);

if (view != null) {
child.setActivity(getActivity());
if (this instanceof DecorViewProxy) {
child.isDecorView = true;
}
TiUIView cv = child.getOrCreateView();

view.add(cv);
view.insertAt(cv, position);
}
}

private void handleAdd(TiViewProxy child)
{
children.add(child);
child.parent = new WeakReference<TiViewProxy>(this);
if (view != null) {
child.setActivity(getActivity());
if (this instanceof DecorViewProxy) {
child.isDecorView = true;
}
view.add(child.getOrCreateView());
}
}

Expand Down Expand Up @@ -806,7 +893,6 @@ protected KrollDict handleToImage()
return view.toImage();
}


/**
* Fires an event that can optionally be "bubbled" to the parent view.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ public void add(TiUIView child)
{
add(child, -1);
}

/**
* Adds a child view into the ViewGroup in specific position.
* @param child the view to be added.
* @param position position the view to be added.
*/
public void insertAt(TiUIView child, int position)
{
add(child, position);
}

private void add(TiUIView child, int childIndex)
{
Expand All @@ -171,7 +181,14 @@ private void add(TiUIView child, int childIndex)
((ViewGroup) nv).addView(cv, child.getLayoutParams());
}
}
children.add(child);
if(children.contains(child)) {
children.remove(child);
}
if(childIndex == -1) {
children.add(child);
} else {
children.add(childIndex, child);
}
child.parent = proxy;
}
}
Expand Down
8 changes: 0 additions & 8 deletions iphone/Classes/TiUIScrollViewProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,16 @@ -(CGFloat)autoWidthForSize:(CGSize)size
if (TiLayoutRuleIsVertical(layoutProperties.layoutStyle)) {
//Vertical layout. Just get the maximum child width
CGFloat thisWidth = 0.0;
pthread_rwlock_rdlock(&childrenLock);
NSArray* subproxies = [self children];
for (TiViewProxy * thisChildProxy in subproxies) {
thisWidth = [thisChildProxy minimumParentWidthForSize:contentSize];
if (result < thisWidth) {
result = thisWidth;
}
}
pthread_rwlock_unlock(&childrenLock);
}
else if (TiLayoutRuleIsHorizontal(layoutProperties.layoutStyle)) {
//Horizontal Layout with auto width. Stretch Indefinitely.
pthread_rwlock_rdlock(&childrenLock);
NSArray* subproxies = [self children];
for (TiViewProxy * thisChildProxy in subproxies) {
if ([thisChildProxy widthIsAutoFill]) {
Expand All @@ -157,7 +154,6 @@ -(CGFloat)autoWidthForSize:(CGSize)size
result += [thisChildProxy minimumParentWidthForSize:contentSize];
}
}
pthread_rwlock_unlock(&childrenLock);
}
else {
result = [super autoWidthForSize:contentSize];
Expand Down Expand Up @@ -198,7 +194,6 @@ -(CGFloat)autoHeightForSize:(CGSize)size

CGFloat result = 0.0;
if (TiLayoutRuleIsVertical(layoutProperties.layoutStyle)) {
pthread_rwlock_rdlock(&childrenLock);
NSArray* subproxies = [self children];
for (TiViewProxy * thisChildProxy in subproxies) {
if ([thisChildProxy heightIsAutoFill]) {
Expand All @@ -212,13 +207,11 @@ -(CGFloat)autoHeightForSize:(CGSize)size
result += [thisChildProxy minimumParentHeightForSize:contentSize];
}
}
pthread_rwlock_unlock(&childrenLock);
}
else if (TiLayoutRuleIsHorizontal(layoutProperties.layoutStyle)) {
BOOL horizontalWrap = TiLayoutFlagsHasHorizontalWrap(&layoutProperties);
if(flexibleContentWidth) {
CGFloat thisHeight = 0;
pthread_rwlock_rdlock(&childrenLock);
NSArray* subproxies = [self children];
for (TiViewProxy * thisChildProxy in subproxies) {
if ([thisChildProxy heightIsAutoFill]) {
Expand All @@ -235,7 +228,6 @@ -(CGFloat)autoHeightForSize:(CGSize)size
result = thisHeight;
}
}
pthread_rwlock_unlock(&childrenLock);
}
else {
//Not flexible width and wraps
Expand Down
2 changes: 0 additions & 2 deletions iphone/Classes/TiUITableViewRowProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -704,12 +704,10 @@ -(BOOL)isAttached
// TODO: Add child locking methods for whenever we have to touch children outside TiViewProxy
-(void)willShow
{
pthread_rwlock_rdlock(&childrenLock);
NSArray* subproxies = [self children];
for (TiViewProxy* child in subproxies) {
[child setParentVisible:YES];
}
pthread_rwlock_unlock(&childrenLock);
}

-(void)triggerAttach
Expand Down
2 changes: 1 addition & 1 deletion iphone/Classes/TiViewProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ enum
TiViewProxy *parent;
pthread_rwlock_t childrenLock;
NSMutableArray *children;
NSMutableArray *pendingAdds;
// NSMutableArray *pendingAdds;

#pragma mark Visual components
TiUIView *view;
Expand Down

0 comments on commit 0d80790

Please sign in to comment.