com.brandao.tictactoe.statistics.StatisticFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.brandao.tictactoe.statistics.StatisticFragment.java

Source

/*
 * Copyright (c) 2016 Skytrait
 *
 * 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.
 *
 * www.skytrait.com
 *
 */

package com.brandao.tictactoe.statistics;

import android.app.Fragment;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.brandao.tictactoe.R;
import com.brandao.tictactoe.endgame.StatisticsDatabase;
import com.brandao.tictactoe.extras.Feedback;
import com.brandao.tictactoe.skytrait.SkytraitUtils;

public class StatisticFragment extends Fragment {
    public final String TAG = "StatisticFragment";

    public static StatisticFragment newInstance() {
        StatisticFragment f = new StatisticFragment();

        return f;
    }

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

        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.statistics_fragment, container, false);

        return v;
    }

    @Override
    public void onViewCreated(final View v, @Nullable Bundle savedInstanceState) {
        Typeface titleFont = Typeface.createFromAsset(getActivity().getAssets(), "fonts/DroidSans-Bold.ttf");

        if (((TextView) v.findViewById(R.id.tic)) != null) {
            ((TextView) v.findViewById(R.id.tic)).setTypeface(titleFont);
            ((TextView) v.findViewById(R.id.tac)).setTypeface(titleFont);
            ((TextView) v.findViewById(R.id.toe)).setTypeface(titleFont);
        }

        ((TextView) v.findViewById(R.id.easy_title)).setTypeface(titleFont);
        ((TextView) v.findViewById(R.id.medium_title)).setTypeface(titleFont);
        ((TextView) v.findViewById(R.id.hard_title)).setTypeface(titleFont);

        ((TextView) v.findViewById(R.id.games_won_title_easy)).setTypeface(titleFont);
        ((TextView) v.findViewById(R.id.games_lost_title_easy)).setTypeface(titleFont);
        ((TextView) v.findViewById(R.id.games_tied_title_easy)).setTypeface(titleFont);

        ((TextView) v.findViewById(R.id.games_won_num_easy)).setTypeface(titleFont);
        ((TextView) v.findViewById(R.id.games_lost_num_easy)).setTypeface(titleFont);
        ((TextView) v.findViewById(R.id.games_tied_num_easy)).setTypeface(titleFont);

        ((ImageView) v.findViewById(R.id.games_won_image_easy))
                .setBackgroundResource(R.drawable.ic_green_x_android);
        ((ImageView) v.findViewById(R.id.games_lost_image_easy)).setBackgroundResource(R.drawable.ic_red_o_android);
        ((ImageView) v.findViewById(R.id.games_tied_image_easy))
                .setBackgroundResource(R.drawable.ic_red_green_android);

        ((TextView) v.findViewById(R.id.games_won_title_med)).setTypeface(titleFont);
        ((TextView) v.findViewById(R.id.games_lost_title_med)).setTypeface(titleFont);
        ((TextView) v.findViewById(R.id.games_tied_title_med)).setTypeface(titleFont);

        ((TextView) v.findViewById(R.id.games_won_num_med)).setTypeface(titleFont);
        ((TextView) v.findViewById(R.id.games_lost_num_med)).setTypeface(titleFont);
        ((TextView) v.findViewById(R.id.games_tied_num_med)).setTypeface(titleFont);

        ((ImageView) v.findViewById(R.id.games_won_image_med)).setBackgroundResource(R.drawable.ic_green_x_android);
        ((ImageView) v.findViewById(R.id.games_lost_image_med)).setBackgroundResource(R.drawable.ic_red_o_android);
        ((ImageView) v.findViewById(R.id.games_tied_image_med))
                .setBackgroundResource(R.drawable.ic_red_green_android);

        ((TextView) v.findViewById(R.id.games_won_title_hard)).setTypeface(titleFont);
        ((TextView) v.findViewById(R.id.games_lost_title_hard)).setTypeface(titleFont);
        ((TextView) v.findViewById(R.id.games_tied_title_hard)).setTypeface(titleFont);

        ((TextView) v.findViewById(R.id.games_won_num_hard)).setTypeface(titleFont);
        ((TextView) v.findViewById(R.id.games_lost_num_hard)).setTypeface(titleFont);
        ((TextView) v.findViewById(R.id.games_tied_num_hard)).setTypeface(titleFont);

        ((ImageView) v.findViewById(R.id.games_won_image_hard))
                .setBackgroundResource(R.drawable.ic_green_x_android);
        ((ImageView) v.findViewById(R.id.games_lost_image_hard)).setBackgroundResource(R.drawable.ic_red_o_android);
        ((ImageView) v.findViewById(R.id.games_tied_image_hard))
                .setBackgroundResource(R.drawable.ic_red_green_android);

        v.findViewById(R.id.reset).setOnClickListener(new android.view.View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                SkytraitUtils
                        .promptDialog(getActivity(), getString(R.string.statistics_dialog_message_reset),
                                getString(R.string.statistics_dialog_title_reset))
                        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                StatisticsDatabase.clearStatistics(getActivity());

                                setValuesFromDatabase(v);

                                Feedback.feedback(getActivity(), R.raw.game_over_sound);

                                Snackbar.make(((StatisticActivity) getActivity()).getCoordinatorLayout(),
                                        R.string.statistics_cleared, Snackbar.LENGTH_SHORT).show();
                            }
                        }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        }).show();
            }
        });

        v.findViewById(R.id.back_button).setOnClickListener(new android.view.View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Feedback.feedback(getActivity(), R.raw.game_over_sound);

                NavUtils.navigateUpFromSameTask(getActivity());
            }
        });

        ((TextView) v.findViewById(R.id.reset)).setTypeface(titleFont);
        ((TextView) v.findViewById(R.id.back_button)).setTypeface(titleFont);

        setValuesFromDatabase(v);
    }

    public void setValuesFromDatabase(View v) {
        ((TextView) v.findViewById(R.id.games_won_num_easy))
                .setText("" + StatisticsDatabase.getGamesWonEasy(getActivity()));
        ((TextView) v.findViewById(R.id.games_lost_num_easy))
                .setText("" + StatisticsDatabase.getGamesLostEasy(getActivity()));
        ((TextView) v.findViewById(R.id.games_tied_num_easy))
                .setText("" + StatisticsDatabase.getGamesTiedEasy(getActivity()));

        ((TextView) v.findViewById(R.id.games_won_num_med))
                .setText("" + StatisticsDatabase.getGamesWonMedium(getActivity()));
        ((TextView) v.findViewById(R.id.games_lost_num_med))
                .setText("" + StatisticsDatabase.getGamesLostMedium(getActivity()));
        ((TextView) v.findViewById(R.id.games_tied_num_med))
                .setText("" + StatisticsDatabase.getGamesTiedMedium(getActivity()));

        ((TextView) v.findViewById(R.id.games_won_num_hard))
                .setText("" + StatisticsDatabase.getGamesWonHard(getActivity()));
        ((TextView) v.findViewById(R.id.games_lost_num_hard))
                .setText("" + StatisticsDatabase.getGamesLostHard(getActivity()));
        ((TextView) v.findViewById(R.id.games_tied_num_hard))
                .setText("" + StatisticsDatabase.getGamesTiedHard(getActivity()));
    }
}