diff options
author | Allan Wang <me@allanwang.ca> | 2019-12-07 18:19:51 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-07 18:19:51 -0800 |
commit | 0db38bce21d883dfb94f51b9dc2ab440a9782c52 (patch) | |
tree | b6dcbd597d387b07c0ec10eea6d9332580f7c6b7 /sample/src | |
parent | 09c6e4d1defb46616c57accbf505d0c237fea3d6 (diff) | |
parent | cbdb7b55c894da4655644c4d3db2def2b89cd989 (diff) | |
download | kau-0db38bce21d883dfb94f51b9dc2ab440a9782c52.tar.gz kau-0db38bce21d883dfb94f51b9dc2ab440a9782c52.tar.bz2 kau-0db38bce21d883dfb94f51b9dc2ab440a9782c52.zip |
Merge pull request #235 from AllanWang/viewbinding
ViewBinding
Diffstat (limited to 'sample/src')
5 files changed, 106 insertions, 34 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> diff --git a/sample/src/main/res/xml/kau_changelog.xml b/sample/src/main/res/xml/kau_changelog.xml index 5b4ff9c..d8c167f 100644 --- a/sample/src/main/res/xml/kau_changelog.xml +++ b/sample/src/main/res/xml/kau_changelog.xml @@ -13,7 +13,7 @@ <item text=":core: Remove statusBarLight toggle" /> <item text=":core: Remove kau_status_bar_height; height should be found programmatically" /> <item text=":fastadapter: Migrate fastadapter to v4.x.x" /> - <item text="" /> + <item text=":fastadapter-viewbinding: Create helper items for ViewBinding" /> <item text="" /> <version title="v5.1.0" /> |