package com.etiennelawlor.imagegallery.library.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.etiennelawlor.imagegallery.library.R;
import com.etiennelawlor.imagegallery.library.adapters.ImageGalleryAdapter;
import com.etiennelawlor.imagegallery.library.enums.PaletteColorType;

import java.util.List;

public class ImageGalleryActivity extends AppCompatActivity {

    // region Member Variables
    private ImageGalleryAdapter mImageGalleryAdapter;
    private List<String> mImages;
    private PaletteColorType mPaletteColorType;

    private Toolbar mToolbar;
    private ViewPager mViewPager;
    // endregion

    // region Listeners
    private ViewPager.OnPageChangeListener mViewPagerOnPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (mViewPager != null) {
                mViewPager.setCurrentItem(position);

                setActionBarTitle(position);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };
    // endregion

    // region Lifecycle Methods
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_image_gallery);

        bindViews();

        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null)
            actionBar.setDisplayHomeAsUpEnabled(true);


        Intent intent = getIntent();
        if (intent != null) {
            Bundle extras = intent.getExtras();
            if (extras != null) {
                mImages = extras.getStringArrayList("images");
                mPaletteColorType = (PaletteColorType) extras.get("palette_color_type");
            }
        }

        mImageGalleryAdapter = new ImageGalleryAdapter(mImages, mPaletteColorType);
        mViewPager.setAdapter(mImageGalleryAdapter);

        if (mImages.size() > 1) {
            setActionBarTitle(0);
        }

        mViewPager.addOnPageChangeListener(mViewPagerOnPageChangeListener);
    }
    // endregion

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mViewPager.removeOnPageChangeListener(mViewPagerOnPageChangeListener);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }

    // region Helper Methods
    private void setActionBarTitle(int position) {
        if (mViewPager != null) {
            int totalPages = mViewPager.getAdapter().getCount();
            getSupportActionBar().setTitle(String.format("%d of %d", (position + 1), totalPages));
        }
    }

    private void bindViews() {
        mViewPager = (ViewPager) findViewById(R.id.vp);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
    }
    // endregion
}

@@ -2205,187 +2205,13 @@
     
-mImageGalleryAdapter = new ImageGalleryAdapter(mImages, mPaletteColorType);%0A        mViewPager.setAdapter(mImageGalleryAdapter);%0A%0A        if (mImages.size() %3E 1) %7B%0A            
 set
+Up
 Acti
@@ -2225,9 +2225,8 @@
 tle(
-0
 );%0A 
@@ -2236,84 +2236,23 @@
     
-%7D%0A%0A        mViewPager.addOnPageChangeListener(mViewPagerOnPageChangeListener
+setUpViewPager(
 );%0A 
@@ -3138,32 +3138,412 @@
 .toolbar);%0A    %7D
+%0A%0A    private void setUpActionBarTitle()%7B%0A        if (mImages.size() %3E 1) %7B%0A            setActionBarTitle(0);%0A        %7D%0A    %7D%0A%0A    private void setUpViewPager()%7B%0A        mImageGalleryAdapter = new ImageGalleryAdapter(mImages, mPaletteColorType);%0A        mViewPager.setAdapter(mImageGalleryAdapter);%0A        mViewPager.addOnPageChangeListener(mViewPagerOnPageChangeListener);%0A    %7D
 %0A    // endregio

Clean up