package fr.utc.assos.uvweb.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

import java.util.Locale;

import fr.utc.assos.uvweb.R;
import fr.utc.assos.uvweb.ui.fragment.NewsfeedFragment;
import fr.utc.assos.uvweb.ui.view.SlidingTabLayout;
import fr.utc.assos.uvweb.ui.fragment.UvListFragment;

public class MainActivity extends ToolbarActivity {
    private static final int FRAGMENT_COUNT = 2;
    private static final int FRAGMENT_POSITION_NEWSFEED = 0;
    private static final int FRAGMENT_POSITION_UVLIST = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);

        PagerAdapter adapter = new HomeFragmentPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);

        slidingTabLayout.setSelectedIndicatorColors(getResources().getColor(R.color.white));
        slidingTabLayout.setDividerColors(getResources().getColor(android.R.color.transparent));
        slidingTabLayout.setCustomTabViewTextColor(getResources().getColor(R.color.white));
        slidingTabLayout.setViewPager(pager);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_about:
                startActivity(new Intent(this, AboutActivity.class));
                return true;
            case R.id.action_licenses:
                startActivity(new Intent(this, LicensesActivity.class));
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private class HomeFragmentPagerAdapter extends FragmentStatePagerAdapter {
        public HomeFragmentPagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case FRAGMENT_POSITION_NEWSFEED:
                    return new NewsfeedFragment();
                case FRAGMENT_POSITION_UVLIST:
                    return new UvListFragment();
                default:
                    throw new IllegalArgumentException();
            }
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            Locale locale = Locale.getDefault();
            switch (position) {
                case FRAGMENT_POSITION_NEWSFEED:
                    return getString(R.string.title_newsfeed).toUpperCase(locale);
                case FRAGMENT_POSITION_UVLIST:
                    return getString(R.string.title_uv_list).toUpperCase(locale);
            }
            return null;
        }
    }
}

@@ -84,17 +84,104 @@
 oid.
-os.Bundle
+content.SharedPreferences;%0Aimport android.os.Bundle;%0Aimport android.preference.PreferenceManager
 ;%0Aim
@@ -906,16 +906,211 @@
 T = 1;%0A%0A
+    private static final String PREF_LAST_TAB = %22pref_last_tab%22;%0A    private static final int PREF_LAST_TAB_DEFAULT = 0;%0A%0A    private SharedPreferences preferences;%0A    private ViewPager pager;%0A%0A
     @Ove
@@ -1216,24 +1216,25 @@
 State);%0A
+%0A
         
 ViewPage
@@ -1225,25 +1225,91 @@
         
-ViewPager
+preferences = PreferenceManager.getDefaultSharedPreferences(this);%0A%0A       
  pager =
@@ -1568,16 +1568,569 @@
 dapter);
+%0A        slidingTabLayout.setOnPageChangeListener(new ViewPager.OnPageChangeListener() %7B%0A            @Override%0A            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) %7B%0A                // no-op%0A            %7D%0A%0A            @Override%0A            public void onPageSelected(int position) %7B%0A                saveLastTabIndex(position);%0A            %7D%0A%0A            @Override%0A            public void onPageScrollStateChanged(int state) %7B%0A                // no-op%0A            %7D%0A        %7D);%0A%0A        loadLastTabIndex();
 %0A%0A      
@@ -2450,32 +2450,290 @@
 (pager);%0A    %7D%0A%0A
+    private void loadLastTabIndex() %7B%0A        pager.setCurrentItem(preferences.getInt(PREF_LAST_TAB, PREF_LAST_TAB_DEFAULT));%0A    %7D%0A%0A    private void saveLastTabIndex(int position) %7B%0A        preferences.edit().putInt(PREF_LAST_TAB, position).apply();%0A    %7D%0A%0A
     @Override%0A  

Save last selected tab in preferences