Ask save changes answer

pull/172/head
M66B 5 years ago
parent 9a494ecf65
commit 49e50c7d33

@ -23,18 +23,21 @@ import android.app.Dialog;
import android.content.ClipboardManager; import android.content.ClipboardManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Rect;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.text.Editable;
import android.text.Spanned; import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -43,6 +46,7 @@ import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.snackbar.Snackbar;
import static android.app.Activity.RESULT_OK; import static android.app.Activity.RESULT_OK;
@ -52,17 +56,17 @@ public class FragmentAnswer extends FragmentBase {
private CheckBox cbFavorite; private CheckBox cbFavorite;
private CheckBox cbHide; private CheckBox cbHide;
private EditTextCompose etText; private EditTextCompose etText;
private ImageButton ibInfo;
private BottomNavigationView style_bar; private BottomNavigationView style_bar;
private BottomNavigationView bottom_navigation;
private ContentLoadingProgressBar pbWait; private ContentLoadingProgressBar pbWait;
private Group grpReady; private Group grpReady;
private long id = -1; private long id = -1;
private long copy = -1; private long copy = -1;
private boolean dirty = false;
private static final int REQUEST_LINK = 1; private static final int REQUEST_LINK = 1;
private final static int REQUEST_DELETE = 2; private final static int REQUEST_SAVE = 2;
private final static int REQUEST_DELETE = 3;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@ -81,6 +85,7 @@ public class FragmentAnswer extends FragmentBase {
@Nullable @Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setSubtitle(R.string.title_answer_caption); setSubtitle(R.string.title_answer_caption);
setHasOptionsMenu(true);
view = (ViewGroup) inflater.inflate(R.layout.fragment_answer, container, false); view = (ViewGroup) inflater.inflate(R.layout.fragment_answer, container, false);
@ -89,25 +94,12 @@ public class FragmentAnswer extends FragmentBase {
cbFavorite = view.findViewById(R.id.cbFavorite); cbFavorite = view.findViewById(R.id.cbFavorite);
cbHide = view.findViewById(R.id.cbHide); cbHide = view.findViewById(R.id.cbHide);
etText = view.findViewById(R.id.etText); etText = view.findViewById(R.id.etText);
ibInfo = view.findViewById(R.id.ibInfo);
style_bar = view.findViewById(R.id.style_bar); style_bar = view.findViewById(R.id.style_bar);
bottom_navigation = view.findViewById(R.id.bottom_navigation);
pbWait = view.findViewById(R.id.pbWait); pbWait = view.findViewById(R.id.pbWait);
grpReady = view.findViewById(R.id.grpReady); grpReady = view.findViewById(R.id.grpReady);
int height = getContext().getResources().getDisplayMetrics().heightPixels;
View decor = getActivity().getWindow().getDecorView();
decor.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
Rect rect = new Rect();
decor.getWindowVisibleDisplayFrame(rect);
view.setPadding(0, 0, 0, height - rect.bottom);
}
});
etText.setSelectionListener(new EditTextCompose.ISelection() { etText.setSelectionListener(new EditTextCompose.ISelection() {
@Override @Override
public void onSelected(boolean selection) { public void onSelected(boolean selection) {
@ -115,13 +107,6 @@ public class FragmentAnswer extends FragmentBase {
} }
}); });
ibInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new FragmentInfo().show(getParentFragmentManager(), "rule:info");
}
});
style_bar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { style_bar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override @Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) { public boolean onNavigationItemSelected(@NonNull MenuItem item) {
@ -129,19 +114,25 @@ public class FragmentAnswer extends FragmentBase {
} }
}); });
bottom_navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { addKeyPressedListener(new ActivityBase.IKeyPressedListener() {
@Override @Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { public boolean onKeyPressed(int keyCode) {
switch (menuItem.getItemId()) { return false;
case R.id.action_delete: }
onActionDelete();
return true; @Override
case R.id.action_save: public boolean onBackPressed() {
onActionSave(); if (dirty) {
return true; Bundle aargs = new Bundle();
default: aargs.putString("question", getString(R.string.title_ask_save));
return false;
} FragmentDialogAsk fragment = new FragmentDialogAsk();
fragment.setArguments(aargs);
fragment.setTargetFragment(FragmentAnswer.this, REQUEST_SAVE);
fragment.show(getParentFragmentManager(), "account:save");
return true;
} else
return false;
} }
}); });
@ -173,7 +164,35 @@ public class FragmentAnswer extends FragmentBase {
cbFavorite.setChecked(answer == null ? false : answer.favorite); cbFavorite.setChecked(answer == null ? false : answer.favorite);
cbHide.setChecked(answer == null ? false : answer.hide); cbHide.setChecked(answer == null ? false : answer.hide);
etText.setText(answer == null ? null : HtmlHelper.fromHtml(answer.text)); etText.setText(answer == null ? null : HtmlHelper.fromHtml(answer.text));
bottom_navigation.findViewById(R.id.action_delete).setVisibility(answer == null ? View.GONE : View.VISIBLE);
TextWatcher watcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Do nothing
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
dirty = true;
}
@Override
public void afterTextChanged(Editable s) {
// Do nothing
}
};
CompoundButton.OnCheckedChangeListener checker = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
dirty = true;
}
};
etName.addTextChangedListener(watcher);
cbFavorite.setOnCheckedChangeListener(checker);
cbHide.setOnCheckedChangeListener(checker);
etText.addTextChangedListener(watcher);
pbWait.setVisibility(View.GONE); pbWait.setVisibility(View.GONE);
grpReady.setVisibility(View.VISIBLE); grpReady.setVisibility(View.VISIBLE);
@ -186,7 +205,37 @@ public class FragmentAnswer extends FragmentBase {
}.execute(this, args, "answer:get"); }.execute(this, args, "answer:get");
} }
private void onActionDelete() { @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_operations, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.menu_delete).setVisible(id > 0);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_help:
onMenuHelp();
return true;
case R.id.menu_delete:
onMenuDelete();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void onMenuHelp() {
new FragmentInfo().show(getParentFragmentManager(), "answer:info");
}
private void onMenuDelete() {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putString("question", getString(R.string.title_ask_delete_answer)); args.putString("question", getString(R.string.title_ask_delete_answer));
@ -196,12 +245,43 @@ public class FragmentAnswer extends FragmentBase {
fragment.show(getParentFragmentManager(), "answer:delete"); fragment.show(getParentFragmentManager(), "answer:delete");
} }
private void onActionSave() { @Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
switch (requestCode) {
case REQUEST_LINK:
if (resultCode == RESULT_OK && data != null)
onLinkSelected(data.getBundleExtra("args"));
break;
case REQUEST_SAVE:
if (resultCode == RESULT_OK)
onSave();
else
finish();
break;
case REQUEST_DELETE:
if (resultCode == RESULT_OK)
onDelete();
break;
}
} catch (Throwable ex) {
Log.e(ex);
}
}
private void onLinkSelected(Bundle args) {
String link = args.getString("link");
StyleHelper.apply(R.id.menu_link, etText, link);
}
private void onSave() {
etText.clearComposingText(); etText.clearComposingText();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", id);
args.putString("name", etName.getText().toString()); args.putString("name", etName.getText().toString().trim());
args.putBoolean("favorite", cbFavorite.isChecked()); args.putBoolean("favorite", cbFavorite.isChecked());
args.putBoolean("hide", cbHide.isChecked()); args.putBoolean("hide", cbHide.isChecked());
args.putString("text", HtmlHelper.toHtml(etText.getText())); args.putString("text", HtmlHelper.toHtml(etText.getText()));
@ -225,6 +305,9 @@ public class FragmentAnswer extends FragmentBase {
boolean hide = args.getBoolean("hide"); boolean hide = args.getBoolean("hide");
String text = args.getString("text"); String text = args.getString("text");
if (TextUtils.isEmpty(name))
throw new IllegalArgumentException(context.getString(R.string.title_no_name));
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
if (id < 0) { if (id < 0) {
EntityAnswer answer = new EntityAnswer(); EntityAnswer answer = new EntityAnswer();
@ -252,36 +335,14 @@ public class FragmentAnswer extends FragmentBase {
@Override @Override
protected void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex); if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else
Log.unexpectedError(getParentFragmentManager(), ex);
} }
}.execute(this, args, "answer:save"); }.execute(this, args, "answer:save");
} }
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
switch (requestCode) {
case REQUEST_LINK:
if (resultCode == RESULT_OK && data != null)
onLinkSelected(data.getBundleExtra("args"));
break;
case REQUEST_DELETE:
if (resultCode == RESULT_OK)
onDelete();
break;
}
} catch (Throwable ex) {
Log.e(ex);
}
}
private void onLinkSelected(Bundle args) {
String link = args.getString("link");
StyleHelper.apply(R.id.menu_link, etText, link);
}
private void onDelete() { private void onDelete() {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", id);

@ -68,15 +68,6 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/vSeparator" /> app:layout_constraintTop_toBottomOf="@id/vSeparator" />
<ImageButton
android:id="@+id/ibInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/title_setup_help"
app:layout_constraintBottom_toBottomOf="@id/etText"
app:layout_constraintEnd_toEndOf="@id/etText"
app:srcCompat="@drawable/baseline_info_24" />
<com.google.android.material.bottomnavigation.BottomNavigationView <com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/style_bar" android:id="@+id/style_bar"
android:layout_width="0dp" android:layout_width="0dp"
@ -85,23 +76,11 @@
app:itemIconTint="@color/action_foreground" app:itemIconTint="@color/action_foreground"
app:itemTextColor="@color/action_foreground" app:itemTextColor="@color/action_foreground"
app:labelVisibilityMode="unlabeled" app:labelVisibilityMode="unlabeled"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/action_answer_style" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:itemIconTint="@color/action_foreground"
app:itemTextColor="@color/action_foreground"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/action_answer" /> app:layout_constraintTop_toBottomOf="@id/etText"
app:menu="@menu/action_answer_style" />
<eu.faircode.email.ContentLoadingProgressBar <eu.faircode.email.ContentLoadingProgressBar
android:id="@+id/pbWait" android:id="@+id/pbWait"
@ -118,5 +97,5 @@
android:id="@+id/grpReady" android:id="@+id/grpReady"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
app:constraint_referenced_ids="etName,vSeparator,etText,bottom_navigation" /> app:constraint_referenced_ids="etName,cbFavorite,cbHide,vSeparator,etText" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

@ -1,13 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_delete"
android:icon="@drawable/baseline_delete_24"
android:title="@string/title_delete" />
<item
android:id="@+id/action_save"
android:icon="@drawable/baseline_save_alt_24"
android:title="@string/title_save" />
</menu>

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_help"
android:icon="@drawable/baseline_help_24"
android:title=""
app:showAsAction="always" />
<item
android:id="@+id/menu_delete"
android:title="@string/title_delete"
app:showAsAction="never" />
</menu>
Loading…
Cancel
Save