Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:
Coding

Valery ValeryPonomarenko

:octocat:
Coding
View GitHub Profile
@ValeryPonomarenko
ValeryPonomarenko / DestinationFragment.kt
Created Apr 24, 2019
Rendering a Screen Based On Its ViewState
View DestinationFragment.kt
class DestinationFragment : Fragment() {
//code...
@Inject
internal lateinit var renderer: Renderer<DestinationViewState>
private val viewModel by lazy { /* code ... */ }
//code...
View AirportSeachViewStateRenderers.kt
//EmptyQuery state
class EmptyQueryViewStateRenderer @Inject constructor(): ViewStateRenderer<DestinationViewState> {
override fun render(view: View, viewState: DestinationViewState) {
view.text_message.visibility = View.VISIBLE
view.progress.visibility = View.GONE
view.recyclerView_destinations.visibility = View.GONE
view.text_message.text = "Enter the city you want to flight"
}
}
View DestinationViewStateRendererPicker.kt
class DestinationViewStateRendererPicker : ViewStateRendererPicker<DestinationViewState> {
override fun pick(viewState: DestinationViewState): KClass<out ViewStateRenderer<DestinationViewState>> =
when {
viewState.query.isBlank() -> EmptyQueryViewStateRenderer::class
viewState.isLoading -> LoadingViewStateRenderer::class
viewState.destinations.isEmpty() -> EmptyViewStateRenderer::class
else -> RegularViewStateRenderer::class
}
}
@ValeryPonomarenko
ValeryPonomarenko / DestinationViewState.kt
Created Apr 24, 2019
Rendering a Screen Based On Its ViewState
View DestinationViewState.kt
data class DestinationViewState(
val destinations: List<SpannableCity> = listOf(),
val query: String = "",
val isLoading: Boolean = false
)
@ValeryPonomarenko
ValeryPonomarenko / ViewStateRenderer.kt
Last active Apr 24, 2019
Rendering a Screen Based On Its ViewState
View ViewStateRenderer.kt
interface ViewStateRenderer<VS> {
fun render(view: View, viewState: VS)
}
@ValeryPonomarenko
ValeryPonomarenko / ViewStateRendererPicker.kt
Created Apr 10, 2019
Rendering a Screen Based On Its ViewState
View ViewStateRendererPicker.kt
interface ViewStateRendererPicker<VS> {
fun pick(viewState: VS): KClass<out ViewStateRenderer<VS>>
}
@ValeryPonomarenko
ValeryPonomarenko / Renderer.kt
Last active Apr 24, 2019
Rendering a Screen Based On Its ViewState
View Renderer.kt
class Renderer<VS>(
private val viewStateRendererPicker: ViewStateRendererPicker<VS>,
private val renderers: Map<KClass<out ViewStateRenderer<VS>>, ViewStateRenderer<VS>>
) {
fun render(view: View, viewState: VS) {
val rendererClass = viewStateRendererPicker.pick(viewState)
renderers[rendererClass]?.render(view, viewState)
?: throw IllegalStateException("There is no ViewStateRenderer for ${rendererClass.simpleName}")
}
}
@ValeryPonomarenko
ValeryPonomarenko / SomeFragment.kt
Last active Jun 13, 2019
Rendering a Screen Based On Its ViewState
View SomeFragment.kt
viewModel.viewState.observe(this) {
when (it) {
is SearchViewState.Entering -> {
text_destination_from.text = it.from.name
text_small_from.text = it.from.shortName
text_destination_to.text = it.to.name
text_small_to.text = it.to.shortName
}
is SearchViewState.Loaded -> {
adapter.flights = it.flights
@ValeryPonomarenko
ValeryPonomarenko / nav_graph.xml
Last active Feb 1, 2019
Navigation in Multi-Module Projects
View nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_graph"
app:startDestination="@id/questionsFragment">
<fragment
android:id="@+id/questionsFragment"
android:name="me.vponomarenko.modular.navigation.questions.QuestionsFragment"
android:label="QuestionsFragment">
<action
@ValeryPonomarenko
ValeryPonomarenko / activity_main.xml
Created Jan 31, 2019
Navigation in Multi-Module Projects
View activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />