Skip to content

Commit

Permalink
Remove useless class parameter. modify parseParams's return type to lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
twiceyuan committed Nov 29, 2020
1 parent a981fae commit 58f8fb7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 30 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,26 @@

```kotlin
class ReceiverActivity : AppCompatActivity() {
// 定义参数类型

// 定义一个 data bean
data class Params(
val name: String,
val phone: String,
val emails: ArrayList<String>?,
val parcelableBean: ParcelableBean,
val nestedBean: Father,
val age: Int
) : SafetyActivityParams(ReceiverActivity::class.java)
) : ActivityParams<ReceiverActivity>() // 通过泛型和 host 类绑定

// 解析
private val args by lazy { parseParams<Params>() }
// 解析接收到的类型
private val args by parseParams<Params>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_receiver)
val binding = ActivityReceiverBinding.inflate(layoutInflater)
setContentView(binding.root)

// 直接取值
tv_content.text = args.toJsonTree()
binding.tvContent.text = args.toJsonTree() // 直接取值进行使用
}
}
```
Expand Down Expand Up @@ -85,6 +86,6 @@ dependencies {
-keep public class kotlin.reflect.jvm.internal.impl.builtins.* { public *; }
```

## TODO
## Java 兼容性

Java 下反射会丢失构造器参数的名称,暂时没有找到好的方案来在 Java 下使用这个工具
Java 下反射会丢失构造器参数的名称,暂无兼容 Java 计划
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,47 @@ import kotlin.reflect.jvm.javaType
/**
* Created by twiceYuan on 2018/3/26.
*
* Pass the args from intent
* 在 Activity、Fragment 之间类型安全地传递参数
*/

sealed class SafetyParams

private fun <TargetClass : Class<*>> SafetyParams.genericTypeParam(): TargetClass {
@Suppress("UNCHECKED_CAST")
return (this::class.java.genericSuperclass as ParameterizedType)
.actualTypeArguments[0]
as TargetClass
}

/**
* 定义传递参数数据 Model 所要继承的类。例如:
* ```
* data class Starter(val name: String) : SafetyParams(SomeActivity::class.java)
* ```
*/
abstract class SafetyActivityParams(
@Transient private val targetClass: Class<out Activity>
) : SafetyParams() {
abstract class ActivityParams<ActivityType : Activity> : SafetyParams() {

private val targetClass: Class<ActivityType> by lazy { genericTypeParam() }

fun launch(context: Context) {
context.startActivityWithArgs(targetClass, this)
}

@Suppress("unused")
fun intent(context: Context) = Intent(context, targetClass).apply {
putExtras(this@SafetyActivityParams.toBundle())
putExtras(this@ActivityParams.toBundle())
}
}

abstract class SafetyFragmentParams<fragment : Fragment>(
@Transient private val targetClass: Class<fragment>
) : SafetyParams() {
fun newInstance(): fragment = targetClass.newInstance().apply {
arguments = this@SafetyFragmentParams.toBundle()
abstract class FragmentParams<FragmentType : Fragment> : SafetyParams() {

private val targetClass: Class<FragmentType> by lazy { genericTypeParam() }

@Suppress("UNCHECKED_CAST")
fun newInstance(): FragmentType {
return targetClass.newInstance().apply {
arguments = this@FragmentParams.toBundle()
}
}
}

Expand All @@ -68,6 +79,7 @@ private fun Context.startActivityWithArgs(targetClass: Class<out Activity>, args
private fun SafetyParams.toBundle(): Bundle {

val bundle = Bundle()

// 临时读取一个 field
@Suppress("UNCHECKED_CAST")
fun <T> Field.read(obj: Any): T? {
Expand Down Expand Up @@ -193,12 +205,16 @@ private fun SafetyParams.toBundle(): Bundle {

class DataBeanNotLegalException(msg: String) : RuntimeException(msg)

inline fun <reified Data : SafetyParams> Activity.parseParams(): Data {
return intent?.extras?.toArgs() ?: throw java.lang.RuntimeException("No arguments passed.")
inline fun <reified Data : SafetyParams> Activity.parseParams(): Lazy<Data> {
return lazy {
intent?.extras?.toArgs() ?: throw java.lang.RuntimeException("No arguments passed.")
}
}

inline fun <reified Data : SafetyParams> Fragment.parseParams(): Data {
return arguments?.toArgs() ?: throw java.lang.RuntimeException("No arguments passed.")
inline fun <reified Data : SafetyParams> Fragment.parseParams(): Lazy<Data> {
return lazy {
arguments?.toArgs() ?: throw java.lang.RuntimeException("No arguments passed.")
}
}

inline fun <reified Data : SafetyParams> Bundle.toArgs(): Data {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package com.twiceyuan.safetyparams.sample

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.twiceyuan.safetyparams.R
import com.twiceyuan.safetyparams.databinding.ActivityReceiverBinding
import com.twiceyuan.safetyparams.library.SafetyActivityParams
import com.twiceyuan.safetyparams.library.ActivityParams
import com.twiceyuan.safetyparams.library.parseParams
import com.twiceyuan.safetyparams.sample.bean.Father
import com.twiceyuan.safetyparams.sample.bean.ParcelableBean
Expand All @@ -23,9 +22,9 @@ class ReceiverActivity : AppCompatActivity() {
val parcelableBean: ParcelableBean,
val nestedBean: Father,
val age: Int
) : SafetyActivityParams(ReceiverActivity::class.java)
) : ActivityParams<ReceiverActivity>()

private val args by lazy { parseParams<Params>() }
private val args by parseParams<Params>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.twiceyuan.safetyparams.R
import com.twiceyuan.safetyparams.databinding.ActivityReceiverBinding
import com.twiceyuan.safetyparams.library.SafetyFragmentParams
import com.twiceyuan.safetyparams.library.FragmentParams
import com.twiceyuan.safetyparams.library.parseParams
import com.twiceyuan.safetyparams.sample.bean.Father
import com.twiceyuan.safetyparams.sample.bean.ParcelableBean
Expand All @@ -21,9 +21,9 @@ class ReceiverFragment : DialogFragment() {
val nestedBean: Father,
val emails: ArrayList<String>?,
val parcelableBean: ParcelableBean
) : SafetyFragmentParams<ReceiverFragment>(ReceiverFragment::class.java)
) : FragmentParams<ReceiverFragment>()

private val params by lazy { parseParams<Params>() }
private val params by parseParams<Params>()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.activity_receiver, container, false)
Expand Down

0 comments on commit 58f8fb7

Please sign in to comment.