Skip to content

Commit

Permalink
fix: Handle exchanging code lists at the runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Dec 29, 2023
1 parent 532c8d5 commit 2a04be5
Showing 1 changed file with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,22 @@

/**
* Custom base adapter that to use it in CodeView auto complete and snippets feature
*
* <p>
* CodeViewAdapter supports to take a list of code which can include Keywords and snippets
*
* @since 1.1.0
*/
public class CodeViewAdapter extends BaseAdapter implements Filterable {

private List<Code> codeList;
private List<Code> originalCodes;
private List<Code> currentSuggestions;
private final LayoutInflater layoutInflater;
private final int codeViewLayoutId;
private final int codeViewTextViewId;

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

TextView textViewName = convertView.findViewById(codeViewTextViewId);

Code currentCode = codeList.get(position);
Code currentCode = currentSuggestions.get(position);
if (currentCode != null) {
textViewName.setText(currentCode.getCodeTitle());
}

return convertView;
}

@Override
public int getCount() {
return codeList.size();
return currentSuggestions.size();
}

@Override
public Object getItem(int position) {
return codeList.get(position);
return currentSuggestions.get(position);
}

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

/**
* Update the current code list with new list
*
* @param newCodeList The new code list
*/
public void updateCodes(List<Code> newCodeList) {
codeList.clear();
codeList.addAll(newCodeList);
currentSuggestions.clear();
originalCodes.clear();
originalCodes.addAll(newCodeList);
notifyDataSetChanged();
}

/**
* Clear the current code list and notify data set changed
*/
public void clearCodes() {
codeList.clear();
originalCodes.clear();
currentSuggestions.clear();
notifyDataSetChanged();
}

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

if (originalCodes == null) {
originalCodes = new ArrayList<>(codeList);
}


// If no prefix text, show all codes
if (constraint == null || constraint.length() == 0) {
results.values = originalCodes;
results.count = originalCodes.size();
} else {
String filterPattern = constraint.toString().toLowerCase().trim();
return results;
}

for (Code item : originalCodes) {
if (item.getCodePrefix().toLowerCase().contains(filterPattern)) {
suggestions.add(item);
}
// Calculate suggestions based on current text
String filterPattern = constraint.toString().toLowerCase().trim();
for (Code item : originalCodes) {
if (item.getCodePrefix().toLowerCase().contains(filterPattern)) {
suggestions.add(item);
}
results.values = suggestions;
results.count = suggestions.size();
}

results.values = suggestions;
results.count = suggestions.size();
return results;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
codeList = (List<Code>) results.values;
currentSuggestions = (List<Code>) results.values;
notifyDataSetChanged();
}

Expand Down

0 comments on commit 2a04be5

Please sign in to comment.