From 8ae74d97d480f4ba42d5441f4450e8845b8c225d Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Sat, 27 Aug 2016 12:34:08 +0200 Subject: [PATCH] Reworked search dialog to use layout resource and some code cleanup --- .../activity/MainActivity.java | 60 +++++-------------- .../dfa/diaspora_android/util/Helpers.java | 20 +++++++ .../res/layout/dialog_search__people_tags.xml | 14 +++++ 3 files changed, 50 insertions(+), 44 deletions(-) create mode 100644 app/src/main/res/layout/dialog_search__people_tags.xml diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java index ffcf3464..044554d5 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java @@ -323,7 +323,7 @@ public class MainActivity extends AppCompatActivity // Create the File where the photo should go File photoFile; try { - photoFile = createImageFile(); + photoFile = Helpers.createImageFile(); takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath); } catch (IOException ex) { // Error occurred while creating the File @@ -429,19 +429,6 @@ public class MainActivity extends AppCompatActivity onNavigationItemSelected(navView.getMenu().findItem(R.id.nav_stream)); } - private File createImageFile() throws IOException { - // Create an image file name - String timeStamp = new SimpleDateFormat("dd-MM-yy_HH-mm", Locale.getDefault()).format(new Date()); - String imageFileName = "JPEG_" + timeStamp + "_"; - File storageDir = Environment.getExternalStoragePublicDirectory( - Environment.DIRECTORY_PICTURES); - return File.createTempFile( - imageFileName, /* prefix */ - ".jpg", /* suffix */ - storageDir /* directory */ - ); - } - @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); @@ -568,7 +555,7 @@ public class MainActivity extends AppCompatActivity if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_STREAM)) { setTitle(R.string.nav_stream); } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_POSTS)) { - setTitle(R.string.diaspora); //TODO: Extract posts title somehow? + setTitle(R.string.diaspora); } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_NOTIFICATIONS)) { setTitle(R.string.notifications); } else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_CONVERSATIONS)) { @@ -709,50 +696,35 @@ public class MainActivity extends AppCompatActivity case R.id.action_search: { if (WebHelper.isOnline(MainActivity.this)) { final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - LinearLayout layout = new LinearLayout(this); - layout.setOrientation(LinearLayout.VERTICAL); - layout.setGravity(Gravity.CENTER_HORIZONTAL); - final EditText input = new EditText(this); - input.setSingleLine(true); - layout.setPadding(50, 0, 50, 0); - input.setHint(R.string.app_hashtag); - layout.addView(input); - final DialogInterface.OnClickListener onSearchAccepted = new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - boolean wasClickedOnSearchForPeople = which == DialogInterface.BUTTON_NEGATIVE; - - String inputTag = input.getText().toString().trim(); - String cleanTag = inputTag.replaceAll(wasClickedOnSearchForPeople ? "\\*" : "\\#", ""); - // this validate the input data for tagfind - if (cleanTag == null || cleanTag.equals("")) { + View layout = getLayoutInflater().inflate(R.layout.dialog_search__people_tags, null); + final EditText input = (EditText) layout.findViewById(R.id.dialog_search__input); + final DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int which) { + String query = input.getText().toString().trim().replaceAll((which == DialogInterface.BUTTON_NEGATIVE ? "\\*" : "\\#"), ""); + if(query.equals("")) { Snackbar.make(contentLayout, R.string.search_alert_bypeople_validate_needsomedata, Snackbar.LENGTH_LONG).show(); - } else { // User have added a search tag - if (wasClickedOnSearchForPeople) { - webView.loadUrlNew(urls.getSearchPeopleUrl(cleanTag)); - } else { - webView.loadUrlNew(urls.getSearchTagsUrl(cleanTag)); - } + } else { + webView.loadUrl(which == DialogInterface.BUTTON_NEGATIVE ? urls.getSearchPeopleUrl(query) : urls.getSearchTagsUrl(query)); } - getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); imm.hideSoftInputFromWindow(input.getWindowToken(), 0); } }; - final AlertDialog dialog = new AlertDialog.Builder(this) - .setView(layout) - .setTitle(R.string.search_alert_title) + final android.support.v7.app.AlertDialog dialog = new android.support.v7.app.AlertDialog.Builder(this) + .setView(layout).setTitle(R.string.search_alert_title) .setCancelable(true) - .setPositiveButton(R.string.search_alert_tag, onSearchAccepted) - .setNegativeButton(R.string.search_alert_people, onSearchAccepted) + .setPositiveButton(R.string.search_alert_tag, clickListener) + .setNegativeButton(R.string.search_alert_people, clickListener) .create(); input.setOnEditorActionListener(new TextView.OnEditorActionListener() { public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { dialog.hide(); - onSearchAccepted.onClick(null, 0); + clickListener.onClick(null, 0); return true; } return false; diff --git a/app/src/main/java/com/github/dfa/diaspora_android/util/Helpers.java b/app/src/main/java/com/github/dfa/diaspora_android/util/Helpers.java index a58abbc8..f424eda3 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/util/Helpers.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/util/Helpers.java @@ -22,9 +22,16 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.Uri; +import android.os.Environment; import com.github.dfa.diaspora_android.R; +import java.io.File; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + public class Helpers { public static void animateToActivity(Activity from, Class to, boolean finishFromActivity) { @@ -44,4 +51,17 @@ public class Helpers { } catch (Exception ignored) { } } + + public static File createImageFile() throws IOException { + // Create an image file name + String timeStamp = new SimpleDateFormat("dd-MM-yy_HH-mm", Locale.getDefault()).format(new Date()); + String imageFileName = "JPEG_" + timeStamp + "_"; + File storageDir = Environment.getExternalStoragePublicDirectory( + Environment.DIRECTORY_PICTURES); + return File.createTempFile( + imageFileName, /* prefix */ + ".jpg", /* suffix */ + storageDir /* directory */ + ); + } } diff --git a/app/src/main/res/layout/dialog_search__people_tags.xml b/app/src/main/res/layout/dialog_search__people_tags.xml new file mode 100644 index 00000000..3270b387 --- /dev/null +++ b/app/src/main/res/layout/dialog_search__people_tags.xml @@ -0,0 +1,14 @@ + + + + + \ No newline at end of file