Skip to content

Commit

Permalink
fix(android): remove getter and setter methods from TabGroup (#12661)
Browse files Browse the repository at this point in the history
Fixes TIMOB-28404
  • Loading branch information
garymathews authored and sgtcoolguy committed Apr 5, 2021
1 parent 61541c1 commit d13c884
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 304 deletions.
207 changes: 58 additions & 149 deletions android/modules/ui/src/java/ti/modules/titanium/ui/TabGroupProxy.java
@@ -1,6 +1,6 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2019 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2009-2021 by Axway, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
Expand All @@ -11,9 +11,7 @@

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.AsyncResult;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiMessenger;
import org.appcelerator.titanium.TiActivity;
import org.appcelerator.titanium.TiActivityWindow;
import org.appcelerator.titanium.TiActivityWindows;
Expand All @@ -22,6 +20,7 @@
import org.appcelerator.titanium.TiBlob;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiRootActivity;
import org.appcelerator.titanium.proxy.ActivityProxy;
import org.appcelerator.titanium.proxy.TiWindowProxy;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiRHelper;
Expand All @@ -34,7 +33,6 @@

import android.app.Activity;
import android.content.Intent;
import android.os.Message;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
Expand Down Expand Up @@ -65,8 +63,8 @@ public class TabGroupProxy extends TiWindowProxy implements TiActivityWindow

protected static final int MSG_LAST_ID = MSG_FIRST_ID + 999;

private ArrayList<TabProxy> tabs = new ArrayList<TabProxy>();
private WeakReference<AppCompatActivity> tabGroupActivity;
private ArrayList<TabProxy> tabs = new ArrayList<>();
private WeakReference<AppCompatActivity> tabGroupActivity = new WeakReference<>(null);
private TabProxy selectedTab;
private String tabGroupTitle = null;
private static int id_toolbar;
Expand All @@ -78,69 +76,13 @@ public TabGroupProxy()
defaultValues.put(TiC.PROPERTY_SMOOTH_SCROLL_ON_TAB_CLICK, true);
}

@Override
public boolean handleMessage(Message msg)
{
switch (msg.what) {
case MSG_ADD_TAB: {
AsyncResult result = (AsyncResult) msg.obj;
handleAddTab((TabProxy) result.getArg());
result.setResult(null);
return true;
}
case MSG_REMOVE_TAB: {
AsyncResult result = (AsyncResult) msg.obj;
handleRemoveTab((TabProxy) result.getArg());
result.setResult(null);
return true;
}
case MSG_SET_ACTIVE_TAB: {
AsyncResult result = (AsyncResult) msg.obj;
handleSetActiveTab((TabProxy) result.getArg());
result.setResult(null);
return true;
}
case MSG_GET_ACTIVE_TAB: {
AsyncResult result = (AsyncResult) msg.obj;
result.setResult(handleGetActiveTab());
return true;
}
case MSG_SET_TABS: {
AsyncResult result = (AsyncResult) msg.obj;
handleSetTabs(result.getArg());
result.setResult(null);
return true;
}
case MSG_DISABLE_TAB_NAVIGATION: {
AsyncResult result = (AsyncResult) msg.obj;
handleDisableTabNavigation(TiConvert.toBoolean(result.getArg()));
result.setResult(null);
return true;
}
default: {
return super.handleMessage(msg);
}
}
}

public int getTabIndex(TabProxy tabProxy)
{
return tabs.indexOf(tabProxy);
}

@Kroll.method
public void disableTabNavigation(boolean disable)
{
if (TiApplication.isUIThread()) {
handleDisableTabNavigation(disable);

return;
}

TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_DISABLE_TAB_NAVIGATION), disable);
}

private void handleDisableTabNavigation(boolean disable)
{
TiUIAbstractTabGroup tabGroup = (TiUIAbstractTabGroup) view;
if (tabGroup != null) {
Expand All @@ -155,20 +97,6 @@ public void addTab(TabProxy tab)
return;
}

if (TiApplication.isUIThread()) {
handleAddTab(tab);
return;
}

TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_ADD_TAB), tab);
}

private void handleAddTab(TabProxy tab)
{
if (tab == null) {
return;
}

// Set the tab's parent to this tab group.
// This allows for certain events to bubble up.
tab.setTabGroup(this);
Expand All @@ -183,18 +111,6 @@ private void handleAddTab(TabProxy tab)

@Kroll.method
public void removeTab(TabProxy tab)
{
if (TiApplication.isUIThread()) {
handleRemoveTab(tab);

} else {
TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_REMOVE_TAB), tab);
}

tab.setParent(null);
}

public void handleRemoveTab(TabProxy tab)
{
int indexToRemove = tabs.indexOf(tab);
// Guard for trying to remove a Tab that has not been added.
Expand All @@ -207,9 +123,25 @@ public void handleRemoveTab(TabProxy tab)
if (tabGroup != null) {
tabGroup.removeTabAt(indexToRemove);
}

tab.setParent(null);
}

@Kroll.method
@Kroll.getProperty
public TabProxy getActiveTab()
{
//selectedTab may not be set when user queries activeTab, so we return
//the first tab (default selected tab) if it exists.
if (selectedTab != null) {
return selectedTab;
} else if (tabs.size() > 0) {
return tabs.get(0);
} else {
return null;
}
}

@Kroll.setProperty
public void setActiveTab(Object tabOrIndex)
{
TabProxy tab = parseTab(tabOrIndex);
Expand All @@ -218,37 +150,49 @@ public void setActiveTab(Object tabOrIndex)
return;
}

if (TiApplication.isUIThread()) {
handleSetActiveTab(tab);

} else {
TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_SET_ACTIVE_TAB), tab);
}
}

protected void handleSetActiveTab(TabProxy tab)
{
TiUIAbstractTabGroup tabGroup = (TiUIAbstractTabGroup) view;
if (tabGroup != null) {
// Change the selected tab of the group.
// Once the change is completed onTabSelected() will be
// called to fire events and update the active tab.
tabGroup.selectTab(tab);
} else {
// Mark this tab to be selected when the tab group opens.
selectedTab = tab;
}
selectedTab = tab;
}

@Kroll.method
@Kroll.getProperty(name = "activity")
public ActivityProxy _getActivity()
{
final AppCompatActivity activity = tabGroupActivity.get();

if (activity instanceof TiBaseActivity) {
return ((TiBaseActivity) activity).getActivityProxy();
}
return null;
}

@Kroll.getProperty
public TabProxy[] getTabs()
{
return tabs.toArray(new TabProxy[tabs.size()]);
}

@Kroll.setProperty
public void setTabs(Object obj)
{
if (TiApplication.isUIThread()) {
handleSetTabs(obj);
return;
for (final TabProxy tab : getTabs()) {
removeTab(tab);
}
tabs.clear();

TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_SET_TABS), obj);
if (obj instanceof Object[]) {
Object[] objArray = (Object[]) obj;
for (Object tabProxy : objArray) {
if (tabProxy instanceof TabProxy) {
addTab((TabProxy) tabProxy);
}
}
}
}

private TabProxy parseTab(Object tabOrIndex)
Expand All @@ -274,23 +218,6 @@ private TabProxy parseTab(Object tabOrIndex)
return tab;
}

private void handleSetTabs(Object obj)
{
if (tabs != null) {
tabs.clear();
} else {
tabs = new ArrayList<TabProxy>();
}
if (obj instanceof Object[]) {
Object[] objArray = (Object[]) obj;
for (Object tabProxy : objArray) {
if (tabProxy instanceof TabProxy) {
handleAddTab((TabProxy) tabProxy);
}
}
}
}

@Override
public void handleCreationDict(KrollDict options)
{
Expand All @@ -307,17 +234,12 @@ public void handleCreationDict(KrollDict options)
Log.e(TAG, "Invalid orientationMode array. Must only contain orientation mode constants.");
}
}
}

@Kroll.method
public TabProxy getActiveTab()
{
if (TiApplication.isUIThread()) {
return handleGetActiveTab();

} else {
return (TabProxy) TiMessenger.sendBlockingMainMessage(
getMainHandler().obtainMessage(MSG_GET_ACTIVE_TAB, tab));
if (options.containsKeyAndNotNull(TiC.PROPERTY_TABS)) {
setTabs(options.get(TiC.PROPERTY_TABS));
}
if (options.containsKeyAndNotNull(TiC.PROPERTY_ACTIVE_TAB)) {
setActiveTab(options.get(TiC.PROPERTY_ACTIVE_TAB));
}
}

Expand Down Expand Up @@ -351,19 +273,6 @@ public void setTitle(String title)
}
}

private TabProxy handleGetActiveTab()
{
//selectedTab may not be set when user queries activeTab, so we return
//the first tab (default selected tab) if it exists.
if (selectedTab != null) {
return selectedTab;
} else if (tabs.size() > 0) {
return tabs.get(0);
} else {
return null;
}
}

@Override
protected void handleOpen(KrollDict options)
{
Expand Down Expand Up @@ -481,12 +390,12 @@ protected void handlePostOpen()
}
}

TabProxy activeTab = handleGetActiveTab();
TabProxy activeTab = getActiveTab();
if (activeTab != null) {
selectedTab = null;
// If tabHost's selected tab is same as the active tab, we need
// to invoke onTabSelected so focus/blur event fire appropriately
tg.selectTab(activeTab);
selectedTab = activeTab;
}

// Selected tab should have been focused by now.
Expand Down

0 comments on commit d13c884

Please sign in to comment.