<commit_before>/*
 * UpdateHelper.kt
 * Implements the UpdateHelper class
 * A UpdateHelper provides methods to update a single station or the whole collection of stations
 *
 * This file is part of
 * TRANSISTOR - Radio App for Android
 *
 * Copyright (c) 2015-20 - Y20K.org
 * Licensed under the MIT-License
 * http://opensource.org/licenses/MIT
 */


package org.y20k.transistor.helpers

import android.content.Context
import org.y20k.transistor.Keys
import org.y20k.transistor.core.Collection
import org.y20k.transistor.core.Station
import org.y20k.transistor.search.RadioBrowserResult
import org.y20k.transistor.search.RadioBrowserSearch


/*
 * UpdateHelper class
 */
class UpdateHelper(private val context: Context, private val updateHelperListener: UpdateHelperListener, private var collection: Collection): RadioBrowserSearch.RadioBrowserSearchListener {

    /* Define log tag */
    private val TAG: String = LogHelper.makeLogTag(UpdateHelper::class.java)


    /* Listener Interface */
    interface UpdateHelperListener {
        fun onStationUpdated(collection: Collection, positionPriorUpdate: Int, positionAfterUpdate: Int)
    }


    /* Overrides onRadioBrowserSearchResults from RadioBrowserSearchListener */
    override fun onRadioBrowserSearchResults(results: Array<RadioBrowserResult>) {
        if (results.isNotEmpty()){
            // get station from results
            val station: Station = results[0].toStation()
            // get position
            val positionPriorUpdate = CollectionHelper.getStationPositionFromRadioBrowserStationUuid(collection, station.radioBrowserStationUuid)
            // update (and sort) collection
            collection = CollectionHelper.updateStation(context, collection, station)
            // get new position
            val positionAfterUpdate: Int = CollectionHelper.getStationPositionFromRadioBrowserStationUuid(collection, station.radioBrowserStationUuid)
            // hand over results
            updateHelperListener.onStationUpdated(collection, positionPriorUpdate, positionAfterUpdate)
        }
    }


    /* Updates the whole collection of stations */
    fun updateCollection() {
        PreferencesHelper.saveLastUpdateCollection(context)
        collection.stations.forEach {station ->
            updateStation(station)
        }
    }


    /* Initiates update of a station's information */
    fun updateStation(station: Station) {
        if (station.radioBrowserStationUuid.isNotEmpty()) {
            // get updated station from radio browser - results are handled by onRadioBrowserSearchResults
            val radioBrowserSearch: RadioBrowserSearch = RadioBrowserSearch(context, this)
            radioBrowserSearch.searchStation(context, station.radioBrowserStationUuid, Keys.SEARCH_TYPE_BY_UUID)
        } else if (station.remoteStationLocation.isNotEmpty()) {
            // download playlist // todo check content type detection is necessary here
            DownloadHelper.downloadPlaylists(context, arrayOf(station.remoteStationLocation))
        } else {
            LogHelper.w(TAG, "Unable to update station: ${station.name}.")
        }
    }


}<commit_msg>prevents a race condition in update of collection<commit_after>/*
 * UpdateHelper.kt
 * Implements the UpdateHelper class
 * A UpdateHelper provides methods to update a single station or the whole collection of stations
 *
 * This file is part of
 * TRANSISTOR - Radio App for Android
 *
 * Copyright (c) 2015-20 - Y20K.org
 * Licensed under the MIT-License
 * http://opensource.org/licenses/MIT
 */


package org.y20k.transistor.helpers

import android.content.Context
import org.y20k.transistor.Keys
import org.y20k.transistor.core.Collection
import org.y20k.transistor.core.Station
import org.y20k.transistor.search.RadioBrowserResult
import org.y20k.transistor.search.RadioBrowserSearch


/*
 * UpdateHelper class
 */
class UpdateHelper(private val context: Context, private val updateHelperListener: UpdateHelperListener, private var collection: Collection): RadioBrowserSearch.RadioBrowserSearchListener {

    /* Define log tag */
    private val TAG: String = LogHelper.makeLogTag(UpdateHelper::class.java)


    /* Main class variables */
    private var radioBrowserSearchCounter: Int = 0
    private var remoteStationLocationsList: MutableList<String> = mutableListOf()


    /* Listener Interface */
    interface UpdateHelperListener {
        fun onStationUpdated(collection: Collection, positionPriorUpdate: Int, positionAfterUpdate: Int)
    }


    /* Overrides onRadioBrowserSearchResults from RadioBrowserSearchListener */
    override fun onRadioBrowserSearchResults(results: Array<RadioBrowserResult>) {
        if (results.isNotEmpty()){
            // get station from results
            val station: Station = results[0].toStation()
            // get position
            val positionPriorUpdate = CollectionHelper.getStationPositionFromRadioBrowserStationUuid(collection, station.radioBrowserStationUuid)
            // update (and sort) collection
            collection = CollectionHelper.updateStation(context, collection, station)
            // get new position
            val positionAfterUpdate: Int = CollectionHelper.getStationPositionFromRadioBrowserStationUuid(collection, station.radioBrowserStationUuid)
            // hand over results
            updateHelperListener.onStationUpdated(collection, positionPriorUpdate, positionAfterUpdate)
            // decrease counter
            radioBrowserSearchCounter--
            // all downloads from radio browser succeeded
            if (radioBrowserSearchCounter == 0 && remoteStationLocationsList.isNotEmpty()) {
                // direct download of playlists
                DownloadHelper.downloadPlaylists(context, remoteStationLocationsList.toTypedArray())
            }
        }
    }


    /* Updates the whole collection of stations */
    fun updateCollection() {
        PreferencesHelper.saveLastUpdateCollection(context)
        collection.stations.forEach {station ->
            if (station.radioBrowserStationUuid.isNotEmpty()) {
                // increase counter
                radioBrowserSearchCounter++
                // request download from radio browser
                downloadFromRadioBrowser(station.radioBrowserStationUuid)
            } else if (station.remoteStationLocation.isNotEmpty()) {
                // add playlist link to list for later download
                remoteStationLocationsList.add(station.remoteStationLocation)
            } else {
                LogHelper.w(TAG, "Unable to update station: ${station.name}.")
            }
        }
    }


    /* Initiates update of a station's information */
    fun updateStation(station: Station) {
        if (station.radioBrowserStationUuid.isNotEmpty()) {
            // request download from radio browser
            downloadFromRadioBrowser(station.radioBrowserStationUuid)
        } else if (station.remoteStationLocation.isNotEmpty()) {
            // direct playlist download
            DownloadHelper.downloadPlaylists(context, arrayOf(station.remoteStationLocation))
        } else {
            LogHelper.w(TAG, "Unable to update station: ${station.name}.")
        }
    }


    /* Get updated station from radio browser - results are handled by onRadioBrowserSearchResults */
    private fun downloadFromRadioBrowser(radioBrowserStationUuid: String) {
        val radioBrowserSearch: RadioBrowserSearch = RadioBrowserSearch(context, this)
        radioBrowserSearch.searchStation(context, radioBrowserStationUuid, Keys.SEARCH_TYPE_BY_UUID)
    }

}
{'fraction_non_alphanumeric': 0.07538744418177042, 'fraction_numerical': 0.005778828473863935, 'mean_word_length': 3.1498637602179835, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2368064', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1792, 'n_words': 469}