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-16912][TIMOB-8222] TabGroup fixes #5674

Merged
merged 4 commits into from
May 12, 2014
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -56,7 +56,6 @@ public class TabGroupProxy extends TiWindowProxy implements TiActivityWindow
private WeakReference<ActionBarActivity> tabGroupActivity;
private TabProxy selectedTab;
private boolean isFocused;
private int setTabOrIndex;

public TabGroupProxy()
{
Expand Down Expand Up @@ -172,30 +171,12 @@ public void handleRemoveTab(TabProxy tab) {
tabs.remove(tab);
}

@Kroll.setProperty @Kroll.method
@Kroll.method
public void setActiveTab(Object tabOrIndex)
{
TabProxy tab;
if (tabOrIndex instanceof Number) {
int tabIndex = ((Number) tabOrIndex).intValue();
if (!this.opened && tabIndex > 0) {
setTabOrIndex = tabIndex;
return;
} else if (tabIndex < 0 || tabIndex >= tabs.size()) {
Log.e(TAG, "Invalid tab index.");
return;
}
tab = tabs.get(tabIndex);

} else if (tabOrIndex instanceof TabProxy) {
tab = (TabProxy) tabOrIndex;
if (!tabs.contains(tab)) {
Log.e(TAG, "Cannot activate tab not in this group.");
return;
}

} else {
Log.e(TAG, "No valid tab provided when setting active tab.");
TabProxy tab = parseTab(tabOrIndex);

if(tab == null) {
return;
}

Expand Down Expand Up @@ -233,6 +214,29 @@ public void setTabs(Object obj)
TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_SET_TABS), obj);
}


private TabProxy parseTab(Object tabOrIndex) {
TabProxy tab = null;
if (tabOrIndex instanceof Number) {
int tabIndex = ((Number) tabOrIndex).intValue();
if (tabIndex < 0 || tabIndex >= tabs.size()) {
Log.e(TAG, "Invalid tab index.");
} else {
tab = tabs.get(tabIndex);
}

} else if (tabOrIndex instanceof TabProxy) {
if (!tabs.contains((TabProxy) tabOrIndex)) {
Log.e(TAG, "Cannot activate tab not in this group.");
} else {
tab = (TabProxy) tabOrIndex;
}
} else {
Log.e(TAG, "No valid tab provided when setting active tab.");
}
return tab;
}

private void handleSetTabs(Object obj)
{
tabs.clear();
Expand Down Expand Up @@ -261,13 +265,9 @@ public void handleCreationDict(KrollDict options) {
Log.e(TAG, "Invalid orientationMode array. Must only contain orientation mode constants.");
}
}

if (options.containsKey(TiC.PROPERTY_ACTIVE_TAB)) {
setActiveTab(options.get(TiC.PROPERTY_ACTIVE_TAB));
}
}

@Kroll.getProperty @Kroll.method
@Kroll.method
public TabProxy getActiveTab() {
if (TiApplication.isUIThread()) {
return handleGetActiveTab();
Expand Down Expand Up @@ -327,7 +327,7 @@ public void windowCreated(TiBaseActivity activity) {
}

@Override
public void handlePostOpen()
protected void handlePostOpen()
{
super.handlePostOpen();

Expand All @@ -342,11 +342,7 @@ public void handlePostOpen()
for (TabProxy tab : tabs) {
tg.addTab(tab);
}
if (setTabOrIndex < 0 || setTabOrIndex >= tabs.size()) {
Log.e(TAG, "Invalid tab index.");
} else {
setActiveTab(setTabOrIndex);
}

TabProxy activeTab = handleGetActiveTab();
if (activeTab != null) {
selectedTab = null;
Expand Down
38 changes: 16 additions & 22 deletions android/modules/ui/src/js/tabgroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ exports.bootstrap = function(Titanium) {

if (options) {
tabGroup._tabs = options.tabs || [];
tabGroup._activeTab = options.activeTab || -1;
} else {
tabGroup._tabs = [];
tabGroup._activeTab = -1;
}

// Keeps track of the current tab group state
tabGroup.currentState = tabGroup.state.closed;

tabGroup._activeTab = -1;

// Set the activity property here since we bind it to _internalActivity for window proxies by default
Object.defineProperty(TabGroup.prototype, "activity", { get: tabGroup.getActivity});

Expand Down Expand Up @@ -84,35 +84,28 @@ exports.bootstrap = function(Titanium) {
}
}

var _setActiveTab = TabGroup.prototype.setActiveTab;

TabGroup.prototype.setActiveTab = function(taborindex) {
if ( (this.currentState == this.state.opened) ||
(this.currentState == this.state.opening) ){
_setActiveTab.call(this,taborindex);
} else {
if (!isNaN(parseFloat(taborindex)) && isFinite(taborindex)) {
if (taborindex >= 0 && taborindex < this._tabs.length) {
this._activeTab = this._tabs[taborindex];
}
} else {
this._activeTab = taborindex;
}

}
}

var _getActiveTab = TabGroup.prototype.getActiveTab;

TabGroup.prototype.getActiveTab = function() {
if (this.currentState == this.state.opened) {
if (this.currentState == this.state.opened){
return _getActiveTab.call(this);
}

if (this._activeTab != -1) {
return this._activeTab;
}
return null;
}

var _setActiveTab = TabGroup.prototype.setActiveTab;

TabGroup.prototype.setActiveTab = function(taborindex) {
this._activeTab = taborindex;
if ( (this.currentState == this.state.opened) ||
(this.currentState == this.state.opening) ){
_setActiveTab.call(this,taborindex);
}
}


TabGroup.prototype.getTabs = function() {
return this._tabs;
Expand All @@ -136,6 +129,7 @@ exports.bootstrap = function(Titanium) {
}

Object.defineProperty(TabGroup.prototype, "tabs", { get: TabGroup.prototype.getTabs, set: TabGroup.prototype.setTabs });
Object.defineProperty(TabGroup.prototype, "activeTab", { get: TabGroup.prototype.getActiveTab, set: TabGroup.prototype.setActiveTab });

}