Skip to content

Instantly share code, notes, and snippets.

@attilakruchio
Last active August 20, 2022 15:58
Show Gist options
  • Save attilakruchio/31ef0d7af9cf571b49cc81446054b89c to your computer and use it in GitHub Desktop.
Save attilakruchio/31ef0d7af9cf571b49cc81446054b89c to your computer and use it in GitHub Desktop.
@HiltViewModel
class EventViewModel @Inject constructor(
private val savedStateHandle: SavedStateHandle,
// ...
) : ViewModel() {
private val _selectedEventId = savedStateHandle.getStateFlow<Int?>(
KEY_SELECTED_EVENT_ID, null
)
private val _eventFilter = savedStateHandle.getStateFlow<EventFilter>(
KEY_EVENT_FILTER, EventFilter.ALL
)
// ...
fun setSelectedEventId(id: Int?) {
savedStateHandle[KEY_SELECTED_EVENT_ID] = id
}
fun setEventFilter(filter: EventFilter) {
savedStateHandle[KEY_EVENT_FILTER] = filter
}
companion object {
private const val KEY_SELECTED_EVENT_ID = "selected_event_id"
private const val KEY_EVENT_FILTER = "event_filter"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment