aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/prefs/WPStartOverPreference.java
blob: 0fed50c24c7b43ffa57adef3a690d1690bc7ef23 (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
package org.wordpress.android.ui.prefs;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import org.wordpress.android.R;
import org.wordpress.android.WordPress;
import org.wordpress.android.models.Blog;
import org.wordpress.android.util.UrlUtils;

/**
 * Calypso-style Preference that has an icon and a widget in the correct place. If there is a button
 * with id R.id.button, an onPreferenceClick listener is added.
 */

public class WPStartOverPreference extends WPPreference {
    private String mButtonText;
    private int mButtonTextColor;
    private boolean mButtonTextAllCaps;
    private Drawable mPrefIcon;

    public WPStartOverPreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.WPStartOverPreference);

        for (int i = 0; i < array.getIndexCount(); ++i) {
            int index = array.getIndex(i);
            if (index == R.styleable.WPStartOverPreference_buttonText) {
                mButtonText = array.getString(index);
            } else if (index == R.styleable.WPStartOverPreference_buttonTextColor) {
                mButtonTextColor = array.getColor(index, ContextCompat.getColor(context, R.color.black));
            } else if (index == R.styleable.WPStartOverPreference_buttonTextAllCaps) {
                mButtonTextAllCaps = array.getBoolean(index, false);
            } else if (index == R.styleable.WPStartOverPreference_preficon) {
                mPrefIcon = VectorDrawableCompat.create(context.getResources(), array.getResourceId(index, 0), null);
            }
        }

        array.recycle();
    }

    @Override
    protected void onBindView(@NonNull View view) {
        super.onBindView(view);

        if (view.findViewById(R.id.pref_icon) != null) {
            ImageView imageView = (ImageView) view.findViewById(R.id.pref_icon);
            imageView.setImageDrawable(mPrefIcon);
        }

        if (view.findViewById(R.id.button) != null) {
            final WPStartOverPreference wpStartOverPreference = this;

            Button button = (Button) view.findViewById(R.id.button);
            button.setText(mButtonText);
            button.setTextColor(mButtonTextColor);
            button.setAllCaps(mButtonTextAllCaps);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getOnPreferenceClickListener().onPreferenceClick(wpStartOverPreference);
                }
            });
        }

        if (view.findViewById(R.id.domain) != null) {
            TextView textView = (TextView) view.findViewById(R.id.domain);
            Blog blog = WordPress.getCurrentBlog();
            textView.setText(UrlUtils.getHost(blog.getHomeURL()));
        }
    }
}