Skip to content

Commit

Permalink
fix5 for search, dynamic array instead of large size nulled parts
Browse files Browse the repository at this point in the history
  • Loading branch information
pavly-gerges committed Jul 2, 2021
1 parent 8321016 commit b1f669e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import android.view.View;

public interface ActionInjector {
void execute(View uiState, int position);
void execute(View uiState, int position, String currentItem);
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,22 @@ public String[] search(String[] searchList, String[] searchKeyWords, ActionInjec
synchronized(this) {
final String[][] resultList = {new String[0]};
return Executors.callable(() -> {
for (int pos = 0; pos < searchList.length; pos++) {
for (String keyword : searchKeyWords) {
if (searchList[pos].replaceAll(" ","").trim().toLowerCase().contains(keyword.replaceAll(" ","").trim().toLowerCase())) {
String[] temp;
for (int i = 0; i < searchKeyWords.length; i++) {
for (int j = 0; j < searchList.length; j++) {
if (searchList[j].replaceAll(" ","").trim().toLowerCase().contains(
searchKeyWords[i].replaceAll(" ","").trim().toLowerCase())) {
//dynamic array conception
if(pos >= resultList[0].length-1){
resultList[0] = Arrays.copyOf(resultList[0], resultList[0].length+1);
if(i > resultList[0].length-1){
temp = resultList[0];
resultList[0] = new String[temp.length+1];
for(int position=0; position < temp.length; position++){
resultList[0][position] = temp[position];
}
}
resultList[0][pos] = searchList[pos];
resultList[0][i] = searchKeyWords[i];
if(injector != null){
injector.execute(getChildUiStateByIndex(pos), pos);
injector.execute(getChildUiStateByIndex(j), j, resultList[0][i]);
}
break;
}
Expand All @@ -211,25 +217,6 @@ public String[] search(String[] searchList, String[] searchKeyWords, ActionInjec
}
}

/**
* Revert the search results executed by the search function, to the full length of UiStates, this doesn't stop the searching thread though, it rather waits until it finishes searching.
* @param actionInjector injects actions to execute accompanied by the reversion.
* @throws Exception throws an exception if the revert custom thread fails.
* @apiNote <b> <T extends Object> synchronized(T)</b> marks a thread-safe function by the dead locking other threads synchronized on the same object scheduled or started by the thread factory.
*/
public void revertSearchEngine(@Nullable ActionInjector actionInjector) throws Exception {
synchronized(this){
//format the states
removeAllViews();
Executors.callable(() -> forEachUiState((UiStatesLooper.Modifiable.Looper) (currentView, position) -> {
if (actionInjector != null) {
actionInjector.execute(currentView, position);
}
addView(currentView, position);
})).call();
}
}

/**
* Sort a String list either by A_Z or Z_A swapping algorithm, the sort runs in an async task in the same called thread.
* @param list the list to order sort for.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ public void onClick(View v) {
if(v.getId() == 'S'){
Toast.makeText(uiPager.getContext(), "Search Button Clicked", Toast.LENGTH_LONG).show();
try {
uiPager.search(sortedList, new String[]{"Revert Search", "PAvlY", "Thomas"}, (uiState, position) -> {
uiPager.removeAllViews();
uiPager.search(sortedList, new String[]{"Revert Search", "PAvlY", "Thomas"}, (uiState, position, currentItem) -> {
uiPager.addView(uiState);
uiState.setBackgroundColor(Color.MAGENTA);
if(uiState.getId() == 'P'){
uiState.setBackgroundColor(Color.RED);
Expand All @@ -73,16 +75,7 @@ public void onClick(View v) {
}
}else if(v.getId() == 'R'){
Toast.makeText(uiPager.getContext(), "Revert Search clicked", Toast.LENGTH_LONG).show();
try {
uiPager.revertSearchEngine((uiState, position) -> {
uiState.setBackgroundColor(Color.GREEN);
if(uiState.getId() == 'R'){
uiState.setBackgroundColor(Color.RED);
}
});
} catch (Exception e) {
e.printStackTrace();
}

}else if(v.getId() == 'D'){
uiPager.animate().scaleY(0).scaleX(0).rotationY(45).setDuration(800).withEndAction(()->{
uiPager.detachAllUiStates();
Expand Down

0 comments on commit b1f669e

Please sign in to comment.