<commit_before>package com.droibit.quickly.main

import com.droibit.quickly.data.config.ApplicationConfig
import com.droibit.quickly.data.repository.appinfo.AppInfo
import com.droibit.quickly.data.repository.appinfo.AppInfoRepository
import com.droibit.quickly.data.repository.settings.ShowSettingsRepository
import com.droibit.quickly.main.MainContract.LoadAppInfoTask.LoadEvent
import com.jakewharton.rxrelay.BehaviorRelay
import rx.Observable
import rx.schedulers.Schedulers
import java.util.concurrent.TimeUnit

class LoadAppInfoTask(
        private val appInfoRepository: AppInfoRepository,
        private val showSettingsRepository: ShowSettingsRepository,
        private val appConfig: ApplicationConfig,
        private val appInfosRelay: BehaviorRelay<LoadEvent>,
        private val runningRelay: BehaviorRelay<Boolean>) : MainContract.LoadAppInfoTask {

    override fun asObservable(): Observable<LoadEvent> = appInfosRelay

    @Suppress("HasPlatformType")
    override fun isRunning() = runningRelay.distinctUntilChanged()

    override fun requestLoad(forceReload: Boolean) {
        Observable.zip(
                Observable.just(null).timestamp(),
                appInfoRepository.loadAll(forceReload)
                        .flatMap { Observable.from(it).filter { filterIfOnlyInstalled(it) }.toList() }
                        .timestamp()
        ) { f, s -> Pair(s.timestampMillis - f.timestampMillis, s.value) }
                .flatMap {
                    val (elapsedTimeMillis, appInfos) = it
                    return@flatMap Observable.just(appInfos).run {
                        val delayMillis = appConfig.minTaskDurationMillis - elapsedTimeMillis
                        if (delayMillis > 0L) delay(delayMillis, TimeUnit.MILLISECONDS, Schedulers.immediate()) else this
                    }
                }.map { LoadEvent.OnResult(appInfos = it) }
                .subscribeOn(Schedulers.io())
                .doOnSubscribe { runningRelay.call(true) }
                .doOnUnsubscribe { runningRelay.call(false) }
                .subscribe { appInfosRelay.call(it) }

    }

    private fun filterIfOnlyInstalled(appInfo: AppInfo): Boolean {
        if (showSettingsRepository.isShowSystem) {
            return true
        }
        return !appInfo.preInstalled
    }
}<commit_msg>Refactoring<commit_after>package com.droibit.quickly.main

import com.droibit.quickly.data.config.ApplicationConfig
import com.droibit.quickly.data.repository.appinfo.AppInfo
import com.droibit.quickly.data.repository.appinfo.AppInfoRepository
import com.droibit.quickly.data.repository.settings.ShowSettingsRepository
import com.droibit.quickly.main.MainContract.LoadAppInfoTask.LoadEvent
import com.jakewharton.rxrelay.BehaviorRelay
import rx.Observable
import rx.schedulers.Schedulers
import timber.log.Timber
import java.util.concurrent.TimeUnit

class LoadAppInfoTask(
        private val appInfoRepository: AppInfoRepository,
        private val showSettingsRepository: ShowSettingsRepository,
        private val appConfig: ApplicationConfig,
        private val appInfosRelay: BehaviorRelay<LoadEvent>,
        private val runningRelay: BehaviorRelay<Boolean>) : MainContract.LoadAppInfoTask {

    override fun asObservable(): Observable<LoadEvent> = appInfosRelay

    @Suppress("HasPlatformType")
    override fun isRunning() = runningRelay.distinctUntilChanged()

    override fun requestLoad(forceReload: Boolean) {
        Observable.zip(
                Observable.just(null).timestamp(),
                loadAppInfos(forceReload).timestamp()
        ) { f, s -> Pair(s.timestampMillis - f.timestampMillis, s.value) }
                .flatMap {
                    val (elapsedTimeMillis, appInfos) = it
                    Timber.d("loadAppInfo(forceReload=$forceReload): ${elapsedTimeMillis}ms")
                    return@flatMap Observable.just(appInfos).run {
                        val delayMillis = appConfig.minTaskDurationMillis - elapsedTimeMillis
                        if (delayMillis > 0L) delay(delayMillis, TimeUnit.MILLISECONDS, Schedulers.immediate()) else this
                    }
                }.map { LoadEvent.OnResult(appInfos = it) }
                .subscribeOn(Schedulers.io())
                .doOnSubscribe { runningRelay.call(true) }
                .doOnUnsubscribe { runningRelay.call(false) }
                .subscribe { appInfosRelay.call(it) }

    }

    private fun filterIfOnlyInstalled(appInfo: AppInfo): Boolean {
        if (showSettingsRepository.isShowSystem) {
            return true
        }
        return !appInfo.preInstalled
    }

    private fun loadAppInfos(forceReload: Boolean): Observable<List<AppInfo>> {
        return appInfoRepository.loadAll(forceReload)
                .flatMap { Observable.from(it).filter { filterIfOnlyInstalled(it) }.toList() }
    }
}
{'fraction_non_alphanumeric': 0.08734073160706946, 'fraction_numerical': 0.0004110152075626798, 'mean_word_length': 2.793452844894778, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '6345082', 'n_tokens_mistral': 1367, 'n_tokens_neox': 1332, 'n_words': 230}