content: /*
   * Copyright 2019 ACINQ SAS
   *
   * 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 fr.acinq.eclair.wallet.utils;
  
  import android.content.Context;
  import androidx.annotation.NonNull;
  import androidx.viewpager.widget.ViewPager;
  import android.text.Editable;
  import android.view.GestureDetector;
  import android.view.MotionEvent;
  import android.view.View;
  
  /**
   * Created by DPA on 05/02/19.
   */
  public interface TechnicalHelper {
    abstract class SimpleTextWatcher implements android.text.TextWatcher {
      @Override
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  
      }
  
      @Override
      public void afterTextChanged(Editable s) {
  
      }
    }
  
    /**
     * Detects left and right swipes across a view.
     */
    public class OnSwipeTouchListener implements View.OnTouchListener {
  
      private final GestureDetector gestureDetector;
  
      public OnSwipeTouchListener(final Context context) {
        gestureDetector = new GestureDetector(context, new GestureListener());
      }
  
      public void onSwipeLeft() {
      }
  
      public void onSwipeRight() {
      }
  
      public void onSwipeTop() {
      }
  
      public void onSwipeBottom() {
      }
  
      public void onClick() {
      }
  
      public boolean onTouch(final View v, final MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
      }
  
      private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
  
        private static final int SWIPE_DISTANCE_THRESHOLD = 20;
        private static final int SWIPE_VELOCITY_THRESHOLD = 30;
  
        @Override
        public boolean onDown(final MotionEvent e) {
          return false;
        }
  
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
          onClick();
          return super.onSingleTapUp(e);
        }
  
        @Override
        public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY) {
          float distanceX = e2.getX() - e1.getX();
          float distanceY = e2.getY() - e1.getY();
          // left <-> right
          if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
            if (distanceX > 0) {
              onSwipeRight();
            } else {
              onSwipeLeft();
            }
            return true;
          }
          // top <-> bottom
          if (Math.abs(distanceY) > Math.abs(distanceX) && Math.abs(distanceY) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
            if (distanceY > 0) {
              onSwipeBottom();
            } else {
              onSwipeTop();
            }
            return true;
          }
          return false;
        }
      }
    }
  
    // -- see https://developer.android.com/training/animation/screen-slide.html#zoom-out
    public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
      private static final float MIN_SCALE = 0.94f;
      private static final float MIN_ALPHA = 0.6f;
  
      public void transformPage(@NonNull View view, float position) {
        int pageWidth = view.getWidth();
        int pageHeight = view.getHeight();
  
        if (position < -1) { // [-Infinity,-1)
          // This page is way off-screen to the left.
          view.setAlpha(0f);
        } else if (position <= 1) { // [-1,1]
          // Modify the default slide transition to shrink the page as well
          float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
          float vertMargin = pageHeight * (1 - scaleFactor) / 2;
          float horzMargin = pageWidth * (1 - scaleFactor) / 2;
          if (position < 0) {
            view.setTranslationX(horzMargin - vertMargin / 2);
          } else {
            view.setTranslationX(-horzMargin + vertMargin / 2);
          }
  
          // Scale the page down (between MIN_SCALE and 1)
          view.setPivotX((float) pageWidth / 2);
          view.setPivotY(pageHeight);
          view.setScaleX(scaleFactor);
          view.setScaleY(scaleFactor);
  
          // Fade the page relative to its size.
          view.setAlpha(MIN_ALPHA +
            (scaleFactor - MIN_SCALE) /
              (1 - MIN_SCALE) * (1 - MIN_ALPHA));
  
        } else { // (1,+Infinity]
          // This page is way off-screen to the right.
          view.setAlpha(0f);
        }
      }
    }
  
  }
  