Generic simple loader

This commit is contained in:
M66B 2018-08-11 05:02:13 +00:00
parent 8532027f01
commit 4aca3880e4
6 changed files with 116 additions and 84 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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