aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/widgets/PostListButton.java
blob: dfab4c22b9c8ebfca2445d1e2bf362499e0504c0 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package org.wordpress.android.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.wordpress.android.R;

/*
 * buttons in footer of post cards
 */
public class PostListButton extends LinearLayout {

    private ImageView mImageView;
    private TextView mTextView;
    private int mButtonType = BUTTON_NONE;

    // from attrs.xml
    public static final int BUTTON_NONE     = 0;
    public static final int BUTTON_EDIT     = 1;
    public static final int BUTTON_VIEW     = 2;
    public static final int BUTTON_PREVIEW  = 3;
    public static final int BUTTON_STATS    = 4;
    public static final int BUTTON_TRASH    = 5;
    public static final int BUTTON_DELETE   = 6;
    public static final int BUTTON_PUBLISH  = 7;
    public static final int BUTTON_MORE     = 8;
    public static final int BUTTON_BACK     = 9;

    public PostListButton(Context context){
        super(context);
        initView(context, null);
    }

    public PostListButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public PostListButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        inflate(context, R.layout.post_list_button, this);

        mImageView = (ImageView) findViewById(R.id.image);
        mTextView = (TextView) findViewById(R.id.text);

        int buttonType = 0;
        if (attrs != null) {
            TypedArray a = context.getTheme().obtainStyledAttributes(
                    attrs,
                    R.styleable.wpPostListButton,
                    0, 0);
            try {
                buttonType = a.getInteger(R.styleable.wpPostListButton_wpPostButtonType, 0);
            } finally {
                a.recycle();
            }
        }

        setButtonType(buttonType);
    }


    public int getButtonType() {
        return mButtonType;
    }

    public void setButtonType(int buttonType) {
        if (buttonType == mButtonType) {
            return;
        }

        mButtonType = buttonType;
        mTextView.setText(getButtonTextResId(buttonType));
        mImageView.setImageResource(getButtonIconResId(buttonType));
    }

    public static int getButtonTextResId(int buttonType) {
        switch (buttonType) {
            case BUTTON_EDIT:
                return R.string.button_edit;
            case BUTTON_VIEW:
                return R.string.button_view;
            case BUTTON_PREVIEW:
                return R.string.button_preview;
            case BUTTON_STATS:
                return R.string.button_stats;
            case BUTTON_TRASH:
                return R.string.button_trash;
            case BUTTON_DELETE:
                return R.string.button_delete;
            case BUTTON_PUBLISH:
                return R.string.button_publish;
            case BUTTON_MORE:
                return R.string.button_more;
            case BUTTON_BACK:
                return R.string.button_back;
            default:
                return 0;
        }
    }

    public static int getButtonIconResId(int buttonType) {
        switch (buttonType) {
            case BUTTON_EDIT:
                return R.drawable.noticon_edit;
            case BUTTON_VIEW:
                return R.drawable.noticon_view;
            case BUTTON_PREVIEW:
                return R.drawable.noticon_view;
            case BUTTON_STATS:
                return R.drawable.noticon_stats;
            case BUTTON_TRASH:
                return R.drawable.noticon_trash;
            case BUTTON_DELETE:
                return R.drawable.noticon_trash;
            case BUTTON_PUBLISH:
                return R.drawable.noticon_publish;
            case BUTTON_MORE:
                return R.drawable.noticon_more;
            case BUTTON_BACK:
                return R.drawable.noticon_back;
            default:
                return 0;
        }
    }
}