<commit_before>/*
 * Copyright (C) 2021, <NAME>
 * All rights reserved.
 */
package tm.alashow.datmusic.ui.downloads

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.width
import androidx.compose.material.DropdownMenu
import androidx.compose.material.DropdownMenuItem
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.res.stringResource
import com.tonyodev.fetch2.Status
import tm.alashow.datmusic.domain.entities.AudioDownloadItem

sealed class AudioDownloadItemAction(open val audio: AudioDownloadItem) {
    data class Play(override val audio: AudioDownloadItem) : AudioDownloadItemAction(audio)
    data class PlayNext(override val audio: AudioDownloadItem) : AudioDownloadItemAction(audio)
    data class Resume(override val audio: AudioDownloadItem) : AudioDownloadItemAction(audio)
    data class Pause(override val audio: AudioDownloadItem) : AudioDownloadItemAction(audio)
    data class Cancel(override val audio: AudioDownloadItem) : AudioDownloadItemAction(audio)
    data class Retry(override val audio: AudioDownloadItem) : AudioDownloadItemAction(audio)
    data class Open(override val audio: AudioDownloadItem) : AudioDownloadItemAction(audio)
    data class Remove(override val audio: AudioDownloadItem) : AudioDownloadItemAction(audio)
    data class Delete(override val audio: AudioDownloadItem) : AudioDownloadItemAction(audio)

    companion object {
        fun from(actionLabelRes: Int, audio: AudioDownloadItem) = when (actionLabelRes) {
            R.string.downloads_download_play -> Play(audio)
            R.string.downloads_download_playNext -> PlayNext(audio)
            R.string.downloads_download_pause -> Pause(audio)
            R.string.downloads_download_resume -> Resume(audio)
            R.string.downloads_download_cancel -> Cancel(audio)
            R.string.downloads_download_retry -> Retry(audio)
            R.string.downloads_download_open -> Open(audio)
            R.string.downloads_download_remove -> Remove(audio)
            R.string.downloads_download_delete -> Delete(audio)
            else -> error("Unknown action: $actionLabelRes")
        }
    }
}

@OptIn(ExperimentalStdlibApi::class)
@Composable
internal fun AudioDownloadDropdownMenu(
    audioDownload: AudioDownloadItem,
    expanded: Boolean,
    onExpandedChange: (Boolean) -> Unit,
    modifier: Modifier = Modifier,
    onDropdownSelect: (Int) -> Unit
) {
    val items = buildList {
        val downloadInfo = audioDownload.downloadInfo
        add(R.string.downloads_download_play)
        when (downloadInfo.status) {
            Status.DOWNLOADING, Status.QUEUED, Status.PAUSED -> add(R.string.downloads_download_cancel)
            Status.CANCELLED, Status.FAILED -> add(R.string.downloads_download_delete)
            else -> Unit
        }
        if (downloadInfo.status == Status.COMPLETED) {
            add(R.string.downloads_download_open)
            add(R.string.downloads_download_remove)
            add(R.string.downloads_download_delete)
        }
    }

    if (items.isNotEmpty()) {
        IconButton(
            onClick = { onExpandedChange(true) },
            modifier = modifier
        ) {
            Icon(
                painter = rememberVectorPainter(Icons.Default.MoreVert),
                contentDescription = stringResource(R.string.audio_menu_cd),
            )<commit_msg>Playback: play next audio menu action in downloads<commit_after>/*
 * Copyright (C) 2021, <NAME>
 * All rights reserved.
 */
package tm.alashow.datmusic.ui.downloads

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.width
import androidx.compose.material.DropdownMenu
import androidx.compose.material.DropdownMenuItem
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.res.stringResource
import com.tonyodev.fetch2.Status
import tm.alashow.datmusic.domain.entities.AudioDownloadItem

sealed class AudioDownloadItemAction(open val audio: AudioDownloadItem) {
    data class Play(override val audio: AudioDownloadItem) : AudioDownloadItemAction(audio)
    data class PlayNext(override val audio: AudioDownloadItem) : AudioDownloadItemAction(audio)
    data class Resume(override val audio: AudioDownloadItem) : AudioDownloadItemAction(audio)
    data class Pause(override val audio: AudioDownloadItem) : AudioDownloadItemAction(audio)
    data class Cancel(override val audio: AudioDownloadItem) : AudioDownloadItemAction(audio)
    data class Retry(override val audio: AudioDownloadItem) : AudioDownloadItemAction(audio)
    data class Open(override val audio: AudioDownloadItem) : AudioDownloadItemAction(audio)
    data class Remove(override val audio: AudioDownloadItem) : AudioDownloadItemAction(audio)
    data class Delete(override val audio: AudioDownloadItem) : AudioDownloadItemAction(audio)

    companion object {
        fun from(actionLabelRes: Int, audio: AudioDownloadItem) = when (actionLabelRes) {
            R.string.downloads_download_play -> Play(audio)
            R.string.downloads_download_playNext -> PlayNext(audio)
            R.string.downloads_download_pause -> Pause(audio)
            R.string.downloads_download_resume -> Resume(audio)
            R.string.downloads_download_cancel -> Cancel(audio)
            R.string.downloads_download_retry -> Retry(audio)
            R.string.downloads_download_open -> Open(audio)
            R.string.downloads_download_remove -> Remove(audio)
            R.string.downloads_download_delete -> Delete(audio)
            else -> error("Unknown action: $actionLabelRes")
        }
    }
}

@OptIn(ExperimentalStdlibApi::class)
@Composable
internal fun AudioDownloadDropdownMenu(
    audioDownload: AudioDownloadItem,
    expanded: Boolean,
    onExpandedChange: (Boolean) -> Unit,
    modifier: Modifier = Modifier,
    onDropdownSelect: (Int) -> Unit
) {
    val items = buildList {
        val downloadInfo = audioDownload.downloadInfo
        add(R.string.downloads_download_play)
        add(R.string.downloads_download_playNext)
        when (downloadInfo.status) {
            Status.DOWNLOADING, Status.QUEUED, Status.PAUSED -> add(R.string.downloads_download_cancel)
            Status.CANCELLED, Status.FAILED -> add(R.string.downloads_download_delete)
            else -> Unit
        }
        if (downloadInfo.status == Status.COMPLETED) {
            add(R.string.downloads_download_open)
            add(R.string.downloads_download_remove)
            add(R.string.downloads_download_delete)
        }
    }

    if (items.isNotEmpty()) {
        IconButton(
            onClick = { onExpandedChange(true) },
            modifier = modifier
        ) {
            Icon(
                painter = rememberVectorPainter(Icons.Default.MoreVert),
                contentDescription = stringResource(R.string.audio_menu_cd),
            )
{'fraction_non_alphanumeric': 0.08561777315837339, 'fraction_numerical': 0.0012992074834351046, 'mean_word_length': 3.6569872958257714, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 125, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6893689', 'n_tokens_mistral': 2155, 'n_tokens_neox': 2140, 'n_words': 404}