Skip to content

Commit

Permalink
feat(android): add support for TabGroup in NavigationWindow (#11166)
Browse files Browse the repository at this point in the history
* feat(android): add support for TabGroup in NavigationWindow

Allows usage of a TabGroup as a root or a child view in NavigationWindow

* feat(android): add the support through openWindow method

* feat(android): fix support for TabGroup in NavigationWindow

* feat(android): fix bug in popToRootWindow

remove all but the root window from the array
  • Loading branch information
ypbnv authored and ssekhri committed Oct 29, 2019
1 parent c3806d0 commit a886ccd
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 16 deletions.
Expand Up @@ -8,6 +8,7 @@

import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.proxy.TiWindowProxy;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -17,7 +18,7 @@ public class NavigationWindowProxy extends WindowProxy
{
private static final String TAG = "NavigationWindowProxy";

private List<WindowProxy> windows = new ArrayList<>();
private List<TiWindowProxy> windows = new ArrayList<>();

public NavigationWindowProxy()
{
Expand All @@ -33,8 +34,10 @@ public void open(@Kroll.argument(optional = true) Object arg)
// FIXME: Shouldn't this complain/blow up if window isn't specified?
if (getProperties().containsKeyAndNotNull(TiC.PROPERTY_WINDOW)) {
opened = true;
WindowProxy window = (WindowProxy) getProperties().get(TiC.PROPERTY_WINDOW);
openWindow(window, arg);
Object rootView = getProperties().get(TiC.PROPERTY_WINDOW);
if (rootView instanceof WindowProxy || rootView instanceof TabGroupProxy) {
openWindow(rootView, arg);
}
return;
}
super.open(arg);
Expand All @@ -47,8 +50,9 @@ public void popToRootWindow(@Kroll.argument(optional = true) Object arg)
{
// Keep first "root" window
for (int i = windows.size() - 1; i > 0; i--) {
WindowProxy window = windows.get(i);
closeWindow(window, arg);
TiWindowProxy window = windows.get(i);
window.close(arg);
windows.remove(window);
}
}

Expand All @@ -65,27 +69,44 @@ public void close(@Kroll.argument(optional = true) Object arg)

// clang-format off
@Kroll.method
public void openWindow(WindowProxy window, @Kroll.argument(optional = true) Object arg)
public void openWindow(Object childToOpen, @Kroll.argument(optional = true) Object arg)
// clang-format on
{
if (!opened) {
open(null);
}
window.setNavigationWindow(this);
windows.add(window);
window.open(arg);

// Guard for types different from Window and TabGroup
if (!(childToOpen instanceof TiWindowProxy)) {
return;
}
windows.add(((TiWindowProxy) childToOpen));
((TiWindowProxy) childToOpen).setNavigationWindow(this);
if (childToOpen instanceof WindowProxy) {
((WindowProxy) childToOpen).open(arg);
} else if (childToOpen instanceof TabGroupProxy) {
// tabgroup.js deals with passing the tabs from the creation dictionary to the native setTabs method.
// In this case we need to do it manually since the JS "open()" does not get called.
((TabGroupProxy) childToOpen).callPropertySync(TiC.PROPERTY_OPEN, new Object[] { arg });
}
}

// clang-format off
@Kroll.method
public void closeWindow(WindowProxy window, @Kroll.argument(optional = true) Object arg)
public void closeWindow(Object childToClose, @Kroll.argument(optional = true) Object arg)
// clang-format on
{
// TODO: If they try to close root window, yell at them:
// DebugLog(@"[ERROR] Can not close the root window of the NavigationWindow. Close the NavigationWindow instead.");
windows.remove(window);
window.close(arg);
window.setNavigationWindow(null);

// Guard for types different from Window and TabGroup
if (!(childToClose instanceof TiWindowProxy)) {
return;
}

windows.remove(childToClose);
((TiWindowProxy) childToClose).close(arg);
((TiWindowProxy) childToClose).setNavigationWindow(null);
}

@Override
Expand All @@ -94,7 +115,7 @@ public String getApiName()
return "Ti.UI.NavigationWindow";
}

public WindowProxy getRootWindowProxy()
public TiWindowProxy getRootTiWindowProxy()
{
if (!windows.isEmpty()) {
return windows.get(0);
Expand Down
Expand Up @@ -24,6 +24,7 @@
import org.appcelerator.titanium.TiRootActivity;
import org.appcelerator.titanium.proxy.TiWindowProxy;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiRHelper;
import org.appcelerator.titanium.util.TiUIHelper;

import ti.modules.titanium.ui.android.AndroidModule;
Expand All @@ -37,6 +38,8 @@
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;

// clang-format off
@Kroll.proxy(creatableInModule = UIModule.class,
Expand Down Expand Up @@ -68,6 +71,7 @@ public class TabGroupProxy extends TiWindowProxy implements TiActivityWindow
private TabProxy selectedTab;
private String tabGroupTitle = null;
private boolean isFocused;
private static int id_toolbar;

public TabGroupProxy()
{
Expand Down Expand Up @@ -418,6 +422,30 @@ public void windowCreated(TiBaseActivity activity, Bundle savedInstanceState)
}
setModelListener(view);

// add toolbar to NavigationWindow
if (this.getNavigationWindow() != null) {
if (activity.getSupportActionBar() == null) {
try {
if (id_toolbar == 0) {
id_toolbar = TiRHelper.getResource("layout.titanium_ui_toolbar");
}
} catch (TiRHelper.ResourceNotFoundException e) {
android.util.Log.e(TAG, "XML resources could not be found!!!");
}
LayoutInflater inflater = LayoutInflater.from(activity);
Toolbar toolbar = (Toolbar) inflater.inflate(id_toolbar, null, false);

activity.setSupportActionBar(toolbar);
}
activity.getSupportActionBar().setHomeButtonEnabled(
!getProperties().optBoolean(TiC.PROPERTY_HIDES_BACK_BUTTON, false));
// Get a reference to the root window in the NavigationWindow.
TiWindowProxy rootTiWindowProxy =
((NavigationWindowProxy) this.getNavigationWindow()).getRootTiWindowProxy();
// If the root window matches this window do not show the Up navigation button.
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(rootTiWindowProxy != this);
}

// Need to handle the cached activity proxy properties in the JS side.
callPropertySync(PROPERTY_POST_TAB_GROUP_CREATED, null);
}
Expand Down
Expand Up @@ -309,9 +309,10 @@ public void windowCreated(TiBaseActivity activity, Bundle savedInstanceState)
activity.getSupportActionBar().setHomeButtonEnabled(
!getProperties().optBoolean(TiC.PROPERTY_HIDES_BACK_BUTTON, false));
// Get a reference to the root window in the NavigationWindow.
WindowProxy rootWindowProxy = ((NavigationWindowProxy) this.getNavigationWindow()).getRootWindowProxy();
TiWindowProxy rootTiWindowProxy =
((NavigationWindowProxy) this.getNavigationWindow()).getRootTiWindowProxy();
// If the root window matches this window do not show the Up navigation button.
activityProxy.getActionBar().setDisplayHomeAsUp(rootWindowProxy != this);
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(rootTiWindowProxy != this);
}

// Handle barColor property.
Expand Down
5 changes: 5 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/TiC.java
Expand Up @@ -2389,6 +2389,11 @@ public class TiC
*/
public static final String PROPERTY_OKID = "okid";

/**
* @module.api
*/
public static final String PROPERTY_OPEN = "open";

/**
* @module.api
*/
Expand Down
49 changes: 49 additions & 0 deletions tests/Resources/ti.ui.navigationwindow.addontest.js
@@ -0,0 +1,49 @@
/*
* Appcelerator Titanium Mobile
* Copyright (c) 2017-Present by Axway. 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 */
/* eslint no-unused-expressions: "off" */
'use strict';
var should = require('./utilities/assertions');

describe.windowsMissing('Titanium.UI.NavigationWindow', function () {

function createTab(title) {
var windowForTab = Ti.UI.createWindow({ title: title });
var tab = Ti.UI.createTab({
title: title,
window: windowForTab
});
return tab;
}

it('have TabGroup as a root window', function () {
var tabGroup = Ti.UI.createTabGroup({ title: 'TabGroup',
tabs: [ createTab('Tab 1'),
createTab('Tab 2'),
createTab('Tab 3') ]
});
var navigationWindow = Ti.UI.createNavigationWindow({
window: tabGroup,
});
navigationWindow.open();
});

it('have a TabGroup child in stack', function () {
var rootWin = Ti.UI.createWindow(),
navigationWindow = Ti.UI.createNavigationWindow({
window: rootWin
}),
tabGroup = Ti.UI.createTabGroup({ title: 'TabGroup',
tabs: [ createTab('Tab 1'),
createTab('Tab 2'),
createTab('Tab 3') ]
});
navigationWindow.open();
navigationWindow.openWindow(tabGroup);
});

});

0 comments on commit a886ccd

Please sign in to comment.