package org.fossasia.pslab.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import org.fossasia.pslab.fragment.ControlFragmentAdvanced;
import org.fossasia.pslab.fragment.ControlFragmentMain;
import org.fossasia.pslab.fragment.ControlFragmentRead;

import org.fossasia.pslab.R;

/**
 * Created by viveksb007 on 10/5/17.
 */

public class ControlActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_control);
        BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);

        bottomNavigationView.setOnNavigationItemSelectedListener
                (new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        Fragment selectedFragment = null;
                        switch (item.getItemId()) {
                            case R.id.action_item1:
                                selectedFragment = ControlFragmentMain.newInstance();
                                break;
                            case R.id.action_item2:
                                selectedFragment = ControlFragmentRead.newInstance();
                                break;
                            case R.id.action_item3:
                                selectedFragment = ControlFragmentAdvanced.newInstance();
                                break;
                        }
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.frame_layout_control, selectedFragment);
                        transaction.commit();
                        return true;
                    }
                });

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_layout_control, ControlFragmentMain.newInstance());
        transaction.commit();
    }

    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle(R.string.closing_control_title)
                .setMessage(R.string.closing_control_message)
                .setPositiveButton(R.string.dialog_yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                })
                .setNegativeButton(R.string.dialog_no, null)
                .show();
    }
}

@@ -1365,16 +1365,132 @@
 = null;%0A
+                        Fragment fragment=getSupportFragmentManager().findFragmentById(R.id.frame_layout_control);%0A%0A
         
@@ -1585,16 +1585,100 @@
 _item1:%0A
+                                if (!(fragment instanceof ControlFragmentMain))%0A    
         
@@ -1846,16 +1846,100 @@
 _item2:%0A
+                                if (!(fragment instanceof ControlFragmentRead))%0A    
         
@@ -2107,16 +2107,104 @@
 _item3:%0A
+                                if (!(fragment instanceof ControlFragmentAdvanced))%0A    
         
@@ -2342,32 +2342,92 @@
               %7D%0A
+                        if (selectedFragment != null) %7B%0A    
                 
@@ -2532,32 +2532,36 @@
                 
+    
 transaction.repl
@@ -2626,32 +2626,36 @@
                 
+ 
+   
 transaction.comm
@@ -2656,24 +2656,50 @@
 n.commit();%0A
+                        %7D%0A
             

Fix #696 removed unnecessary refresh in ControlActivity (#697)