1
0
Fork 0
mirror of https://github.com/gsantner/dandelion synced 2024-12-22 10:58:01 +01:00

Reworked search dialog to use layout resource and some code cleanup

This commit is contained in:
vanitasvitae 2016-08-27 12:34:08 +02:00
parent 18eeb76079
commit 8ae74d97d4
3 changed files with 50 additions and 44 deletions

View file

@ -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;

View file

@ -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 */
);
}
}

View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_gravity="center_horizontal"
android:paddingStart="@dimen/activity_horizontal_margin_half" android:paddingEnd="@dimen/activity_horizontal_margin_half">
<EditText
android:id="@+id/dialog_search__input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="@string/app_hashtag"
/>
</LinearLayout>