aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/reader/actions/ReaderActions.java
blob: 5dd01edefff4a20b96789addda438728ab76d0d0 (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
package org.wordpress.android.ui.reader.actions;

import org.wordpress.android.models.ReaderBlog;
import org.wordpress.android.models.ReaderComment;

/**
 * classes in this package serve as a middleman between local data and server data - used by
 * reader activities/fragments/adapters that wish to perform actions on posts, blogs & topics,
 * or wish to get the latest data from the server.
 *
 * methods in this package which change state (like, follow, etc.) are generally optimistic
 * and work like this:
 *
 *  1. caller asks method to send a network request which changes state
 *  2. method changes state in local data and returns to caller *before* network request completes
 *  3. caller can access local state change without waiting for the network request
 *  4. if the network request fails, the method restores the previous state of the local data
 *  5. if caller passes a listener, it can be alerted to the actual success/failure of the request
 *
 *  note that all methods MUST be called from the UI thread in order to guarantee that listeners
 *  are alerted on the UI thread
 */
public class ReaderActions {

    private ReaderActions() {
        throw new AssertionError();
    }

    /*
     * listener when a specific action is performed (liking a post, etc.)
     */
    public interface ActionListener {
        void onActionResult(boolean succeeded);
    }

    /*
     * helper routine for telling an action listener the call succeeded or failed w/o having to null check
     */
    public static void callActionListener(ActionListener actionListener, boolean succeeded) {
        if (actionListener != null) {
            actionListener.onActionResult(succeeded);
        }
    }

    /*
     * listener when the failure status code is required
     */
    public interface OnRequestListener {
        void onSuccess();
        void onFailure(int statusCode);
    }

    /*
     * listener when submitting a comment
     */
    public interface CommentActionListener {
        void onActionResult(boolean succeeded, ReaderComment newComment);
    }

    /*
     * result when updating data (getting latest posts or comments for a post, etc.)
     */
    public enum UpdateResult {
        HAS_NEW,    // new posts/comments/etc. have been retrieved
        CHANGED,    // no new posts/comments, but existing ones have changed
        UNCHANGED,  // no new or changed posts/comments
        FAILED;     // request failed
        public boolean isNewOrChanged() {
            return (this == HAS_NEW || this == CHANGED);
        }
    }
    public interface UpdateResultListener {
        void onUpdateResult(UpdateResult result);
    }

    /*
     * used by adapters to notify when more data should be loaded
     */
    public interface DataRequestedListener {
        void onRequestData();
    }

    /*
     * used by blog preview when requesting latest info about a blog
     */
    public interface UpdateBlogInfoListener {
        void onResult(ReaderBlog blogInfo);
    }

}