<commit_before>/*
 * Copyright 2016-present The Material Motion Authors. All Rights Reserved.
 *
 * 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.google.android.material.motion.streams.sources;

import android.support.annotation.NonNull;

import com.google.android.material.motion.gestures.GestureRecognizer;
import com.google.android.material.motion.gestures.GestureRecognizer.GestureRecognizerState;
import com.google.android.material.motion.gestures.GestureRecognizer.GestureStateChangeListener;
import com.google.android.material.motion.observable.IndefiniteObservable.Connector;
import com.google.android.material.motion.observable.IndefiniteObservable.Disconnector;
import com.google.android.material.motion.streams.MotionObservable;
import com.google.android.material.motion.streams.MotionObservable.MotionObserver;

/**
 * A source for gestures.
 */
public final class GestureSource {

  private static final GestureSource GESTURE_SOURCE = new GestureSource();

  /**
   * Creates a gesture source that will connect to the provided gesture recognizer.
   */
  public static <T extends GestureRecognizer> MotionObservable<T> from(T gesture) {
    return GESTURE_SOURCE.create(gesture);
  }

  /**
   * Creates a gesture source that will connect to the provided gesture recognizer.
   */
  public <T extends GestureRecognizer> MotionObservable<T> create(final T gesture) {
    return new MotionObservable<>(new Connector<MotionObserver<T>>() {
      @NonNull
      @Override
      public Disconnector connect(MotionObserver<T> observer) {
        final GestureConnection connection = new GestureConnection<>(gesture, observer);
        return new Disconnector() {
          @Override
          public void disconnect() {
            connection.disconnect();
          }
        };
      }
    });
  }

  private static class GestureConnection<T extends GestureRecognizer> {

    private final T gesture;
    private final MotionObserver<T> observer;

    private GestureConnection(
      T gesture, MotionObserver<T> observer) {
      this.gesture = gesture;
      this.observer = observer;

      gesture.addStateChangeListener(gestureStateChangeListener);

      propagate();
    }

    private void disconnect() {
      gesture.removeStateChangeListener(gestureStateChangeListener);
    }

    private void propagate() {
      @GestureRecognizerState int state = gesture.getState();
      boolean isActive = state == GestureRecognizer.BEGAN || state == GestureRecognizer.CHANGED;

      if (isActive) {
        observer.state(MotionObservable.ACTIVE);
      }

      observer.next(gesture);

      if (!isActive) {
        observer.state(MotionObservable.AT_REST);
      }
    }

    private final GestureStateChangeListener gestureStateChangeListener =
      new GestureStateChangeListener() {
        @Override
        public void onStateChanged(GestureRecognizer gesture) {
          propagate();
        }
      };
  }
}<commit_msg>Only propogate gesture state when it changes.<commit_after>/*
 * Copyright 2016-present The Material Motion Authors. All Rights Reserved.
 *
 * 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.google.android.material.motion.streams.sources;

import android.support.annotation.NonNull;

import com.google.android.material.motion.gestures.GestureRecognizer;
import com.google.android.material.motion.gestures.GestureRecognizer.GestureRecognizerState;
import com.google.android.material.motion.gestures.GestureRecognizer.GestureStateChangeListener;
import com.google.android.material.motion.observable.IndefiniteObservable.Connector;
import com.google.android.material.motion.observable.IndefiniteObservable.Disconnector;
import com.google.android.material.motion.streams.MotionObservable;
import com.google.android.material.motion.streams.MotionObservable.MotionObserver;

/**
 * A source for gestures.
 */
public final class GestureSource {

  private static final GestureSource GESTURE_SOURCE = new GestureSource();

  /**
   * Creates a gesture source that will connect to the provided gesture recognizer.
   */
  public static <T extends GestureRecognizer> MotionObservable<T> from(T gesture) {
    return GESTURE_SOURCE.create(gesture);
  }

  /**
   * Creates a gesture source that will connect to the provided gesture recognizer.
   */
  public <T extends GestureRecognizer> MotionObservable<T> create(final T gesture) {
    return new MotionObservable<>(new Connector<MotionObserver<T>>() {
      @NonNull
      @Override
      public Disconnector connect(MotionObserver<T> observer) {
        final GestureConnection connection = new GestureConnection<>(gesture, observer);
        return new Disconnector() {
          @Override
          public void disconnect() {
            connection.disconnect();
          }
        };
      }
    });
  }

  private static class GestureConnection<T extends GestureRecognizer> {

    private final T gesture;
    private final MotionObserver<T> observer;
    private boolean propagatedState = false;

    private GestureConnection(
      T gesture, MotionObserver<T> observer) {
      this.gesture = gesture;
      this.observer = observer;

      gesture.addStateChangeListener(gestureStateChangeListener);

      propagate();
    }

    private void disconnect() {
      gesture.removeStateChangeListener(gestureStateChangeListener);
    }

    private void propagate() {
      @GestureRecognizerState int state = gesture.getState();

      if (state == GestureRecognizer.BEGAN || (state == GestureRecognizer.CHANGED && !propagatedState)) {
        observer.state(MotionObservable.ACTIVE);
        propagatedState = true;
      }

      observer.next(gesture);

      if (state == GestureRecognizer.CANCELLED || state == GestureRecognizer.RECOGNIZED) {
        observer.state(MotionObservable.AT_REST);
        propagatedState = true;
      }
    }

    private final GestureStateChangeListener gestureStateChangeListener =
      new GestureStateChangeListener() {
        @Override
        public void onStateChanged(GestureRecognizer gesture) {
          propagate();
        }
      };
  }
}
{'fraction_non_alphanumeric': 0.08482893998878295, 'fraction_numerical': 0.002243409983174425, 'mean_word_length': 3.6168284789644014, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6652313', 'n_tokens_mistral': 1963, 'n_tokens_neox': 1840, 'n_words': 513}