Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions readium/navigator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ dependencies {
implementation(libs.androidx.webkit)
// Needed to avoid a crash with API 31, see https://stackoverflow.com/a/69152986/1474476
implementation("androidx.work:work-runtime-ktx:2.7.1")
implementation("com.duolingo.open:rtl-viewpager:1.0.3")
// ChrisBane/PhotoView ( for the Zoom handling )
implementation(libs.photoview)

Expand All @@ -94,7 +93,6 @@ dependencies {
api(libs.bundles.exoplayer)
implementation(libs.google.material)
implementation(libs.timber)
implementation("com.shopgun.android:utils:1.0.9")
implementation(libs.joda.time)
implementation(libs.bundles.coroutines)
implementation(libs.kotlinx.serialization.json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import android.view.animation.DecelerateInterpolator
import android.view.animation.Interpolator
import android.widget.FrameLayout
import androidx.core.view.ViewCompat
import com.shopgun.android.utils.NumberUtils
import java.util.*
import kotlin.math.abs
import kotlin.math.min
import kotlin.math.roundToInt
import kotlin.math.roundToLong
Expand Down Expand Up @@ -94,12 +94,15 @@ class R2FXLLayout : FrameLayout {
private var mOnDoubleTapListeners: MutableList<OnDoubleTapListener>? = null
private var onLongTapListeners: MutableList<OnLongTapListener>? = null

private fun Float.equalsDelta(other: Float, delta: Float = 0.001f) =
this == other || abs(this - other) < delta

var scale: Float
get() = getMatrixValue(scaleMatrix, Matrix.MSCALE_X)
set(scale) = setScale(scale, true)

val isScaled: Boolean
get() = !NumberUtils.isEqual(scale, 1.0f, 0.05f)
get() = !scale.equalsDelta(1.0f, 0.05f)

private val translateDeltaBounds: RectF
get() {
Expand Down Expand Up @@ -317,8 +320,8 @@ class R2FXLLayout : FrameLayout {

override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
val scale = scale
val newScale = NumberUtils.clamp(minScale, scale, maxScale)
if (NumberUtils.isEqual(newScale, scale)) {
val newScale = scale.coerceIn(minScale, maxScale)
if (newScale.equalsDelta(scale)) {
// only fling if no scale is needed - scale will happen on ACTION_UP
flingRunnable = FlingRunnable(context)
flingRunnable!!.fling(velocityX.toInt(), velocityY.toInt())
Expand Down Expand Up @@ -424,7 +427,7 @@ class R2FXLLayout : FrameLayout {
}
fixFocusPoint(focusX, focusY)
if (!isAllowOverScale) {
newScale = NumberUtils.clamp(minScale, newScale, maxScale)
newScale = newScale.coerceIn(minScale, maxScale)
}
if (animate) {
animatedZoomRunnable = AnimatedZoomRunnable()
Expand All @@ -443,12 +446,12 @@ class R2FXLLayout : FrameLayout {
var tdy = dy
if (clamp) {
val bounds = translateDeltaBounds
tdx = NumberUtils.clamp(bounds.left, dx, bounds.right)
tdy = NumberUtils.clamp(bounds.top, dy, bounds.bottom)
tdx = dx.coerceIn(bounds.left, bounds.right)
tdy = dy.coerceIn(bounds.top, bounds.bottom)
}
val destPosX = tdx + posX
val destPosY = tdy + posY
if (!NumberUtils.isEqual(destPosX, posX) || !NumberUtils.isEqual(destPosY, posY)) {
if (!destPosX.equalsDelta(posX) || !destPosY.equalsDelta(posY)) {
translateMatrix.setTranslate(-destPosX, -destPosY)
matrixUpdated()
invalidate()
Expand Down Expand Up @@ -519,16 +522,16 @@ class R2FXLLayout : FrameLayout {
private var mTargetY: Float = 0.toFloat()

internal fun doScale(): Boolean {
return !NumberUtils.isEqual(mZoomStart, mZoomEnd)
return !mZoomStart.equalsDelta(mZoomEnd)
}

internal fun doTranslate(): Boolean {
return !NumberUtils.isEqual(mStartX, mTargetX) || !NumberUtils.isEqual(mStartY, mTargetY)
return !mStartX.equalsDelta(mTargetX) || !mStartY.equalsDelta(mTargetY)
}

internal fun runValidation(): Boolean {
val scale = scale
val newScale = NumberUtils.clamp(minScale, scale, maxScale)
val newScale = scale.coerceIn(minScale, maxScale)
scale(scale, newScale, focusX, focusY, true)
if (animatedZoomRunnable!!.doScale() || animatedZoomRunnable!!.doTranslate()) {
ViewCompat.postOnAnimation(this@R2FXLLayout, animatedZoomRunnable!!)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package org.readium.r2.navigator.pager;

/*
* Copyright 2016–2020 Duolingo
*
* 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.
*/

import android.database.DataSetObserver;
import android.os.Parcelable;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

public class DelegatingPagerAdapter extends PagerAdapter {

private final PagerAdapter mDelegate;

DelegatingPagerAdapter(@NonNull final PagerAdapter delegate) {
this.mDelegate = delegate;
delegate.registerDataSetObserver(new MyDataSetObserver(this));
}

PagerAdapter getDelegate() {
return mDelegate;
}

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

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

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

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

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

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

@Deprecated
@Override
public void startUpdate(@NonNull View container) {
mDelegate.startUpdate(container);
}

@Deprecated
@Override
public @NonNull
Object instantiateItem(@NonNull View container, int position) {
return mDelegate.instantiateItem(container, position);
}

@Deprecated
@Override
public void destroyItem(@NonNull View container, int position, @NonNull Object object) {
mDelegate.destroyItem(container, position, object);
}

@Deprecated
@Override
public void setPrimaryItem(@NonNull View container, int position, @NonNull Object object) {
mDelegate.setPrimaryItem(container, position, object);
}

@Deprecated
@Override
public void finishUpdate(@NonNull View container) {
mDelegate.finishUpdate(container);
}

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

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

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

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

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

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

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

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

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

private void superNotifyDataSetChanged() {
super.notifyDataSetChanged();
}

private static class MyDataSetObserver extends DataSetObserver {

final DelegatingPagerAdapter mParent;

private MyDataSetObserver(DelegatingPagerAdapter mParent) {
this.mParent = mParent;
}

@Override
public void onChanged() {
if (mParent != null) {
mParent.superNotifyDataSetChanged();
}
}

@Override
public void onInvalidated() {
onChanged();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.duolingo.open.rtlviewpager.DelegatingPagerAdapter;

import org.readium.r2.shared.publication.ReadingProgression;

import java.util.HashMap;
Expand Down
Loading