<commit_before>/*
 * Copyright (c) 2022 toastkidjp.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
 */

package jp.toastkid.media.music.view

import android.content.ComponentName
import android.net.Uri
import android.support.v4.media.MediaBrowserCompat
import android.support.v4.media.session.MediaControllerCompat
import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat
import androidx.activity.ComponentActivity
import androidx.annotation.StringRes
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.DropdownMenu
import androidx.compose.material.DropdownMenuItem
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.net.toUri
import androidx.core.os.bundleOf
import androidx.lifecycle.ViewModelProvider
import coil.compose.AsyncImage
import jp.toastkid.lib.BrowserViewModel
import jp.toastkid.lib.ContentViewModel
import jp.toastkid.lib.preference.PreferenceApplier
import jp.toastkid.media.R
import jp.toastkid.media.music.MediaPlayerService
import jp.toastkid.media.music.popup.MediaPlayerPopupViewModel
import jp.toastkid.media.music.popup.playback.speed.PlayingSpeed
import kotlinx.coroutines.launch

@Composable
fun MusicListUi() {
    val activity = LocalContext.current as? ComponentActivity ?: return
    val viewModelProvider = ViewModelProvider(activity)
    val browserViewModel = viewModelProvider.get(BrowserViewModel::class.java)
    val mediaPlayerPopupViewModel = viewModelProvider.get(MediaPlayerPopupViewModel::class.java)

    var mediaBrowser: MediaBrowserCompat? = null

    val controllerCallback = object : MediaControllerCompat.Callback() {

        override fun onPlaybackStateChanged(state: PlaybackStateCompat?) {
            when (state?.state) {
                PlaybackStateCompat.STATE_SKIPPING_TO_PREVIOUS -> {
                    mediaPlayerPopupViewModel.previous()?.let {
                        play(it, attemptToGetMediaController(activity), mediaPlayerPopupViewModel)
                    }
                }
                PlaybackStateCompat.STATE_SKIPPING_TO_NEXT -> {
                    mediaPlayerPopupViewModel.next()?.let {
                        play(it, attemptToGetMediaController(activity), mediaPlayerPopupViewModel)
                    }
                }
                PlaybackStateCompat.STATE_PLAYING -> mediaPlayerPopupViewModel.playing = true
                PlaybackStateCompat.STATE_PAUSED,
                PlaybackStateCompat.STATE_STOPPED -> mediaPlayerPopupViewModel.playing = false
                else -> Unit
            }
        }
    }

    val subscriptionCallback = object : MediaBrowserCompat.SubscriptionCallback() {

        override fun onChildrenLoaded(
            parentId: String,
            children: MutableList<MediaBrowserCompat.MediaItem>
        ) {
            if (children.isEmpty()) {
                //hide()
                return
            }

            attemptToGetMediaController(activity)?.also {
                it.transportControls?.prepare()
            }

            mediaPlayerPopupViewModel?.nextMusics(children)
        }
    }

    val connectionCallback = object : MediaBrowserCompat.ConnectionCallback() {

        override fun onConnected() {
            val mediaBrowser = mediaBrowser ?: return
            val sessionToken: MediaSessionCompat.Token = mediaBrowser?.sessionToken ?: return
            val mediaControllerCompat =
                MediaControllerCompat(activity, sessionToken)
            mediaControllerCompat.registerCallback(controllerCallback)
            MediaControllerCompat.setMediaController(
                activity,
                mediaControllerCompat
            )

            mediaBrowser?.subscribe(mediaBrowser.root, subscriptionCallback)<commit_msg>refactor #496 Rename local variable.<commit_after>/*
 * Copyright (c) 2022 toastkidjp.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
 */

package jp.toastkid.media.music.view

import android.content.ComponentName
import android.net.Uri
import android.support.v4.media.MediaBrowserCompat
import android.support.v4.media.session.MediaControllerCompat
import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat
import androidx.activity.ComponentActivity
import androidx.annotation.StringRes
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.DropdownMenu
import androidx.compose.material.DropdownMenuItem
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.net.toUri
import androidx.core.os.bundleOf
import androidx.lifecycle.ViewModelProvider
import coil.compose.AsyncImage
import jp.toastkid.lib.BrowserViewModel
import jp.toastkid.lib.ContentViewModel
import jp.toastkid.lib.preference.PreferenceApplier
import jp.toastkid.media.R
import jp.toastkid.media.music.MediaPlayerService
import jp.toastkid.media.music.popup.MediaPlayerPopupViewModel
import jp.toastkid.media.music.popup.playback.speed.PlayingSpeed
import kotlinx.coroutines.launch

@Composable
fun MusicListUi() {
    val activity = LocalContext.current as? ComponentActivity ?: return
    val viewModelProvider = ViewModelProvider(activity)
    val browserViewModel = viewModelProvider.get(BrowserViewModel::class.java)
    val mediaPlayerPopupViewModel = viewModelProvider.get(MediaPlayerPopupViewModel::class.java)

    var mediaBrowser: MediaBrowserCompat? = null

    val controllerCallback = object : MediaControllerCompat.Callback() {

        override fun onPlaybackStateChanged(state: PlaybackStateCompat?) {
            when (state?.state) {
                PlaybackStateCompat.STATE_SKIPPING_TO_PREVIOUS -> {
                    mediaPlayerPopupViewModel.previous()?.let {
                        play(it, attemptToGetMediaController(activity), mediaPlayerPopupViewModel)
                    }
                }
                PlaybackStateCompat.STATE_SKIPPING_TO_NEXT -> {
                    mediaPlayerPopupViewModel.next()?.let {
                        play(it, attemptToGetMediaController(activity), mediaPlayerPopupViewModel)
                    }
                }
                PlaybackStateCompat.STATE_PLAYING -> mediaPlayerPopupViewModel.playing = true
                PlaybackStateCompat.STATE_PAUSED,
                PlaybackStateCompat.STATE_STOPPED -> mediaPlayerPopupViewModel.playing = false
                else -> Unit
            }
        }
    }

    val subscriptionCallback = object : MediaBrowserCompat.SubscriptionCallback() {

        override fun onChildrenLoaded(
            parentId: String,
            children: MutableList<MediaBrowserCompat.MediaItem>
        ) {
            if (children.isEmpty()) {
                //hide()
                return
            }

            attemptToGetMediaController(activity)?.also {
                it.transportControls?.prepare()
            }

            mediaPlayerPopupViewModel?.nextMusics(children)
        }
    }

    val connectionCallback = object : MediaBrowserCompat.ConnectionCallback() {

        override fun onConnected() {
            val mediaBrowserNonNull = mediaBrowser ?: return
            val sessionToken: MediaSessionCompat.Token = mediaBrowserNonNull?.sessionToken ?: return
            val mediaControllerCompat =
                MediaControllerCompat(activity, sessionToken)
            mediaControllerCompat.registerCallback(controllerCallback)
            MediaControllerCompat.setMediaController(
                activity,
                mediaControllerCompat
            )

            mediaBrowserNonNull?.subscribe(mediaBrowserNonNull.root, subscriptionCallback)
{'fraction_non_alphanumeric': 0.0740073827316107, 'fraction_numerical': 0.0024308994327901325, 'mean_word_length': 3.5599343185550083, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '416924', 'n_tokens_mistral': 2994, 'n_tokens_neox': 2864, 'n_words': 501}