-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathHanimeDownloadingRvAdapter.kt
More file actions
260 lines (239 loc) · 9.86 KB
/
Copy pathHanimeDownloadingRvAdapter.kt
File metadata and controls
260 lines (239 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package com.yenaly.han1meviewer.ui.adapter
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.RenderEffect
import android.graphics.Shader
import android.os.Build
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.core.view.updateLayoutParams
import androidx.interpolator.view.animation.FastOutSlowInInterpolator
import androidx.recyclerview.widget.DiffUtil
import com.chad.library.adapter4.BaseDifferAdapter
import com.chad.library.adapter4.viewholder.DataBindingHolder
import com.google.android.material.button.MaterialButton
import com.yenaly.han1meviewer.R
import com.yenaly.han1meviewer.databinding.ItemHanimeDownloadingBinding
import com.yenaly.han1meviewer.logic.entity.download.HanimeDownloadEntity
import com.yenaly.han1meviewer.logic.state.DownloadState
import com.yenaly.han1meviewer.ui.fragment.home.download.DownloadingFragment
import com.yenaly.han1meviewer.util.HImageMeower.loadUnhappily
import com.yenaly.han1meviewer.util.addUpdateListener
import com.yenaly.han1meviewer.util.setStateViewLayout
import com.yenaly.han1meviewer.util.showAlertDialog
import com.yenaly.han1meviewer.worker.HanimeDownloadManagerV2
import com.yenaly.han1meviewer.worker.HanimeDownloadWorker
import com.yenaly.yenaly_libs.utils.dpF
import com.yenaly.yenaly_libs.utils.formatFileSizeV2
import com.yenaly.yenaly_libs.utils.logFieldsChange
import java.lang.ref.WeakReference
/**
* @project Han1meViewer
* @author Yenaly Liew
* @time 2023/11/26 026 17:05
*/
class HanimeDownloadingRvAdapter(private val fragment: DownloadingFragment) :
BaseDifferAdapter<HanimeDownloadEntity, DataBindingHolder<ItemHanimeDownloadingBinding>>(
COMPARATOR
) {
init {
isStateViewEnable = true
}
companion object {
const val TAG = "HanimeDownloadingRvAdapter"
private val interpolator = FastOutSlowInInterpolator()
private const val DOWNLOADING = 1
private const val STATE = 1 shl 1
private const val PROGRESS = 1 shl 2
val COMPARATOR = object : DiffUtil.ItemCallback<HanimeDownloadEntity>() {
override fun areContentsTheSame(
oldItem: HanimeDownloadEntity,
newItem: HanimeDownloadEntity,
): Boolean {
return oldItem == newItem
}
override fun areItemsTheSame(
oldItem: HanimeDownloadEntity,
newItem: HanimeDownloadEntity,
): Boolean {
return oldItem.id == newItem.id
}
override fun getChangePayload(
oldItem: HanimeDownloadEntity,
newItem: HanimeDownloadEntity,
): Any {
logFieldsChange(TAG, oldItem, newItem)
var bitset = 0
if (oldItem.downloadedLength != newItem.downloadedLength)
bitset = bitset or DOWNLOADING
if (oldItem.state != newItem.state)
bitset = bitset or STATE
if (oldItem.progress != newItem.progress)
bitset = bitset or PROGRESS
return bitset
}
}
}
@SuppressLint("SetTextI18n")
override fun onBindViewHolder(
holder: DataBindingHolder<ItemHanimeDownloadingBinding>,
position: Int,
item: HanimeDownloadEntity?,
) {
item ?: return
holder.binding.tvTitle.text = item.title
holder.binding.ivCover.loadUnhappily(item.coverUri, item.coverUrl)
Log.d(TAG, "调用 load")
holder.itemView.post {
holder.binding.vCoverBg.updateLayoutParams {
height = holder.itemView.height
}
holder.binding.ivCoverBg.updateLayoutParams {
height = holder.itemView.height
}
}
holder.binding.clProgress.post {
holder.binding.vProgress.updateLayoutParams {
width = holder.binding.clProgress.width * item.progress / 100
}
}
holder.binding.ivCoverBg.apply {
loadUnhappily(item.coverUri, item.coverUrl)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val renderEffect = RenderEffect.createBlurEffect(
8.dpF, 8.dpF,
Shader.TileMode.CLAMP
)
setRenderEffect(renderEffect)
}
}
holder.binding.tvDownloadedSize.text = item.downloadedLength.formatFileSizeV2()
holder.binding.tvSize.text = item.length.formatFileSizeV2()
holder.binding.tvQuality.text = item.quality
// holder.binding.tvProgress.text = "${item.progress}%"
// holder.binding.pbProgress.setProgress(item.progress, true)
holder.binding.btnStart.handleStartButton(item)
}
@SuppressLint("SetTextI18n")
override fun onBindViewHolder(
holder: DataBindingHolder<ItemHanimeDownloadingBinding>,
position: Int,
item: HanimeDownloadEntity?,
payloads: List<Any>,
) {
if (payloads.isEmpty() || payloads.first() == 0)
return super.onBindViewHolder(holder, position, item, payloads)
item ?: return
val bitset = payloads.first() as Int
if (bitset and DOWNLOADING != 0) {
holder.binding.btnStart.handleStartButton(item)
holder.binding.tvDownloadedSize.text = item.downloadedLength.formatFileSizeV2()
}
if (bitset and STATE != 0) {
holder.binding.btnStart.handleStartButton(item)
}
if (bitset and PROGRESS != 0) {
// holder.binding.tvProgress.text = "${item.progress}%"
// holder.binding.pbProgress.setProgress(item.progress, true)
// 根据百分比,设置 vProgress 的宽度,比如 50% 就设置成 itemView 50% 的宽度
val weakViewProgress = WeakReference(holder.binding.vProgress)
ValueAnimator.ofInt(
holder.binding.vProgress.width,
holder.itemView.width * item.progress / 100
).apply {
// must be less than HanimeDownloadWorker.RESPONSE_INTERVAL
duration = 300L.coerceAtMost(HanimeDownloadWorker.RESPONSE_INTERVAL)
interpolator = HanimeDownloadingRvAdapter.interpolator
addUpdateListener(fragment) {
weakViewProgress.get()?.updateLayoutParams {
width = it.animatedValue as Int
}
}
}.start()
}
}
override fun onCreateViewHolder(
context: Context,
parent: ViewGroup,
viewType: Int,
): DataBindingHolder<ItemHanimeDownloadingBinding> {
return DataBindingHolder(
ItemHanimeDownloadingBinding.inflate(
LayoutInflater.from(context), parent, false
)
).also { viewHolder ->
viewHolder.binding.btnStart.setOnClickListener {
val pos = viewHolder.bindingAdapterPosition
val item = getItem(pos) ?: return@setOnClickListener
// val isDownloading: Boolean
if (item.isDownloading) {
// isDownloading = false
// cancelUniqueWorkAndPause(item.copy(isDownloading = false))
HanimeDownloadManagerV2.stopTask(item)
} else {
// isDownloading = true
// continueWork(item.copy(isDownloading = true))
HanimeDownloadManagerV2.resumeTask(item)
}
viewHolder.binding.btnStart.handleStartButton(item, rotate = true)
}
viewHolder.binding.btnCancel.setOnClickListener {
val pos = viewHolder.bindingAdapterPosition
val item = getItem(pos) ?: return@setOnClickListener
context.showAlertDialog {
setTitle(R.string.sure_to_delete)
setMessage(
context.getString(R.string.prepare_to_delete_s, item.title)
)
setPositiveButton(R.string.confirm) { _, _ ->
// cancelUniqueWorkAndDelete(item)
HanimeDownloadManagerV2.deleteTask(item)
}
setNegativeButton(R.string.cancel, null)
}
}
}
}
@SuppressLint("SetTextI18n")
private fun MaterialButton.handleStartButton(
item: HanimeDownloadEntity,
rotate: Boolean = false
) {
val state = if (rotate) {
when (item.state) {
DownloadState.Unknown,
DownloadState.Queued,
DownloadState.Paused -> DownloadState.Downloading
DownloadState.Downloading -> DownloadState.Paused
DownloadState.Finished,
DownloadState.Failed -> DownloadState.Unknown
}
} else {
item.state
}
when (state) {
DownloadState.Queued -> {
setText(R.string.already_in_queue)
setIconResource(R.drawable.ic_baseline_play_arrow_24)
}
DownloadState.Downloading -> {
// setText(R.string.pause)
// setIconResource(R.drawable.ic_baseline_pause_24)
text = "${item.progress}%"
setIconResource(R.drawable.ic_baseline_pause_24)
}
DownloadState.Paused -> {
setText(R.string.continues)
setIconResource(R.drawable.ic_baseline_play_arrow_24)
}
DownloadState.Failed -> {
setText(R.string.retry)
setIconResource(R.drawable.baseline_error_outline_24)
}
DownloadState.Finished, DownloadState.Unknown -> {} // do nothing
}
}
}