Skip to content

Commit

Permalink
feat(android): sync tab and actionbar title (#12499)
Browse files Browse the repository at this point in the history
* feat(android): sync tab and actionbar title

* use window title

* update

* fix truncated title

* doc

* update docs and variable name

* fix linter
  • Loading branch information
m1ga committed Mar 21, 2022
1 parent c404163 commit 6972587
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
Expand Up @@ -45,6 +45,7 @@
TiC.PROPERTY_TABS_BACKGROUND_COLOR,
TiC.PROPERTY_TABS_BACKGROUND_SELECTED_COLOR,
TiC.PROPERTY_SWIPEABLE,
TiC.PROPERTY_AUTO_TAB_TITLE,
TiC.PROPERTY_EXIT_ON_CLOSE,
TiC.PROPERTY_SMOOTH_SCROLL_ON_TAB_CLICK
})
Expand All @@ -68,11 +69,13 @@ public class TabGroupProxy extends TiWindowProxy implements TiActivityWindow
private Object selectedTab; // NOTE: Can be TabProxy or Number
private String tabGroupTitle = null;
private static int id_toolbar;
private boolean autoTabTitle = false;

public TabGroupProxy()
{
super();
defaultValues.put(TiC.PROPERTY_SWIPEABLE, true);
defaultValues.put(TiC.PROPERTY_AUTO_TAB_TITLE, autoTabTitle);
defaultValues.put(TiC.PROPERTY_SMOOTH_SCROLL_ON_TAB_CLICK, true);
}

Expand Down Expand Up @@ -246,6 +249,9 @@ public void handleCreationDict(KrollDict options)
}
}

if (options.containsKeyAndNotNull(TiC.PROPERTY_AUTO_TAB_TITLE)) {
autoTabTitle = options.getBoolean(TiC.PROPERTY_AUTO_TAB_TITLE);
}
if (options.containsKeyAndNotNull(TiC.PROPERTY_TABS)) {
setTabs(options.get(TiC.PROPERTY_TABS));
}
Expand Down Expand Up @@ -277,7 +283,11 @@ 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);
if (autoTabTitle) {
((TiUIAbstractTabGroup) view).updateTitle("");
} else {
((TiUIAbstractTabGroup) view).updateTitle(title);
}
} else {
// If the native view is not yet drawn save the value to be passed during creation.
this.tabGroupTitle = title;
Expand Down Expand Up @@ -336,7 +346,11 @@ public void windowCreated(TiBaseActivity activity, Bundle savedInstanceState)
}
// 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);
if (autoTabTitle) {
((TiUIAbstractTabGroup) view).updateTitle("");
} else {
((TiUIAbstractTabGroup) view).updateTitle(this.tabGroupTitle);
}
}
setModelListener(view);

Expand Down Expand Up @@ -381,6 +395,15 @@ public void onWindowActivityCreated()
// Finish open handling by loading proxy settings.
handlePostOpen();

if (autoTabTitle) {
// expand title to fix truncated title
TiUIAbstractTabGroup tabGroup = (TiUIAbstractTabGroup) view;
tabGroup.updateTitle(tabs.get(0).getProperty(TiC.PROPERTY_TITLE).toString() + " "
+ tabs.get(0).getProperty(TiC.PROPERTY_TITLE).toString());

// clear it again
tabGroup.updateTitle("");
}
super.onWindowActivityCreated();
}

Expand All @@ -394,18 +417,18 @@ protected void handlePostOpen()
}

// Load any tabs added before the tab group opened.
TiUIAbstractTabGroup tg = (TiUIAbstractTabGroup) view;
TiUIAbstractTabGroup tabGroup = (TiUIAbstractTabGroup) view;
for (TabProxy tab : tabs) {
if (tab != null) {
tg.addTab(tab);
tabGroup.addTab(tab);
}
}

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

Expand Down
Expand Up @@ -158,14 +158,16 @@ public abstract class TiUIAbstractTabGroup extends TiUIView
// endregion

// region private fields
private boolean autoTabTitle = false;
private int lastTab = -1;
private AtomicLong fragmentIdGenerator = new AtomicLong();
private ArrayList<Long> tabFragmentIDs = new ArrayList<Long>();
protected ArrayList<TiUITab> tabs = new ArrayList<TiUITab>();
private final boolean isUsingSolidTitaniumTheme;
private int colorBackgroundInt;
private int colorSurfaceInt;
private int colorPrimaryInt;
private int colorOnSurfaceInt;
private final AtomicLong fragmentIdGenerator = new AtomicLong();
private final ArrayList<Long> tabFragmentIDs = new ArrayList<>();
protected ArrayList<TiUITab> tabs = new ArrayList<>();
// endregion

public TiUIAbstractTabGroup(final TabGroupProxy proxy, TiBaseActivity activity)
Expand Down Expand Up @@ -422,6 +424,12 @@ public void selectTab(int tabIndex)
@Override
public void onPageScrolled(int i, float v, int i1)
{
if (autoTabTitle && i != lastTab) {
if (tabs.get(i).getWindowProxy() != null) {
updateTitle(tabs.get(i).getWindowProxy().getProperty(TiC.PROPERTY_TITLE).toString());
}
lastTab = i;
}
}

@Override
Expand Down Expand Up @@ -470,6 +478,9 @@ public void processProperties(KrollDict d)
if (d.containsKey(TiC.PROPERTY_SWIPEABLE)) {
this.swipeable = d.getBoolean(TiC.PROPERTY_SWIPEABLE);
}
if (d.containsKey(TiC.PROPERTY_AUTO_TAB_TITLE)) {
this.autoTabTitle = d.getBoolean(TiC.PROPERTY_AUTO_TAB_TITLE);
}
if (d.containsKey(TiC.PROPERTY_SMOOTH_SCROLL_ON_TAB_CLICK)) {
this.smoothScrollOnTabClick = d.getBoolean(TiC.PROPERTY_SMOOTH_SCROLL_ON_TAB_CLICK);
}
Expand All @@ -488,6 +499,8 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
this.swipeable = TiConvert.toBoolean(newValue);
} else if (key.equals(TiC.PROPERTY_SMOOTH_SCROLL_ON_TAB_CLICK)) {
this.smoothScrollOnTabClick = TiConvert.toBoolean(newValue);
} else if (key.equals(TiC.PROPERTY_AUTO_TAB_TITLE)) {
this.autoTabTitle = TiConvert.toBoolean(newValue);
} else if (key.equals(TiC.PROPERTY_TABS_BACKGROUND_COLOR)) {
for (TiUITab tabView : tabs) {
updateTabBackgroundDrawable(tabs.indexOf(tabView));
Expand Down
Expand Up @@ -53,7 +53,7 @@ public View getContentView()
return windowProxy.getOrCreateView().getOuterView();
}

private TiWindowProxy getWindowProxy()
public TiWindowProxy getWindowProxy()
{
if (proxy != null) {
Object windowProxy = proxy.getProperty(TiC.PROPERTY_WINDOW);
Expand Down
5 changes: 5 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/TiC.java
Expand Up @@ -268,6 +268,11 @@ public class TiC
public static final String PROPERTY_AUTOROTATE = "autorotate";
public static final String PROPERTY_AUTO_REDIRECT = "autoRedirect";
public static final String PROPERTY_AUTO_ENCODE_URL = "autoEncodeUrl";
public static final String PROPERTY_AUTO_TAB_TITLE = "autoTabTitle";

/**
* @module.api
*/
public static final String PROPERTY_BACKGROUND_COLOR = "backgroundColor";
public static final String PROPERTY_BACKGROUND_DISABLED_COLOR = "backgroundDisabledColor";
public static final String PROPERTY_BACKGROUND_DISABLED_IMAGE = "backgroundDisabledImage";
Expand Down
7 changes: 7 additions & 0 deletions apidoc/Titanium/UI/TabGroup.yml
Expand Up @@ -324,6 +324,13 @@ properties:
default: true
platforms: [iphone, ipad, macos]

- name: autoTabTitle
summary: If set to `true` it will automatically set the actionbar title to the current tabs window title.
type: Boolean
default: false
since: 10.2.0
platforms: [android]

- name: barColor
summary: |
Default navigation bar color (typically for the **More** tab), as a color name or hex triplet.
Expand Down

0 comments on commit 6972587

Please sign in to comment.