content: /*
   * MIT License
   *
   * Copyright (c) 2018 Alibaba Group
   *
   * Permission is hereby granted, free of charge, to any person obtaining a copy
   * of this software and associated documentation files (the "Software"), to deal
   * in the Software without restriction, including without limitation the rights
   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   * copies of the Software, and to permit persons to whom the Software is
   * furnished to do so, subject to the following conditions:
   *
   * The above copyright notice and this permission notice shall be included in all
   * copies or substantial portions of the Software.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   * SOFTWARE.
   */
  
  package com.alibaba.android.rx;
  
  import android.os.Looper;
  import android.view.View;
  import android.view.View.OnClickListener;
  import io.reactivex.Observable;
  import io.reactivex.Observer;
  import io.reactivex.android.MainThreadDisposable;
  import io.reactivex.disposables.Disposables;
  
  /**
   * Created by longerian on 2018/3/23.
   *
   * @author longerian
   * @date 2018/03/23
   */
  
  public class ViewClickObservable extends Observable<Object> {
  
      public static ViewClickObservable from(View view) {
          return new ViewClickObservable(view);
      }
  
      private final View view;
  
      private ViewClickObservable(View view) {
          this.view = view;
      }
  
      @Override protected void subscribeActual(Observer<? super Object> observer) {
          if (!checkMainThread(observer)) {
              return;
          }
          Listener listener = new Listener(view, observer);
          observer.onSubscribe(listener);
          view.setOnClickListener(listener);
      }
  
      public static boolean checkMainThread(Observer<?> observer) {
          if (Looper.myLooper() != Looper.getMainLooper()) {
              observer.onSubscribe(Disposables.empty());
              observer.onError(new IllegalStateException(
                  "Expected to be called on the main thread but was " + Thread.currentThread().getName()));
              return false;
          }
          return true;
      }
  
      static final class Listener extends MainThreadDisposable implements OnClickListener {
          private final View view;
          private final Observer<? super Object> observer;
  
          Listener(View view, Observer<? super Object> observer) {
              this.view = view;
              this.observer = observer;
          }
  
          @Override public void onClick(View v) {
              if (!isDisposed()) {
                  observer.onNext(v);
              }
          }
  
          @Override protected void onDispose() {
              view.setOnClickListener(null);
          }
      }
  }
  