205 lines
7.5 KiB
Kotlin
205 lines
7.5 KiB
Kotlin
package melody.offline.music.activity
|
|
|
|
import android.annotation.SuppressLint
|
|
import android.view.View
|
|
import androidx.media3.common.MediaItem
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
import com.gyf.immersionbar.ktx.immersionBar
|
|
import kotlinx.coroutines.channels.Channel
|
|
import kotlinx.coroutines.isActive
|
|
import kotlinx.coroutines.selects.select
|
|
import melody.offline.music.App
|
|
import melody.offline.music.R
|
|
import melody.offline.music.adapter.OfflineSongsAdapter
|
|
import melody.offline.music.ads.AdPlacement
|
|
import melody.offline.music.ads.LolAdWrapper
|
|
import melody.offline.music.bean.OfflineBean
|
|
import melody.offline.music.databinding.ActivityOfflineSongsBinding
|
|
import melody.offline.music.util.AnalysisUtil
|
|
import melody.offline.music.util.LogTag
|
|
import org.json.JSONObject
|
|
|
|
class MoOfflineSongsActivity : MoBaseActivity() {
|
|
private val requests: Channel<Request> = Channel(Channel.UNLIMITED)
|
|
|
|
sealed class Request {
|
|
data object TryAgain : Request()
|
|
data object OnFavorites : Request()
|
|
data class OnDownload(val mediaItem: MediaItem) : Request()
|
|
data class UpdateFavorite(val id: String) : Request()
|
|
}
|
|
|
|
private lateinit var binding: ActivityOfflineSongsBinding
|
|
private var adapter: OfflineSongsAdapter? = null
|
|
private var offlineList: MutableList<OfflineBean> = mutableListOf()
|
|
|
|
override suspend fun main() {
|
|
binding = ActivityOfflineSongsBinding.inflate(layoutInflater)
|
|
setContentView(binding.root)
|
|
LolAdWrapper.shared.showAdTiming(this, AdPlacement.INST_ME_PAGE_LIST)
|
|
initImmersionBar()
|
|
initView()
|
|
initAdapter()
|
|
LolAdWrapper.shared.loadAdIfNotCached(this, AdPlacement.INST_ME_PAGE_LIST)
|
|
initData()
|
|
onReceive()
|
|
}
|
|
|
|
private fun initImmersionBar() {
|
|
immersionBar {
|
|
statusBarDarkFont(false)
|
|
statusBarView(binding.view)
|
|
}
|
|
}
|
|
|
|
@SuppressLint("NotifyDataSetChanged")
|
|
private suspend fun onReceive() {
|
|
while (isActive) {
|
|
select<Unit> {
|
|
requests.onReceive {
|
|
when (it) {
|
|
Request.TryAgain -> {
|
|
initData()
|
|
}
|
|
|
|
is Request.OnDownload -> {
|
|
|
|
}
|
|
|
|
Request.OnFavorites -> {
|
|
if (meController != null && meController.currentMediaItem != null) {
|
|
val currentMediaItem = meController.currentMediaItem
|
|
val jsonObject = JSONObject()
|
|
jsonObject.put(
|
|
"song_title", "${currentMediaItem?.mediaMetadata?.title}"
|
|
)
|
|
val songMap = mutableMapOf(
|
|
Pair(
|
|
AnalysisUtil.PARAM_VALUE, jsonObject.toString()
|
|
)
|
|
)
|
|
val currentFavoriteBean =
|
|
App.appFavoriteDBManager.getFavoriteBeanByID(currentMediaItem?.mediaId!!)
|
|
if (currentFavoriteBean != null) {
|
|
currentFavoriteBean.isFavorite = !currentFavoriteBean.isFavorite
|
|
App.appFavoriteDBManager.updateFavoriteBean(currentFavoriteBean)
|
|
if (currentFavoriteBean.isFavorite) {
|
|
AnalysisUtil.logEvent(
|
|
AnalysisUtil.PLAYER_B_LOVE_CLICK, songMap
|
|
)
|
|
} else {
|
|
AnalysisUtil.logEvent(
|
|
AnalysisUtil.PLAYER_B_UN_LOVE_CLICK, songMap
|
|
)
|
|
}
|
|
} else {
|
|
insertFavoriteData(currentMediaItem)
|
|
AnalysisUtil.logEvent(AnalysisUtil.PLAYER_B_LOVE_CLICK, songMap)
|
|
}
|
|
requests.trySend(Request.UpdateFavorite(currentMediaItem.mediaId))
|
|
}
|
|
}
|
|
|
|
is Request.UpdateFavorite -> {
|
|
|
|
}
|
|
}
|
|
}
|
|
events.onReceive {
|
|
when (it) {
|
|
Event.ActivityOnResume -> {
|
|
activityOnResume()
|
|
}
|
|
|
|
Event.AutomaticallySwitchSongs -> {
|
|
if (adapter != null) {
|
|
adapter?.notifyDataSetChanged()
|
|
}
|
|
}
|
|
|
|
else -> {}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@SuppressLint("NotifyDataSetChanged")
|
|
private fun activityOnResume() {
|
|
addMusicPlayerViewToLayout(binding.playMusicLayout)
|
|
|
|
if (adapter != null) {
|
|
adapter?.notifyDataSetChanged()
|
|
}
|
|
|
|
LolAdWrapper.shared.loadAdShowNativeAd(
|
|
this,
|
|
AdPlacement.NATIVE_ME_PAGE_LIST,
|
|
binding.frameAd,
|
|
R.layout.ad_layout_admob_banner,
|
|
R.layout.ad_layout_max_banner
|
|
)
|
|
}
|
|
|
|
private fun initView() {
|
|
binding.backBtn.setOnClickListener {
|
|
finish()
|
|
}
|
|
binding.tryAgainBtn.setOnClickListener {
|
|
requests.trySend(Request.TryAgain)
|
|
}
|
|
}
|
|
|
|
private fun initAdapter() {
|
|
adapter = OfflineSongsAdapter(this, offlineList)
|
|
adapter?.setOnFavoriteClickListener(object : OfflineSongsAdapter.OnFavoriteClickListener {
|
|
override fun onFavoriteClick(position: Int) {
|
|
requests.trySend(Request.OnFavorites)
|
|
}
|
|
})
|
|
binding.rv.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
|
|
binding.rv.adapter = adapter
|
|
}
|
|
|
|
@SuppressLint("NotifyDataSetChanged")
|
|
private suspend fun initData() {
|
|
showLoadingUi()
|
|
|
|
offlineList.clear()
|
|
val offlineBeans = App.appOfflineDBManager.getAllOfflineBeans()
|
|
// 更新
|
|
offlineBeans.forEach { offlineBean ->
|
|
val favoriteBean = App.appFavoriteDBManager.getFavoriteBeanByID(offlineBean.videoId)
|
|
if (offlineBean.id == favoriteBean?.id) {
|
|
offlineBean.isFavorite = favoriteBean.isFavorite
|
|
App.appOfflineDBManager.updateOfflineBean(offlineBean)
|
|
}
|
|
}
|
|
val filteredBeans = offlineBeans.filter { it.bytesDownloaded?.let { bytes -> bytes > 0 } == true }
|
|
offlineList.addAll(filteredBeans)
|
|
if (offlineList.size > 0) {
|
|
showDataUi()
|
|
} else {
|
|
showNoContentUi()
|
|
}
|
|
if (adapter != null) {
|
|
adapter?.notifyDataSetChanged()
|
|
}
|
|
|
|
}
|
|
|
|
private fun showDataUi() {
|
|
binding.loadingLayout.visibility = View.GONE
|
|
binding.noContentLayout.visibility = View.GONE
|
|
}
|
|
|
|
private fun showLoadingUi() {
|
|
binding.loadingLayout.visibility = View.VISIBLE
|
|
binding.noContentLayout.visibility = View.GONE
|
|
}
|
|
|
|
private fun showNoContentUi() {
|
|
binding.loadingLayout.visibility = View.GONE
|
|
binding.noContentLayout.visibility = View.VISIBLE
|
|
}
|
|
} |