<commit_before>package org.stepic.droid.core.presenters

import org.stepic.droid.analytic.Analytic
import org.stepic.droid.concurrency.IMainHandler
import org.stepic.droid.core.presenters.contracts.StepQualityView
import org.stepic.droid.model.Video
import org.stepic.droid.preferences.UserPreferences
import org.stepic.droid.store.operations.DatabaseFacade
import java.util.concurrent.ThreadPoolExecutor

class StepQualityPresenter(val threadPoolExecutor: ThreadPoolExecutor,
                           val mainHandler: IMainHandler,
                           val databaseFacade: DatabaseFacade,
                           val userPreferences: UserPreferences,
                           val analytic: Analytic) : PresenterBase<StepQualityView>() {

    fun determineQuality(stepVideo: Video?) {
        if (stepVideo == null) {
            analytic.reportEvent(Analytic.Video.QUALITY_NOT_DETERMINATED)
        } else {
            threadPoolExecutor.execute {
                val video = databaseFacade.getCachedVideoById(stepVideo.id)
                val quality: String
                if (video == null) {
                    val resultQuality: String = try {

                        val weWant = Integer.parseInt(userPreferences.qualityVideo)
                        val urls = stepVideo.urls
                        var bestDelta = Integer.MAX_VALUE
                        var bestIndex = 0
                        for (i in urls.indices) {
                            val current = Integer.parseInt(urls[i].quality)
                            val delta = Math.abs(current - weWant)
                            if (delta < bestDelta) {
                                bestDelta = delta
                                bestIndex = i
                            }

                        }
                        urls[bestIndex].quality
                    } catch (e: NumberFormatException) {
                        userPreferences.qualityVideo
                    }


                    quality = resultQuality
                } else {
                    quality = video.quality
                }
                if (quality.isNullOrBlank()) {
                    analytic.reportEvent(Analytic.Video.QUALITY_NOT_DETERMINATED)
                } else {
                    val qualityForView = quality + "p"
                    mainHandler.post {
                        view?.showQuality(qualityForView)
                    }
                }
            }
        }

    }

}
<commit_msg>fix crash on empty urls: #Apps-810 fixed<commit_after>package org.stepic.droid.core.presenters

import org.stepic.droid.analytic.Analytic
import org.stepic.droid.concurrency.IMainHandler
import org.stepic.droid.core.presenters.contracts.StepQualityView
import org.stepic.droid.model.Video
import org.stepic.droid.preferences.UserPreferences
import org.stepic.droid.store.operations.DatabaseFacade
import java.util.concurrent.ThreadPoolExecutor

class StepQualityPresenter(val threadPoolExecutor: ThreadPoolExecutor,
                           val mainHandler: IMainHandler,
                           val databaseFacade: DatabaseFacade,
                           val userPreferences: UserPreferences,
                           val analytic: Analytic) : PresenterBase<StepQualityView>() {

    fun determineQuality(stepVideo: Video?) {
        if (stepVideo == null) {
            analytic.reportEvent(Analytic.Video.QUALITY_NOT_DETERMINATED)
        } else {
            threadPoolExecutor.execute {
                val video = databaseFacade.getCachedVideoById(stepVideo.id)
                val quality: String
                if (video == null) {
                    val resultQuality: String = try {
                        val weWant = Integer.parseInt(userPreferences.qualityVideo)
                        val urls = stepVideo.urls //TODO: URLS can be empty here: Try to research it
                        var bestDelta = Integer.MAX_VALUE
                        var bestIndex = 0
                        for (i in urls.indices) {
                            val current = Integer.parseInt(urls[i].quality)
                            val delta = Math.abs(current - weWant)
                            if (delta < bestDelta) {
                                bestDelta = delta
                                bestIndex = i
                            }

                        }
                        urls[bestIndex].quality
                    } catch (e: Exception) {
                        userPreferences.qualityVideo
                    }

                    quality = resultQuality
                } else {
                    quality = video.quality
                }
                if (quality.isNullOrBlank()) {
                    analytic.reportEvent(Analytic.Video.QUALITY_NOT_DETERMINATED)
                } else {
                    val qualityForView = quality + "p"
                    mainHandler.post {
                        view?.showQuality(qualityForView)
                    }
                }
            }
        }

    }

}

{'fraction_non_alphanumeric': 0.06681119432400473, 'fraction_numerical': 0.0009854158454867955, 'mean_word_length': 1.2984601449275361, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '5565391', 'n_tokens_mistral': 1254, 'n_tokens_neox': 1153, 'n_words': 226}