Skip to content

Instantly share code, notes, and snippets.

@senamit2708
Created July 6, 2023 09:10
Show Gist options
  • Save senamit2708/cf1e0ec7e03ddbadc232f3bf85e72f6d to your computer and use it in GitHub Desktop.
Save senamit2708/cf1e0ec7e03ddbadc232f3bf85e72f6d to your computer and use it in GitHub Desktop.
Sealed Class Learning
package com.example.coroutinelearning.entity
sealed class Employee //there is no need of {}, its optional
//if you use {}, u have to call class with use of Employee class, like Employee.Manager from fragment/activity. else u will be
//able to directly call it.
data class Manager(val name: String, val age: Int, val team: List<String>) : Employee()
class SeniorDev(val name: String, val age: Int, val project: String) : Employee()
object JuniorDev : Employee()
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btnEmployee"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="32dp"
android:text="btnEmployee"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnManager"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="32dp"
android:text="Manager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnEmployee" />
<Button
android:id="@+id/btnJuniorDev"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="32dp"
android:text="Junior Dev"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnManager" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.coroutinelearning.fragments
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.example.coroutinelearning.databinding.SealedLearningBinding
import com.example.coroutinelearning.entity.Employee
import com.example.coroutinelearning.entity.JuniorDev
import com.example.coroutinelearning.entity.Manager
import com.example.coroutinelearning.entity.SeniorDev
class SealedLearningFrag: Fragment(), View.OnClickListener{
private val TAG = SealedLearningFrag::class.simpleName
private lateinit var binding: SealedLearningBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = SealedLearningBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.btnEmployee.setOnClickListener(this)
binding.btnManager.setOnClickListener(this)
binding.btnJuniorDev.setOnClickListener(this)
}
override fun onClick(view: View?) {
when(view){
binding.btnEmployee ->{
val employee: Employee = SeniorDev("Amit", 32, "android")
employeeMessage(employee)
}
binding.btnManager ->{
val employee: Employee = Manager("Sonali", 34, listOf("apple", "android"))
employeeMessage(employee)
}
binding.btnJuniorDev -> {
val employee: Employee = JuniorDev
employeeMessage(employee)
}
else -> {
Toast.makeText(requireContext(), "nothing selected", Toast.LENGTH_SHORT).show()
}
}
}
private fun employeeMessage(employee: Employee) {
val message = when(employee){
is Manager -> {
"Welcome ${employee.name}, your age: ${employee.age} to office"
}
is SeniorDev -> {
"Welcome ${employee.name}, your age: ${employee.age} to office"
}
is JuniorDev -> {
"Welcome onBoard, we wish you an awesome experience"
}
//no else is required, as all cases are handled here.
}
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment