aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/util/BackgroundTask.java
blob: be5dd09f8d6c5fc6e5a8379f9d731cb568938e4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package net.lacolaco.smileessence.util;

import android.os.AsyncTask;
import net.lacolaco.smileessence.logging.Logger;

public abstract class BackgroundTask<Result, Progress> {
    private Consumer<Result> then;
    private Consumer<Progress> progress;
    private Consumer<Exception> fail;
    private Exception exception;
    private final InnerAsyncTask task;

    public BackgroundTask() {
        this.task = new InnerAsyncTask();
    }

    public BackgroundTask<Result, Progress> onDone(Consumer<Result> cb) {
        this.then = cb;
        return this;
    }

    public BackgroundTask<Result, Progress> onProgress(Consumer<Progress> cb) {
        this.progress = cb;
        return this;
    }

    public BackgroundTask<Result, Progress> onFail(Consumer<Exception> cb) {
        this.fail = cb;
        return this;
    }

    public BackgroundTask<Result, Progress> onDoneUI(Consumer<Result> cb) {
        return onDone(r -> new UIHandler().post(() -> cb.accept(r)));
    }

    public BackgroundTask<Result, Progress> onProgressUI(Consumer<Progress> cb) {
        return onProgress(p -> new UIHandler().post(() -> cb.accept(p)));
    }

    public BackgroundTask<Result, Progress> onFailUI(Consumer<Exception> cb) {
        return onFail(e -> new UIHandler().post(() -> cb.accept(e)));
    }

    public boolean cancel() {
        return task.cancel(true);
    }

    public final BackgroundTask<Result, Progress> execute() {
        task.execute();
        return this;
    }

    public final Result getImmediately() throws Exception {
        Result result = task.get();
        if (exception == null) {
            return result;
        } else {
            throw exception;
        }
    }

    protected void fail(Exception ex) {
        exception = ex;
        ex.printStackTrace();
        Logger.error(ex);

        if (!task.isCancelled() && fail != null) {
            fail.accept(ex);
        }
    }

    protected void progress(Progress value) {
        if (!task.isCancelled() && exception == null && progress != null) {
            progress.accept(value);
        }
    }

    protected abstract Result doInBackground() throws Exception;

    private class InnerAsyncTask extends AsyncTask<Void, Progress, Result> {
        @Override
        protected final void onPostExecute(Result result) {
            if (!isCancelled() && exception == null && then != null) {
                then.accept(result);
            }
        }

        @Override
        protected Result doInBackground(Void... params) {
            try {
                return BackgroundTask.this.doInBackground();
            } catch (Exception ex) {
                fail(ex);
                return null;
            }
        }
    }
}