package eu.nerro.wolappla.ui;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import eu.nerro.wolappla.R;

import static eu.nerro.wolappla.util.LogUtils.LOGV;
import static eu.nerro.wolappla.util.LogUtils.makeLogTag;

/**
 * A launcher activity containing:
 * <ul>
 * <li>view to show a list of devices which can be turned on
 * <li>view to scan network for enabled devices
 * </ul>
 */
public class HomeActivity extends FragmentActivity implements ActionBar.TabListener {
  private static final String TAG = makeLogTag(HomeActivity.class);

  private static final int NAVIGATION_TABS_COUNT = 2;
  private static final int NAVIGATION_TAB_DEVICES = 0;
  private static final int NAVIGATION_TAB_SCANNING = 1;

  private ViewPager mViewPager;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    mViewPager = (ViewPager) findViewById(R.id.pager);
    final ActionBar actionBar = getActionBar();
    if (mViewPager != null && actionBar != null) {
      mViewPager.setAdapter(new HomePagerAdapter(getSupportFragmentManager()));

      LOGV(TAG, "onCreate(): navigation tabs are initialized.");
      actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
      actionBar.setDisplayHomeAsUpEnabled(false);
      actionBar.addTab(actionBar.newTab()
          .setText(R.string.title_devices)
          .setTabListener(this));
      actionBar.addTab(actionBar.newTab()
          .setText(R.string.title_scanning)
          .setTabListener(this));
    }
  }

  @Override
  public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    mViewPager.setCurrentItem(tab.getPosition());
  }

  @Override
  public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
  }

  @Override
  public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
  }

  private class HomePagerAdapter extends FragmentPagerAdapter {
    public HomePagerAdapter(FragmentManager fragmentManager) {
      super(fragmentManager);
    }

    @Override
    public Fragment getItem(int position) {
      switch (position) {
        case NAVIGATION_TAB_DEVICES:
          return new DevicesFragment();

        case NAVIGATION_TAB_SCANNING:
          return new ScanningFragment();
      }

      return null;
    }

    @Override
    public int getCount() {
      return NAVIGATION_TABS_COUNT;
    }
  }
}

@@ -742,19 +742,51 @@
 Listener
+, ViewPager.OnPageChangeListener
  %7B%0A
-
   privat
@@ -1430,16 +1430,64 @@
 ger()));
+%0A      mViewPager.setOnPageChangeListener(this);
 %0A%0A      
@@ -2182,32 +2182,32 @@
  %7D%0A%0A  @Override%0A
-
   public void on
@@ -2287,16 +2287,393 @@
  %7B%0A  %7D%0A%0A
+  @Override%0A  public void onPageSelected(int position) %7B%0A    final ActionBar actionBar = getActionBar();%0A    if (actionBar != null) %7B%0A      actionBar.setSelectedNavigationItem(position);%0A    %7D%0A  %7D%0A%0A  @Override%0A  public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) %7B%0A  %7D%0A%0A  @Override%0A  public void onPageScrollStateChanged(int state) %7B%0A  %7D%0A%0A
   privat

Fix change of tabs when swipe 'device' and 'scanning' views