Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ jobs:

build:
name: Build
strategy:
matrix:
os: [ ubuntu-latest, windows-latest, macos-latest ]
runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
Expand Down
12 changes: 2 additions & 10 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id("com.android.application")
id("kotlin-android")
id("kotlin-parcelize")
}

android {
Expand Down
36 changes: 7 additions & 29 deletions app/src/main/java/com/kronos/sample/entity/TestEntity.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.kronos.sample.entity

import android.os.Parcel
import android.os.Parcelable

import com.kronos.diffutil.IDifference
import com.kronos.diffutil.IEqualsAdapter

import kotlinx.parcelize.Parcelize
import java.util.Random

data class TestEntity(var id: Int = 0,
var displayTime: Long = 0,
var text: String? = Random().nextInt(10000).toString()) : Parcelable, IDifference, IEqualsAdapter {
@Parcelize
data class TestEntity(
var id: Int = 0,
var displayTime: Long = 0,
var text: String? = Random().nextInt(10000).toString()
) : Parcelable, IDifference, IEqualsAdapter {

override val uniqueId: String
get() = id.toString()
Expand All @@ -19,27 +20,4 @@ data class TestEntity(var id: Int = 0,
displayTime = System.currentTimeMillis()
text = "更新数据"
}


constructor(source: Parcel) : this(
source.readInt(),
source.readLong(),
source.readString()
)

override fun describeContents() = 0

override fun writeToParcel(dest: Parcel, flags: Int) = with(dest) {
writeInt(id)
writeLong(displayTime)
writeString(text)
}

companion object {
@JvmField
val CREATOR: Parcelable.Creator<TestEntity> = object : Parcelable.Creator<TestEntity> {
override fun createFromParcel(source: Parcel): TestEntity = TestEntity(source)
override fun newArray(size: Int): Array<TestEntity?> = arrayOfNulls(size)
}
}
}
31 changes: 16 additions & 15 deletions diflib/src/main/java/com/kronos/diffutil/ParcelDiffHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@ import java.util.concurrent.CopyOnWriteArrayList
class ParcelDiffHelper<T : Parcelable> : BaseDiffHelper<T>() {

override fun clone() {
try {
itemsCursor?.apply {
snapshot = CopyOnWriteArrayList()
for (entity in this) {
val parcel = Parcel.obtain()
(entity as Parcelable).writeToParcel(parcel, 0)
parcel.setDataPosition(0)
val constructor = entity.javaClass.getDeclaredConstructor(Parcel::class.java)
constructor.isAccessible = true
val dateEntity = constructor.newInstance(parcel) as T
snapshot?.add(dateEntity)
parcel.recycle()
}
itemsCursor?.run {
snapshot = CopyOnWriteArrayList()
forEach {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用了 deepCopy 的扩展之后没必要异常处理了

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

之前是担心出现沙雕异常导致的

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那就先去掉喽?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

恩 问题不大 我觉得

snapshot?.add(it.deepCopy() ?: it)
}
} catch (e: Exception) {
e.printStackTrace()
}
}

private fun <T : Parcelable> T.deepCopy(): T? {
var parcel: Parcel? = null
return try {
parcel = Parcel.obtain().also {
it.writeParcelable(this, 0)
it.setDataPosition(0)
}
parcel.readParcelable(this::class.java.classLoader)
} finally {
parcel?.recycle()
}
}
Comment on lines +19 to +30
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这种深拷贝实现更具有普适性吧,entity.javaClass.getDeclaredConstructor(Parcel::class.java) 那里会在用了 kotlin-parcelize 插件之后抛异常的,因为没有那个构造器

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

估计当时写的时候是没考虑kotlin-parcelize 我觉得可以啊 这么改

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我昨晚照抄代码发现的抛异常问题来着

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实我后面想用霍老的深拷贝框架的 但是太懒了 就没动手

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实一般场景下用 data class 的 copy 就好了,完全深拷贝不是很有必要

}