aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/plans/PlanFragment.java
blob: e22ee623c55e68edd4874dc26f4a6024055a517d (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package org.wordpress.android.ui.plans;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
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 com.android.volley.toolbox.NetworkImageView;

import org.wordpress.android.R;
import org.wordpress.android.WordPress;
import org.wordpress.android.ui.plans.models.Feature;
import org.wordpress.android.ui.plans.models.Plan;
import org.wordpress.android.ui.plans.models.PlanFeaturesHighlightSection;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.HtmlUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

public class PlanFragment extends Fragment {
    private static final String SITE_PLAN = "SITE_PLAN";

    private ViewGroup mPlanContainerView;
    private Plan mPlanDetails;

    public static PlanFragment newInstance(Plan sitePlan) {
        PlanFragment fragment = new PlanFragment();
        fragment.setSitePlan(sitePlan);
        AppLog.d(AppLog.T.PLANS, "PlanFragment newInstance");
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            if (savedInstanceState.containsKey(SITE_PLAN)) {
                Serializable serial = savedInstanceState.getSerializable(SITE_PLAN);
                if (serial instanceof Plan) {
                    setSitePlan((Plan) serial);
                }
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.plan_fragment, container, false);
        mPlanContainerView = (LinearLayout) rootView.findViewById(R.id.plan_container);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        showPlans();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putSerializable(SITE_PLAN, mPlanDetails);
        super.onSaveInstanceState(outState);
    }

    private void showPlans() {
        if (!isAdded()) return;

        if (mPlanDetails == null) {
            // TODO This should never happen - Fix this. Close the activity?
            AppLog.w(AppLog.T.PLANS, "empty plan data in fragment");
            return;
        }

        int iconSize = getActivity().getResources().getDimensionPixelSize(R.dimen.plan_icon_size);
        NetworkImageView imgPlan = (NetworkImageView) getView().findViewById(R.id.image_plan_icon);
        String iconUrl = PlansUtils.getIconUrlForPlan(mPlanDetails, iconSize);
        if (!TextUtils.isEmpty(iconUrl)) {
            imgPlan.setImageUrl(iconUrl, WordPress.imageLoader);
            imgPlan.setVisibility(View.VISIBLE);
        } else {
            imgPlan.setVisibility(View.GONE);
        }

        // show product short name in bold, ex: "WordPress.com <b>Premium</b>"
        TextView txtProductName = (TextView) getView().findViewById(R.id.text_product_name);
        String productShortName = mPlanDetails.getProductNameShort();
        String productName = mPlanDetails.getProductName().replace(productShortName,
                "<b>" + productShortName + "</b>");
        txtProductName.setText(Html.fromHtml(productName));

        TextView txtTagLine = (TextView) getView().findViewById(R.id.text_tagline);
        txtTagLine.setText(HtmlUtils.fastUnescapeHtml(mPlanDetails.getTagline()));

        addFeaturesToHighlight();
    }

    private void addFeaturesToHighlight() {
        HashMap<String, Feature> globalFeatures = PlansUtils.getFeatures();
        if (globalFeatures == null) {
            AppLog.w(AppLog.T.PLANS, "no global features");
            return;
        }

        ArrayList<PlanFeaturesHighlightSection> sectionsToHighlight = mPlanDetails.getFeaturesHighlightSections();
        if (sectionsToHighlight == null) {
            AppLog.w(AppLog.T.PLANS, "no sections to highlight");
            return;
        }

        for (PlanFeaturesHighlightSection section : sectionsToHighlight) {
            // add section title if it's not empty
            addSectionTitle(section.getTitle());
            // add features to highlight in this section
            ArrayList<String> featuresToHighlight = section.getFeatures();
            for (String featureSlug : featuresToHighlight) {
                addFeature(globalFeatures.get(featureSlug));
            }
        }
    }

    private void addSectionTitle(String title) {
        if (TextUtils.isEmpty(title)) return;

        LayoutInflater inflater = LayoutInflater.from(getActivity());
        ViewGroup view = (ViewGroup) inflater.inflate(R.layout.plan_section_title, mPlanContainerView, false);

        TextView txtTitle = (TextView) view.findViewById(R.id.text_section_title);
        txtTitle.setText(HtmlUtils.fastUnescapeHtml(title));

        mPlanContainerView.addView(view);
    }

    private void addFeature(Feature feature) {
        if (feature == null) return;

        LayoutInflater inflater = LayoutInflater.from(getActivity());
        ViewGroup view = (ViewGroup) inflater.inflate(R.layout.plan_feature_item, mPlanContainerView, false);

        TextView txtTitle = (TextView) view.findViewById(R.id.text_feature_title);
        TextView txtDescription = (TextView) view.findViewById(R.id.text_feature_description);
        String title = HtmlUtils.fastUnescapeHtml(feature.getTitleForPlan(mPlanDetails.getProductID()));
        String description = HtmlUtils.fastUnescapeHtml(feature.getDescriptionForPlan(mPlanDetails.getProductID()));
        txtTitle.setText(title);
        txtDescription.setText(description);

        // TODO: right now icon is always empty, so we show noticon_publish as a placeholder
        NetworkImageView imgIcon = (NetworkImageView) view.findViewById(R.id.image_icon);
        String iconUrl = feature.getIconForPlan(mPlanDetails.getProductID());
        if (!TextUtils.isEmpty(iconUrl)) {
            imgIcon.setImageUrl(iconUrl, WordPress.imageLoader);
        } else {
            imgIcon.setDefaultImageResId(R.drawable.noticon_publish);
        }

        mPlanContainerView.addView(view);
    }

    private void setSitePlan(@NonNull Plan sitePlan) {
        mPlanDetails = sitePlan;
    }

}