<commit_before>package com.kickstarter.viewmodels;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Pair;

import com.kickstarter.KSApplication;
import com.kickstarter.libs.CurrentUser;
import com.kickstarter.libs.RefTag;
import com.kickstarter.libs.ViewModel;
import com.kickstarter.libs.rx.transformers.Transformers;
import com.kickstarter.libs.utils.RefTagUtils;
import com.kickstarter.models.Project;
import com.kickstarter.models.Reward;
import com.kickstarter.models.User;
import com.kickstarter.services.ApiClientType;
import com.kickstarter.ui.activities.ProjectActivity;
import com.kickstarter.ui.adapters.ProjectAdapter;
import com.kickstarter.ui.viewholders.ProjectViewHolder;
import com.kickstarter.ui.viewholders.RewardViewHolder;
import com.kickstarter.viewmodels.inputs.ProjectViewModelInputs;
import com.kickstarter.viewmodels.outputs.ProjectViewModelOutputs;

import java.net.CookieManager;
import java.net.HttpCookie;

import javax.inject.Inject;

import rx.Observable;
import rx.subjects.BehaviorSubject;
import rx.subjects.PublishSubject;

public final class ProjectViewModel extends ViewModel<ProjectActivity> implements ProjectAdapter.Delegate, ProjectViewModelInputs, ProjectViewModelOutputs {
  protected @Inject ApiClientType client;
  protected @Inject CurrentUser currentUser;
  protected @Inject CookieManager cookieManager;

  /**
   * A light-weight value to hold two ref tags and a project. Two ref tags are stored: one comes from parceled
   * data in the activity and the other comes from the ref stored in a cookie associated to the project.
   */
  private class RefTagsAndProject {
    final @Nullable RefTag activityRefTag;
    final @Nullable RefTag cookieRefTag;
    final @NonNull Project project;

    private RefTagsAndProject(final @Nullable RefTag activityRefTag, final @Nullable RefTag cookieRefTag, final @NonNull Project project) {
      this.activityRefTag = activityRefTag;
      this.cookieRefTag = cookieRefTag;
      this.project = project;
    }
  }

  // INPUTS
  private final PublishSubject<Project> initializer = PublishSubject.create();
  public void initializer(final @NonNull Project project) {
    initializer.onNext(project);
  }
  private final PublishSubject<Void> backProjectClicked = PublishSubject.create();
  public void backProjectClicked() {
    this.backProjectClicked.onNext(null);
  }
  private final PublishSubject<Void> shareClicked = PublishSubject.create();
  public void shareClicked() {
    this.shareClicked.onNext(null);
  }
  private final PublishSubject<Void> blurbClicked = PublishSubject.create();
  public void blurbClicked() {
    this.blurbClicked.onNext(null);
  }
  private final PublishSubject<Void> commentsClicked = PublishSubject.create();
  public void commentsClicked() {
    this.commentsClicked.onNext(null);
  }
  private final PublishSubject<Void> creatorNameClicked = PublishSubject.create();
  public void creatorNameClicked() {
    this.creatorNameClicked.onNext(null);
  }
  private final PublishSubject<Void> managePledgeClicked = PublishSubject.create();
  public void managePledgeClicked() {
    this.managePledgeClicked.onNext(null);
  }
  private final PublishSubject<Void> updatesClicked = PublishSubject.create();
  public void updatesClicked() {
    this.updatesClicked.onNext(null);
  }
  private final PublishSubject<Void> playVideoClicked = PublishSubject.create();
  public void playVideoClicked() {
    this.playVideoClicked.onNext(null);
  }
  private final PublishSubject<Void> viewPledgeClicked = PublishSubject.create();
  public void viewPledgeClicked() {
    this.viewPledgeClicked.onNext(null);
  }
  private final PublishSubject<Void> starClicked = PublishSubject.create();
  public void starClicked() {
    this.starClicked.onNext(null);
  }
  private final PublishSubject<Reward> rewardClicked = PublishSubject.create();
  public void rewardClicked(final @NonNull Reward reward) {
    this.rewardClicked.onNext(reward);
  }
  private final PublishSubject<Void> loginSuccess = PublishSubject.create();
  public void loginSuccess() {
    this.loginSuccess.onNext(null);
  }
  private final PublishSubject<RefTag> activityRefTag = PublishSubject.create();
  public void activityRefTag(final @Nullable RefTag refTag) {
    activityRefTag.onNext(refTag);
  }
  public final ProjectViewModelInputs inputs = this;

  // OUTPUTS
  final BehaviorSubject<Project> project = BehaviorSubject.create();
  public Observable<Project> project() {
    return this.project;
  }
  public Observable<Project> showShareSheet() {
    return this.project.compose(Transformers.takeWhen(this.shareClicked));
  }
  public Observable<Project> playVideo() {
    return this.project.compose(Transformers.takeWhen(this.playVideoClicked));
  }
  public Observable<Project> showCampaign() {
    return this.project.compose(Transformers.takeWhen(this.blurbClicked));
  }
  public Observable<Project> showCreator() {
    return this.project.compose(Transformers.takeWhen(this.creatorNameClicked));
  }
  public Observable<Project> showUpdates() {
    return this.project.compose(Transformers.takeWhen(this.updatesClicked));
  }
  public Observable<Project> showComments() {
    return this.project.compose(Transformers.takeWhen(this.commentsClicked));
  }
  public Observable<Project> startCheckout() {
    return this.project.compose(Transformers.takeWhen(this.backProjectClicked));
  }
  public Observable<Project> startManagePledge() {
    return this.project.compose(Transformers.takeWhen(this.managePledgeClicked));
  }
  public Observable<Project> startViewPledge() {
    return this.project.compose(Transformers.takeWhen(this.viewPledgeClicked));
  }
  public Observable<Pair<Project, Reward>> startCheckoutWithReward() {
    return this.project.compose(Transformers.takePairWhen(this.rewardClicked));
  }
  private final PublishSubject<Void> showStarredPrompt = PublishSubject.create();
  public Observable<Void> showStarredPrompt() {
    return this.showStarredPrompt;
  }
  private final PublishSubject<Void> showLoginTout = PublishSubject.create();
  public Observable<Void> showLoginTout() {
    return this.showLoginTout;
  }
  public final ProjectViewModelOutputs outputs = this;

  @Override
  protected void onCreate(final @NonNull Context context, @Nullable final Bundle savedInstanceState) {
    super.onCreate(context, savedInstanceState);
    ((KSApplication) context.getApplicationContext()).component().inject(this);

    final Observable<User> loggedInUserOnStarClick = currentUser.observable()
      .compose(Transformers.takeWhen(starClicked))
      .filter(u -> u != null);

    final Observable<User> loggedOutUserOnStarClick = currentUser.observable()
      .compose(Transformers.takeWhen(starClicked))
      .filter(u -> u == null);

    final Observable<Project> projectOnUserChangeStar = initializer
      .compose(Transformers.takeWhen(loggedInUserOnStarClick))
      .switchMap(this::toggleProjectStar)
      .share();

    final Observable<Project> starredProjectOnLoginSuccess = initializer
      .compose(Transformers.takeWhen(loginSuccess))
      .take(1)
      .switchMap(this::starProject)
      .share();

    addSubscription(
      initializer
        .mergeWith(projectOnUserChangeStar)
        .mergeWith(starredProjectOnLoginSuccess)
        .subscribe(this.project::onNext)
    );

    addSubscription(
      projectOnUserChangeStar.mergeWith(starredProjectOnLoginSuccess)
        .filter(Project::isStarred)
        .subscribe(__ -> this.showStarredPrompt.onNext(null))
    );

    addSubscription(
      loggedOutUserOnStarClick.subscribe(__ ->
          this.showLoginTout.onNext(null)
      )
    );

    addSubscription(shareClicked.subscribe(__ -> koala.trackShowProjectShareSheet()));

    addSubscription(projectOnUserChangeStar.mergeWith(starredProjectOnLoginSuccess)
      .subscribe(koala::trackProjectStar));

    // An observable of the ref tag stored in the cookie for the project. Can emit `null`.
    final Observable<RefTag> cookieRefTag = project
      .take(1)
      .map(p -> RefTagUtils.storedCookieRefTagForProject(p, cookieManager));

    Observable.combineLatest(activityRefTag, cookieRefTag, project, RefTagsAndProject::new)
      .take(1)
      .subscribe(data -> {
        // If a cookie hasn't been set for this ref+project then do so.
        if (data.cookieRefTag == null && data.activityRefTag != null) {
          final HttpCookie cookie = RefTagUtils.buildCookieForRefTagAndProject(data.activityRefTag, data.project);
          cookieManager.getCookieStore().add(null, cookie);
        }

        koala.trackProjectShow(
          data.project,
          data.activityRefTag,
          RefTagUtils.storedCookieRefTagForProject(data.project, cookieManager)
        );
      });
  }

  public void projectViewHolderBlurbClicked(final @NonNull ProjectViewHolder viewHolder) {
    this.blurbClicked();
  }
<commit_msg>Wrap subscription in addSubscription.<commit_after>package com.kickstarter.viewmodels;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Pair;

import com.kickstarter.KSApplication;
import com.kickstarter.libs.CurrentUser;
import com.kickstarter.libs.RefTag;
import com.kickstarter.libs.ViewModel;
import com.kickstarter.libs.rx.transformers.Transformers;
import com.kickstarter.libs.utils.RefTagUtils;
import com.kickstarter.models.Project;
import com.kickstarter.models.Reward;
import com.kickstarter.models.User;
import com.kickstarter.services.ApiClientType;
import com.kickstarter.ui.activities.ProjectActivity;
import com.kickstarter.ui.adapters.ProjectAdapter;
import com.kickstarter.ui.viewholders.ProjectViewHolder;
import com.kickstarter.ui.viewholders.RewardViewHolder;
import com.kickstarter.viewmodels.inputs.ProjectViewModelInputs;
import com.kickstarter.viewmodels.outputs.ProjectViewModelOutputs;

import java.net.CookieManager;
import java.net.HttpCookie;

import javax.inject.Inject;

import rx.Observable;
import rx.subjects.BehaviorSubject;
import rx.subjects.PublishSubject;

public final class ProjectViewModel extends ViewModel<ProjectActivity> implements ProjectAdapter.Delegate, ProjectViewModelInputs, ProjectViewModelOutputs {
  protected @Inject ApiClientType client;
  protected @Inject CurrentUser currentUser;
  protected @Inject CookieManager cookieManager;

  /**
   * A light-weight value to hold two ref tags and a project. Two ref tags are stored: one comes from parceled
   * data in the activity and the other comes from the ref stored in a cookie associated to the project.
   */
  private class RefTagsAndProject {
    final @Nullable RefTag activityRefTag;
    final @Nullable RefTag cookieRefTag;
    final @NonNull Project project;

    private RefTagsAndProject(final @Nullable RefTag activityRefTag, final @Nullable RefTag cookieRefTag, final @NonNull Project project) {
      this.activityRefTag = activityRefTag;
      this.cookieRefTag = cookieRefTag;
      this.project = project;
    }
  }

  // INPUTS
  private final PublishSubject<Project> initializer = PublishSubject.create();
  public void initializer(final @NonNull Project project) {
    initializer.onNext(project);
  }
  private final PublishSubject<Void> backProjectClicked = PublishSubject.create();
  public void backProjectClicked() {
    this.backProjectClicked.onNext(null);
  }
  private final PublishSubject<Void> shareClicked = PublishSubject.create();
  public void shareClicked() {
    this.shareClicked.onNext(null);
  }
  private final PublishSubject<Void> blurbClicked = PublishSubject.create();
  public void blurbClicked() {
    this.blurbClicked.onNext(null);
  }
  private final PublishSubject<Void> commentsClicked = PublishSubject.create();
  public void commentsClicked() {
    this.commentsClicked.onNext(null);
  }
  private final PublishSubject<Void> creatorNameClicked = PublishSubject.create();
  public void creatorNameClicked() {
    this.creatorNameClicked.onNext(null);
  }
  private final PublishSubject<Void> managePledgeClicked = PublishSubject.create();
  public void managePledgeClicked() {
    this.managePledgeClicked.onNext(null);
  }
  private final PublishSubject<Void> updatesClicked = PublishSubject.create();
  public void updatesClicked() {
    this.updatesClicked.onNext(null);
  }
  private final PublishSubject<Void> playVideoClicked = PublishSubject.create();
  public void playVideoClicked() {
    this.playVideoClicked.onNext(null);
  }
  private final PublishSubject<Void> viewPledgeClicked = PublishSubject.create();
  public void viewPledgeClicked() {
    this.viewPledgeClicked.onNext(null);
  }
  private final PublishSubject<Void> starClicked = PublishSubject.create();
  public void starClicked() {
    this.starClicked.onNext(null);
  }
  private final PublishSubject<Reward> rewardClicked = PublishSubject.create();
  public void rewardClicked(final @NonNull Reward reward) {
    this.rewardClicked.onNext(reward);
  }
  private final PublishSubject<Void> loginSuccess = PublishSubject.create();
  public void loginSuccess() {
    this.loginSuccess.onNext(null);
  }
  private final PublishSubject<RefTag> activityRefTag = PublishSubject.create();
  public void activityRefTag(final @Nullable RefTag refTag) {
    activityRefTag.onNext(refTag);
  }
  public final ProjectViewModelInputs inputs = this;

  // OUTPUTS
  final BehaviorSubject<Project> project = BehaviorSubject.create();
  public Observable<Project> project() {
    return this.project;
  }
  public Observable<Project> showShareSheet() {
    return this.project.compose(Transformers.takeWhen(this.shareClicked));
  }
  public Observable<Project> playVideo() {
    return this.project.compose(Transformers.takeWhen(this.playVideoClicked));
  }
  public Observable<Project> showCampaign() {
    return this.project.compose(Transformers.takeWhen(this.blurbClicked));
  }
  public Observable<Project> showCreator() {
    return this.project.compose(Transformers.takeWhen(this.creatorNameClicked));
  }
  public Observable<Project> showUpdates() {
    return this.project.compose(Transformers.takeWhen(this.updatesClicked));
  }
  public Observable<Project> showComments() {
    return this.project.compose(Transformers.takeWhen(this.commentsClicked));
  }
  public Observable<Project> startCheckout() {
    return this.project.compose(Transformers.takeWhen(this.backProjectClicked));
  }
  public Observable<Project> startManagePledge() {
    return this.project.compose(Transformers.takeWhen(this.managePledgeClicked));
  }
  public Observable<Project> startViewPledge() {
    return this.project.compose(Transformers.takeWhen(this.viewPledgeClicked));
  }
  public Observable<Pair<Project, Reward>> startCheckoutWithReward() {
    return this.project.compose(Transformers.takePairWhen(this.rewardClicked));
  }
  private final PublishSubject<Void> showStarredPrompt = PublishSubject.create();
  public Observable<Void> showStarredPrompt() {
    return this.showStarredPrompt;
  }
  private final PublishSubject<Void> showLoginTout = PublishSubject.create();
  public Observable<Void> showLoginTout() {
    return this.showLoginTout;
  }
  public final ProjectViewModelOutputs outputs = this;

  @Override
  protected void onCreate(final @NonNull Context context, @Nullable final Bundle savedInstanceState) {
    super.onCreate(context, savedInstanceState);
    ((KSApplication) context.getApplicationContext()).component().inject(this);

    final Observable<User> loggedInUserOnStarClick = currentUser.observable()
      .compose(Transformers.takeWhen(starClicked))
      .filter(u -> u != null);

    final Observable<User> loggedOutUserOnStarClick = currentUser.observable()
      .compose(Transformers.takeWhen(starClicked))
      .filter(u -> u == null);

    final Observable<Project> projectOnUserChangeStar = initializer
      .compose(Transformers.takeWhen(loggedInUserOnStarClick))
      .switchMap(this::toggleProjectStar)
      .share();

    final Observable<Project> starredProjectOnLoginSuccess = initializer
      .compose(Transformers.takeWhen(loginSuccess))
      .take(1)
      .switchMap(this::starProject)
      .share();

    addSubscription(
      initializer
        .mergeWith(projectOnUserChangeStar)
        .mergeWith(starredProjectOnLoginSuccess)
        .subscribe(this.project::onNext)
    );

    addSubscription(
      projectOnUserChangeStar.mergeWith(starredProjectOnLoginSuccess)
        .filter(Project::isStarred)
        .subscribe(__ -> this.showStarredPrompt.onNext(null))
    );

    addSubscription(
      loggedOutUserOnStarClick.subscribe(__ ->
          this.showLoginTout.onNext(null)
      )
    );

    addSubscription(shareClicked.subscribe(__ -> koala.trackShowProjectShareSheet()));

    addSubscription(projectOnUserChangeStar.mergeWith(starredProjectOnLoginSuccess)
      .subscribe(koala::trackProjectStar));

    // An observable of the ref tag stored in the cookie for the project. Can emit `null`.
    final Observable<RefTag> cookieRefTag = project
      .take(1)
      .map(p -> RefTagUtils.storedCookieRefTagForProject(p, cookieManager));

    addSubscription(
      Observable.combineLatest(activityRefTag, cookieRefTag, project, RefTagsAndProject::new)
        .take(1)
        .subscribe(data -> {
          // If a cookie hasn't been set for this ref+project then do so.
          if (data.cookieRefTag == null && data.activityRefTag != null) {
            final HttpCookie cookie = RefTagUtils.buildCookieForRefTagAndProject(data.activityRefTag, data.project);
            cookieManager.getCookieStore().add(null, cookie);
          }

          koala.trackProjectShow(
            data.project,
            data.activityRefTag,
            RefTagUtils.storedCookieRefTagForProject(data.project, cookieManager)
          );
        })
    );
  }

  public void projectViewHolderBlurbClicked(final @NonNull ProjectViewHolder viewHolder) {
    this.blurbClicked();
  }

{'fraction_non_alphanumeric': 0.09561466570812366, 'fraction_numerical': 0.00033180335121384727, 'mean_word_length': 4.695748031496063, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 91, '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': '3315790', 'n_tokens_mistral': 5163, 'n_tokens_neox': 5142, 'n_words': 1018}