package org.mifos.selfserviceapp.ui.activities;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import org.mifos.selfserviceapp.R;
import org.mifos.selfserviceapp.ui.activities.base.BaseActivity;
import org.mifos.selfserviceapp.ui.fragments.SavingAccountsDetailFragment;
import org.mifos.selfserviceapp.ui.fragments.SavingAccountsTransactionFragment;
import org.mifos.selfserviceapp.utils.Constants;

/**
 * Created by Rajan Maurya on 05/03/17.
 */

public class SavingsAccountContainerActivity extends BaseActivity {

    private long savingsId;
    private Menu menu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_container);

        savingsId = getIntent().getExtras().getLong(Constants.SAVINGS_ID);

        addOnBackStackChangedListener();
        replaceFragment(SavingAccountsDetailFragment.newInstance(savingsId), false, R.id.container);
        showBackButton();
    }

    private void addOnBackStackChangedListener() {
        if (getSupportFragmentManager() == null) {
            return;
        }
        getSupportFragmentManager()
                .addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
                    @Override
                    public void onBackStackChanged() {
                        FragmentManager fragmentManager = getSupportFragmentManager();
                        Fragment fragment = fragmentManager.findFragmentById(R.id.container);
                        if (fragment instanceof SavingAccountsTransactionFragment) {
                            setMenuTransactionButton(false);
                        } else {
                            setMenuTransactionButton(true);
                        }
                    }
                });
    }

    private void setMenuTransactionButton(boolean isEnabled) {
        menu.getItem(0).setEnabled(isEnabled);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_saving_account, menu);
        this.menu = menu;
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_transactions:
                replaceFragment(SavingAccountsTransactionFragment.newInstance(savingsId),
                        true, R.id.container);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

@@ -72,95 +72,8 @@
 le;%0A
-import android.support.v4.app.Fragment;%0Aimport android.support.v4.app.FragmentManager;%0A
 impo
@@ -94,16 +94,16 @@
 w.Menu;%0A
+
 import a
@@ -614,31 +614,8 @@
 Id;%0A
-    private Menu menu;%0A
     
@@ -859,49 +859,8 @@
 );%0A%0A
-        addOnBackStackChangedListener();%0A
     
@@ -993,971 +993,8 @@
  %7D%0A%0A
-    private void addOnBackStackChangedListener() %7B%0A        if (getSupportFragmentManager() == null) %7B%0A            return;%0A        %7D%0A        getSupportFragmentManager()%0A                .addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() %7B%0A                    @Override%0A                    public void onBackStackChanged() %7B%0A                        FragmentManager fragmentManager = getSupportFragmentManager();%0A                        Fragment fragment = fragmentManager.findFragmentById(R.id.container);%0A                        if (fragment instanceof SavingAccountsTransactionFragment) %7B%0A                            setMenuTransactionButton(false);%0A                        %7D else %7B%0A                            setMenuTransactionButton(true);%0A                        %7D%0A                    %7D%0A                %7D);%0A    %7D%0A%0A    private void setMenuTransactionButton(boolean isEnabled) %7B%0A        menu.getItem(0).setEnabled(isEnabled);%0A    %7D%0A%0A
     
@@ -1102,24 +1102,24 @@
 Inflater();%0A
+
         infl
@@ -1170,34 +1170,8 @@
 u);%0A
-        this.menu = menu;%0A
     

Revert "Fix: Transaction option menu button is disabled when its fragment is active (#189)"