package com.mobilesolutionworks.android.imagepaging;

import android.support.v4.view.PagerAdapter;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

/**
 * Created by yunarta on 18/9/14.
 */
public abstract class ZoomableDraweeViewPagerAdapter extends PagerAdapter {

    SparseArray<ZoomableDraweeViewForPager> mImageViewPagerMap;

    public ZoomableDraweeViewPagerAdapter() {
        mImageViewPagerMap = new SparseArray<ZoomableDraweeViewForPager>();
    }

    protected abstract void setupImageView(ImageView imageView, int position);

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ZoomableDraweeViewForPager imageView = new ZoomableDraweeViewForPager(container.getContext());

        mImageViewPagerMap.put(position, imageView);

        ImageViewPager pager = (ImageViewPager) container;

        ViewGroup.LayoutParams params = pager.generateDefaultLayoutParams();
        params.width = container.getMeasuredWidth();
        params.height = container.getMeasuredHeight();

        container.addView(imageView, params);
        setupImageView(imageView, position);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object view) {
        container.removeView((View) view);
        mImageViewPagerMap.delete(position);
    }

    public ZoomableDraweeViewForPager getImage(int position) {
        return mImageViewPagerMap.get(position);
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        super.setPrimaryItem(container, position, object);

        ZoomableDraweeViewForPager pager;

        pager = mImageViewPagerMap.get(position - 1);
        if (pager != null) {
            pager.resetZoom();
        }

        pager = mImageViewPagerMap.get(position + 1);
        if (pager != null) {
            pager.resetZoom();
        }
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    public boolean allowPageScroll(int position) {
//        ZoomableDraweeViewForPager imageView = mImageViewPagerMap.get(position);
//        return imageView.isHittingEdge(-dx) && imageView.isScrollable();
        return false;
    }
}

@@ -216,16 +216,180 @@
 eView;%0A%0A
+import jp.co.vcube.android.gate.ux.fresco.plugin.zoomable.DefaultZoomableController;%0Aimport jp.co.vcube.android.gate.ux.fresco.plugin.zoomable.ZoomableController;%0A%0A
 /**%0A * C
@@ -960,16 +960,276 @@
 text());
+%0A        ZoomableController c = imageView.getZoomableController();%0A        if (c instanceof DefaultZoomableController) %7B%0A            DefaultZoomableController controller = (DefaultZoomableController) c;%0A            controller.setMaxScaleFactor(4.0f);%0A        %7D
 %0A%0A      

Increase zoom factor