diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/AboutActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/AboutActivity.java index 3463c56a0..17c6d656b 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/AboutActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/AboutActivity.java @@ -128,7 +128,7 @@ public class AboutActivity extends AppCompatActivity implements OnRetrieveSearcA } @Override - public void onRetrieveSearchAccounts(APIResponse apiResponse) { + public void onRetrieveSearchAccounts(APIResponse apiResponse, String search) { about_developer.setEnabled(true); final List accounts = apiResponse.getAccounts(); if( accounts != null && accounts.size() > 0 && accounts.get(0) != null) { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java index 6a67f795d..3d8042228 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java @@ -28,6 +28,7 @@ import android.graphics.drawable.BitmapDrawable; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; +import android.os.Handler; import android.speech.RecognizerIntent; import android.support.design.widget.FloatingActionButton; import android.support.v4.content.ContextCompat; @@ -36,6 +37,7 @@ import android.support.v7.app.AppCompatActivity; import android.text.Editable; import android.text.Html; import android.text.TextWatcher; +import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; @@ -142,7 +144,8 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc private Toast mToast; private LinearLayout drawer_layout; private HorizontalScrollView picture_scrollview; - private String search; + private int currentCursorPosition, searchLength; + private boolean canDisplayMessage; @Override protected void onCreate(Bundle savedInstanceState) { @@ -177,7 +180,7 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc pp_progress = (ProgressBar) actionBar.getCustomView().findViewById(R.id.pp_progress); } - + canDisplayMessage = true; //By default the toot is not restored so the id -1 is defined @@ -392,6 +395,7 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc }); String pattern = "^(.|\\s)*(@([a-zA-Z0-9_]{2,}))$"; final Pattern sPattern = Pattern.compile(pattern); + toot_content.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @@ -399,22 +403,24 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc public void onTextChanged(CharSequence s, int start, int before, int count) {} @Override public void afterTextChanged(Editable s) { - int length; - //Only check last 20 characters to avoid lags - if( s.toString().length() < 20 ){ //Less than 20 characters are written - length = s.toString().length(); + 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 + if( currentCursorPosition < 15 ){ //Less than 15 characters are written before the cursor position + searchLength = currentCursorPosition; }else { - length = 20; + searchLength = 15; } - Matcher m = sPattern.matcher(s.toString().substring(s.toString().length()- length, s.toString().length())); + Matcher m = sPattern.matcher(s.toString().substring(currentCursorPosition- searchLength, currentCursorPosition)); if(m.matches()) { - search = m.group(3); - if( pp_progress != null && pp_actionBar != null) { + String search = m.group(3); + if (pp_progress != null && pp_actionBar != null) { pp_progress.setVisibility(View.VISIBLE); pp_actionBar.setVisibility(View.GONE); } new RetrieveSearchAccountsAsyncTask(getApplicationContext(),search,TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - }else{toot_content.dismissDropDown();} int totalChar = toot_cw_content.length() + toot_content.length(); int remainChar = (maxChar - totalChar); @@ -427,8 +433,20 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc } }else { toot_it.setEnabled(false); - showAToast(getString(R.string.toot_no_space)); toot_space_left.setTextColor( Color.RED); + //Delay the advertising message to avoid to flood the user + if( canDisplayMessage ){ + canDisplayMessage = false; + showAToast(getString(R.string.toot_no_space)); + final Handler handler = new Handler(); + handler.postDelayed(new Runnable() { + @Override + public void run() { + canDisplayMessage = true; + } + }, 4000); + } + } toot_space_left.setText(String.valueOf(remainChar)); } @@ -445,6 +463,7 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc break; } } + currentCursorPosition = toot_content.getOffsetForPosition(event.getX(), event.getY()); return false; } }); @@ -904,7 +923,7 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc } @Override - public void onRetrieveSearchAccounts(APIResponse apiResponse) { + public void onRetrieveSearchAccounts(APIResponse apiResponse, final String search) { if( pp_progress != null && pp_actionBar != null) { pp_progress.setVisibility(View.GONE); pp_actionBar.setVisibility(View.VISIBLE); @@ -923,13 +942,22 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc toot_content.showDropDown(); toot_content.setThreshold(0); toot_content.setAdapter(accountsListAdapter); - final String toot_content_str = toot_content.getText().toString().replace("@"+search,""); + final String oldContent = toot_content.getText().toString(); + toot_content.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { Account account = accounts.get(position); - toot_content.setText(toot_content_str + " @" + account.getAcct() + " "); - toot_content.setSelection(toot_content.getText().length()-1); + String deltaSearch = oldContent.substring(currentCursorPosition-searchLength, currentCursorPosition); + deltaSearch = deltaSearch.replace("@"+search,""); + String newContent = oldContent.substring(0,currentCursorPosition-searchLength); + newContent += deltaSearch; + newContent += "@" + account.getAcct() + " "; + int newPosition = newContent.length() -1; + if( currentCursorPosition < oldContent.length() - 1) + newContent += oldContent.substring(currentCursorPosition, oldContent.length()-1); + toot_content.setText(newContent); + toot_content.setSelection(newPosition); toot_content.dismissDropDown(); } }); @@ -1049,8 +1077,6 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc ic_show.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - - AlertDialog.Builder alert = new AlertDialog.Builder(TootActivity.this); alert.setTitle(R.string.toot_reply_content_title); final TextView input = new TextView(TootActivity.this); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveDeveloperAccountsAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveDeveloperAccountsAsyncTask.java index 5a8b1aff5..c4d41ea49 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveDeveloperAccountsAsyncTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveDeveloperAccountsAsyncTask.java @@ -31,7 +31,6 @@ public class RetrieveDeveloperAccountsAsyncTask extends AsyncTask @Override protected void onPostExecute(Void result) { - listener.onRetrieveSearchAccounts(apiResponse); + listener.onRetrieveSearchAccounts(apiResponse, query); } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnRetrieveSearcAccountshInterface.java b/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnRetrieveSearcAccountshInterface.java index b46c8b92e..1134a005a 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnRetrieveSearcAccountshInterface.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnRetrieveSearcAccountshInterface.java @@ -22,5 +22,5 @@ import fr.gouv.etalab.mastodon.client.APIResponse; * Interface for search accounts */ public interface OnRetrieveSearcAccountshInterface { - void onRetrieveSearchAccounts(APIResponse apiResponse); + void onRetrieveSearchAccounts(APIResponse apiResponse, String shearch); }