com.ap.github.base.BaseFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.ap.github.base.BaseFragment.java

Source

/*
 * Copyright (C) 2016 AdvancingPainters (https://github.com/AdvancingPainters).
 *
 * 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.ap.github.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.CheckResult;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.ap.github.ui.activitys.SimpleFragmentActivity;
import com.trello.rxlifecycle.components.support.RxFragment;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;

/**
 * base fragment
 * Created by Spencer on 4/1/16.
 */
public abstract class BaseFragment extends RxFragment {

    protected Context mContext;
    protected Context mApplicationContext;
    protected Activity mActivity;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        mContext = activity.getBaseContext();
        mApplicationContext = activity.getApplicationContext();
        mActivity = activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(getResourceId(), container, false);
        ButterKnife.bind(this, rootView);

        if (needRegisterEventBus()) {
            EventBus.getDefault().register(this);
        }

        init();
        return rootView;
    }

    @LayoutRes
    protected abstract int getResourceId();

    protected abstract void init();

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

        ButterKnife.unbind(this);

        if (needRegisterEventBus()) {
            EventBus.getDefault().unregister(this);
        }
    }

    public void finish() {
        getActivity().finish();
    }

    protected boolean needRegisterEventBus() {
        return true;
    }

    public void showProgressBar() {
        if (getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).showProgressBar();
        }
    }

    public void hideProgressBar() {
        if (getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).hideProgressBar();
        }
    }

    public boolean progressBarIsShowing() {
        return getActivity() instanceof BaseActivity && ((BaseActivity) getActivity()).progressBarIsShowing();
    }

    public void setOnRefreshListener(@NonNull SwipeRefreshLayout.OnRefreshListener refreshListener) {
        if (getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).setRefreshListener(refreshListener);
        }
    }

    public void needShowLeftButton(boolean needShow) {
        Button leftButton = getLeftButton();
        if (leftButton == null) {
            return;
        }

        leftButton.setVisibility(needShow ? View.VISIBLE : View.INVISIBLE);
    }

    public void needShowRightButton(boolean needShow) {
        Button rightButton = getRightButton();
        if (rightButton == null) {
            return;
        }

        rightButton.setVisibility(needShow ? View.VISIBLE : View.INVISIBLE);
    }

    @CheckResult
    @Nullable
    public Button getLeftButton() {
        if (!(getActivity() instanceof SimpleFragmentActivity)) {
            return null;
        }

        return ((SimpleFragmentActivity) getActivity()).getLeftButton();
    }

    @CheckResult
    @Nullable
    public Button getRightButton() {
        if (!(getActivity() instanceof SimpleFragmentActivity)) {
            return null;
        }

        return ((SimpleFragmentActivity) getActivity()).getRightButton();
    }

    public void setOnLeftButtonClickListener(
            @NonNull SimpleFragmentActivity.OnLeftButtonClickListener onLeftButtonClickListener) {
        if (getActivity() instanceof SimpleFragmentActivity) {
            ((SimpleFragmentActivity) getActivity()).setOnLeftButtonClickListener(onLeftButtonClickListener);
        }
    }

    public void setOnRightButtonClickListener(
            @NonNull SimpleFragmentActivity.OnRightButtonClickListener onRightButtonClickListener) {
        if (getActivity() instanceof SimpleFragmentActivity) {
            ((SimpleFragmentActivity) getActivity()).setOnRightButtonClickListener(onRightButtonClickListener);
        }
    }
}