This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ForecastHeaderViewHolder( | |
binding: ViewholderForecastHeaderBinding | |
) : RecyclerViewViewHolder<ForecastHeaderBindingItem>(binding) { | |
override fun bind(bindingItem: ForecastHeaderBindingItem) { | |
(binding as ViewholderForecastHeaderBinding).state = bindingItem.forecastHeaderState | |
binding.executePendingBindings() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class ForecastHeaderState( | |
override val id: Long, | |
val date: CharSequence, | |
val sunrise: CharSequence, | |
val sunset: CharSequence, | |
) : Diffable | |
data class ForecastCardState( | |
override val id: Long, | |
val header: CharSequence, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<layout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:bind="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools"> | |
<data> | |
<variable | |
name="state" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onCreateView( | |
inflater: LayoutInflater, | |
container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View { | |
binding = FragmentForecastBinding.inflate(inflater, container, false) | |
binding.lifecycleOwner = this | |
return binding.root | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun ForecastHeader( | |
state: ForecastItem.ForecastHeader, | |
modifier: Modifier = Modifier, | |
) { | |
Column( | |
modifier = modifier, | |
horizontalAlignment = Alignment.CenterHorizontally, | |
) { | |
Text( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun ForecastWeatherCard( | |
state: ForecastItem.ForecastCard, | |
modifier: Modifier = Modifier, | |
elevation: Dp = CardElevation, | |
) { | |
Card(modifier = modifier, elevation = elevation) { | |
Column(modifier = Modifier.fillMaxWidth().padding(MarginSingle)) { | |
Row(verticalAlignment = Alignment.CenterVertically) { | |
Icon( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LazyColumn( | |
modifier = Modifier.fillMaxWidth().padding(bottom = MarginSingle), | |
verticalArrangement = Arrangement.spacedBy(MarginSingle), | |
horizontalAlignment = Alignment.CenterHorizontally | |
) { | |
items(state.forecastItems) { item -> | |
when (item) { | |
is ForecastItem.ForecastHeader -> ForecastHeader( | |
state = item, | |
modifier = Modifier.fillMaxWidth(.85f), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class ForecastItem { | |
data class ForecastHeader( | |
val id: Long, | |
val date: String, | |
val sunrise: String, | |
val sunset: String, | |
) : ForecastItem() | |
data class ForecastCard( | |
val id: Long, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@HiltViewModel | |
class CityViewModel @Inject constructor() : ViewModel(), DefaultLifecycleObserver { | |
override fun onStart(owner: LifecycleOwner) { | |
// fragment is now started, run tasks | |
} | |
override fun onStop(owner: LifecycleOwner) { | |
// fragment is now stopped, cancel running tasks | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@HiltViewModel | |
class CityViewModel @Inject constructor() : ViewModel() { | |
fun onStart() { | |
// start task - the composable has entered the composition | |
} | |
fun onStop() { | |
// cancel task - the composable has left the composition | |
} |