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

fix(android): Fix a regressions of changing TabGroup's title after it was created. (8_0_X) #10779

Merged
merged 5 commits into from
Mar 25, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -35,6 +35,7 @@
import android.os.Build;
import android.os.Message;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;

// clang-format off
Expand Down Expand Up @@ -65,6 +66,7 @@ public class TabGroupProxy extends TiWindowProxy implements TiActivityWindow
private ArrayList<TabProxy> tabs = new ArrayList<TabProxy>();
private WeakReference<AppCompatActivity> tabGroupActivity;
private TabProxy selectedTab;
private String tabGroupTitle = null;
private boolean isFocused;

public TabGroupProxy()
Expand Down Expand Up @@ -348,6 +350,36 @@ public TabProxy getActiveTab()
}
}

@Kroll.getProperty
public String getTitle()
{
// If the native view is drawn get the title value from the SupportActionBar
if (view != null) {
if (getActivity() != null) {
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null) {
return actionBar.getTitle().toString();
}
}
return null;
} else {
// If the native view is not drawn return tha latest String saved as a title
return tabGroupTitle;
}
}

@Kroll.setProperty
public void setTitle(String title)
{
// If the native view is drawn directly set the String as a title for the SupportActionBar.
if (view != null) {
((TiUIAbstractTabGroup) view).updateTitle(title);
} else {
// If the native view is not yet drawn save the value to be passed during creation.
this.tabGroupTitle = title;
}
}

private TabProxy handleGetActiveTab()
{
//selectedTab may not be set when user queries activeTab, so we return
Expand Down Expand Up @@ -411,7 +443,10 @@ public void windowCreated(TiBaseActivity activity, Bundle savedInstanceState)
} else {
view = new TiUIBottomNavigationTabGroup(this, activity);
}

// If we have set a title before the creation of the native view, set it now.
if (this.tabGroupTitle != null) {
((TiUIAbstractTabGroup) view).updateTitle(this.tabGroupTitle);
}
setModelListener(view);

handlePostOpen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -386,6 +387,16 @@ public TabProxy getSelectedTab()
return ((TabGroupProxy) getProxy()).getTabList().get(this.tabGroupViewPager.getCurrentItem());
}

public void updateTitle(String title)
{
// Get a reference to the ActionBar
ActionBar actionBar = ((AppCompatActivity) proxy.getActivity()).getSupportActionBar();
// Guard for trying to update the ActionBar's title when a theme without one is used.
if (actionBar != null) {
actionBar.setTitle(title);
}
}

/**
* Implemenation of the FragmentPagerAdapter
*/
Expand Down
32 changes: 32 additions & 0 deletions tests/Resources/ti.ui.tabgroup.addontest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Appcelerator Titanium Mobile
* Copyright (c) 2011-Present by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* eslint-env mocha */
/* global Ti */
/* eslint no-unused-expressions: "off" */
'use strict';
const should = require('./utilities/assertions'); // eslint-disable-line no-unused-vars

// skipping many test on Windows due to lack of event firing, see https://jira.appcelerator.org/browse/TIMOB-26690
describe('Titanium.UI.TabGroup', () => {

it('title after drawing the TabGroup', () => {
var winA = Ti.UI.createWindow(),
winB = Ti.UI.createWindow(),
tabA = Ti.UI.createTab({ title: 'titleA', window: winA }),
tabB = Ti.UI.createTab({ title: 'titleB', window: winB }),
tabGroup = Ti.UI.createTabGroup({ tabs: [ tabA, tabB ] });
tabGroup.addEventListener('open', () => {
tabGroup.title = 'newTitle';
tabGroup.setActiveTab(tabB);
});
tabB.addEventListener('selected', () => {
should(tabGroup.title).be.a.String;
should(tabGroup.title).eql('newTitle');
});
});

});