aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsAbstractInsightsFragment.java
blob: 8f24efd8fbbbfe44925420a0aabad4e2027cca71 (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
package org.wordpress.android.ui.stats;


import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.wordpress.android.R;


public abstract class StatsAbstractInsightsFragment extends StatsAbstractFragment {
    public static final String TAG = StatsAbstractInsightsFragment.class.getSimpleName();

    private TextView mErrorLabel;
    private LinearLayout mEmptyModulePlaceholder;
    LinearLayout mResultContainer;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.stats_insights_generic_fragment, container, false);
        TextView moduleTitleTextView = (TextView) view.findViewById(R.id.stats_module_title);
        moduleTitleTextView.setText(getTitle());

        mEmptyModulePlaceholder = (LinearLayout) view.findViewById(R.id.stats_empty_module_placeholder);
        mResultContainer = (LinearLayout) view.findViewById(R.id.stats_module_result_container);
        mErrorLabel = (TextView) view.findViewById(R.id.stats_error_text);
        return view;
    }

    @Override
    protected void showPlaceholderUI() {
        mErrorLabel.setVisibility(View.GONE);
        mResultContainer.setVisibility(View.GONE);
        mEmptyModulePlaceholder.setVisibility(View.VISIBLE);
    }

    @Override
    protected void showErrorUI(String label) {
        if (!isAdded()) {
            return;
        }

        // Use the generic error message when the string passed to this method is null.
        if (TextUtils.isEmpty(label)) {
            label = "<b>" + getString(R.string.error_refresh_stats) + "</b>";
        }

        if (label.contains("<")) {
            mErrorLabel.setText(Html.fromHtml(label));
        } else {
            mErrorLabel.setText(label);
        }
        mErrorLabel.setVisibility(View.VISIBLE);
        mResultContainer.setVisibility(View.GONE);
        mEmptyModulePlaceholder.setVisibility(View.GONE);
    }

    /**
     * Insights module all have the same basic implementation of updateUI. Let's provide a common code here.
     */
    @Override
    protected void updateUI() {
        if (!isAdded()) {
            return;
        }

        // Another check that the data is available. At this point it should be available.
        if (!hasDataAvailable()) {
            showErrorUI();
            return;
        }

        // not an error - update the module UI here
        mErrorLabel.setVisibility(View.GONE);
        mResultContainer.setVisibility(View.VISIBLE);
        mEmptyModulePlaceholder.setVisibility(View.GONE);

        mResultContainer.removeAllViews();
    }
}