Improves autocomplete

This commit is contained in:
tom79 2017-08-10 09:03:42 +02:00
parent 08aed22ec8
commit e7e5d165b4
5 changed files with 24 additions and 19 deletions

View File

@ -128,7 +128,7 @@ public class AboutActivity extends AppCompatActivity implements OnRetrieveSearcA
} }
@Override @Override
public void onRetrieveSearchAccounts(APIResponse apiResponse, String search) { public void onRetrieveSearchAccounts(APIResponse apiResponse) {
about_developer.setEnabled(true); about_developer.setEnabled(true);
final List<Account> accounts = apiResponse.getAccounts(); final List<Account> accounts = apiResponse.getAccounts();
if( accounts != null && accounts.size() > 0 && accounts.get(0) != null) { if( accounts != null && accounts.size() > 0 && accounts.get(0) != null) {

View File

@ -398,22 +398,25 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc
toot_content.addTextChangedListener(new TextWatcher() { toot_content.addTextChangedListener(new TextWatcher() {
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {} public void beforeTextChanged(CharSequence s, int start, int count, int after) {
@Override currentCursorPosition = toot_content.getSelectionStart();
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
if( toot_content.getSelectionStart() > 0 )
currentCursorPosition = toot_content.getSelectionStart();
else if( toot_content.getText().length() == 0)
currentCursorPosition = 0;
//Only check last 15 characters before cursor position to avoid lags //Only check last 15 characters before cursor position to avoid lags
if( currentCursorPosition < 15 ){ //Less than 15 characters are written before the cursor position if( currentCursorPosition < 15 ){ //Less than 15 characters are written before the cursor position
searchLength = currentCursorPosition; searchLength = currentCursorPosition;
}else { }else {
searchLength = 15; searchLength = 15;
} }
Matcher m = sPattern.matcher(s.toString().substring(currentCursorPosition- searchLength, currentCursorPosition)); }
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if( currentCursorPosition- (searchLength-1) < 0 || currentCursorPosition == 0 || currentCursorPosition > s.toString().length())
return;
Matcher m = sPattern.matcher(s.toString().substring(currentCursorPosition- (searchLength-1), currentCursorPosition));
if(m.matches()) { if(m.matches()) {
String search = m.group(3); String search = m.group(3);
if (pp_progress != null && pp_actionBar != null) { if (pp_progress != null && pp_actionBar != null) {
@ -463,7 +466,6 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc
break; break;
} }
} }
currentCursorPosition = toot_content.getOffsetForPosition(event.getX(), event.getY());
return false; return false;
} }
}); });
@ -923,7 +925,7 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc
} }
@Override @Override
public void onRetrieveSearchAccounts(APIResponse apiResponse, final String search) { public void onRetrieveSearchAccounts(APIResponse apiResponse) {
if( pp_progress != null && pp_actionBar != null) { if( pp_progress != null && pp_actionBar != null) {
pp_progress.setVisibility(View.GONE); pp_progress.setVisibility(View.GONE);
pp_actionBar.setVisibility(View.VISIBLE); pp_actionBar.setVisibility(View.VISIBLE);
@ -938,22 +940,25 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc
final List<Account> accounts = apiResponse.getAccounts(); final List<Account> accounts = apiResponse.getAccounts();
if( accounts != null && accounts.size() > 0){ if( accounts != null && accounts.size() > 0){
AccountsSearchAdapter accountsListAdapter = new AccountsSearchAdapter(TootActivity.this, accounts); AccountsSearchAdapter accountsListAdapter = new AccountsSearchAdapter(TootActivity.this, accounts);
toot_content.showDropDown(); toot_content.showDropDown();
toot_content.setThreshold(0); toot_content.setThreshold(0);
toot_content.setAdapter(accountsListAdapter); toot_content.setAdapter(accountsListAdapter);
final String oldContent = toot_content.getText().toString(); final String oldContent = toot_content.getText().toString();
String[] searchA = oldContent.substring(0,currentCursorPosition+1).split("@");
final String search = searchA[searchA.length-1];
toot_content.setOnItemClickListener(new AdapterView.OnItemClickListener() { toot_content.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Account account = accounts.get(position); Account account = accounts.get(position);
String deltaSearch = oldContent.substring(currentCursorPosition-searchLength, currentCursorPosition); String deltaSearch = oldContent.substring(currentCursorPosition-searchLength, currentCursorPosition);
deltaSearch = deltaSearch.replace("@"+search,""); if( !search.equals(""))
deltaSearch = deltaSearch.replace("@"+search,"");
String newContent = oldContent.substring(0,currentCursorPosition-searchLength); String newContent = oldContent.substring(0,currentCursorPosition-searchLength);
newContent += deltaSearch; newContent += deltaSearch;
newContent += "@" + account.getAcct() + " "; newContent += "@" + account.getAcct() + " ";
int newPosition = newContent.length() -1; int newPosition = newContent.length();
if( currentCursorPosition < oldContent.length() - 1) if( currentCursorPosition < oldContent.length() - 1)
newContent += oldContent.substring(currentCursorPosition, oldContent.length()-1); newContent += oldContent.substring(currentCursorPosition, oldContent.length()-1);
toot_content.setText(newContent); toot_content.setText(newContent);

View File

@ -45,7 +45,7 @@ public class RetrieveDeveloperAccountsAsyncTask extends AsyncTask<Void, Void, Vo
} }
@Override @Override
protected void onPostExecute(Void result) { protected void onPostExecute(Void result) {
listener.onRetrieveSearchAccounts(apiResponse, null); listener.onRetrieveSearchAccounts(apiResponse);
} }
} }

View File

@ -49,7 +49,7 @@ public class RetrieveSearchAccountsAsyncTask extends AsyncTask<Void, Void, Void>
@Override @Override
protected void onPostExecute(Void result) { protected void onPostExecute(Void result) {
listener.onRetrieveSearchAccounts(apiResponse, query); listener.onRetrieveSearchAccounts(apiResponse);
} }
} }

View File

@ -22,5 +22,5 @@ import fr.gouv.etalab.mastodon.client.APIResponse;
* Interface for search accounts * Interface for search accounts
*/ */
public interface OnRetrieveSearcAccountshInterface { public interface OnRetrieveSearcAccountshInterface {
void onRetrieveSearchAccounts(APIResponse apiResponse, String shearch); void onRetrieveSearchAccounts(APIResponse apiResponse);
} }