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 --- .../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 +- 4 files changed, 105 insertions(+), 33 deletions(-) (limited to 'sample/src') 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