Skip to content

Commit

Permalink
fix(android): amend TabGroup selected tab (#12695)
Browse files Browse the repository at this point in the history
Fixes TIMOB-28404
  • Loading branch information
build committed Apr 7, 2021
1 parent aa7a8c2 commit c08379b
Showing 1 changed file with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class TabGroupProxy extends TiWindowProxy implements TiActivityWindow

private ArrayList<TabProxy> tabs = new ArrayList<>();
private WeakReference<AppCompatActivity> tabGroupActivity = new WeakReference<>(null);
private TabProxy selectedTab;
private Object selectedTab; // NOTE: Can be TabProxy or Number
private String tabGroupTitle = null;
private static int id_toolbar;

Expand Down Expand Up @@ -128,7 +128,7 @@ public void removeTab(TabProxy tab)
}

@Kroll.getProperty
public TabProxy getActiveTab()
public Object getActiveTab()
{
//selectedTab may not be set when user queries activeTab, so we return
//the first tab (default selected tab) if it exists.
Expand All @@ -144,8 +144,14 @@ public TabProxy getActiveTab()
@Kroll.setProperty
public void setActiveTab(Object tabOrIndex)
{
TabProxy tab = parseTab(tabOrIndex);
if (view == null) {

// Pre-select tab if TabGroup not open.
selectedTab = tabOrIndex;
}

// Attempt to parse tab.
TabProxy tab = parseTab(tabOrIndex);
if (tab == null) {
return;
}
Expand All @@ -157,7 +163,6 @@ public void setActiveTab(Object tabOrIndex)
// called to fire events and update the active tab.
tabGroup.selectTab(tab);
}
selectedTab = tab;
}

@Kroll.getProperty(name = "activity")
Expand Down Expand Up @@ -390,12 +395,15 @@ protected void handlePostOpen()
}
}

TabProxy activeTab = getActiveTab();
Object activeTab = getActiveTab();
if (activeTab != 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;
TabProxy tab = parseTab(activeTab);
if (tab != null) {
tg.selectTab(tab);
selectedTab = tab;
}
}

// Selected tab should have been focused by now.
Expand Down Expand Up @@ -454,7 +462,8 @@ public void onWindowFocusChange(boolean focused)
}
isFocused = focused;

if (selectedTab == null) {
TabProxy tab = parseTab(selectedTab);
if (tab == null) {
// If no tab is selected fall back to the default behavior.
super.onWindowFocusChange(focused);
return;
Expand All @@ -464,7 +473,7 @@ public void onWindowFocusChange(boolean focused)
// the currently selected tab. No UI state change is required
// since no tab selection actually occurred. This should only
// happen if the activity is paused or the window stack changed.
selectedTab.onFocusChanged(focused, null);
tab.onFocusChanged(focused, null);
}

public void onTabSelected(int position)
Expand All @@ -479,17 +488,17 @@ public void onTabSelected(int position)
*/
public void onTabSelected(TabProxy tabProxy)
{
TabProxy previousSelectedTab = selectedTab;
TabProxy previousSelectedTab = parseTab(selectedTab);
selectedTab = tabProxy;

// Focus event data which will be dispatched to the selected tab.
// The 'source' of these events will always be the tab being focused.
KrollDict focusEventData = new KrollDict();
focusEventData.put(TiC.EVENT_PROPERTY_SOURCE, selectedTab);
focusEventData.put(TiC.EVENT_PROPERTY_SOURCE, tabProxy);
focusEventData.put(TiC.EVENT_PROPERTY_PREVIOUS_TAB, previousSelectedTab);
focusEventData.put(TiC.EVENT_PROPERTY_PREVIOUS_INDEX, tabs.indexOf(previousSelectedTab));
focusEventData.put(TiC.EVENT_PROPERTY_TAB, selectedTab);
focusEventData.put(TiC.EVENT_PROPERTY_INDEX, tabs.indexOf(selectedTab));
focusEventData.put(TiC.EVENT_PROPERTY_TAB, tabProxy);
focusEventData.put(TiC.EVENT_PROPERTY_INDEX, tabs.indexOf(tabProxy));

// We cannot modify event data after firing an event with it.
// To change the 'source' to the previously selected tab we must clone it.
Expand All @@ -502,8 +511,8 @@ public void onTabSelected(TabProxy tabProxy)
previousSelectedTab.onSelectionChanged(false);
previousSelectedTab.onFocusChanged(false, blurEventData);
}
selectedTab.onSelectionChanged(true);
selectedTab.onFocusChanged(true, focusEventData);
tabProxy.onSelectionChanged(true);
tabProxy.onFocusChanged(true, focusEventData);

tabProxy.fireEvent(TiC.EVENT_SELECTED, null, false);
}
Expand Down

0 comments on commit c08379b

Please sign in to comment.