|
| 1 | +package org.readium.r2.navigator.pager; |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright 2016–2020 Duolingo |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +import android.database.DataSetObserver; |
| 20 | +import android.os.Parcelable; |
| 21 | +import android.view.View; |
| 22 | +import android.view.ViewGroup; |
| 23 | + |
| 24 | +import androidx.annotation.NonNull; |
| 25 | +import androidx.viewpager.widget.PagerAdapter; |
| 26 | + |
| 27 | +public class DelegatingPagerAdapter extends PagerAdapter { |
| 28 | + |
| 29 | + private final PagerAdapter mDelegate; |
| 30 | + |
| 31 | + DelegatingPagerAdapter(@NonNull final PagerAdapter delegate) { |
| 32 | + this.mDelegate = delegate; |
| 33 | + delegate.registerDataSetObserver(new MyDataSetObserver(this)); |
| 34 | + } |
| 35 | + |
| 36 | + PagerAdapter getDelegate() { |
| 37 | + return mDelegate; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public int getCount() { |
| 42 | + return mDelegate.getCount(); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void startUpdate(@NonNull ViewGroup container) { |
| 47 | + mDelegate.startUpdate(container); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public @NonNull |
| 52 | + Object instantiateItem(@NonNull ViewGroup container, int position) { |
| 53 | + return mDelegate.instantiateItem(container, position); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { |
| 58 | + mDelegate.destroyItem(container, position, object); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) { |
| 63 | + mDelegate.setPrimaryItem(container, position, object); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void finishUpdate(@NonNull ViewGroup container) { |
| 68 | + mDelegate.finishUpdate(container); |
| 69 | + } |
| 70 | + |
| 71 | + @Deprecated |
| 72 | + @Override |
| 73 | + public void startUpdate(@NonNull View container) { |
| 74 | + mDelegate.startUpdate(container); |
| 75 | + } |
| 76 | + |
| 77 | + @Deprecated |
| 78 | + @Override |
| 79 | + public @NonNull |
| 80 | + Object instantiateItem(@NonNull View container, int position) { |
| 81 | + return mDelegate.instantiateItem(container, position); |
| 82 | + } |
| 83 | + |
| 84 | + @Deprecated |
| 85 | + @Override |
| 86 | + public void destroyItem(@NonNull View container, int position, @NonNull Object object) { |
| 87 | + mDelegate.destroyItem(container, position, object); |
| 88 | + } |
| 89 | + |
| 90 | + @Deprecated |
| 91 | + @Override |
| 92 | + public void setPrimaryItem(@NonNull View container, int position, @NonNull Object object) { |
| 93 | + mDelegate.setPrimaryItem(container, position, object); |
| 94 | + } |
| 95 | + |
| 96 | + @Deprecated |
| 97 | + @Override |
| 98 | + public void finishUpdate(@NonNull View container) { |
| 99 | + mDelegate.finishUpdate(container); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { |
| 104 | + return mDelegate.isViewFromObject(view, object); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public Parcelable saveState() { |
| 109 | + return mDelegate.saveState(); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void restoreState(Parcelable state, ClassLoader loader) { |
| 114 | + mDelegate.restoreState(state, loader); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public int getItemPosition(@NonNull Object object) { |
| 119 | + return mDelegate.getItemPosition(object); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void notifyDataSetChanged() { |
| 124 | + mDelegate.notifyDataSetChanged(); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void registerDataSetObserver(@NonNull DataSetObserver observer) { |
| 129 | + mDelegate.registerDataSetObserver(observer); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void unregisterDataSetObserver(@NonNull DataSetObserver observer) { |
| 134 | + mDelegate.unregisterDataSetObserver(observer); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public CharSequence getPageTitle(int position) { |
| 139 | + return mDelegate.getPageTitle(position); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public float getPageWidth(int position) { |
| 144 | + return mDelegate.getPageWidth(position); |
| 145 | + } |
| 146 | + |
| 147 | + private void superNotifyDataSetChanged() { |
| 148 | + super.notifyDataSetChanged(); |
| 149 | + } |
| 150 | + |
| 151 | + private static class MyDataSetObserver extends DataSetObserver { |
| 152 | + |
| 153 | + final DelegatingPagerAdapter mParent; |
| 154 | + |
| 155 | + private MyDataSetObserver(DelegatingPagerAdapter mParent) { |
| 156 | + this.mParent = mParent; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public void onChanged() { |
| 161 | + if (mParent != null) { |
| 162 | + mParent.superNotifyDataSetChanged(); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void onInvalidated() { |
| 168 | + onChanged(); |
| 169 | + } |
| 170 | + |
| 171 | + } |
| 172 | + |
| 173 | +} |
0 commit comments