<commit_before>package com.example.features.dashboard.presenter;

import android.os.Build;
import android.widget.Toast;
import com.example.features.dashboard.analytics.FetchShotsEvent;
import com.example.features.dashboard.analytics.ShotFetchingFailureEvent;
import com.example.features.dashboard.view.MainView;
import com.example.model.Shot;
import com.example.model.api.ShotResponse;
import com.example.networking.RestService;
import com.example.tools.analytics.AnalyticsHelper;
import com.example.util.mvp.base.Mapper;
import com.hannesdorfmann.mosby3.mvp.MvpNullObjectBasePresenter;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;

import javax.annotation.Nullable;
import javax.inject.Inject;

public class MainPresenter extends MvpNullObjectBasePresenter<MainView> {

    private final RestService restService;
    private final AnalyticsHelper analyticsHelper;
    private final Mapper<ShotResponse, Shot> shotMapper;
    @Nullable private Disposable subscription;

    @Inject
    public MainPresenter(RestService restService, AnalyticsHelper analyticsHelper, Mapper<ShotResponse, Shot> shotMapper) {
        this.restService = restService;
        this.analyticsHelper = analyticsHelper;
        this.shotMapper = shotMapper;
    }

    public void fetchShots() {
        Timber.i("Fetching shots..");
        analyticsHelper.logEvent(new FetchShotsEvent());
        getView().showLoadingBar();
        subscription = restService.getShots()
                .subscribeOn(Schedulers.io())
                .flatMapObservable(Observable::fromIterable)
                .filter(shot -> shot.getImagesData() != null && shot.getImagesData().getTeaserImageUrl() != null)
                .map(shotMapper::map)
                .observeOn(AndroidSchedulers.mainThread())
                .toList()
                .subscribe(shots -> getView().displayShotsList(shots), throwable -> {
                    analyticsHelper.logEvent(new ShotFetchingFailureEvent());
                    Timber.e(throwable, "An error occurred while fetching shots.");
                    getView().showLoadingFailureError();
                });
    }

    @Override
    public void detachView(boolean retainInstance) {
        super.detachView(retainInstance);
        if (subscription != null && !subscription.isDisposed()) {
            subscription.dispose();
        }
    }

    public void onUpdateTileMenuItemClicked() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            getView().requestActiveTileUpdate();
        } else {<commit_msg>[Issue-56]: Change orientation change while loading. Made sure that we only stop the rxjava subscription when the presenter instancei s not being retained. Means that rotating the device before loading finishes works properly.<commit_after>package com.example.features.dashboard.presenter;

import android.os.Build;
import android.widget.Toast;
import com.example.features.dashboard.analytics.FetchShotsEvent;
import com.example.features.dashboard.analytics.ShotFetchingFailureEvent;
import com.example.features.dashboard.view.MainView;
import com.example.model.Shot;
import com.example.model.api.ShotResponse;
import com.example.networking.RestService;
import com.example.tools.analytics.AnalyticsHelper;
import com.example.util.mvp.base.Mapper;
import com.hannesdorfmann.mosby3.mvp.MvpNullObjectBasePresenter;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;

import javax.annotation.Nullable;
import javax.inject.Inject;

public class MainPresenter extends MvpNullObjectBasePresenter<MainView> {

    private final RestService restService;
    private final AnalyticsHelper analyticsHelper;
    private final Mapper<ShotResponse, Shot> shotMapper;
    @Nullable private Disposable subscription;

    @Inject
    public MainPresenter(RestService restService, AnalyticsHelper analyticsHelper, Mapper<ShotResponse, Shot> shotMapper) {
        this.restService = restService;
        this.analyticsHelper = analyticsHelper;
        this.shotMapper = shotMapper;
    }

    public void fetchShots() {
        Timber.i("Fetching shots..");
        analyticsHelper.logEvent(new FetchShotsEvent());
        getView().showLoadingBar();
        subscription = restService.getShots()
                .subscribeOn(Schedulers.io())
                .flatMapObservable(Observable::fromIterable)
                .filter(shot -> shot.getImagesData() != null && shot.getImagesData().getTeaserImageUrl() != null)
                .map(shotMapper::map)
                .observeOn(AndroidSchedulers.mainThread())
                .toList()
                .subscribe(shots -> getView().displayShotsList(shots), throwable -> {
                    analyticsHelper.logEvent(new ShotFetchingFailureEvent());
                    Timber.e(throwable, "An error occurred while fetching shots.");
                    getView().showLoadingFailureError();
                });
    }

    @Override
    public void detachView(boolean retainInstance) {
        super.detachView(retainInstance);
        if (!retainInstance && subscription != null && !subscription.isDisposed()) {
            subscription.dispose();
        }
    }

    public void onUpdateTileMenuItemClicked() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            getView().requestActiveTileUpdate();
        } else {
{'fraction_non_alphanumeric': 0.09779516358463727, 'fraction_numerical': 0.0007112375533428165, 'mean_word_length': 3.377431906614786, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, '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': '4942832', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1515, 'n_words': 287}