Advertisement
Guest User

Untitled

a guest
May 25th, 2016
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.62 KB | None | 0 0
  1. package com.vyda.stream.fragments;
  2.  
  3. import android.annotation.TargetApi;
  4. import android.app.Activity;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.os.Build;
  8. import android.os.Bundle;
  9. import android.support.annotation.Nullable;
  10. import android.support.v4.app.Fragment;
  11. import android.support.v4.app.FragmentManager;
  12. import android.view.LayoutInflater;
  13. import android.view.View;
  14. import android.view.ViewGroup;
  15. import android.view.animation.Animation;
  16. import android.view.animation.AnimationUtils;
  17. import android.widget.TextView;
  18.  
  19. import com.vyda.stream.R;
  20. import com.vyda.stream.StreamApplication;
  21. import com.vyda.stream.activities.BaseActivity;
  22. import com.vyda.stream.backend.RestService;
  23. import com.vyda.stream.helpers.BusHelper;
  24. import com.vyda.stream.helpers.Global;
  25.  
  26. import javax.inject.Inject;
  27.  
  28. import butterknife.ButterKnife;
  29. import butterknife.InjectView;
  30. import butterknife.Optional;
  31.  
  32. public abstract class BaseFragment extends Fragment {
  33.  
  34.     @Inject
  35.     protected RestService restService;
  36.  
  37.     @Optional
  38.     @InjectView(R.id.popover_header_text)
  39.     protected TextView tvHeader;
  40.  
  41.    
  42.     protected int backgroundColor = 0;
  43.  
  44.     @Override
  45.     public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  46.         super.onViewCreated(view, savedInstanceState);
  47.         ((StreamApplication)getActivity().getApplication()).getComponent().inject(this);
  48.  
  49.         onViewsFragment(view, savedInstanceState);
  50.     }
  51.  
  52.     @Nullable
  53.     @Override
  54.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  55.         int contentView = onFragmentContentView();
  56.  
  57.         if(contentView == 0)
  58.             throw new IllegalArgumentException("onFragmentContentView must be not equal 0");
  59.  
  60.         View layout = inflater.inflate(contentView, container, false);
  61.         ButterKnife.inject(this, layout);
  62.  
  63.         onCreateFragmentView(layout, container, savedInstanceState);
  64.  
  65.         return layout;
  66.     }
  67.  
  68.     @Override
  69.     public void onResume() {
  70.         super.onResume();
  71.         BusHelper.getInstance().register(this);
  72.     }
  73.  
  74.     @Override
  75.     public void onPause() {
  76.         super.onPause();
  77.         BusHelper.getInstance().unregister(this);
  78.     }
  79.  
  80.     @Override
  81.     public void onDestroyView() {
  82.         super.onDestroyView();
  83.         ButterKnife.reset(this);
  84.     }
  85.  
  86.     @Override
  87.     public Animation onCreateAnimation(int transit, final boolean enter, int nextAnim) {
  88.         return AnimationUtils.loadAnimation(getActivity(), enter ?  R.anim.fade_in : R.anim.fade_out);
  89.     }
  90.  
  91.     @Override
  92.     public void onActivityResult(int requestCode, int resultCode, Intent data) {
  93.         super.onActivityResult(requestCode, resultCode, data);
  94.         if(requestCode == Global.RequestCode.RELOGIN && resultCode == Activity.RESULT_OK) {
  95.             updateViewAfterLogin();
  96.         }
  97.     }
  98.  
  99.     @Override
  100.     @TargetApi(Build.VERSION_CODES.M)
  101.     public void onAttach(Context context) {
  102.         super.onAttach(context);
  103.         onAttachFragment(context);
  104.     }
  105.  
  106.     @Override
  107.     @SuppressWarnings("deprecation")
  108.     public void onAttach(Activity activty) {
  109.         super.onAttach(activty);
  110.         onAttachFragment(activty);
  111.     }
  112.  
  113.     public boolean onBackPressed() {
  114.         return false;
  115.     }
  116.  
  117.     /**
  118.      * Use this method instead #onAttach(Activity)
  119.      * and #onAttach(Context)
  120.      * @param context
  121.      */
  122.     protected void onAttachFragment(Context context) {
  123.  
  124.     }
  125.  
  126.     protected abstract int onFragmentContentView();
  127.  
  128.     /**
  129.      * Override this method instead onCreateView
  130.      */
  131.     protected abstract void onCreateFragmentView(View v, ViewGroup container, Bundle savedInstanceState);
  132.  
  133.     /**
  134.      * Override this method instead onViewCreated
  135.      */
  136.     protected abstract void onViewsFragment(View view, Bundle savedInstanceState);
  137.  
  138.     public BaseFragment setBackground(int backgroundColor) {
  139.         this.backgroundColor = backgroundColor;
  140.         return this;
  141.     }
  142.  
  143.     public void runActivity(Class<?> className) {
  144.         ((BaseActivity) getActivity()).runActivity(className);
  145.     }
  146.  
  147.     public void runActivity(Class<?> className, Bundle bundle) {
  148.         ((BaseActivity) getActivity()).runActivity(className, bundle);
  149.     }
  150.  
  151.     public void runActivityForResult(Class<?> className, Bundle bundle, int request) {
  152.         ((BaseActivity) getActivity()).runActivityForResult(className, bundle, request);
  153.     }
  154.  
  155.     public void replaceFragment(Fragment fragment) {
  156.         ((BaseActivity) getActivity()).replaceFragment(fragment);
  157.     }
  158.  
  159.     protected void showFragment(Fragment fragment, String tag) {
  160.         ((BaseActivity) getActivity()).showFragment(fragment, tag);
  161.     }
  162.  
  163.     protected void showFragment(Fragment fragment, String tag, int id) {
  164.         ((BaseActivity) getActivity()).showFragment(fragment, tag, id);
  165.     }
  166.  
  167.     protected void showFragmentWithoutStack(Fragment fragment, String tag) {
  168.         ((BaseActivity) getActivity()).showFragmentWithoutStack(fragment, tag);
  169.     }
  170.  
  171.     protected void showFragmentWithoutStack(Fragment fragment, String tag, int id) {
  172.         ((BaseActivity) getActivity()).showFragmentWithoutStack(fragment, tag, id);
  173.     }
  174.  
  175.     protected void showChildFragment(FragmentManager fragmentManager, Fragment fragment, String tag, int id) {
  176.         ((BaseActivity) getActivity()).showChildFragment(fragmentManager, fragment, tag, id);
  177.     }
  178.  
  179.     public void showSnackbar(Fragment fragment, String tag) {
  180.         ((BaseActivity) getActivity()).showSnackbar(fragment, tag);
  181.     }
  182.  
  183.     public void updateViewAfterLogin() {}
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement