Skip to content

Commit

Permalink
Update behavioural events API (#1923)
Browse files Browse the repository at this point in the history
* Try out new behavioural event API

* Remove sample & tests

* Add licensing header

* Create type alias for wrapper

* Add modifier
Maria Neumayer authored Apr 9, 2024
1 parent 77a546e commit 4edcdc3
Showing 8 changed files with 62 additions and 276 deletions.
Original file line number Diff line number Diff line change
@@ -18,7 +18,6 @@

package net.skyscanner.backpack.demo.compose

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
@@ -49,7 +48,6 @@ import net.skyscanner.backpack.compose.tokens.Airports
import net.skyscanner.backpack.compose.tokens.City
import net.skyscanner.backpack.compose.tokens.Landmark
import net.skyscanner.backpack.compose.tokens.UseLocation
import net.skyscanner.backpack.compose.utils.BehaviouralCallback
import net.skyscanner.backpack.demo.R
import net.skyscanner.backpack.demo.components.AppSearchModalComponent
import net.skyscanner.backpack.demo.meta.ComposeStory
@@ -119,15 +117,6 @@ internal fun DefaultAppSearchModalSample(
onClose = { showModal.value = false },
onInputChanged = { destination.value = it },
clearAction = BpkClearAction(stringResource(id = R.string.text_field_clear_action_description)) { destination.value = "" },
behaviouralCallback = object : BehaviouralCallback {
override fun onDrawn(element: Any) {
Log.i("AppSearchModalStory", "onDrawn $element")
}

override fun onClick(element: Any) {
Log.i("AppSearchModalStory", "onClick $element")
}
},
)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ import net.skyscanner.backpack.compose.modal.BpkModalState
import net.skyscanner.backpack.compose.modal.rememberBpkModalState
import net.skyscanner.backpack.compose.navigationbar.NavIcon
import net.skyscanner.backpack.compose.textfield.BpkClearAction
import net.skyscanner.backpack.compose.utils.BehaviouralCallback
import net.skyscanner.backpack.compose.utils.BpkBehaviouralEventWrapper

sealed class BpkAppSearchModalResult {
data class Content(
@@ -79,7 +79,7 @@ fun BpkAppSearchModal(
clearAction: BpkClearAction,
modifier: Modifier = Modifier,
state: BpkModalState = rememberBpkModalState(),
behaviouralCallback: BehaviouralCallback? = null,
behaviouralEventWrapper: BpkBehaviouralEventWrapper? = null,
) {
val coroutineScope = rememberCoroutineScope()
BpkModal(
@@ -101,7 +101,7 @@ fun BpkAppSearchModal(
results = results,
onInputChanged = onInputChanged,
clearAction = clearAction,
behaviouralCallback = behaviouralCallback,
behaviouralEventWrapper = behaviouralEventWrapper,
)
}
}
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ import net.skyscanner.backpack.compose.textfield.BpkClearAction
import net.skyscanner.backpack.compose.textfield.BpkTextField
import net.skyscanner.backpack.compose.tokens.BpkSpacing
import net.skyscanner.backpack.compose.tokens.Search
import net.skyscanner.backpack.compose.utils.BehaviouralCallback
import net.skyscanner.backpack.compose.utils.BpkBehaviouralEventWrapper

@Composable
internal fun BpkAppSearchModalImpl(
@@ -40,8 +40,8 @@ internal fun BpkAppSearchModalImpl(
results: BpkAppSearchModalResult,
onInputChanged: (String) -> Unit,
clearAction: BpkClearAction,
behaviouralCallback: BehaviouralCallback?,
modifier: Modifier = Modifier,
behaviouralEventWrapper: BpkBehaviouralEventWrapper? = null,
) {
when (results) {
is BpkAppSearchModalResult.Error -> {
@@ -63,7 +63,7 @@ internal fun BpkAppSearchModalImpl(
clearAction = clearAction,
)
if (results is BpkAppSearchModalResult.Content) {
BpkSearchModalContent(results = results, behaviouralCallback = behaviouralCallback)
BpkSearchModalContent(results = results, behaviouralEventWrapper = behaviouralEventWrapper)
} else if (results is BpkAppSearchModalResult.Loading) {
BpkSearchModalLoading(results)
}
Original file line number Diff line number Diff line change
@@ -24,13 +24,13 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import net.skyscanner.backpack.compose.appsearchmodal.BpkAppSearchModalResult
import net.skyscanner.backpack.compose.tokens.BpkSpacing
import net.skyscanner.backpack.compose.utils.BehaviouralCallback
import net.skyscanner.backpack.compose.utils.BpkBehaviouralEventWrapper

@Composable
internal fun BpkSearchModalContent(
results: BpkAppSearchModalResult.Content,
behaviouralCallback: BehaviouralCallback?,
modifier: Modifier = Modifier,
behaviouralEventWrapper: BpkBehaviouralEventWrapper? = null,
) {
LazyColumn(modifier = modifier) {
results.shortcuts?.let {
@@ -50,11 +50,20 @@ internal fun BpkSearchModalContent(
}
}
items(section.items) {
BpkSectionItem(
item = it,
modifier = Modifier.padding(BpkSpacing.Base),
behaviouralCallback = behaviouralCallback,
)
if (behaviouralEventWrapper != null) {
behaviouralEventWrapper(it, Modifier) {
BpkSectionItem(
item = it,
modifier = Modifier.padding(BpkSpacing.Base),
clickHandleScope = this,
)
}
} else {
BpkSectionItem(
item = it,
modifier = Modifier.padding(BpkSpacing.Base),
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -36,8 +36,8 @@ import net.skyscanner.backpack.compose.icon.BpkIconSize
import net.skyscanner.backpack.compose.text.BpkText
import net.skyscanner.backpack.compose.theme.BpkTheme
import net.skyscanner.backpack.compose.tokens.BpkSpacing
import net.skyscanner.backpack.compose.utils.BehaviouralCallback
import net.skyscanner.backpack.compose.utils.registerForBehaviouralEvents
import net.skyscanner.backpack.compose.utils.BpkClickHandleScope
import net.skyscanner.backpack.compose.utils.clickable

@Composable
internal fun BpkSectionHeading(
@@ -65,11 +65,14 @@ internal fun BpkSectionHeading(
}

@Composable
internal fun BpkSectionItem(item: BpkItem, modifier: Modifier = Modifier, behaviouralCallback: BehaviouralCallback? = null) {
internal fun BpkSectionItem(item: BpkItem, modifier: Modifier = Modifier, clickHandleScope: BpkClickHandleScope? = null) {
Row(
modifier = modifier
.fillMaxWidth()
.registerForBehaviouralEvents(item = item, behaviouralCallback = behaviouralCallback, onClick = item.onItemSelected),
.clickable {
item.onItemSelected()
clickHandleScope?.notifyClick()
},
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(BpkSpacing.Base),
) {

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Backpack for Android - Skyscanner's Design System
*
* Copyright 2018 Skyscanner Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.skyscanner.backpack.compose.utils

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier

/**
* This is used for registering components for behavioural events. Right now this supports `onClick` events, while any other
* events must be handled by the consumer.
*/

typealias BpkBehaviouralEventWrapper = @Composable (item: Any, modifier: Modifier, content: @Composable BpkClickHandleScope.() -> Unit) -> Unit

interface BpkClickHandleScope {
fun notifyClick()
}

0 comments on commit 4edcdc3

Please sign in to comment.