/*
* Copyright (C) 2017 MINDORKS NEXTGEN PRIVATE LIMITED
* 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
*
* https://mindorks.com/license/apache-v2
*
* 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.mindorks.framework.mvp.ui.base;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.view.View;
import com.mindorks.framework.mvp.di.component.ActivityComponent;
import butterknife.Unbinder;
/**
* Created by janisharali on 27/01/17.
*/
public abstract class BaseFragment extends Fragment implements MvpView {
private BaseActivity mActivity;
private Unbinder mUnBinder;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(false);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof BaseActivity) {
BaseActivity activity = (BaseActivity) context;
this.mActivity = activity;
activity.onFragmentAttached();
}
}
@Override
public void showLoading() {
if (mActivity != null) {
mActivity.showLoading();
}
}
@Override
public void hideLoading() {
if (mActivity != null) {
mActivity.hideLoading();
}
}
@Override
public void onError(String message) {
if (mActivity != null) {
mActivity.onError(message);
}
}
@Override
public void onError(@StringRes int resId) {
if (mActivity != null) {
mActivity.onError(resId);
}
}
@Override
public boolean isNetworkConnected() {
if (mActivity != null) {
return mActivity.isNetworkConnected();
}
return false;
}
@Override
public void onDetach() {
mActivity = null;
super.onDetach();
}
@Override
public void hideKeyboard() {
if (mActivity != null) {
mActivity.hideKeyboard();
}
}
@Override
public void openActivityOnTokenExpire() {
if (mActivity != null) {
mActivity.openActivityOnTokenExpire();
}
}
public ActivityComponent getActivityComponent() {
return mActivity.getActivityComponent();
}
public BaseActivity getBaseActivity() {
return mActivity;
}
public void setUnBinder(Unbinder unBinder) {
mUnBinder = unBinder;
}
protected abstract void setUp(View view);
@Override
public void onDestroy() {
if (mUnBinder != null) {
mUnBinder.unbind();
}
super.onDestroy();
}
public interface Callback {
void onFragmentAttached();
void onFragmentDetached(String tag);
}
}