Skip to content

Instantly share code, notes, and snippets.

View Raiden18's full-sized avatar
🎯
Focusing

Paul Karpukhin Raiden18

🎯
Focusing
View GitHub Profile
class ViewModel(
// ...
): ViewModel(){
val buttonSizeLiveData = MutableLiveData<ButtonSize>()
// ...
private fun methodThatDoesSomething() {
// ...
@Raiden18
Raiden18 / Button.kt
Last active June 2, 2022 04:11
Replaced when statement with polymorphysm.
/*
* Here we moved dp values from getButtonHeight method.
* getButtonHeight method has been removed.
*/
sealed class ButtonSize(open val heightDpInt: Int) {
object Small : ButtonSize(16)
object Medium : ButtonSize(24)
object Large : ButtonSize(32)
object Huge : ButtonSize(40)
data class Custom(override val heightDpInt: Int): ButtonSize(heightDpInt)
@Raiden18
Raiden18 / Button.kt
Last active March 23, 2023 15:51
Button.kt More complex example with polymorphism
sealed class ButtonSize(
open val heightDpInt: Int,
open val color: Color,
open val textResId: Int
) {
object Small : ButtonSize(16, Color.Red, R.string.small_button_text)
object Medium : ButtonSize(24, Color.Gray, R.string.medium_button_text)
object Large : ButtonSize(32, Color.Green, R.string.large_button_text)
object Huge : ButtonSize(40, Color.Blue, R.string.huge_button_text)
data class Custom(
sealed class ButtonSize() {
object Small : ButtonSize()
object Medium : ButtonSize()
object Large : ButtonSize()
object Huge : ButtonSize()
data class Custom(
val heightDpInt: Int,
val color: Color,
val textResId: Int
) : ButtonSize()
data class YourDomainDto(
val field1: String,
val field2: String
)
data class YourModelResponse(
@SerializedName("field1")
val field1: String,
@SerializedName("field2")
val field2: String
)
data class YourUiModel(
val title: String,
val description: String,
val visibility: Int // View.GONE, etc
)
class YourViewHolder(
view: View
): RecyclerView.ViewHolder(view) {
//...
// Icon from android resource
{
"icon": {
type: "resources"
"name": "resources_name"
}
}
// Icon from url
{
data class IconResponse(
@SerializedName("type")
val type: IconTypeEnum,
@SerializedName("name")
val name: String?,
@SerializedName("url")
val url: String?
)
sealed class Icon {
data class Resources(val name: String)
data class Url(val url: String)
}