Skip to content

Commit ce5d708

Browse files
committed
cleanup
1 parent 8c395a9 commit ce5d708

File tree

4 files changed

+56
-80
lines changed

4 files changed

+56
-80
lines changed

lib-n-ide/src/main/java/com/duy/ide/editor/EditorDelegate.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,16 @@ public void setCodeFormatProvider(@Nullable CodeFormatProvider codeFormatProvide
628628
this.mCodeFormatProvider = codeFormatProvider;
629629
}
630630

631+
/**
632+
* Set suggestion provider for editor
633+
* This method will be called when editor view is created
634+
*/
631635
@Override
632636
public void setSuggestionProvider(@Nullable SuggestionProvider provider) {
633637
this.mSuggestionProvider = provider;
638+
if (this.mEditText != null) {
639+
this.mEditText.setSuggestEnable(true);
640+
}
634641
}
635642

636643
public static class SavedState implements Parcelable {

lib-n-ide/src/main/java/com/duy/ide/editor/internal/suggestion/SuggestionAdapter.java

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import android.view.ViewGroup;
1010
import android.widget.AdapterView;
1111
import android.widget.ArrayAdapter;
12-
import android.widget.Filter;
1312
import android.widget.TextView;
1413

1514
import com.duy.ide.code.api.SuggestItem;
@@ -33,44 +32,7 @@ public class SuggestionAdapter extends ArrayAdapter<SuggestItem> {
3332
@Nullable
3433
private AdapterView.OnItemClickListener mListener;
3534

36-
private Filter mSuggestionFilter = new Filter() {
37-
@Override
38-
public CharSequence convertResultToString(Object value) {
39-
if (value == null) {
40-
return "";
41-
}
42-
return ((SuggestItem) value).getInsertText();
43-
}
44-
45-
@Override
46-
protected FilterResults performFiltering(CharSequence constraint) {
47-
FilterResults filterResults = new FilterResults();
48-
mFilteredData.clear();
49-
if (constraint != null) {
50-
for (SuggestItem item : mOriginData) {
51-
// if (item.compareTo(constraint.toString()) == 0) {
52-
mFilteredData.add(item);
53-
// }
54-
}
55-
filterResults.values = mFilteredData;
56-
filterResults.count = mFilteredData.size();
57-
}
58-
return filterResults;
59-
}
60-
61-
@Override
62-
@SuppressWarnings("unchecked")
63-
@UiThread
64-
protected void publishResults(CharSequence constraint, FilterResults results) {
65-
ArrayList<SuggestItem> filteredList = (ArrayList<SuggestItem>) results.values;
66-
clear();
67-
if (filteredList != null && filteredList.size() > 0) {
68-
addAll(filteredList);
69-
}
70-
notifyDataSetChanged();
71-
}
72-
};
73-
35+
private Integer mTextColor = null;
7436

7537
public SuggestionAdapter(@NonNull Context context,
7638
@NonNull List<SuggestItem> objects) {
@@ -93,6 +55,11 @@ public View getView(final int position, @Nullable View convertView, @NonNull Vie
9355
TextView txtReturnType = convertView.findViewById(R.id.txt_return_type);
9456
TextView txtHeader = convertView.findViewById(R.id.txt_suggest_header);
9557

58+
if (mTextColor != null){
59+
txtName.setTextColor(mTextColor);
60+
txtReturnType.setTextColor(mTextColor);
61+
txtHeader.setTextColor(mTextColor);
62+
}
9663
if (item != null) {
9764
txtName.setText(ensureNotNull(item.getName()));
9865
txtReturnType.setText(ensureNotNull(item.getReturnType()));
@@ -128,12 +95,6 @@ public void addData(@NonNull Collection<? extends SuggestItem> collection) {
12895
mOriginData.addAll(collection);
12996
}
13097

131-
@NonNull
132-
@Override
133-
public Filter getFilter() {
134-
return mSuggestionFilter;
135-
}
136-
13798
public List<SuggestItem> getAllItems() {
13899
return mOriginData;
139100
}
@@ -146,4 +107,8 @@ public void setData(List<SuggestItem> data) {
146107
clear();
147108
addAll(data);
148109
}
110+
111+
public void setTextColor(int textColor) {
112+
this.mTextColor = textColor;
113+
}
149114
}

lib-n-ide/src/main/java/com/duy/ide/editor/theme/model/EditorTheme.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ public int getSelectionColor() {
3636
return getColor(Attr.SELECTION_COLOR);
3737
}
3838

39+
public int getDropdownBgColor() {
40+
return getColor(Attr.DROPDOWN_BACKGROUND);
41+
}
42+
43+
public int getDropdownFgColor() {
44+
return getColor(Attr.DROPDOWN_FOREGROUND);
45+
}
46+
3947
public int getBgColor() {
4048
return getColor(Attr.BG_COLOR);
4149

lib-n-ide/src/main/java/com/duy/ide/editor/view/SuggestionEditor.java

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import android.graphics.Rect;
66
import android.graphics.drawable.ColorDrawable;
77
import android.os.Build;
8-
import android.support.annotation.IdRes;
98
import android.support.annotation.NonNull;
109
import android.support.annotation.Nullable;
1110
import android.support.v7.widget.ListPopupWindow;
@@ -24,17 +23,23 @@
2423
import com.duy.ide.editor.internal.suggestion.SuggestionAdapter;
2524
import com.duy.ide.editor.theme.model.EditorTheme;
2625

26+
import java.util.ArrayList;
2727
import java.util.List;
2828

2929
public class SuggestionEditor extends EditActionSupportEditor {
3030

3131
private static final String TAG = "SuggestionEditor";
3232
private final Rect mTmpRect = new Rect();
33-
@Nullable
33+
3434
private SuggestionAdapter mAdapter;
3535
@Nullable
3636
private OnSuggestItemClickListener mOnSuggestItemClickListener;
37+
38+
/**
39+
* Small popup window to display list suggestion
40+
*/
3741
private ListPopupWindow mPopup;
42+
private boolean mSuggestionEnable = false;
3843

3944
private AdapterView.OnItemClickListener mSuggestClickListener
4045
= new AdapterView.OnItemClickListener() {
@@ -46,9 +51,6 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
4651
}
4752
};
4853

49-
@IdRes
50-
private int mDropDownAnchorId = View.NO_ID;
51-
5254
public SuggestionEditor(Context context) {
5355
super(context);
5456
init(context, null, 0);
@@ -66,19 +68,27 @@ public SuggestionEditor(Context context, AttributeSet attrs, int defStyleAttr) {
6668
}
6769

6870
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
69-
updateCharHeight();
71+
mAdapter = new SuggestionAdapter(getContext(), new ArrayList<SuggestItem>());
72+
mAdapter.setListener(mSuggestClickListener);
73+
7074
mPopup = new ListPopupWindow(context, attrs, defStyleAttr, 0);
7175
mPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
76+
mPopup.setAdapter(mAdapter);
7277
if (getEditorTheme() != null) {
73-
mPopup.setBackgroundDrawable(new ColorDrawable(getEditorTheme().getBgColor()));
78+
setThemeForPopup(getEditorTheme());
7479
}
7580
}
7681

82+
private void setThemeForPopup(EditorTheme theme) {
83+
mPopup.setBackgroundDrawable(new ColorDrawable(theme.getDropdownBgColor()));
84+
mAdapter.setTextColor(theme.getDropdownFgColor());
85+
}
86+
7787
@Override
7888
public void setTheme(@NonNull EditorTheme editorTheme) {
7989
super.setTheme(editorTheme);
8090
if (mPopup != null) {
81-
mPopup.setBackgroundDrawable(new ColorDrawable(editorTheme.getBgColor()));
91+
setThemeForPopup(editorTheme);
8292
}
8393
}
8494

@@ -90,6 +100,9 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
90100
}
91101

92102
protected void onPopupChangePosition() {
103+
if (!mSuggestionEnable) {
104+
return;
105+
}
93106
try {
94107
Layout layout = getLayout();
95108
//not ready
@@ -147,6 +160,9 @@ protected void onDropdownChangeSize() {
147160
}
148161

149162
public void showDropDown() {
163+
if (!mSuggestionEnable) {
164+
return;
165+
}
150166
if (!isPopupShowing()) {
151167
if (hasFocus()) {
152168
try {
@@ -163,11 +179,7 @@ public void showDropDown() {
163179
*/
164180
private void showDropDownImpl() {
165181
if (mPopup.getAnchorView() == null) {
166-
if (mDropDownAnchorId != View.NO_ID) {
167-
mPopup.setAnchorView(getRootView().findViewById(mDropDownAnchorId));
168-
} else {
169-
mPopup.setAnchorView(this);
170-
}
182+
mPopup.setAnchorView(this);
171183
}
172184
if (!isPopupShowing()) {
173185
// Make sure the list does not obscure the IME when shown for the first time.
@@ -180,8 +192,10 @@ private void showDropDownImpl() {
180192

181193
@Override
182194
public void setSuggestEnable(boolean enable) {
183-
if (!enable) {
184-
mAdapter = null;
195+
mSuggestionEnable = enable;
196+
if (mSuggestionEnable) {
197+
onDropdownChangeSize();
198+
} else {
185199
dismissDropDown();
186200
}
187201
}
@@ -191,12 +205,8 @@ public void setSuggestEnable(boolean enable) {
191205
*/
192206
@Override
193207
public void setSuggestData(@NonNull List<SuggestItem> data) {
194-
if (mAdapter != null) {
195-
mAdapter.clearAllData();
196-
} else {
197-
mAdapter = new SuggestionAdapter(getContext(), data);
198-
mAdapter.setListener(mSuggestClickListener);
199-
mPopup.setAdapter(mAdapter);
208+
if (!mSuggestionEnable) {
209+
return;
200210
}
201211
mAdapter.setData(data);
202212
mAdapter.notifyDataSetChanged();
@@ -206,20 +216,6 @@ public void setSuggestData(@NonNull List<SuggestItem> data) {
206216
}
207217
}
208218

209-
@Override
210-
public void setTextSize(int unit, float size) {
211-
float oldTextSize = getTextSize();
212-
super.setTextSize(unit, size);
213-
if (oldTextSize != size) {
214-
updateCharHeight();
215-
}
216-
}
217-
218-
private void updateCharHeight() {
219-
// Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
220-
// mCharHeight = (int) (fontMetrics.bottom - fontMetrics.top);
221-
}
222-
223219
@Override
224220
public void setOnSuggestItemClickListener(@Nullable OnSuggestItemClickListener listener) {
225221
this.mOnSuggestItemClickListener = listener;

0 commit comments

Comments
 (0)