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

Timob 11604 3 0 x: Implementing SearchView #3732

Merged
merged 12 commits into from
Jan 21, 2013
2 changes: 1 addition & 1 deletion android/modules/ui/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:versionCode="1"
android:versionName="1.0">

<uses-sdk android:minSdkVersion="8" />
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="11" />

<application android:label="@string/app_name">
</application></manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2013 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.
*/
package ti.modules.titanium.ui;


import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.view.TiUIView;

import ti.modules.titanium.ui.android.AndroidModule;
import ti.modules.titanium.ui.widget.searchview.TiUISearchView;
import android.app.Activity;
import android.os.Build;

@Kroll.proxy(creatableInModule = AndroidModule.class, propertyAccessors = {
TiC.PROPERTY_ICONIFIED, TiC.PROPERTY_ICONIFIED_BY_DEFAULT, TiC.PROPERTY_HINT_TEXT, TiC.PROPERTY_VALUE })
public class SearchViewProxy extends TiViewProxy {

private static final String TAG = "SearchProxy";

public SearchViewProxy() {
super();
defaultValues.put(TiC.PROPERTY_ICONIFIED_BY_DEFAULT, true);
}

@Override
public TiUIView createView(Activity activity) {
if (Build.VERSION.SDK_INT >= TiC.API_LEVEL_HONEYCOMB) {
return new TiUISearchView(this);
}

Log.e(TAG, "SearchView is only supported on target API 11+");
return null;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.view.TiUIView;

import ti.modules.titanium.ui.SearchBarProxy;
import ti.modules.titanium.ui.TableViewProxy;
import ti.modules.titanium.ui.widget.searchbar.TiUISearchBar;
import ti.modules.titanium.ui.widget.searchview.TiUISearchView;
import ti.modules.titanium.ui.widget.tableview.TableViewModel;
import ti.modules.titanium.ui.widget.tableview.TiTableView;
import ti.modules.titanium.ui.widget.tableview.TiTableView.OnItemClickedListener;
Expand Down Expand Up @@ -110,41 +112,51 @@ public void processProperties(KrollDict d)
tableView.setOnItemLongClickListener(this);

if (d.containsKey(TiC.PROPERTY_SEARCH)) {
RelativeLayout layout = new RelativeLayout(proxy.getActivity());
layout.setGravity(Gravity.NO_GRAVITY);
layout.setPadding(0, 0, 0, 0);

TiViewProxy searchView = (TiViewProxy) d.get(TiC.PROPERTY_SEARCH);
TiUISearchBar searchBar = (TiUISearchBar)searchView.getOrCreateView();
searchBar.setOnSearchChangeListener(tableView);
searchBar.getNativeView().setId(102);

RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
p.addRule(RelativeLayout.ALIGN_PARENT_TOP);
p.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
p.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

TiDimension rawHeight;
if (searchView.hasProperty("height")) {
rawHeight = TiConvert.toTiDimension(searchView.getProperty("height"), 0);
TiUIView search = searchView.getOrCreateView();
if (searchView instanceof SearchBarProxy) {
((TiUISearchBar)search).setOnSearchChangeListener(tableView);
} else {
((TiUISearchView)search).setOnSearchChangeListener(tableView);
}
if (!(d.containsKey(TiC.PROPERTY_SEARCH_AS_CHILD) && !TiConvert.toBoolean(d.get(TiC.PROPERTY_SEARCH_AS_CHILD)))) {


search.getNativeView().setId(102);

RelativeLayout layout = new RelativeLayout(proxy.getActivity());
layout.setGravity(Gravity.NO_GRAVITY);
layout.setPadding(0, 0, 0, 0);

RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
p.addRule(RelativeLayout.ALIGN_PARENT_TOP);
p.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
p.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

TiDimension rawHeight;
if (searchView.hasProperty("height")) {
rawHeight = TiConvert.toTiDimension(searchView.getProperty("height"), 0);
} else {
rawHeight = TiConvert.toTiDimension("52dp", 0);
}
p.height = rawHeight.getAsPixels(layout);

layout.addView(search.getNativeView(), p);

p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
p.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
p.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
p.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
p.addRule(RelativeLayout.BELOW, 102);
layout.addView(tableView, p);
setNativeView(layout);
} else {
rawHeight = TiConvert.toTiDimension("52dp", 0);
setNativeView(tableView);
}
p.height = rawHeight.getAsPixels(layout);

layout.addView(searchBar.getNativeView(), p);

p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
p.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
p.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
p.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
p.addRule(RelativeLayout.BELOW, 102);
layout.addView(tableView, p);
setNativeView(layout);
} else {
setNativeView(tableView);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2013 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.
*/

package ti.modules.titanium.ui.widget.searchview;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiUIHelper;
import org.appcelerator.titanium.view.TiUIView;

import ti.modules.titanium.ui.widget.searchbar.TiUISearchBar.OnSearchChangeListener;
import android.widget.SearchView;

public class TiUISearchView extends TiUIView implements SearchView.OnQueryTextListener, SearchView.OnCloseListener {
private SearchView searchView;

public static final String TAG = "SearchView";

protected OnSearchChangeListener searchChangeListener;

public TiUISearchView(TiViewProxy proxy) {
super(proxy);

searchView = new SearchView(proxy.getActivity());
searchView.setOnQueryTextListener(this);
searchView.setOnCloseListener(this);
searchView.setOnQueryTextFocusChangeListener(this);

setNativeView(searchView);

}

@Override
public void processProperties(KrollDict props) {
super.processProperties(props);

// Check if the hint text is specified when the view is created.
if (props.containsKey(TiC.PROPERTY_HINT_TEXT)) {
searchView.setQueryHint(props.getString(TiC.PROPERTY_HINT_TEXT));
}
if (props.containsKey(TiC.PROPERTY_VALUE)) {
searchView.setQuery(props.getString(TiC.PROPERTY_VALUE), false);
}
if (props.containsKey(TiC.PROPERTY_ICONIFIED)) {
searchView.setIconified(props.getBoolean(TiC.PROPERTY_ICONIFIED));
}
if (props.containsKey(TiC.PROPERTY_ICONIFIED_BY_DEFAULT)) {
searchView.setIconifiedByDefault(props.getBoolean(TiC.PROPERTY_ICONIFIED_BY_DEFAULT));
}
if (props.containsKey(TiC.PROPERTY_SUBMIT_ENABLED)) {
searchView.setSubmitButtonEnabled((props.getBoolean(TiC.PROPERTY_SUBMIT_ENABLED)));
}
}

@Override
public void propertyChanged(String key, Object oldValue, Object newValue, KrollProxy proxy) {

if (key.equals(TiC.PROPERTY_HINT_TEXT)) {
searchView.setQueryHint((String) newValue);
} else if (key.equals(TiC.PROPERTY_VALUE)) {
searchView.setQuery((String) newValue, false);
} else if (key.equals(TiC.PROPERTY_ICONIFIED)) {
searchView.setIconified(TiConvert.toBoolean(newValue));
} else if (key.equals(TiC.PROPERTY_ICONIFIED_BY_DEFAULT)) {
searchView.setIconifiedByDefault(TiConvert.toBoolean(newValue));
} else if (key.equals(TiC.PROPERTY_SUBMIT_ENABLED)) {
searchView.setSubmitButtonEnabled(TiConvert.toBoolean(newValue));
} else {
super.propertyChanged(key, oldValue, newValue, proxy);
}
}

@Override
public boolean onClose() {
proxy.fireEvent(TiC.EVENT_CANCEL, null);
return false;
}

@Override
public boolean onQueryTextChange(String query) {
proxy.setProperty(TiC.PROPERTY_VALUE, query);
if (searchChangeListener != null) {
searchChangeListener.filterBy(query);
}
proxy.fireEvent(TiC.EVENT_CHANGE, null);
return false;
}

@Override
public boolean onQueryTextSubmit(String query) {
TiUIHelper.showSoftKeyboard(nativeView, false);
proxy.fireEvent(TiC.EVENT_SUBMIT, null);
return false;
}

public void setOnSearchChangeListener(OnSearchChangeListener listener) {
searchChangeListener = listener;
}



}
26 changes: 26 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/TiC.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,11 @@ public class TiC
* @module.api
*/
public static final String EVENT_STOP = "stop";

/**
* @module.api
*/
public static final String EVENT_SUBMIT = "submit";

/**
* @module.api
Expand Down Expand Up @@ -1167,6 +1172,17 @@ public class TiC
* @module.api
*/
public static final String PROPERTY_ICON_LEVEL = "iconLevel";

/**
* @module.api
*/
public static final String PROPERTY_ICONIFIED = "iconified";

/**
* @module.api
*/
public static final String PROPERTY_ICONIFIED_BY_DEFAULT = "iconifiedByDefault";


/**
* @module.api
Expand Down Expand Up @@ -1690,6 +1706,11 @@ public class TiC
* @module.api
*/
public static final String PROPERTY_SEARCH = "search";

/**
* @module.api
*/
public static final String PROPERTY_SEARCH_AS_CHILD = "searchAsChild";

/**
* @module.api
Expand Down Expand Up @@ -1826,6 +1847,11 @@ public class TiC
*/
public static final String PROPERTY_STYLE = "style";

/**
* @module.api
*/
public static final String PROPERTY_SUBMIT_ENABLED = "submitEnabled";

/**
* @module.api
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.view.TiUIView;

import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

@Kroll.proxy
public class MenuProxy extends KrollProxy
Expand Down Expand Up @@ -139,7 +142,20 @@ public MenuItemProxy handleAdd(KrollDict d)
}

if (d.containsKey(TiC.PROPERTY_ACTION_VIEW)) {
mip.setActionView(d.get(TiC.PROPERTY_ACTION_VIEW));
//check if view has a parent. If not, add it as action view. Otherwise, log error.
Object viewProxy = d.get(TiC.PROPERTY_ACTION_VIEW);
if (viewProxy instanceof TiViewProxy) {
TiUIView view = ((TiViewProxy)viewProxy).peekView();
if (view != null) {
View nativeView = view.getNativeView();
ViewGroup viewParent = (ViewGroup)nativeView.getParent();
if (viewParent != null) {
Log.e(TAG, "View already has a parent. Can't add it as an action view");
} else {
mip.setActionView(viewProxy);
}
}
}
}
if (d.containsKey(TiC.PROPERTY_CHECKABLE)) {
mip.setCheckable(TiConvert.toBoolean(d, TiC.PROPERTY_CHECKABLE));
Expand Down