Skip to content

Commit

Permalink
5.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
angcyo committed Oct 27, 2022
1 parent ac0db8d commit 1b93077
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ abstract class BaseDslStateItem : DslAdapterItem() {
/**是否将激活状态item*/
open var itemStateEnable: Boolean = true

/**失败状态是否支持重试*/
var itemEnableRetry: Boolean = true

/**当状态改变时回调*/
var onItemStateChange: (from: Int, to: Int) -> Unit = { _, _ -> }

Expand Down Expand Up @@ -100,4 +103,10 @@ abstract class BaseDslStateItem : DslAdapterItem() {

/**是否处于状态显示模式*/
open fun isInStateLayout() = itemEnable && itemStateEnable && itemState > 0

override fun updateItemOnHaveDepend(updateSelf: Boolean, filterParams: FilterParams) {
//super.updateItemOnHaveDepend(updateSelf, filterParams)
//直接使用diff更新
updateItemDepend(filterParams)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.angcyo.dsladapter

import android.graphics.Canvas
import android.graphics.Color
import android.text.TextUtils
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
Expand Down Expand Up @@ -62,8 +63,8 @@ class DragCallbackHelper : ItemTouchHelper.Callback() {
}

/**卸载*/
fun uninstall(dragCallbackHelper: DragCallbackHelper) {
dragCallbackHelper.detachFromRecyclerView()
fun uninstall(dragCallbackHelper: DragCallbackHelper?) {
dragCallbackHelper?.detachFromRecyclerView()
}
}

Expand Down Expand Up @@ -270,7 +271,10 @@ class DragCallbackHelper : ItemTouchHelper.Callback() {
}
}

var _drawText = DrawText()
var _drawText = DrawText().apply {
textPaint.color = Color.RED
textPaint.textSize = 14 * dp
}

override fun onChildDraw(
canvas: Canvas,
Expand All @@ -293,11 +297,11 @@ class DragCallbackHelper : ItemTouchHelper.Callback() {
itemView.left.toFloat()
} else {
//向左滑动删除
(itemView.right - _drawText._paint.measureText(swipeTipText.toString()))
(itemView.right - _drawText.textPaint.measureText(swipeTipText.toString()))
}

val y: Float =
itemView.top + itemView.measuredHeight / 2 - _drawText._paint.textHeight() / 2
itemView.top + itemView.measuredHeight / 2 - _drawText.textPaint.textHeight() / 2

canvas.save()
canvas.translate(x, y)
Expand Down
2 changes: 2 additions & 0 deletions Adapter/src/main/java/com/angcyo/dsladapter/DslAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ open class DslAdapter(dataItems: List<DslAdapterItem>? = null) :
if (dslAdapterStatusItem.itemState == status) {
return
}
dslAdapterStatusItem.itemDslAdapter = this
dslAdapterStatusItem.itemState = status
dslAdapterStatusItem.itemChanging = true
}
Expand Down Expand Up @@ -391,6 +392,7 @@ open class DslAdapter(dataItems: List<DslAdapterItem>? = null) :
if (dslLoadMoreItem.itemStateEnable && dslLoadMoreItem.itemState == status) {
return
}
dslLoadMoreItem.itemDslAdapter = this
dslLoadMoreItem.itemState = status
if (notify) {
notifyItemChanged(dslLoadMoreItem, payload)
Expand Down
3 changes: 2 additions & 1 deletion Adapter/src/main/java/com/angcyo/dsladapter/DslAdapterEx.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.os.Handler
import android.os.Looper
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import com.angcyo.dsladapter.annotation.UpdateByDiff
import com.angcyo.dsladapter.annotation.UpdateByNotify
Expand Down Expand Up @@ -304,7 +305,7 @@ fun DslAdapter.renderEmptyItem(
adapterItem.itemLayoutId = R.layout.base_empty_item
adapterItem.itemBindOverride = { itemHolder, _, _, _ ->
itemHolder.itemView.setBgDrawable(background)
itemHolder.itemView.setWidthHeight(-1, height)
itemHolder.itemView.setWidthHeight(ViewGroup.LayoutParams.MATCH_PARENT, height)
}
adapterItem.action()
addLastItem(list, adapterItem)
Expand Down
7 changes: 7 additions & 0 deletions Adapter/src/main/java/com/angcyo/dsladapter/DslAdapterItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ open class DslAdapterItem : LifecycleOwner {
/**唯一标识此item的值*/
var itemTag: String? = null

/**自定义的标识*/
var itemFlag: Int = 0

/**异常标识, 自定义数据*/
var itemThrowable: Throwable? = null

Expand Down Expand Up @@ -1155,6 +1158,8 @@ open class DslAdapterItem : LifecycleOwner {
//<editor-fold desc="定向更新">

/**标识此[Item]是否发生过改变, 可用于实现退出界面提示是否保存内容.*/
@UpdateByDiff
@UpdateByNotify
var itemChanged = false
set(value) {
field = value
Expand All @@ -1166,6 +1171,8 @@ open class DslAdapterItem : LifecycleOwner {

/**[Item]是否正在改变, 会影响[thisAreContentsTheSame]的判断, 并且会在[Diff]计算完之后, 重置为[false]
* [itemUpdateFlag]*/
@UpdateByDiff
@UpdateByNotify
var itemChanging = false
set(value) {
field = value
Expand Down
11 changes: 11 additions & 0 deletions Adapter/src/main/java/com/angcyo/dsladapter/DslAdapterItemEx.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.core.math.MathUtils.clamp
import androidx.lifecycle.Lifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.angcyo.dsladapter.annotation.UpdateByNotify
import com.angcyo.dsladapter.data.Page
import com.angcyo.dsladapter.data.UpdateDataConfig
import com.angcyo.dsladapter.data.updateData
Expand Down Expand Up @@ -527,6 +528,16 @@ fun DslAdapterItem.replaceIt(newItem: DslAdapterItem?, adapter: DslAdapter? = nu
return reslut
}

/**更新[DslAdapterItem]的选中状态, 并且刷新界面*/
@UpdateByNotify
fun DslAdapterItem.updateItemSelected(select: Boolean) {
if (itemIsSelected == select) {
return
}
itemIsSelected = select
updateAdapterItem()
}

//</editor-fold desc="操作扩展">

//<editor-fold desc="更新指定的Item">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ open class DslAdapterStatusItem : BaseDslStateItem() {
?: itemHolder.context.getString(R.string.adapter_error)
//出现错误后, 触击刷新
itemHolder.clickItem {
if (itemState == ADAPTER_STATUS_ERROR) {
if (itemEnableRetry && itemState == ADAPTER_STATUS_ERROR) {
_notifyRefresh(itemHolder)
itemDslAdapter?.updateAdapterStatus(ADAPTER_STATUS_LOADING)
}
Expand Down
10 changes: 6 additions & 4 deletions Adapter/src/main/java/com/angcyo/dsladapter/DslLoadMoreItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ open class DslLoadMoreItem : BaseDslStateItem() {
itemHolder.tv(R.id.base_text_view)?.text = itemErrorThrowable?.message
?: itemHolder.context.getString(R.string.adapter_error)
itemHolder.clickItem {
if (itemState == LOAD_MORE_ERROR || itemState == _LOAD_MORE_RETRY) {
//失败的情况下, 点击触发重新加载
_notifyLoadMore(itemHolder)
updateAdapterItem()
if (itemEnableRetry) {
if (itemState == LOAD_MORE_ERROR || itemState == _LOAD_MORE_RETRY) {
//失败的情况下, 点击触发重新加载
_notifyLoadMore(itemHolder)
updateAdapterItem()
}
}
}
} else {
Expand Down
76 changes: 53 additions & 23 deletions Adapter/src/main/java/com/angcyo/dsladapter/internal/DrawText.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.angcyo.dsladapter.internal

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.os.Build
import android.text.Layout
import android.text.StaticLayout
import android.text.TextPaint
import android.view.ViewGroup
import com.angcyo.dsladapter.dp
import java.lang.Float.max
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

/**
*
* 使用[StaticLayout]进行文本绘制, 支持[SpannableString] 支持换行
* Email:[email protected]
* @author angcyo
* @date 2019/11/06
Expand All @@ -30,30 +30,63 @@ class DrawText {
}
}

/**画笔*/
var textPaint: TextPaint by MakeLayoutProperty(TextPaint(Paint.ANTI_ALIAS_FLAG))

/**需要绘制的文本*/
var drawText: CharSequence? by MakeLayoutProperty(null)

/**文本大小*/
var textSize: Float by MakeLayoutProperty(14 * dp)

/**文本绘制的宽度*/
/**文本允许绘制的宽度*/
var textWidth: Int by MakeLayoutProperty(ViewGroup.LayoutParams.WRAP_CONTENT)

/**文本颜色*/
var textColor = Color.RED

/**相对行间距,相对字体大小,1.5f表示行间距为1.5倍的字体高度。*/
var spacingMult: Float by MakeLayoutProperty(1f)

/**在基础行距上添加多少*/
var spacingAdd: Float by MakeLayoutProperty(0f)
var includePad: Boolean by MakeLayoutProperty(false)

var alignment: Layout.Alignment? by MakeLayoutProperty(Layout.Alignment.ALIGN_NORMAL)

var _paint: TextPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
var _textLayout: Layout? = null
var _textLayout: StaticLayout? = null

//---

fun getWidth() = makeLayout().width

fun getHeight() = makeLayout().height

fun getBounds(rect: Rect): Rect {
val layout = makeLayout()
rect.set(0, 0, layout.width, layout.height)
return rect
}

//---

/**重新创建[StaticLayout]*/
fun makeLayout(): StaticLayout {
val width = if (textWidth >= 0) textWidth else Int.MAX_VALUE
val layout = _makeLayout(width)
_textLayout = layout

fun makeLayout(): Layout {
if (textWidth >= 0) {
//no op
} else {
//重新赋值宽度
var maxWidth = 0f
for (line in 0 until layout.lineCount) {
maxWidth = max(layout.getLineWidth(line), maxWidth)
}
//wrap_content 重新计算宽度
_textLayout = _makeLayout(maxWidth.toInt())
}

return _textLayout!!
}

/**[width]宽度决定了行数 需要>=0*/
fun _makeLayout(width: Int): StaticLayout {
//StaticLayout 只能用一次.

/**
Expand All @@ -69,35 +102,32 @@ class DrawText {
* TextUtils.TruncateAt ellipsize : 从什么位置开始省略
* int ellipsizedWidth : 超过多少开始省略
* */
_paint.textSize = textSize
val text = drawText ?: ""
val width = if (textWidth >= 0) textWidth else _paint.measureText(text.toString()).toInt()

_textLayout = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val layout = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
StaticLayout.Builder.obtain(
text, 0, text.length, _paint,
text, 0, text.length, textPaint,
width
).run {
alignment?.run {
setAlignment(this)
}
setLineSpacing(spacingAdd, spacingMult)
setIncludePad(includePad)
//setMaxLines()
//setEllipsize()
build()
}
} else {
StaticLayout(text, _paint, width, alignment, spacingMult, spacingAdd, includePad)
StaticLayout(text, textPaint, width, alignment, spacingMult, spacingAdd, includePad)
}

return _textLayout!!
return layout
}

/**开始绘制*/
fun onDraw(canvas: Canvas) {
if (drawText.isNullOrEmpty()) {
return
}
_paint.color = textColor

/*
* Layout在绘制的时候, (0, 0) 坐标是文本左上角
* Canvas.drawText, (0, 0) 坐标是文本Baseline的位置
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 2022-10-27

`5.2.0`

- 修复一些已知问题

# 2022-9-9

`5.1.3`
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ allprojects {
```kotlin
dependencies {
//androidx(推荐)
implementation 'com.github.angcyo:DslAdapter:5.1.3'
implementation 'com.github.angcyo:DslAdapter:5.2.0'
//支持DataBinding
implementation 'com.github.angcyo:DslAdapter:3.0.0-binding'

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ org.gradle.jvmargs=-Xmx1536m
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
android.useAndroidX=true
android.enableJetifier=true
android.enableJetifier=false

M_SDK=12
T_SDK=32
Expand Down

0 comments on commit 1b93077

Please sign in to comment.