zack.yovel.clear.controller.foreground.DayFragment.java Source code

Java tutorial

Introduction

Here is the source code for zack.yovel.clear.controller.foreground.DayFragment.java

Source

/*
 * Copyright (c) 2014 Yehezkel (Zack) Yovel
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package zack.yovel.clear.controller.foreground;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import zack.yovel.clear.R;
import zack.yovel.clear.infrastructure.model.datapoints.DataPoint;
import zack.yovel.clear.infrastructure.model.datapoints.Icon;
import zack.yovel.clear.util.ConvertUtil;

public class DayFragment extends Fragment {

    public static final String EXTRA_DATA_POINT = "yovel.zack.clear.EXTRA_DATA_POINT";
    private DataPoint dayDataPoint;

    public static DayFragment getInstance(DataPoint dayDataPoint) {
        DayFragment result = new DayFragment();
        Bundle args = new Bundle(1);
        Gson gson = new Gson();
        String dataPointJson = gson.toJson(dayDataPoint);
        args.putString(EXTRA_DATA_POINT, dataPointJson);
        result.setArguments(args);
        return result;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setDataPoint();
    }

    private void setDataPoint() {
        Bundle args = getArguments();
        Gson gson = new Gson();
        String dataPointJson = args.getString(EXTRA_DATA_POINT);
        dayDataPoint = gson.fromJson(dataPointJson, DataPoint.class);
    }

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

    private void initDisplay() {
        initTextViews();
        initIcon();
    }

    private void initIcon() {

        // get the view
        ImageView ivWeatherIcon = (ImageView) getView().findViewById(R.id.ivWeatherIconCurrently);

        if (ivWeatherIcon != null) {

            // get the icon
            String iconName = dayDataPoint.getIcon();
            Icon icon = ConvertUtil.iconStringToIconEnum(iconName);

            // set the icon to the view
            ivWeatherIcon.setImageResource(icon.resourceId);
        }
    }

    private void initTextViews() {
        initTemps();
        initInfo();
    }

    private void initInfo() {

        // get the view
        TextView tvInfo = (TextView) getView().findViewById(R.id.tvInfoCurrently);

        if (tvInfo != null) {

            // get the template from the view
            int templateResourceId = R.string.template_info_day;
            CharSequence templateTemperatures = getResources().getString(templateResourceId);

            // format the text
            String[] placeHolders = getInfoPlaceHolders();
            int directionResourceId = ConvertUtil.bearingTo16PointsResourceIds(dayDataPoint.getWindBearing());

            String[] values = getInfoValues(directionResourceId);
            CharSequence info = TextUtils.replace(templateTemperatures, placeHolders, values);

            // set the text to the view
            tvInfo.setText(info);
        }
    }

    private String[] getInfoPlaceHolders() {
        return argsToArray("{humidity}", "{windspeed}", "{windunit}", "{winddir}");
    }

    private String[] getInfoValues(int directionResourceId) {
        return argsToArray(String.valueOf((int) (dayDataPoint.getHumidity() * 100)),
                String.valueOf(dayDataPoint.getWindSpeed()), getString(R.string.wind_speed_units),
                getString(directionResourceId));
    }

    /**
     * Using the power of variable length arguments
     * to create a String[] from an arbitrary group of Strings.
     *
     * @param args
     * @return
     */
    private String[] argsToArray(String... args) {
        return args;
    }

    private void initTemps() {
        // get the view
        TextView tvTemps = (TextView) getView().findViewById(R.id.tvTempsCurrently);
        tvTemps.setTextSize(28);

        if (tvTemps != null) {

            // get the template
            int templateResourceId = R.string.template_temperatures_daily;
            CharSequence templateTemperatures = Html.fromHtml(getResources().getString(templateResourceId));

            // format the text
            String[] placeHolders = getTempsPlaceHolders();
            String[] values = getTempsValues(dayDataPoint);
            CharSequence info = TextUtils.replace(templateTemperatures, placeHolders, values);

            // set the text to the view
            tvTemps.setText(info);
        }
    }

    private String[] getTempsValues(DataPoint dataPoint) {
        return argsToArray(String.valueOf(dataPoint.getTemperatureMin()),
                String.valueOf(dataPoint.getTemperatureMax()));
    }

    private String[] getTempsPlaceHolders() {
        return argsToArray("{min}", "{max}");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_current, container, false);
        return view;
    }
}