Browse Source

Generic simple loader

main
M66B 6 years ago
parent
commit
4aca3880e4
6 changed files with 116 additions and 84 deletions
  1. +20
    -19
      app/src/main/java/eu/faircode/email/ActivityView.java
  2. +11
    -14
      app/src/main/java/eu/faircode/email/FragmentAbout.java
  3. +3
    -4
      app/src/main/java/eu/faircode/email/FragmentCompose.java
  4. +61
    -34
      app/src/main/java/eu/faircode/email/FragmentMessage.java
  5. +10
    -8
      app/src/main/java/eu/faircode/email/FragmentMessages.java
  6. +11
    -5
      app/src/main/java/eu/faircode/email/SimpleLoader.java

+ 20
- 19
app/src/main/java/eu/faircode/email/ActivityView.java 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());


+ 11
- 14
app/src/main/java/eu/faircode/email/FragmentAbout.java 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());
}


+ 3
- 4
app/src/main/java/eu/faircode/email/FragmentCompose.java 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);
}


+ 61
- 34
app/src/main/java/eu/faircode/email/FragmentMessage.java 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);


+ 10
- 8
app/src/main/java/eu/faircode/email/FragmentMessages.java 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());
}


+ 11
- 5
app/src/main/java/eu/faircode/email/SimpleLoader.java 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;
}


Loading…
Cancel
Save