/*
 * Copyright (C) 2017 Yota Devices LLC
 * Copyright (C) 2015 Diego Gómez Olvera
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yotadevices.widget.rtlviewpager;

import android.database.DataSetObserver;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

/**
 * PagerAdapter decorator.
 */
class PagerAdapterWrapper extends PagerAdapter {

    @NonNull
    private final PagerAdapter adapter;

    protected PagerAdapterWrapper(@NonNull PagerAdapter adapter) {
        this.adapter = adapter;
    }

    @NonNull
    public PagerAdapter getInnerAdapter() {
        return adapter;
    }

    @Override
    public int getCount() {
        return adapter.getCount();
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        return adapter.getPageTitle(position);
    }

    @Override
    public float getPageWidth(int position) {
        return adapter.getPageWidth(position);
    }

    @Override
    public int getItemPosition(Object object) {
        return adapter.getItemPosition(object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        return adapter.instantiateItem(container, position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        adapter.destroyItem(container, position, object);
    }

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

    @Override
    public void notifyDataSetChanged() {
        adapter.notifyDataSetChanged();
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        adapter.registerDataSetObserver(observer);
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        adapter.unregisterDataSetObserver(observer);
    }

    @Override
    public Parcelable saveState() {
        return adapter.saveState();
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
        adapter.restoreState(state, loader);
    }

    @Override
    public void startUpdate(ViewGroup container) {
        adapter.startUpdate(container);
    }

    @Override
    public void finishUpdate(ViewGroup container) {
        adapter.finishUpdate(container);
    }
}

@@ -685,16 +685,59 @@
 pager;%0A%0A
+import android.database.DataSetObservable;%0A
 import a
@@ -1082,16 +1082,110 @@
 adapter;
+%0A    @NonNull%0A    private final DataSetObservable dataSetObservable = new DataSetObservable();
 %0A%0A    pr
@@ -1269,32 +1269,438 @@
 pter = adapter;%0A
+        this.adapter.registerDataSetObserver(new DataSetObserver() %7B%0A            @Override%0A            public void onChanged() %7B%0A                PagerAdapterWrapper.super.notifyDataSetChanged();%0A                dataSetObservable.notifyChanged();%0A            %7D%0A%0A            @Override%0A            public void onInvalidated() %7B%0A                dataSetObservable.notifyInvalidated();%0A            %7D%0A        %7D);%0A
     %7D%0A%0A    @NonN
@@ -3023,23 +3023,33 @@
         
-a
 da
-pter
+taSetObservable
 .registe
@@ -3041,39 +3041,32 @@
 ervable.register
-DataSet
 Observer(observe
@@ -3168,23 +3168,33 @@
         
-a
 da
-pter
+taSetObservable
 .unregis
@@ -3188,39 +3188,32 @@
 vable.unregister
-DataSet
 Observer(observe

Fix crash when the adapter changes its content.