Refactoring

This commit is contained in:
M66B 2020-07-25 21:11:06 +02:00
parent 9064072e2e
commit 2dd695eb18
2 changed files with 119 additions and 119 deletions

View File

@ -64,6 +64,8 @@ import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.snackbar.Snackbar;
import org.json.JSONArray;
import org.json.JSONObject;
@ -715,10 +717,69 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
return super.onOptionsItemSelected(item);
}
View getContentView() {
if (drawerLayout == null || drawerLayout.getChildCount() == 0)
return null;
return drawerLayout.getChildAt(0);
public void undo(String title, final Bundle args, final SimpleTask<Void> move, final SimpleTask<Void> show) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int undo_timeout = prefs.getInt("undo_timeout", 5000);
if (undo_timeout == 0) {
move.execute(this, args, "undo:move");
return;
}
if (drawerLayout == null || drawerLayout.getChildCount() == 0) {
Log.e("Undo: drawer missing");
return;
}
final View content = drawerLayout.getChildAt(0);
final Snackbar snackbar = Snackbar.make(content, title, Snackbar.LENGTH_INDEFINITE)
.setGestureInsetBottomIgnored(true);
snackbar.setAction(R.string.title_undo, new View.OnClickListener() {
@Override
public void onClick(View v) {
snackbar.dismiss();
snackbar.getView().setTag(true);
show.execute(ActivityView.this, args, "undo:show");
}
});
snackbar.addCallback(new Snackbar.Callback() {
private int margin;
@Override
public void onShown(Snackbar sb) {
ViewGroup.MarginLayoutParams lparam = (ViewGroup.MarginLayoutParams) content.getLayoutParams();
margin = lparam.bottomMargin;
lparam.bottomMargin += snackbar.getView().getHeight();
content.setLayoutParams(lparam);
}
@Override
public void onDismissed(Snackbar transientBottomBar, int event) {
ViewGroup.MarginLayoutParams lparam = (ViewGroup.MarginLayoutParams) content.getLayoutParams();
lparam.bottomMargin = margin;
content.setLayoutParams(lparam);
}
});
snackbar.show();
// Wait
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.i("Move timeout");
if (snackbar.getView().getTag() != null)
return;
// Remove snackbar
if (snackbar.isShown())
snackbar.dismiss();
move.execute(ActivityView.this, args, "undo:move");
}
}, undo_timeout);
}
private void checkFirst() {

View File

@ -211,7 +211,6 @@ import me.everything.android.ui.overscroll.VerticalOverScrollBounceEffectDecorat
import me.everything.android.ui.overscroll.adapters.RecyclerViewOverScrollDecorAdapter;
import static android.app.Activity.RESULT_OK;
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
import static android.text.format.DateUtils.DAY_IN_MILLIS;
import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
import static android.text.format.DateUtils.FORMAT_SHOW_WEEKDAY;
@ -4712,18 +4711,18 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
}.execute(this, args, "messages:move");
}
private void moveUndo(ArrayList<MessageTarget> result) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
final int undo_timeout = prefs.getInt("undo_timeout", 5000);
Bundle args = new Bundle();
private void moveUndo(final ArrayList<MessageTarget> result) {
final Bundle args = new Bundle();
args.putParcelableArrayList("result", result);
new SimpleTask<ArrayList<MessageTarget>>() {
new SimpleTask<Void>() {
@Override
protected ArrayList<MessageTarget> onExecute(Context context, Bundle args) throws Throwable {
protected Void onExecute(Context context, Bundle args) {
ArrayList<MessageTarget> result = args.getParcelableArrayList("result");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final int undo_timeout = prefs.getInt("undo_timeout", 5000);
DB db = DB.getInstance(context);
long now = new Date().getTime();
@ -4744,11 +4743,11 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
db.endTransaction();
}
return result;
return null;
}
@Override
protected void onExecuted(Bundle args, final ArrayList<MessageTarget> result) {
protected void onExecuted(Bundle args, Void data) {
if (viewType == AdapterMessage.ViewType.THREAD) {
PagedList<TupleMessageEx> messages = adapter.getCurrentList();
if (messages != null && result.size() > 0) {
@ -4757,10 +4756,11 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
}
}
final Context context = getContext().getApplicationContext();
Thread thread = new Thread(new Runnable() {
SimpleTask<Void> move = new SimpleTask<Void>() {
@Override
public void run() {
protected Void onExecute(Context context, Bundle args) {
ArrayList<MessageTarget> result = args.getParcelableArrayList("result");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
@ -4777,126 +4777,65 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
}
db.setTransactionSuccessful();
} catch (Throwable ex) {
Log.e(ex);
} finally {
db.endTransaction();
}
ServiceSynchronize.eval(context, "move");
return null;
}
}, "messages:movetimeout");
thread.setPriority(THREAD_PRIORITY_BACKGROUND);
if (undo_timeout == 0) {
thread.start();
return;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.e(ex);
}
};
FragmentActivity factivity = getActivity();
if (!(factivity instanceof ActivityView)) {
SimpleTask<Void> show = new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
ArrayList<MessageTarget> result = args.getParcelableArrayList("result");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
for (MessageTarget target : result) {
Log.i("Move undo id=" + target.id);
db.message().setMessageUiBusy(target.id, null);
db.message().setMessageUiHide(target.id, false);
db.message().setMessageLastAttempt(target.id, new Date().getTime());
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.e(ex);
}
};
FragmentActivity activity = getActivity();
if (!(activity instanceof ActivityView)) {
Log.e("Undo: activity missing");
return;
}
ActivityView activity = (ActivityView) factivity;
View content = activity.getContentView();
if (content == null) {
Log.e("Undo: view missing");
return;
}
// Show undo snackbar
final Snackbar snackbar = Snackbar.make(
content,
getString(R.string.title_move_undo, getDisplay(result), result.size()),
Snackbar.LENGTH_INDEFINITE)
.setGestureInsetBottomIgnored(true);
snackbar.setAction(R.string.title_undo, new View.OnClickListener() {
@Override
public void onClick(View v) {
snackbar.dismiss();
snackbar.getView().setTag(true);
Bundle args = new Bundle();
args.putParcelableArrayList("result", result);
// Show message again
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
ArrayList<MessageTarget> result = args.getParcelableArrayList("result");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
for (MessageTarget target : result) {
Log.i("Move undo id=" + target.id);
db.message().setMessageUiBusy(target.id, null);
db.message().setMessageUiHide(target.id, false);
db.message().setMessageLastAttempt(target.id, new Date().getTime());
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(activity, activity, args, "messages:moveundo");
}
});
snackbar.addCallback(new Snackbar.Callback() {
private int margin;
@Override
public void onShown(Snackbar sb) {
ViewGroup.MarginLayoutParams lparam = (ViewGroup.MarginLayoutParams) content.getLayoutParams();
margin = lparam.bottomMargin;
lparam.bottomMargin += snackbar.getView().getHeight();
content.setLayoutParams(lparam);
}
@Override
public void onDismissed(Snackbar transientBottomBar, int event) {
ViewGroup.MarginLayoutParams lparam = (ViewGroup.MarginLayoutParams) content.getLayoutParams();
lparam.bottomMargin = margin;
content.setLayoutParams(lparam);
}
});
snackbar.show();
// Wait
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.i("Move timeout");
if (snackbar.getView().getTag() != null)
return;
// Remove snackbar
if (snackbar.isShown())
snackbar.dismiss();
thread.start();
}
}, undo_timeout);
String title = getString(R.string.title_move_undo, getDisplay(result), result.size());
((ActivityView) activity).undo(title, args, move, show);
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, args, "messages:movehide");
}.execute(this, args, "undo:hide");
}
private String getDisplay(ArrayList<MessageTarget> result) {