com.josecalles.porridge.group.list.GroupListFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.josecalles.porridge.group.list.GroupListFragment.java

Source

/*
 * Copyright 2015,  Jose Calles
 *
 * 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 com.josecalles.porridge.group.list;

import android.content.Context;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import butterknife.Bind;
import butterknife.ButterKnife;
import com.josecalles.porridge.R;
import com.josecalles.porridge.data.db.Group;
import com.josecalles.porridge.home.HomeActivity;
import com.josecalles.porridge.injection.component.DaggerFragmentComponent;
import com.josecalles.porridge.injection.module.GroupModule;
import java.util.List;
import javax.inject.Inject;
import jp.wasabeef.recyclerview.animators.SlideInUpAnimator;

public class GroupListFragment extends android.app.Fragment implements GroupListContract.View {

    @Bind(R.id.groups_rv)
    RecyclerView mRecyclerView;

    @Bind(R.id.progress_bar)
    ProgressBar mProgressBar;

    @Inject
    GroupListContract.Presenter mPresenter;

    private HomeActivity mActivity;
    private GroupListAdapter mAdapter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

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

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

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);
        injectPresenter();
        setupRecyclerView();

    }

    private void setupRecyclerView() {
        mAdapter = new GroupListAdapter(mActivity);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setItemAnimator(new SlideInUpAnimator());
        mProgressBar.getIndeterminateDrawable()
                .setColorFilter(ContextCompat.getColor(mActivity, R.color.md_orange_700), PorterDuff.Mode.SRC_ATOP);
        mPresenter.fetchGroups();
    }

    private void injectPresenter() {
        mActivity = (HomeActivity) getActivity();
        DaggerFragmentComponent.builder().homeComponent(mActivity.getHomeComponent())
                .groupModule(new GroupModule(this)).build().inject(this);
    }

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

    @Override
    public void onPause() {
        super.onPause();
        mPresenter.onFragmentPaused();
    }

    @Override
    public void showProgressBar() {
        mProgressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgressBar() {
        if (mProgressBar.getVisibility() == View.VISIBLE)
            mProgressBar.setVisibility(View.INVISIBLE);
    }

    @Override
    public boolean isGroupListPopulated() {
        return mAdapter.getItemCount() != 0;
    }

    @Override
    public void removeOldGroupList() {
        mAdapter.removeOldGroupList();
    }

    @Override
    public void updateGroupList(List<Group> groupList) {
        mAdapter.setNewGroupList(groupList);
    }

    public static GroupListFragment newInstance() {
        return new GroupListFragment();
    }
}