Skip to content

Commit 2a04be5

Browse files
committed
fix: Handle exchanging code lists at the runtime
1 parent 532c8d5 commit 2a04be5

1 file changed

Lines changed: 24 additions & 25 deletions

File tree

codeview/src/main/java/com/amrdeveloper/codeview/CodeViewAdapter.java

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,22 @@
4141

4242
/**
4343
* Custom base adapter that to use it in CodeView auto complete and snippets feature
44-
*
44+
* <p>
4545
* CodeViewAdapter supports to take a list of code which can include Keywords and snippets
4646
*
4747
* @since 1.1.0
4848
*/
4949
public class CodeViewAdapter extends BaseAdapter implements Filterable {
5050

51-
private List<Code> codeList;
5251
private List<Code> originalCodes;
52+
private List<Code> currentSuggestions;
5353
private final LayoutInflater layoutInflater;
5454
private final int codeViewLayoutId;
5555
private final int codeViewTextViewId;
5656

5757
public CodeViewAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull List<Code> codes) {
58-
this.codeList = codes;
58+
this.originalCodes = codes;
59+
this.currentSuggestions = new ArrayList<>();
5960
this.layoutInflater = LayoutInflater.from(context);
6061
this.codeViewLayoutId = resource;
6162
this.codeViewTextViewId = textViewResourceId;
@@ -68,23 +69,21 @@ public View getView(int position, View convertView, ViewGroup parent) {
6869
}
6970

7071
TextView textViewName = convertView.findViewById(codeViewTextViewId);
71-
72-
Code currentCode = codeList.get(position);
72+
Code currentCode = currentSuggestions.get(position);
7373
if (currentCode != null) {
7474
textViewName.setText(currentCode.getCodeTitle());
7575
}
76-
7776
return convertView;
7877
}
7978

8079
@Override
8180
public int getCount() {
82-
return codeList.size();
81+
return currentSuggestions.size();
8382
}
8483

8584
@Override
8685
public Object getItem(int position) {
87-
return codeList.get(position);
86+
return currentSuggestions.get(position);
8887
}
8988

9089
@Override
@@ -94,19 +93,22 @@ public long getItemId(int position) {
9493

9594
/**
9695
* Update the current code list with new list
96+
*
9797
* @param newCodeList The new code list
9898
*/
9999
public void updateCodes(List<Code> newCodeList) {
100-
codeList.clear();
101-
codeList.addAll(newCodeList);
100+
currentSuggestions.clear();
101+
originalCodes.clear();
102+
originalCodes.addAll(newCodeList);
102103
notifyDataSetChanged();
103104
}
104105

105106
/**
106107
* Clear the current code list and notify data set changed
107108
*/
108109
public void clearCodes() {
109-
codeList.clear();
110+
originalCodes.clear();
111+
currentSuggestions.clear();
110112
notifyDataSetChanged();
111113
}
112114

@@ -121,32 +123,29 @@ protected FilterResults performFiltering(CharSequence constraint) {
121123
FilterResults results = new FilterResults();
122124
List<Code> suggestions = new ArrayList<>();
123125

124-
if (originalCodes == null) {
125-
originalCodes = new ArrayList<>(codeList);
126-
}
127-
128-
126+
// If no prefix text, show all codes
129127
if (constraint == null || constraint.length() == 0) {
130128
results.values = originalCodes;
131129
results.count = originalCodes.size();
132-
} else {
133-
String filterPattern = constraint.toString().toLowerCase().trim();
130+
return results;
131+
}
134132

135-
for (Code item : originalCodes) {
136-
if (item.getCodePrefix().toLowerCase().contains(filterPattern)) {
137-
suggestions.add(item);
138-
}
133+
// Calculate suggestions based on current text
134+
String filterPattern = constraint.toString().toLowerCase().trim();
135+
for (Code item : originalCodes) {
136+
if (item.getCodePrefix().toLowerCase().contains(filterPattern)) {
137+
suggestions.add(item);
139138
}
140-
results.values = suggestions;
141-
results.count = suggestions.size();
142139
}
143140

141+
results.values = suggestions;
142+
results.count = suggestions.size();
144143
return results;
145144
}
146145

147146
@Override
148147
protected void publishResults(CharSequence constraint, FilterResults results) {
149-
codeList = (List<Code>) results.values;
148+
currentSuggestions = (List<Code>) results.values;
150149
notifyDataSetChanged();
151150
}
152151

0 commit comments

Comments
 (0)