Skip to content

Commit

Permalink
Fix generated View extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyhuo committed Aug 25, 2023
1 parent 3228b6d commit 6726974
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,13 @@ abstract class KaceGenerateAction : WorkAction<KaceGenerateAction.Parameters> {
writer.appendLine("package ${item.targetFilePackageName}.view")
writer.newLine()
writer.appendLine("import android.view.View")
writer.appendLine("import com.kanyun.kace.KaceViewUtils")
writer.appendLine("import $namespace.R")
writer.newLine()

layoutNodeItems.forEach { item ->
writer.appendLine("internal inline val View.${item.viewId}")
writer.appendLine(" get() = findViewById<${item.viewNameWithPackage}>(R.id.${item.viewId})")
writer.appendLine(" get() = KaceViewUtils.findViewById(this, R.id.${item.viewId}, ${item.viewNameWithPackage}::class.java)")
writer.newLine()
}
}
Expand Down
14 changes: 14 additions & 0 deletions kace-runtime/src/main/java/com/kanyun/kace/KaceViewUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.kanyun.kace;

import android.view.View;

/**
* Created by benny at 2023/8/17 15:28.
*/
public class KaceViewUtils {

public static <T extends View> T findViewById(View view, int id, Class<T> viewClass) {
return view.findViewById(id);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.kanyun.kace.sample.demo

import android.content.Context
import android.util.AttributeSet
import android.view.View

/**
* Created by benny at 2023/8/17 15:37.
*/
class GenericView<T>
@JvmOverloads constructor(
context: Context?, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr)
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.kanyun.kace.sample.demo

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.widget.Toast
Expand All @@ -40,6 +41,8 @@ class MainActivity : AppCompatActivity(){
}

button1.text2?.text = "test text"

genericView.setBackgroundColor(Color.YELLOW)
}

override fun onDestroy() {
Expand Down
6 changes: 6 additions & 0 deletions kace-sample/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@
android:layout_width="match_parent"
android:layout_height="100dp" />

<com.kanyun.kace.sample.demo.GenericView
android:id="@+id/genericView"
android:layout_width="match_parent"
android:background="@android:color/holo_blue_bright"
android:layout_height="30dp"/>

</LinearLayout>

0 comments on commit 6726974

Please sign in to comment.