ca.zadrox.dota2esportticker.ui.TeamOverviewFragment.java Source code

Java tutorial

Introduction

Here is the source code for ca.zadrox.dota2esportticker.ui.TeamOverviewFragment.java

Source

/*
 * Copyright 2014 Nicholas Liu
 *
 * 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 ca.zadrox.dota2esportticker.ui;

import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.Arrays;

import ca.zadrox.dota2esportticker.R;
import ca.zadrox.dota2esportticker.data.http.team.model.Team;
import ca.zadrox.dota2esportticker.util.LogUtils;

/**
 * Created by Acco on 12/11/2014.
 */
public class TeamOverviewFragment extends Fragment {
    private static final String TAG = TeamOverviewFragment.class.getSimpleName();

    private Team mTeam;

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

        return mRoot;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LogUtils.LOGD(TAG, "onActivityCreated");

    }

    @Override
    public void onResume() {
        super.onResume();

        if (getActivity() instanceof TeamDetailActivity) {
            mTeam = ((TeamDetailActivity) getActivity()).getTeam();
        }

        if (mTeam == null) {
            LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mBroadcastReceiver,
                    new IntentFilter(TeamDetailActivity.STATUS_LOAD_COMPLETE));
        } else {
            handleTeamInfo();
        }

        LogUtils.LOGD(TAG, "onResume");
        LogUtils.LOGD(TAG, "mTeam: " + (mTeam == null ? "null" : "set"));
    }

    @Override
    public void onPause() {
        super.onPause();
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mBroadcastReceiver);
        LogUtils.LOGD(TAG, "onPause");
    }

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(TeamDetailActivity.STATUS_LOAD_COMPLETE)
                    && getActivity() instanceof TeamDetailActivity) {
                mTeam = ((TeamDetailActivity) getActivity()).getTeam();
                handleTeamInfo();
            }
        }
    };

    private void handleTeamInfo() {
        LogUtils.LOGD(TAG, Arrays.toString(mTeam.recentMatches));
    }
}