Refactoring

This commit is contained in:
M66B 2021-03-28 19:56:35 +02:00
parent ac9e9efcbd
commit 9a1ee17165
3 changed files with 174 additions and 174 deletions

View File

@ -211,15 +211,36 @@ public class ActivityCompose extends ActivityBase implements FragmentManager.OnB
Intent.ACTION_SEND_MULTIPLE.equals(action)); Intent.ACTION_SEND_MULTIPLE.equals(action));
} }
static void undoSend(long id, final Context context, final LifecycleOwner owner, final FragmentManager manager) { static void undoSend(final long id, final Context context, final LifecycleOwner owner, final FragmentManager manager) {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", id);
new SimpleTask<EntityMessage>() { new SimpleTask<Long>() {
@Override @Override
protected EntityMessage onExecute(Context context, Bundle args) { protected Long onExecute(Context context, Bundle args) {
long id = args.getLong("id"); long id = args.getLong("id");
return undoSend(id, context);
}
@Override
protected void onExecuted(Bundle args, Long id) {
if (id == null)
return;
context.startActivity(
new Intent(context, ActivityCompose.class)
.putExtra("action", "edit")
.putExtra("id", id));
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(manager, ex, !(ex instanceof IllegalArgumentException));
}
}.execute(context, owner, args, "undo:sent");
}
static Long undoSend(long id, Context context) {
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
EntityOperation operation = db.operation().getOperation(id, EntityOperation.SEND); EntityOperation operation = db.operation().getOperation(id, EntityOperation.SEND);
@ -280,22 +301,6 @@ public class ActivityCompose extends ActivityBase implements FragmentManager.OnB
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel("send:" + id, 1); nm.cancel("send:" + id, 1);
return message; return message.id;
}
@Override
protected void onExecuted(Bundle args, EntityMessage draft) {
if (draft != null)
context.startActivity(
new Intent(context, ActivityCompose.class)
.putExtra("action", "edit")
.putExtra("id", draft.id));
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(manager, ex, !(ex instanceof IllegalArgumentException));
}
}.execute(context, owner, args, "undo:sent");
} }
} }

View File

@ -746,29 +746,21 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
public void undo(String title, Runnable move, Runnable show) { public void undo(String title, final Bundle args, final SimpleTask<Void> move, final SimpleTask<Void> show) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int undo_timeout = prefs.getInt("undo_timeout", 5000); int undo_timeout = prefs.getInt("undo_timeout", 5000);
if (undo_timeout == 0) if (undo_timeout == 0)
try { move.execute(this, args, "undo:move");
move.run();
} catch (Throwable ex) {
Log.e(ex);
}
else else
undo(undo_timeout, title, move, show); undo(undo_timeout, title, args, move, show);
} }
public void undo(long undo_timeout, String title, final Runnable move, final Runnable show) { public void undo(long undo_timeout, String title, final Bundle args, final SimpleTask move, final SimpleTask show) {
if (drawerLayout == null || drawerLayout.getChildCount() == 0) { if (drawerLayout == null || drawerLayout.getChildCount() == 0) {
Log.e("Undo: drawer missing"); Log.e("Undo: drawer missing");
if (show != null) if (show != null)
try { show.execute(this, args, "undo:show");
show.run();
} catch (Throwable ex) {
Log.e(ex);
}
return; return;
} }
@ -785,11 +777,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
Log.i("Undo timeout"); Log.i("Undo timeout");
snackbar.dismiss(); snackbar.dismiss();
if (move != null) if (move != null)
try { move.execute(ActivityView.this, args, "undo:move");
move.run();
} catch (Throwable ex) {
Log.e(ex);
}
} }
}; };
@ -800,11 +788,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
getMainHandler().removeCallbacks(timeout); getMainHandler().removeCallbacks(timeout);
snackbar.dismiss(); snackbar.dismiss();
if (show != null) if (show != null)
try { show.execute(ActivityView.this, args, "undo:show");
show.run();
} catch (Throwable ex) {
Log.e(ex);
}
} }
}); });
@ -1327,6 +1311,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
} }
private void onUndoSend(Intent intent) { private void onUndoSend(Intent intent) {
long id = intent.getLongExtra("id", -1);
int delayed = intent.getIntExtra("delayed", 0); int delayed = intent.getIntExtra("delayed", 0);
long scheduled = intent.getLongExtra("scheduled", 0); long scheduled = intent.getLongExtra("scheduled", 0);
long now = new Date().getTime(); long now = new Date().getTime();
@ -1338,13 +1323,31 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
if (delayed * 1000L < UNDO_SEND_DELAY * 2 || scheduled - now < UNDO_SEND_DELAY * 2) if (delayed * 1000L < UNDO_SEND_DELAY * 2 || scheduled - now < UNDO_SEND_DELAY * 2)
return; return;
undo(UNDO_SEND_DELAY, getString(R.string.title_sending), null, new Runnable() { SimpleTask<Long> task = new SimpleTask<Long>() {
@Override @Override
public void run() { protected Long onExecute(Context context, Bundle args) {
long id = intent.getLongExtra("id", -1); long id = args.getLong("id");
ActivityCompose.undoSend(id, ActivityView.this, ActivityView.this, getSupportFragmentManager()); return ActivityCompose.undoSend(id, context);
} }
});
@Override
protected void onExecuted(Bundle args, Long id) {
if (id == null)
return;
startActivity(
new Intent(ActivityView.this, ActivityCompose.class)
.putExtra("action", "edit")
.putExtra("id", id));
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getSupportFragmentManager(), ex, !(ex instanceof IllegalArgumentException));
}
};
undo(UNDO_SEND_DELAY, getString(R.string.title_sending), intent.getExtras(), null, task);
} }
private BroadcastReceiver receiver = new BroadcastReceiver() { private BroadcastReceiver receiver = new BroadcastReceiver() {

View File

@ -5399,12 +5399,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
return; return;
} }
String title = getString(R.string.title_move_undo, getDisplay(result, true), result.size()); SimpleTask<Void> move = new SimpleTask<Void>() {
((ActivityView) activity).undo(title,
new Runnable() {
@Override
public void run() {
new SimpleTask<Void>() {
@Override @Override
protected Void onExecute(Context context, Bundle args) { protected Void onExecute(Context context, Bundle args) {
ArrayList<MessageTarget> result = args.getParcelableArrayList("result"); ArrayList<MessageTarget> result = args.getParcelableArrayList("result");
@ -5438,13 +5433,9 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
protected void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
Log.e(ex); Log.e(ex);
} }
}.execute(FragmentMessages.this, args, "undo:move"); };
}
}, SimpleTask<Void> show = new SimpleTask<Void>() {
new Runnable() {
@Override
public void run() {
new SimpleTask<Void>() {
@Override @Override
protected Void onExecute(Context context, Bundle args) { protected Void onExecute(Context context, Bundle args) {
ArrayList<MessageTarget> result = args.getParcelableArrayList("result"); ArrayList<MessageTarget> result = args.getParcelableArrayList("result");
@ -5472,9 +5463,10 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
protected void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
Log.e(ex); Log.e(ex);
} }
}.execute(FragmentMessages.this, args, "undo:show"); };
}
}); String title = getString(R.string.title_move_undo, getDisplay(result, true), result.size());
((ActivityView) activity).undo(title, args, move, show);
} }
@Override @Override