<commit_before>package com.guavabot.marshpermissions.ui.app_list;

import com.guavabot.marshpermissions.Schedulers;
import com.guavabot.marshpermissions.domain.entity.App;
import com.guavabot.marshpermissions.domain.interactor.GetAppListFilteredUseCase;
import com.guavabot.marshpermissions.domain.interactor.ToggleAppHiddenUseCase;
import com.guavabot.marshpermissions.injection.ComponentScope;
import com.guavabot.marshpermissions.ui.Presenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.subscriptions.CompositeSubscription;

/**
 * Presenter for a list of apps in an {@link AppListView}.
 */
@ComponentScope
public class AppListPresenter implements Presenter {

    private final AppListView mAppListView;
    private final GetAppListFilteredUseCase mGetAppListFilteredUseCase;
    private final ToggleAppHiddenUseCase mToggleAppHiddenUseCase;
    private final Schedulers mSchedulers;

    private final CompositeSubscription mSubscriptions = new CompositeSubscription();

    @Inject
    public AppListPresenter(AppListView appListView,
                            GetAppListFilteredUseCase getAppListFilteredUseCase,
                            ToggleAppHiddenUseCase toggleAppHiddenUseCase,
                            Schedulers schedulers) {
        mAppListView = appListView;
        mGetAppListFilteredUseCase = getAppListFilteredUseCase;
        mToggleAppHiddenUseCase = toggleAppHiddenUseCase;
        mSchedulers = schedulers;
    }

    @Override
    public void onStart() {
        mSubscriptions.add(mAppListView.getSearchQuery()
                .doOnNext(__ -> mAppListView.setLoading(true))
                .subscribeOn(mSchedulers.mainThread())
                .observeOn(mSchedulers.io())
                .flatMap(filter -> mGetAppListFilteredUseCase.execute(filter))
                .map((apps) -> Mapper.map(apps))
                .observeOn(mSchedulers.mainThread())
                .doOnNext(__ -> mAppListView.setLoading(false))
                .distinctUntilChanged()
                .subscribe(viewModels -> {
                    mAppListView.setApps(viewModels);
                }));
    }

    @Override
    public void onStop() {
        mSubscriptions.clear();
    }

    /**
     * An app item was clicked.
     */
    public void onItemClicked(AppViewModel app) {
        mAppListView.startAppInfo(app.getPackage());
    }

    /**
     * The button for an item was clicked.
     */
    public void onItemButtonClicked(AppViewModel app) {
        app.toggleHidden();
        mToggleAppHiddenUseCase.execute(app.getPackage(), app.isHidden())
                .subscribeOn(mSchedulers.io())
                .subscribe();
    }

    static class Mapper {

        static List<AppViewModel> map(List<App> apps) {
            List<AppViewModel> viewModelApps = new ArrayList<>(apps.size());
            for (App app : apps) {
                viewModelApps.add(map(app));
            }
            return viewModelApps;
        }

        static AppViewModel map(App app) {
            return new AppViewModel(app.getPackage(), app.getName(), app.isHidden(), app.getPermissions());
        }
    }
}
<commit_msg>set backpressure for executing search<commit_after>package com.guavabot.marshpermissions.ui.app_list;

import com.guavabot.marshpermissions.Schedulers;
import com.guavabot.marshpermissions.domain.entity.App;
import com.guavabot.marshpermissions.domain.interactor.GetAppListFilteredUseCase;
import com.guavabot.marshpermissions.domain.interactor.ToggleAppHiddenUseCase;
import com.guavabot.marshpermissions.injection.ComponentScope;
import com.guavabot.marshpermissions.ui.Presenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.subscriptions.CompositeSubscription;

/**
 * Presenter for a list of apps in an {@link AppListView}.
 */
@ComponentScope
public class AppListPresenter implements Presenter {

    private final AppListView mAppListView;
    private final GetAppListFilteredUseCase mGetAppListFilteredUseCase;
    private final ToggleAppHiddenUseCase mToggleAppHiddenUseCase;
    private final Schedulers mSchedulers;

    private final CompositeSubscription mSubscriptions = new CompositeSubscription();

    @Inject
    public AppListPresenter(AppListView appListView,
                            GetAppListFilteredUseCase getAppListFilteredUseCase,
                            ToggleAppHiddenUseCase toggleAppHiddenUseCase,
                            Schedulers schedulers) {
        mAppListView = appListView;
        mGetAppListFilteredUseCase = getAppListFilteredUseCase;
        mToggleAppHiddenUseCase = toggleAppHiddenUseCase;
        mSchedulers = schedulers;
    }

    @Override
    public void onStart() {
        mSubscriptions.add(mAppListView.getSearchQuery()
                .doOnNext(__ -> mAppListView.setLoading(true))
                .subscribeOn(mSchedulers.mainThread())
                .onBackpressureLatest()
                .observeOn(mSchedulers.io())
                .flatMap(filter -> mGetAppListFilteredUseCase.execute(filter))
                .map((apps) -> Mapper.map(apps))
                .observeOn(mSchedulers.mainThread())
                .doOnNext(__ -> mAppListView.setLoading(false))
                .distinctUntilChanged()
                .subscribe(viewModels -> {
                    mAppListView.setApps(viewModels);
                }));
    }

    @Override
    public void onStop() {
        mSubscriptions.clear();
    }

    /**
     * An app item was clicked.
     */
    public void onItemClicked(AppViewModel app) {
        mAppListView.startAppInfo(app.getPackage());
    }

    /**
     * The button for an item was clicked.
     */
    public void onItemButtonClicked(AppViewModel app) {
        app.toggleHidden();
        mToggleAppHiddenUseCase.execute(app.getPackage(), app.isHidden())
                .subscribeOn(mSchedulers.io())
                .subscribe();
    }

    static class Mapper {

        static List<AppViewModel> map(List<App> apps) {
            List<AppViewModel> viewModelApps = new ArrayList<>(apps.size());
            for (App app : apps) {
                viewModelApps.add(map(app));
            }
            return viewModelApps;
        }

        static AppViewModel map(App app) {
            return new AppViewModel(app.getPackage(), app.getName(), app.isHidden(), app.getPermissions());
        }
    }
}

{'fraction_non_alphanumeric': 0.08860955927995035, 'fraction_numerical': 0.0, 'mean_word_length': 2.5865331107401226, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, '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': '5110314', 'n_tokens_mistral': 1855, 'n_tokens_neox': 1782, 'n_words': 317}