From 955643f7c42603153ff69b4e4bb6a5ed7c9de96b Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 20 Oct 2019 02:15:24 -0700 Subject: Use view binding for sample checkbox and fix pref header --- core/src/main/res/values/constraintLayout_attr.xml | 10 ++++ .../fastadapter/viewbinding/BindingItem.kt | 5 +- .../src/main/res/layout/kau_pref_header.xml | 4 +- sample/build.gradle | 1 + .../kotlin/ca/allanwang/kau/sample/AnimActivity.kt | 14 ++--- .../ca/allanwang/kau/sample/PermissionCheckbox.kt | 67 ++++++++++++++++++++++ .../ca/allanwang/kau/sample/SwipeActivity.kt | 55 +++++++++++------- sample/src/main/res/values/strings_sample.xml | 2 +- 8 files changed, 122 insertions(+), 36 deletions(-) create mode 100644 core/src/main/res/values/constraintLayout_attr.xml diff --git a/core/src/main/res/values/constraintLayout_attr.xml b/core/src/main/res/values/constraintLayout_attr.xml new file mode 100644 index 0000000..e3624e4 --- /dev/null +++ b/core/src/main/res/values/constraintLayout_attr.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/fastadapter-viewbinding/src/main/kotlin/ca/allanwang/fastadapter/viewbinding/BindingItem.kt b/fastadapter-viewbinding/src/main/kotlin/ca/allanwang/fastadapter/viewbinding/BindingItem.kt index d40be3d..a475615 100644 --- a/fastadapter-viewbinding/src/main/kotlin/ca/allanwang/fastadapter/viewbinding/BindingItem.kt +++ b/fastadapter-viewbinding/src/main/kotlin/ca/allanwang/fastadapter/viewbinding/BindingItem.kt @@ -16,6 +16,7 @@ package ca.allanwang.fastadapter.viewbinding import android.content.Context +import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView @@ -45,10 +46,10 @@ abstract class BindingItem(open val data: Any?) : override val type: Int get() = layoutRes - abstract fun createBinding(context: Context, parent: ViewGroup?): Binding + abstract fun createBinding(layoutInflater: LayoutInflater, parent: ViewGroup?): Binding override fun createView(ctx: Context, parent: ViewGroup?): View { - val binding = createBinding(ctx, parent) + val binding = createBinding(LayoutInflater.from(ctx), parent) setBinding(binding.root, binding) return binding.root } diff --git a/kpref-activity/src/main/res/layout/kau_pref_header.xml b/kpref-activity/src/main/res/layout/kau_pref_header.xml index 65504a3..27ef74c 100644 --- a/kpref-activity/src/main/res/layout/kau_pref_header.xml +++ b/kpref-activity/src/main/res/layout/kau_pref_header.xml @@ -3,8 +3,10 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="8dip" - android:paddingEnd="?android:attr/listPreferredItemPaddingRight" + android:clickable="false" + android:focusable="false" android:paddingStart="?android:attr/listPreferredItemPaddingLeft" android:paddingTop="16dip" + android:paddingEnd="?android:attr/listPreferredItemPaddingRight" android:textColor="?colorAccent" android:textSize="14sp" /> \ No newline at end of file diff --git a/sample/build.gradle b/sample/build.gradle index b710998..47baed2 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -124,6 +124,7 @@ android { dependencies { implementation project(':about') implementation project(':fastadapter') + implementation project(':fastadapter-viewbinding') implementation project(':colorpicker') implementation project(':core') implementation project(':core-ui') diff --git a/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt b/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt index a5ef8c3..50b3cf3 100644 --- a/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt +++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt @@ -16,6 +16,7 @@ package ca.allanwang.kau.sample import android.os.Bundle +import ca.allanwang.kau.adapters.SingleFastAdapter import ca.allanwang.kau.internal.KauBaseActivity import ca.allanwang.kau.logging.KL import ca.allanwang.kau.permissions.PERMISSION_ACCESS_COARSE_LOCATION @@ -42,7 +43,7 @@ class AnimActivity : KauBaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - val adapter = FastItemAdapter() + val adapter = SingleFastAdapter() setContentView(fullLinearRecycler(adapter).apply { setBackgroundColor( KPrefSample.bgColor.withAlpha(255) @@ -53,15 +54,8 @@ class AnimActivity : KauBaseActivity() { PERMISSION_ACCESS_COARSE_LOCATION, PERMISSION_ACCESS_FINE_LOCATION, PERMISSION_CAMERA - ).map { PermissionCheckbox(it) }) - adapter.onClickListener = { _, _, item, _ -> - KL.d { "Perm Click" } - kauRequestPermissions(item.permission) { granted, _ -> - toast("${item.permission} enabled: $granted") - adapter.notifyAdapterDataSetChanged() - } - true - } + ).map { PermissionCheckboxModel(it).vh() }) + adapter.addEventHook(PermissionCheckboxViewBinding.clickHook()) kauSwipeOnCreate { edgeFlag = SWIPE_EDGE_LEFT } diff --git a/sample/src/main/kotlin/ca/allanwang/kau/sample/PermissionCheckbox.kt b/sample/src/main/kotlin/ca/allanwang/kau/sample/PermissionCheckbox.kt index 68dde2a..40ad663 100644 --- a/sample/src/main/kotlin/ca/allanwang/kau/sample/PermissionCheckbox.kt +++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/PermissionCheckbox.kt @@ -15,12 +15,26 @@ */ package ca.allanwang.kau.sample +import android.Manifest +import android.view.LayoutInflater import android.view.View +import android.view.ViewGroup import android.widget.CheckBox import android.widget.TextView import androidx.recyclerview.widget.RecyclerView +import ca.allanwang.fastadapter.viewbinding.BindingClickEventHook +import ca.allanwang.fastadapter.viewbinding.BindingItem +import ca.allanwang.fastadapter.viewbinding.BindingLayout +import ca.allanwang.fastadapter.viewbinding.VhModel import ca.allanwang.kau.iitems.KauIItem +import ca.allanwang.kau.logging.KL +import ca.allanwang.kau.permissions.kauRequestPermissions +import ca.allanwang.kau.sample.databinding.PermissionCheckboxBinding import ca.allanwang.kau.utils.hasPermission +import ca.allanwang.kau.utils.toast +import com.mikepenz.fastadapter.FastAdapter +import com.mikepenz.fastadapter.GenericItem +import com.mikepenz.fastadapter.listeners.EventHook /** * Created by Allan Wang on 2017-07-03. @@ -41,3 +55,56 @@ class PermissionCheckbox(val permission: String) : KauIItem(data), + BindingLayout by Companion { + + override fun createBinding( + layoutInflater: LayoutInflater, + parent: ViewGroup? + ): PermissionCheckboxBinding = + PermissionCheckboxBinding.inflate(layoutInflater, parent, false) + + override fun PermissionCheckboxBinding.bindView( + holder: ViewHolder, + payloads: MutableList + ) { + permText.text = data.permission + permCheckbox.apply { + isChecked = holder.itemView.context.hasPermission(data.permission) + isFocusable = false + isClickable = false + jumpDrawablesToCurrentState() // Cancel the animation + } + } + + companion object : BindingLayout { + override val layoutRes: Int + get() = R.layout.permission_checkbox + + fun clickHook(): EventHook = object : BindingClickEventHook(this) { + override fun PermissionCheckboxBinding.onBind(viewHolder: RecyclerView.ViewHolder): View? = root + + override fun PermissionCheckboxBinding.onClick( + v: View, + position: Int, + fastAdapter: FastAdapter, + item: PermissionCheckboxViewBinding + ) { + KL.d { "Perm Click" } + with (v.context) { + kauRequestPermissions(item.data.permission) { granted, _ -> + toast("${item.data.permission} enabled: $granted") + fastAdapter.notifyAdapterDataSetChanged() + } + } + } + } + } +} \ No newline at end of file diff --git a/sample/src/main/kotlin/ca/allanwang/kau/sample/SwipeActivity.kt b/sample/src/main/kotlin/ca/allanwang/kau/sample/SwipeActivity.kt index 5107e18..3976ae3 100644 --- a/sample/src/main/kotlin/ca/allanwang/kau/sample/SwipeActivity.kt +++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/SwipeActivity.kt @@ -18,6 +18,7 @@ package ca.allanwang.kau.sample import android.app.Activity import android.os.Bundle import ca.allanwang.kau.internal.KauBaseActivity +import ca.allanwang.kau.sample.databinding.ActivitySwipeBinding import ca.allanwang.kau.swipe.SWIPE_EDGE_BOTTOM import ca.allanwang.kau.swipe.SWIPE_EDGE_LEFT import ca.allanwang.kau.swipe.SWIPE_EDGE_RIGHT @@ -30,7 +31,6 @@ import ca.allanwang.kau.utils.navigationBarColor import ca.allanwang.kau.utils.rndColor import ca.allanwang.kau.utils.startActivity import ca.allanwang.kau.utils.statusBarColor -import kotlinx.android.synthetic.main.activity_swipe.* /** * Created by Allan Wang on 2017-08-05. @@ -45,29 +45,40 @@ fun Activity.startActivityWithEdge(flag: Int) { class SwipeActivity : KauBaseActivity() { + private lateinit var binding: ActivitySwipeBinding + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_swipe) - listOf(swipe_from_left, swipe_from_right, swipe_from_top, swipe_from_bottom) - .zip(listOf(SWIPE_EDGE_LEFT, SWIPE_EDGE_RIGHT, SWIPE_EDGE_TOP, SWIPE_EDGE_BOTTOM)) - .forEach { (button, edge) -> button.setOnClickListener { startActivityWithEdge(edge) } } - val flag = intent.getIntExtra(SWIPE_EDGE, -1) - swipe_toolbar.title = when (flag) { - SWIPE_EDGE_LEFT -> "Left Edge Swipe" - SWIPE_EDGE_RIGHT -> "Right Edge Swipe" - SWIPE_EDGE_TOP -> "Top Edge Swipe" - SWIPE_EDGE_BOTTOM -> "Bottom Edge Swipe" - else -> "Invalid Edge Swipe" - } - setSupportActionBar(swipe_toolbar) - val headerColor = rndColor.darken(0.6f) - swipe_toolbar.setBackgroundColor(headerColor) - statusBarColor = headerColor - val bg = headerColor.darken(0.2f) - swipe_container.setBackgroundColor(bg) - navigationBarColor = bg - kauSwipeOnCreate { - edgeFlag = flag + binding = ActivitySwipeBinding.inflate(layoutInflater) + setContentView(binding.root) + + binding.apply { + mapOf( + swipeFromLeft to SWIPE_EDGE_LEFT, + swipeFromRight to SWIPE_EDGE_RIGHT, + swipeFromTop to SWIPE_EDGE_TOP, + swipeFromBottom to SWIPE_EDGE_BOTTOM + ).forEach { (button, edge) -> + button.setOnClickListener { startActivityWithEdge(edge) } + } + val flag = intent.getIntExtra(SWIPE_EDGE, -1) + swipeToolbar.title = when (flag) { + SWIPE_EDGE_LEFT -> "Left Edge Swipe" + SWIPE_EDGE_RIGHT -> "Right Edge Swipe" + SWIPE_EDGE_TOP -> "Top Edge Swipe" + SWIPE_EDGE_BOTTOM -> "Bottom Edge Swipe" + else -> "Invalid Edge Swipe" + } + setSupportActionBar(swipeToolbar) + val headerColor = rndColor.darken(0.6f) + swipeToolbar.setBackgroundColor(headerColor) + statusBarColor = headerColor + val bg = headerColor.darken(0.2f) + swipeContainer.setBackgroundColor(bg) + navigationBarColor = bg + kauSwipeOnCreate { + edgeFlag = flag + } } } diff --git a/sample/src/main/res/values/strings_sample.xml b/sample/src/main/res/values/strings_sample.xml index 46a8140..cd2b3d1 100644 --- a/sample/src/main/res/values/strings_sample.xml +++ b/sample/src/main/res/values/strings_sample.xml @@ -1,4 +1,4 @@ - + This is a header This is a description Checkbox 1 -- cgit v1.2.3