<commit_before>package com.morihacky.android.rxjava;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import com.morihacky.android.rxjava.wiring.LogAdapter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.Observer;
import rx.Subscription;
import timber.log.Timber;

import static android.os.Looper.getMainLooper;
import static android.os.Looper.myLooper;

public class TimingDemoFragment
      extends BaseFragment {

    @InjectView(R.id.list_threading_log) ListView _logsList;

    private LogAdapter _adapter;
    private List<String> _logs;

    private Subscription _subscription1 = null;
    private Subscription _subscription2 = null;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        _setupLogger();
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.fragment_demo_timing, container, false);
        ButterKnife.inject(this, layout);
        return layout;
    }

    // -----------------------------------------------------------------------------------

    @OnClick(R.id.btn_demo_timing_1)
    public void Btn1_RunSingleTaskAfter2s() {
        _log(String.format("A1 [%s] --- BTN click", _getCurrentTimestamp()));

        Observable.timer(2, TimeUnit.SECONDS)//
              //.just(1).delay(2, TimeUnit.SECONDS)//
              .subscribe(new Observer<Long>() {
                  @Override
                  public void onCompleted() {
                      _log(String.format("A1 [%s] XXX COMPLETE", _getCurrentTimestamp()));
                  }

                  @Override
                  public void onError(Throwable e) {
                      Timber.e(e, "something went wrong in TimingDemoFragment example");
                  }

                  @Override
                  public void onNext(Long number) {
                      _log(String.format("A1 [%s]     NEXT", _getCurrentTimestamp()));
                  }
              });
    }

    @OnClick(R.id.btn_demo_timing_2)
    public void Btn2_RunTask_IntervalOf1s() {
        if (_subscription1 != null && !_subscription1.isUnsubscribed()) {
            _subscription1.unsubscribe();
            _log(String.format("B2 [%s] XXX BTN KILLED", _getCurrentTimestamp()));
            return;
        }

        _log(String.format("B2 [%s] --- BTN click", _getCurrentTimestamp()));

        _subscription1 = Observable//
              .interval(1, TimeUnit.SECONDS)//
              .subscribe(new Observer<Long>() {
                  @Override
                  public void onCompleted() {
                      _log(String.format("B2 [%s] XXXX COMPLETE", _getCurrentTimestamp()));
                  }

                  @Override
                  public void onError(Throwable e) {
                      Timber.e(e, "something went wrong in TimingDemoFragment example");
                  }

                  @Override
                  public void onNext(Long number) {
                      _log(String.format("B2 [%s]     NEXT", _getCurrentTimestamp()));
                  }
              });
    }

    @OnClick(R.id.btn_demo_timing_4)
    public void Btn4_RunTask5Times_IntervalOf3s() {
        _log(String.format("D4 [%s] --- BTN click", _getCurrentTimestamp()));

        Observable//
              .interval(3, TimeUnit.SECONDS).take(5)//
              .subscribe(new Observer<Long>() {
                  @Override
                  public void onCompleted() {
                      _log(String.format("D4 [%s] XXX COMPLETE", _getCurrentTimestamp()));
                  }<commit_msg>feat: add button 3 example where you start immediately and then go on with intervals<commit_after>package com.morihacky.android.rxjava;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import com.morihacky.android.rxjava.wiring.LogAdapter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.Observer;
import rx.Subscription;
import timber.log.Timber;

import static android.os.Looper.getMainLooper;
import static android.os.Looper.myLooper;

public class TimingDemoFragment
      extends BaseFragment {

    @InjectView(R.id.list_threading_log) ListView _logsList;

    private LogAdapter _adapter;
    private List<String> _logs;

    private Subscription _subscription1 = null;
    private Subscription _subscription2 = null;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        _setupLogger();
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.fragment_demo_timing, container, false);
        ButterKnife.inject(this, layout);
        return layout;
    }

    // -----------------------------------------------------------------------------------

    @OnClick(R.id.btn_demo_timing_1)
    public void Btn1_RunSingleTaskAfter2s() {
        _log(String.format("A1 [%s] --- BTN click", _getCurrentTimestamp()));

        Observable.timer(2, TimeUnit.SECONDS)//
              //.just(1).delay(2, TimeUnit.SECONDS)//
              .subscribe(new Observer<Long>() {
                  @Override
                  public void onCompleted() {
                      _log(String.format("A1 [%s] XXX COMPLETE", _getCurrentTimestamp()));
                  }

                  @Override
                  public void onError(Throwable e) {
                      Timber.e(e, "something went wrong in TimingDemoFragment example");
                  }

                  @Override
                  public void onNext(Long number) {
                      _log(String.format("A1 [%s]     NEXT", _getCurrentTimestamp()));
                  }
              });
    }

    @OnClick(R.id.btn_demo_timing_2)
    public void Btn2_RunTask_IntervalOf1s() {
        if (_subscription1 != null && !_subscription1.isUnsubscribed()) {
            _subscription1.unsubscribe();
            _log(String.format("B2 [%s] XXX BTN KILLED", _getCurrentTimestamp()));
            return;
        }

        _log(String.format("B2 [%s] --- BTN click", _getCurrentTimestamp()));

        _subscription1 = Observable//
              .interval(1, TimeUnit.SECONDS)//
              .subscribe(new Observer<Long>() {
                  @Override
                  public void onCompleted() {
                      _log(String.format("B2 [%s] XXXX COMPLETE", _getCurrentTimestamp()));
                  }

                  @Override
                  public void onError(Throwable e) {
                      Timber.e(e, "something went wrong in TimingDemoFragment example");
                  }

                  @Override
                  public void onNext(Long number) {
                      _log(String.format("B2 [%s]     NEXT", _getCurrentTimestamp()));
                  }
              });
    }

    @OnClick(R.id.btn_demo_timing_3)
    public void Btn3_RunTask_IntervalOf1s_StartImmediately() {
        if (_subscription2 != null && !_subscription2.isUnsubscribed()) {
            _subscription2.unsubscribe();
            _log(String.format("C3 [%s] XXX BTN KILLED", _getCurrentTimestamp()));
            return;
        }

        _log(String.format("C3 [%s] --- BTN click", _getCurrentTimestamp()));

        _subscription2 = Observable//
              .timer(0, 1, TimeUnit.SECONDS)//
              .subscribe(new Observer<Long>() {
                  @Override
                  public void onCompleted() {
                      _log(String.format("C3 [%s] XXXX COMPLETE", _getCurrentTimestamp()));
                  }

                  @Override
                  public void onError(Throwable e) {
                      Timber.e(e, "something went wrong in TimingDemoFragment example");
                  }

                  @Override
                  public void onNext(Long number) {
                      _log(String.format("C3 [%s]     NEXT", _getCurrentTimestamp()));
                  }
              });
    }

    @OnClick(R.id.btn_demo_timing_4)
    public void Btn4_RunTask5Times_IntervalOf3s() {
        _log(String.format("D4 [%s] --- BTN click", _getCurrentTimestamp()));

        Observable//
              .interval(3, TimeUnit.SECONDS).take(5)//
              .subscribe(new Observer<Long>() {
                  @Override
                  public void onCompleted() {
                      _log(String.format("D4 [%s] XXX COMPLETE", _getCurrentTimestamp()));
                  }
{'fraction_non_alphanumeric': 0.11973974184069681, 'fraction_numerical': 0.007975653268968413, 'mean_word_length': 1.8905065210797696, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6347206', 'n_tokens_mistral': 2613, 'n_tokens_neox': 2513, 'n_words': 518}