Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rememberEventSink #1074

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (C) 2023 Slack Technologies, LLC
// SPDX-License-Identifier: Apache-2.0
package com.slack.circuit.foundation

import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.remember
import com.slack.circuit.runtime.CircuitUiEvent
import com.slack.circuit.runtime.presenter.Presenter

/**
* Remembers an [eventSink] for use in a Circuit [Presenter].
*
* It's recommended to set [keys] to non-[State] values captured by [eventSink] that can change.
*/
@Composable
public fun <T : CircuitUiEvent> rememberEventSink(
vararg keys: Any?,
eventSink: (T) -> Unit
): (T) -> Unit {
return remember(*keys) { eventSink }
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import coil.request.ImageRequest
import com.slack.circuit.codegen.annotations.CircuitInject
import com.slack.circuit.foundation.rememberEventSink
import com.slack.circuit.overlay.LocalOverlayHost
import com.slack.circuit.overlay.OverlayHost
import com.slack.circuit.runtime.CircuitUiEvent
Expand Down Expand Up @@ -191,27 +192,31 @@ constructor(
return when {
animals == null -> PetListScreen.State.Loading
animals.isEmpty() -> PetListScreen.State.NoAnimals(isRefreshing)
else ->
else -> {
val eventSink =
rememberEventSink<PetListScreen.Event> { event ->
when (event) {
is PetListScreen.Event.ClickAnimal -> {
navigator.goTo(PetDetailScreen(event.petId, event.photoUrlMemoryCacheKey))
}
is PetListScreen.Event.UpdatedFilters -> {
isUpdateFiltersModalShowing = false
filters = event.newFilters
}
PetListScreen.Event.UpdateFilters -> {
isUpdateFiltersModalShowing = true
}
PetListScreen.Event.Refresh -> isRefreshing = true
}
}
PetListScreen.State.Success(
animals = animals.filter { shouldKeep(filters, it) }.toImmutableList(),
isRefreshing = isRefreshing,
filters = filters,
isUpdateFiltersModalShowing = isUpdateFiltersModalShowing,
) { event ->
when (event) {
is PetListScreen.Event.ClickAnimal -> {
navigator.goTo(PetDetailScreen(event.petId, event.photoUrlMemoryCacheKey))
}
is PetListScreen.Event.UpdatedFilters -> {
isUpdateFiltersModalShowing = false
filters = event.newFilters
}
PetListScreen.Event.UpdateFilters -> {
isUpdateFiltersModalShowing = true
}
PetListScreen.Event.Refresh -> isRefreshing = true
}
}
eventSink = eventSink,
)
}
}
}

Expand Down
Loading