Created
June 19, 2024 18:48
-
-
Save sajjadyousefnia/9cf9adce7731ad91ddf871bd0f503e32 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sands.android.view.adapter | |
import android.app.DownloadManager | |
import android.content.Context | |
import android.content.Intent | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import androidx.core.content.ContextCompat | |
import androidx.core.content.ContextCompat.startActivity | |
import androidx.recyclerview.widget.RecyclerView | |
import com.bumptech.glide.Glide | |
import com.bumptech.glide.load.engine.DiskCacheStrategy | |
import com.sands.android.R | |
import com.sands.android.dao.entity.DownloadModel | |
import com.sands.android.databinding.AdapterRunningDownloadBinding | |
import java.math.BigDecimal | |
import java.math.RoundingMode | |
class AdapterRunningDownload( | |
private val context: Context, private val runningList: MutableList<DownloadModel> | |
) : RecyclerView.Adapter<AdapterRunningDownload.RunningDownloadViewHolder>() { | |
override fun onCreateViewHolder( | |
viewGroup: ViewGroup, viewType: Int | |
): RunningDownloadViewHolder { | |
val adapterView = R.layout.adapter_running_download | |
val itemView = LayoutInflater.from(viewGroup.context).inflate(adapterView, viewGroup, false) | |
return RunningDownloadViewHolder(itemView) | |
} | |
override fun getItemCount(): Int = runningList.size | |
override fun onBindViewHolder(holder: RunningDownloadViewHolder, position: Int) { | |
val item = runningList[position] | |
holder.binding.tvTitle.text = item.title | |
holder.binding.circularProgress.progress = item.percentage | |
holder.binding.tvDownloadedBytes.text = item.downloadedBytes.toString() | |
if (item.downloadedBytes == item.totalBytes) { | |
holder.binding.tvStatus.text = "کامل" | |
} | |
Glide.with(context).load(item.cover) | |
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC) | |
.into(holder.binding.imgCover) | |
val speed = item.currentSpeed | |
holder.binding.tvSpeed.text = chooseBetweenKBandMB(speed) | |
holder.binding.tvDownloadedBytes.text = | |
chooseBetweenKBandMB( | |
item.downloadedBytes, | |
false | |
) + "/" + chooseBetweenKBandMB( | |
item.totalBytes, false | |
) | |
holder.binding.tvRemainingTime.text = item.remaingTime | |
holder.binding.btnChangeState.setOnClickListener { | |
changeState(item) | |
val intent = Intent(DownloadManager.ACTION_VIEW_DOWNLOADS) | |
context.startActivity(intent) | |
} | |
if (item.isStopped == false) { | |
holder.binding.btnChangeState.setImageDrawable( | |
ContextCompat.getDrawable(context, R.drawable.ic_pause) | |
) | |
} else if (item.isStopped == true) { | |
holder.binding.btnChangeState.setImageDrawable( | |
ContextCompat.getDrawable( | |
context, R.drawable.ic_play | |
) | |
) | |
} | |
holder.binding.circularProgress.setOnClickListener { | |
holder.binding.btnChangeState.callOnClick() | |
} | |
holder.binding.root.setOnClickListener { | |
holder.binding.btnChangeState.callOnClick() | |
} | |
} | |
private fun changeState(item: DownloadModel) { | |
if (item.isStopped == false) { | |
item.isStopped = true | |
} else if (item.isStopped == true) { | |
item.isStopped = false | |
} | |
} | |
private fun chooseBetweenKBandMB(value: Long, isSpeed: Boolean = true): String { | |
return when (value.toLong() > 1_000) { | |
false -> { | |
convertBytesToKBString(value, isSpeed) | |
} | |
true -> { | |
convertBytesToMBString(value, isSpeed) | |
} | |
} | |
} | |
private fun convertBytesToKBString(speed: Long, isSpeed: Boolean = true): String { | |
return (BigDecimal(speed.toLong()).divide( | |
BigDecimal(1_000), 2, RoundingMode.HALF_EVEN | |
)).toString() + when (isSpeed) { | |
true -> "KB/s" | |
false -> "KB" | |
} | |
} | |
private fun convertBytesToMBString(speed: Long, isSpeed: Boolean): String { | |
return (BigDecimal(speed.toLong()).divide( | |
BigDecimal(1_000_000), 2, RoundingMode.HALF_EVEN | |
)).toString() + when (isSpeed) { | |
true -> "MB/s" | |
false -> "MB" | |
} | |
} | |
inner class RunningDownloadViewHolder(v: View) : RecyclerView.ViewHolder(v) { | |
val binding = AdapterRunningDownloadBinding.bind(v) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment