package me.echeung.moemoekyun.ui.base;

import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public abstract class BaseFragment<T extends ViewDataBinding> extends Fragment {

    protected T binding;

    // Receiver
    private IntentFilter intentFilter;
    private BroadcastReceiver intentReceiver;
    private boolean receiverRegistered = false;

    @LayoutRes
    protected abstract int getLayout();

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, getLayout(), container, false);

        intentReceiver = getBroadcastReceiver();
        intentFilter = getIntentFilter();

        registerReceiver();

        return binding.getRoot();
    }

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

        if (!receiverRegistered) {
            registerReceiver();
        }
    }

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

        unregisterReceiver();
    }

    @Override
    public void onDestroy() {
        unregisterReceiver();

        if (binding != null) {
            binding.unbind();
        }

        super.onDestroy();
    }

    protected BroadcastReceiver getBroadcastReceiver() {
        return null;
    }

    protected IntentFilter getIntentFilter() {
        return null;
    }

    private void registerReceiver() {
        if (intentReceiver != null && intentFilter != null) {
            getActivity().registerReceiver(intentReceiver, intentFilter);
            receiverRegistered = true;
        }
    }

    private void unregisterReceiver() {
        if (receiverRegistered) {
            getActivity().unregisterReceiver(intentReceiver);
            receiverRegistered = false;
        }
    }

}

@@ -1140,267 +1140,8 @@
  %7D%0A%0A
-    @Override%0A    public void onResume() %7B%0A        super.onResume();%0A%0A        if (!receiverRegistered) %7B%0A            registerReceiver();%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void onPause() %7B%0A        super.onPause();%0A%0A        unregisterReceiver();%0A    %7D%0A%0A
     
@@ -1527,16 +1527,39 @@
     if (
+!receiverRegistered && 
 intentRe

Fix favorites not showing up after logging in