|
@@ -1,156 +0,0 @@
|
1
|
|
-package com.reactnativenavigation.views;
|
2
|
|
-
|
3
|
|
-import android.app.Activity;
|
4
|
|
-import android.graphics.drawable.Drawable;
|
5
|
|
-import android.support.annotation.Nullable;
|
6
|
|
-import android.support.v4.view.MenuItemCompat;
|
7
|
|
-import android.support.v7.widget.SearchView;
|
8
|
|
-import android.support.v7.widget.Toolbar;
|
9
|
|
-import android.view.Menu;
|
10
|
|
-import android.view.MenuItem;
|
11
|
|
-import android.view.View;
|
12
|
|
-import android.view.ViewGroup;
|
13
|
|
-import android.widget.EditText;
|
14
|
|
-import android.widget.ImageButton;
|
15
|
|
-import android.widget.ImageView;
|
16
|
|
-
|
17
|
|
-import com.facebook.react.bridge.Arguments;
|
18
|
|
-import com.facebook.react.bridge.WritableMap;
|
19
|
|
-import com.reactnativenavigation.NavigationApplication;
|
20
|
|
-import com.reactnativenavigation.R;
|
21
|
|
-import com.reactnativenavigation.params.TitleBarButtonParams;
|
22
|
|
-import com.reactnativenavigation.utils.ReflectionUtils;
|
23
|
|
-import com.reactnativenavigation.utils.ViewUtils;
|
24
|
|
-
|
25
|
|
-class TitleBarSearchButton extends TitleBarButton implements SearchView.OnQueryTextListener, View.OnFocusChangeListener, View.OnClickListener {
|
26
|
|
- static final String BUTTON_ID = "searchView";
|
27
|
|
- private SearchView searchView;
|
28
|
|
-
|
29
|
|
- TitleBarSearchButton(Menu menu, View parent, TitleBarButtonParams buttonParams, @Nullable String navigatorEventId) {
|
30
|
|
- super(menu, parent, buttonParams, navigatorEventId);
|
31
|
|
- }
|
32
|
|
-
|
33
|
|
- MenuItem addToMenu(int index) {
|
34
|
|
- ((Activity) parent.getContext()).getMenuInflater().inflate(R.menu.search_item, menu);
|
35
|
|
- MenuItem item = menu.findItem(R.id.toolbar_action_search);
|
36
|
|
- item.setOnMenuItemClickListener(this);
|
37
|
|
- if (buttonParams.icon != null) {
|
38
|
|
- item.setIcon(buttonParams.icon);
|
39
|
|
- }
|
40
|
|
- searchView = (SearchView) MenuItemCompat.getActionView(item);
|
41
|
|
- searchView.setQueryHint(buttonParams.hint);
|
42
|
|
- searchView.setOnQueryTextFocusChangeListener(this);
|
43
|
|
- searchView.setOnQueryTextListener(this);
|
44
|
|
- searchView.setOnSearchClickListener(this);
|
45
|
|
- setColor();
|
46
|
|
- return item;
|
47
|
|
- }
|
48
|
|
-
|
49
|
|
- private void setColor() {
|
50
|
|
- EditText searchEditText = ViewUtils.findChildByClass(searchView, EditText.class);
|
51
|
|
- if (searchEditText != null) {
|
52
|
|
- if (buttonParams.color.hasColor()) {
|
53
|
|
- searchEditText.setTextColor(buttonParams.color.getColor());
|
54
|
|
- searchEditText.setHintTextColor(buttonParams.color.getColor());
|
55
|
|
- }
|
56
|
|
- colorCloseButton(searchEditText);
|
57
|
|
- setImagePlateColor();
|
58
|
|
- }
|
59
|
|
- }
|
60
|
|
-
|
61
|
|
- private void colorCloseButton(EditText searchEditText) {
|
62
|
|
- ViewUtils.performOnChildren((ViewGroup) searchEditText.getParent(), new ViewUtils.PerformOnViewTask() {
|
63
|
|
- @Override
|
64
|
|
- public void runOnView(View view) {
|
65
|
|
- if (view instanceof ImageView) {
|
66
|
|
- if (buttonParams.color.hasColor()) {
|
67
|
|
- ((ImageView) view).setColorFilter(buttonParams.color.getColor());
|
68
|
|
- }
|
69
|
|
- }
|
70
|
|
- }
|
71
|
|
- });
|
72
|
|
- }
|
73
|
|
-
|
74
|
|
- private void setImagePlateColor() {
|
75
|
|
- if (buttonParams.color.hasColor()) {
|
76
|
|
- Object mSearchPlate = ReflectionUtils.getDeclaredField(searchView, "mSearchPlate");
|
77
|
|
- if (mSearchPlate != null) {
|
78
|
|
- Drawable background = ((View) mSearchPlate).getBackground();
|
79
|
|
- if (background != null) {
|
80
|
|
- ViewUtils.tintDrawable(background, buttonParams.color.getColor(), true);
|
81
|
|
- }
|
82
|
|
- }
|
83
|
|
- }
|
84
|
|
- }
|
85
|
|
-
|
86
|
|
- @Override
|
87
|
|
- public boolean onMenuItemClick(MenuItem item) {
|
88
|
|
- setupBackButtonAfterSearchViewIsExpended();
|
89
|
|
- return false;
|
90
|
|
- }
|
91
|
|
-
|
92
|
|
- private void setupBackButtonAfterSearchViewIsExpended() {
|
93
|
|
- ViewUtils.runOnPreDraw(searchView, new Runnable() {
|
94
|
|
- @Override
|
95
|
|
- public void run() {
|
96
|
|
- Object backButton = ViewUtils.findChildByClass((ViewGroup) searchView.getParent(), ImageButton.class);
|
97
|
|
- if (backButton != null) {
|
98
|
|
- setBackButtonClickListener((View) backButton);
|
99
|
|
- colorBackButton((ImageView) backButton);
|
100
|
|
- }
|
101
|
|
- }
|
102
|
|
-
|
103
|
|
- private void colorBackButton(ImageView backButton) {
|
104
|
|
- if (buttonParams.color.hasColor()) {
|
105
|
|
- ViewUtils.tintDrawable(backButton.getDrawable(), buttonParams.color.getColor(), true);
|
106
|
|
- }
|
107
|
|
- }
|
108
|
|
-
|
109
|
|
- private void setBackButtonClickListener(View backButton) {
|
110
|
|
- backButton.setOnClickListener(new View.OnClickListener() {
|
111
|
|
- @Override
|
112
|
|
- public void onClick(View v) {
|
113
|
|
- ((Toolbar) searchView.getParent()).collapseActionView();
|
114
|
|
- sendEvent("searchViewHidden");
|
115
|
|
- }
|
116
|
|
- });
|
117
|
|
- }
|
118
|
|
- });
|
119
|
|
- }
|
120
|
|
-
|
121
|
|
- @Override
|
122
|
|
- public boolean onQueryTextSubmit(String query) {
|
123
|
|
- WritableMap arguments = Arguments.createMap();
|
124
|
|
- arguments.putString("query", query);
|
125
|
|
- sendEvent("searchQuerySubmit", arguments);
|
126
|
|
- return false;
|
127
|
|
- }
|
128
|
|
-
|
129
|
|
- @Override
|
130
|
|
- public boolean onQueryTextChange(String newText) {
|
131
|
|
- WritableMap arguments = Arguments.createMap();
|
132
|
|
- arguments.putString("query", newText);
|
133
|
|
- sendEvent("searchQueryChange", arguments);
|
134
|
|
- return false;
|
135
|
|
- }
|
136
|
|
-
|
137
|
|
- @Override
|
138
|
|
- public void onFocusChange(View v, boolean hasFocus) {
|
139
|
|
- WritableMap arguments = Arguments.createMap();
|
140
|
|
- arguments.putBoolean("hasFocus", hasFocus);
|
141
|
|
- sendEvent("searchFocusChange", arguments);
|
142
|
|
- }
|
143
|
|
-
|
144
|
|
- @Override
|
145
|
|
- public void onClick(View v) {
|
146
|
|
- sendEvent("searchViewShown");
|
147
|
|
- }
|
148
|
|
-
|
149
|
|
- private void sendEvent(String eventId, WritableMap arguments) {
|
150
|
|
- NavigationApplication.instance.getEventEmitter().sendNavigatorEvent(eventId, navigatorEventId, arguments);
|
151
|
|
- }
|
152
|
|
-
|
153
|
|
- private void sendEvent(String eventId) {
|
154
|
|
- NavigationApplication.instance.getEventEmitter().sendNavigatorEvent(eventId, navigatorEventId);
|
155
|
|
- }
|
156
|
|
-}
|