diff options
author | Allan Wang <me@allanwang.ca> | 2019-10-20 02:15:24 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2019-10-20 02:15:24 -0700 |
commit | 955643f7c42603153ff69b4e4bb6a5ed7c9de96b (patch) | |
tree | 296cfcfe95d3df1c8fa1d8ef022772d73cdf566f /sample/src | |
parent | 302d3351e9866b71b1e525c0e5ba883fe022ba5c (diff) | |
download | kau-955643f7c42603153ff69b4e4bb6a5ed7c9de96b.tar.gz kau-955643f7c42603153ff69b4e4bb6a5ed7c9de96b.tar.bz2 kau-955643f7c42603153ff69b4e4bb6a5ed7c9de96b.zip |
Use view binding for sample checkbox and fix pref header
Diffstat (limited to 'sample/src')
4 files changed, 105 insertions, 33 deletions
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<PermissionCheckbox>() + 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<PermissionCheckbox.V val checkbox: CheckBox = v.findViewById(R.id.perm_checkbox) } } + +data class PermissionCheckboxModel(val permission: String) : VhModel { + override fun vh(): GenericItem = PermissionCheckboxViewBinding(this) +} + +class PermissionCheckboxViewBinding( + override val data: PermissionCheckboxModel +) : BindingItem<PermissionCheckboxBinding>(data), + BindingLayout<PermissionCheckboxBinding> by Companion { + + override fun createBinding( + layoutInflater: LayoutInflater, + parent: ViewGroup? + ): PermissionCheckboxBinding = + PermissionCheckboxBinding.inflate(layoutInflater, parent, false) + + override fun PermissionCheckboxBinding.bindView( + holder: ViewHolder, + payloads: MutableList<Any> + ) { + permText.text = data.permission + permCheckbox.apply { + isChecked = holder.itemView.context.hasPermission(data.permission) + isFocusable = false + isClickable = false + jumpDrawablesToCurrentState() // Cancel the animation + } + } + + companion object : BindingLayout<PermissionCheckboxBinding> { + override val layoutRes: Int + get() = R.layout.permission_checkbox + + fun clickHook(): EventHook<PermissionCheckboxViewBinding> = object : BindingClickEventHook<PermissionCheckboxBinding, PermissionCheckboxViewBinding>(this) { + override fun PermissionCheckboxBinding.onBind(viewHolder: RecyclerView.ViewHolder): View? = root + + override fun PermissionCheckboxBinding.onClick( + v: View, + position: Int, + fastAdapter: FastAdapter<PermissionCheckboxViewBinding>, + 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 @@ -<resources> +<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="MissingTranslation"> <string name="header">This is a header</string> <string name="desc">This is a description</string> <string name="checkbox_1">Checkbox 1</string> |