Generic simple loader

pull/50/head
M66B 7 years ago
parent 8532027f01
commit 4aca3880e4

@ -181,9 +181,9 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
fragmentTransaction.commit(); fragmentTransaction.commit();
} }
new SimpleLoader() { new SimpleLoader<Long>() {
@Override @Override
public Object onLoad(Bundle args) throws Throwable { public Long onLoad(Bundle args) throws Throwable {
File file = new File(getCacheDir(), "crash.log"); File file = new File(getCacheDir(), "crash.log");
if (file.exists()) { if (file.exists()) {
DB db = DB.getInstance(ActivityView.this); DB db = DB.getInstance(ActivityView.this);
@ -241,12 +241,12 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
} }
@Override @Override
public void onLoaded(Bundle args, Result result) { public void onLoaded(Bundle args, Long id) {
if (result.ex == null && result.data != null) if (id != null)
startActivity( startActivity(
new Intent(ActivityView.this, ActivityCompose.class) new Intent(ActivityView.this, ActivityCompose.class)
.putExtra("action", "edit") .putExtra("action", "edit")
.putExtra("id", (Long) result.data)); .putExtra("id", id));
} }
}.load(this, LOADER_EXCEPTION, new Bundle()); }.load(this, LOADER_EXCEPTION, new Bundle());
@ -348,7 +348,7 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("time", new Date().getTime()); args.putLong("time", new Date().getTime());
new SimpleLoader() { new SimpleLoader<Object>() {
@Override @Override
public Object onLoad(Bundle args) { public Object onLoad(Bundle args) {
long time = args.getLong("time"); long time = args.getLong("time");
@ -361,9 +361,8 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
} }
@Override @Override
public void onLoaded(Bundle args, Result result) { public void onException(Bundle args, Throwable ex) {
if (result.ex != null) Toast.makeText(ActivityView.this, ex.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(ActivityView.this, result.ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(this, LOADER_SEEN_UNTIL, args); }.load(this, LOADER_SEEN_UNTIL, args);
} }
@ -471,7 +470,7 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
} else if (ACTION_VIEW_MESSAGE.equals(intent.getAction())) { } else if (ACTION_VIEW_MESSAGE.equals(intent.getAction())) {
new SimpleLoader() { new SimpleLoader<Object>() {
@Override @Override
public Object onLoad(Bundle args) { public Object onLoad(Bundle args) {
long id = args.getLong("id"); long id = args.getLong("id");
@ -503,15 +502,17 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
} }
@Override @Override
public void onLoaded(Bundle args, Result result) { public void onLoaded(Bundle args, Object result) {
if (result.ex == null) {
FragmentMessage fragment = new FragmentMessage(); FragmentMessage fragment = new FragmentMessage();
fragment.setArguments(args); fragment.setArguments(args);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("message"); fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("message");
fragmentTransaction.commit(); fragmentTransaction.commit();
} else }
Toast.makeText(ActivityView.this, result.ex.toString(), Toast.LENGTH_LONG).show();
@Override
public void onException(Bundle args, Throwable ex) {
Toast.makeText(ActivityView.this, ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(ActivityView.this, LOADER_MESSAGE_VIEW, intent.getExtras()); }.load(ActivityView.this, LOADER_MESSAGE_VIEW, intent.getExtras());

@ -30,8 +30,6 @@ import android.widget.Toast;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.Date; import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.mail.Address; import javax.mail.Address;
import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetAddress;
@ -40,8 +38,6 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
public class FragmentAbout extends FragmentEx { public class FragmentAbout extends FragmentEx {
private ExecutorService executor = Executors.newCachedThreadPool();
@Override @Override
@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) {
@ -58,9 +54,9 @@ public class FragmentAbout extends FragmentEx {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
btnDebugInfo.setEnabled(false); btnDebugInfo.setEnabled(false);
new SimpleLoader() { new SimpleLoader<Long>() {
@Override @Override
public Object onLoad(Bundle args) throws UnsupportedEncodingException { public Long onLoad(Bundle args) throws UnsupportedEncodingException {
DB db = DB.getInstance(getContext()); DB db = DB.getInstance(getContext());
EntityFolder drafts = db.folder().getPrimaryDrafts(); EntityFolder drafts = db.folder().getPrimaryDrafts();
@ -88,16 +84,17 @@ public class FragmentAbout extends FragmentEx {
} }
@Override @Override
public void onLoaded(Bundle args, Result result) { public void onLoaded(Bundle args, Long id) {
btnDebugInfo.setEnabled(true); btnDebugInfo.setEnabled(true);
if (result.ex == null) {
long id = (Long) result.data;
startActivity(new Intent(getContext(), ActivityCompose.class) startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "edit") .putExtra("action", "edit")
.putExtra("id", id)); .putExtra("id", id));
} else }
Toast.makeText(getContext(), executor.toString(), Toast.LENGTH_LONG).show();
@Override
public void onException(Bundle args, Throwable ex) {
btnDebugInfo.setEnabled(true);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(FragmentAbout.this, ActivityView.LOADER_DEBUG_INFO, new Bundle()); }.load(FragmentAbout.this, ActivityView.LOADER_DEBUG_INFO, new Bundle());
} }

@ -386,7 +386,7 @@ public class FragmentCompose extends FragmentEx {
args.putLong("id", getArguments().getLong("id")); args.putLong("id", getArguments().getLong("id"));
args.putParcelable("uri", data.getData()); args.putParcelable("uri", data.getData());
new SimpleLoader() { new SimpleLoader<Object>() {
@Override @Override
public Object onLoad(Bundle args) throws IOException { public Object onLoad(Bundle args) throws IOException {
Cursor cursor = null; Cursor cursor = null;
@ -454,9 +454,8 @@ public class FragmentCompose extends FragmentEx {
} }
@Override @Override
public void onLoaded(Bundle args, Result result) { public void onException(Bundle args, Throwable ex) {
if (result.ex != null) Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(getContext(), result.ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(this, ActivityCompose.LOADER_COMPOSE_ATTACHMENT, args); }.load(this, ActivityCompose.LOADER_COMPOSE_ATTACHMENT, args);
} }

@ -392,7 +392,7 @@ public class FragmentMessage extends FragmentEx {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", id);
new SimpleLoader() { new SimpleLoader<Object>() {
@Override @Override
public Object onLoad(Bundle args) { public Object onLoad(Bundle args) {
long id = args.getLong("id"); long id = args.getLong("id");
@ -418,12 +418,16 @@ public class FragmentMessage extends FragmentEx {
} }
@Override @Override
public void onLoaded(Bundle args, Result result) { public void onLoaded(Bundle args, Object data) {
item.setEnabled(true); item.setEnabled(true);
item.setIcon(icon); item.setIcon(icon);
}
if (result.ex != null) @Override
Toast.makeText(getContext(), result.ex.toString(), Toast.LENGTH_LONG).show(); public void onException(Bundle args, Throwable ex) {
item.setEnabled(true);
item.setIcon(icon);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(this, ActivityView.LOADER_MESSAGE_SEEN, args); }.load(this, ActivityView.LOADER_MESSAGE_SEEN, args);
} }
@ -438,9 +442,9 @@ public class FragmentMessage extends FragmentEx {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", id);
new SimpleLoader() { new SimpleLoader<Long>() {
@Override @Override
public Object onLoad(Bundle args) { public Long onLoad(Bundle args) {
long id = args.getLong("id"); long id = args.getLong("id");
DB db = DB.getInstance(getContext()); DB db = DB.getInstance(getContext());
EntityMessage draft = db.message().getMessage(id); EntityMessage draft = db.message().getMessage(id);
@ -453,17 +457,20 @@ public class FragmentMessage extends FragmentEx {
} }
@Override @Override
public void onLoaded(Bundle args, Result result) { public void onLoaded(Bundle args, Long id) {
item.setEnabled(true); item.setEnabled(true);
item.setIcon(icon); item.setIcon(icon);
if (result.ex == null)
getContext().startActivity( getContext().startActivity(
new Intent(getContext(), ActivityCompose.class) new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "edit") .putExtra("action", "edit")
.putExtra("id", (long) result.data)); .putExtra("id", id));
else }
Toast.makeText(getContext(), result.ex.toString(), Toast.LENGTH_LONG).show();
@Override
public void onException(Bundle args, Throwable ex) {
item.setEnabled(true);
item.setIcon(icon);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(this, ActivityView.LOADER_MESSAGE_EDIT, args); }.load(this, ActivityView.LOADER_MESSAGE_EDIT, args);
} }
@ -496,7 +503,7 @@ public class FragmentMessage extends FragmentEx {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", id);
new SimpleLoader() { new SimpleLoader<Object>() {
@Override @Override
public Object onLoad(Bundle args) { public Object onLoad(Bundle args) {
long id = args.getLong("id"); long id = args.getLong("id");
@ -529,12 +536,16 @@ public class FragmentMessage extends FragmentEx {
} }
@Override @Override
public void onLoaded(Bundle args, Result result) { public void onLoaded(Bundle args, Object result) {
item.setEnabled(true); item.setEnabled(true);
item.setIcon(icon); item.setIcon(icon);
}
if (result.ex != null) @Override
Toast.makeText(getContext(), result.ex.toString(), Toast.LENGTH_LONG).show(); public void onException(Bundle args, Throwable ex) {
item.setEnabled(true);
item.setIcon(icon);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(FragmentMessage.this, ActivityView.LOADER_MESSAGE_SPAM, args); }.load(FragmentMessage.this, ActivityView.LOADER_MESSAGE_SPAM, args);
} }
@ -561,7 +572,7 @@ public class FragmentMessage extends FragmentEx {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", id);
new SimpleLoader() { new SimpleLoader<Object>() {
@Override @Override
public Object onLoad(Bundle args) { public Object onLoad(Bundle args) {
long id = args.getLong("id"); long id = args.getLong("id");
@ -585,12 +596,16 @@ public class FragmentMessage extends FragmentEx {
} }
@Override @Override
public void onLoaded(Bundle args, Result result) { public void onLoaded(Bundle args, Object result) {
item.setEnabled(true); item.setEnabled(true);
item.setIcon(icon); item.setIcon(icon);
}
if (result.ex != null) @Override
Toast.makeText(getContext(), result.ex.toString(), Toast.LENGTH_LONG).show(); public void onException(Bundle args, Throwable ex) {
item.setEnabled(true);
item.setIcon(icon);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(FragmentMessage.this, ActivityView.LOADER_MESSAGE_TRASH, args); }.load(FragmentMessage.this, ActivityView.LOADER_MESSAGE_TRASH, args);
} }
@ -606,7 +621,7 @@ public class FragmentMessage extends FragmentEx {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", id);
new SimpleLoader() { new SimpleLoader<Object>() {
@Override @Override
public Object onLoad(Bundle args) { public Object onLoad(Bundle args) {
long id = args.getLong("id"); long id = args.getLong("id");
@ -639,12 +654,16 @@ public class FragmentMessage extends FragmentEx {
} }
@Override @Override
public void onLoaded(Bundle args, Result result) { public void onLoaded(Bundle args, Object result) {
item.setEnabled(true); item.setEnabled(true);
item.setIcon(icon); item.setIcon(icon);
}
if (result.ex != null) @Override
Toast.makeText(getContext(), result.ex.toString(), Toast.LENGTH_LONG).show(); public void onException(Bundle args, Throwable ex) {
item.setEnabled(true);
item.setIcon(icon);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(FragmentMessage.this, ActivityView.LOADER_MESSAGE_TRASH, args); }.load(FragmentMessage.this, ActivityView.LOADER_MESSAGE_TRASH, args);
} }
@ -667,7 +686,7 @@ public class FragmentMessage extends FragmentEx {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", id);
new SimpleLoader() { new SimpleLoader<Object>() {
@Override @Override
public Object onLoad(Bundle args) { public Object onLoad(Bundle args) {
long id = args.getLong("id"); long id = args.getLong("id");
@ -700,12 +719,16 @@ public class FragmentMessage extends FragmentEx {
} }
@Override @Override
public void onLoaded(Bundle args, Result result) { public void onLoaded(Bundle args, Object result) {
item.setEnabled(true); item.setEnabled(true);
item.setIcon(icon); item.setIcon(icon);
}
if (result.ex != null) @Override
Toast.makeText(getContext(), result.ex.toString(), Toast.LENGTH_LONG).show(); public void onException(Bundle args, Throwable ex) {
item.setEnabled(true);
item.setIcon(icon);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(FragmentMessage.this, ActivityView.LOADER_MESSAGE_ARCHIVE, args); }.load(FragmentMessage.this, ActivityView.LOADER_MESSAGE_ARCHIVE, args);
} }
@ -789,7 +812,7 @@ public class FragmentMessage extends FragmentEx {
Bundle args = ((MoveLoader) loader).args; Bundle args = ((MoveLoader) loader).args;
args.putLong("target", target.getItemId()); args.putLong("target", target.getItemId());
new SimpleLoader() { new SimpleLoader<Object>() {
@Override @Override
public Object onLoad(Bundle args) { public Object onLoad(Bundle args) {
long id = args.getLong("id"); long id = args.getLong("id");
@ -822,12 +845,16 @@ public class FragmentMessage extends FragmentEx {
} }
@Override @Override
public void onLoaded(Bundle args, Result result) { public void onLoaded(Bundle args, Object result) {
item.setEnabled(true); item.setEnabled(true);
item.setIcon(icon); item.setIcon(icon);
}
if (result.ex != null) @Override
Toast.makeText(getContext(), result.ex.toString(), Toast.LENGTH_LONG).show(); public void onException(Bundle args, Throwable ex) {
item.setEnabled(true);
item.setIcon(icon);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(FragmentMessage.this, ActivityView.LOADER_MESSAGE_MOVE, args); }.load(FragmentMessage.this, ActivityView.LOADER_MESSAGE_MOVE, args);

@ -150,9 +150,9 @@ public class FragmentMessages extends FragmentEx {
} }
}); });
new SimpleLoader() { new SimpleLoader<Long>() {
@Override @Override
public Object onLoad(Bundle args) throws Throwable { public Long onLoad(Bundle args) throws Throwable {
long folder = (args == null ? -1 : args.getLong("folder", -1)); long folder = (args == null ? -1 : args.getLong("folder", -1));
long thread = (args == null ? -1 : args.getLong("thread", -1)); // message ID long thread = (args == null ? -1 : args.getLong("thread", -1)); // message ID
@ -168,12 +168,14 @@ public class FragmentMessages extends FragmentEx {
} }
@Override @Override
public void onLoaded(Bundle args, Result result) { public void onLoaded(Bundle args, Long account) {
if (result.ex == null) { fab.setTag(account);
fab.setTag(result.data);
fab.setVisibility(View.VISIBLE); fab.setVisibility(View.VISIBLE);
} else }
Toast.makeText(getContext(), result.ex.toString(), Toast.LENGTH_LONG).show();
@Override
public void onException(Bundle args, Throwable ex) {
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(this, ActivityView.LOADER_MESSAGE_ACCOUNT, getArguments()); }.load(this, ActivityView.LOADER_MESSAGE_ACCOUNT, getArguments());
} }

@ -10,7 +10,7 @@ import androidx.loader.app.LoaderManager;
import androidx.loader.content.AsyncTaskLoader; import androidx.loader.content.AsyncTaskLoader;
import androidx.loader.content.Loader; import androidx.loader.content.Loader;
public abstract class SimpleLoader { public abstract class SimpleLoader<T> {
private Context context; private Context context;
private LoaderManager manager; private LoaderManager manager;
@ -26,11 +26,14 @@ public abstract class SimpleLoader {
manager.restartLoader(id, args, callbacks).forceLoad(); manager.restartLoader(id, args, callbacks).forceLoad();
} }
public Object onLoad(Bundle args) throws Throwable { public T onLoad(Bundle args) throws Throwable {
return null; return null;
} }
public void onLoaded(Bundle args, Result result) { public void onLoaded(Bundle args, T data) {
}
public void onException(Bundle args, Throwable ex) {
} }
private static class CommonLoader extends AsyncTaskLoader<Result> { private static class CommonLoader extends AsyncTaskLoader<Result> {
@ -72,7 +75,10 @@ public abstract class SimpleLoader {
manager.destroyLoader(loader.getId()); manager.destroyLoader(loader.getId());
CommonLoader common = (CommonLoader) loader; CommonLoader common = (CommonLoader) loader;
onLoaded(common.args, data); if (data.ex == null)
onLoaded(common.args, (T) data.data);
else
onException(common.args, data.ex);
common.args = null; common.args = null;
common.loader = null; common.loader = null;
@ -85,7 +91,7 @@ public abstract class SimpleLoader {
} }
}; };
public static class Result { private static class Result {
Throwable ex; Throwable ex;
Object data; Object data;
} }

Loading…
Cancel
Save