Hey I am working in android kotlin. I have one bas...
# android
v
Hey I am working in android kotlin. I have one base class and three child class which are inherited by base class. Shape.kt
Copy code
open class Shape {
    var data : List<Shape> = emptyList()
}
Three child class which are inherited through Shape class called Circle, Rectangle, Triangle. I create a variable called
Copy code
val shape = Shape()
then I am doing some computation logic inside my this function. Inide that I have data coming from server of all three class data.
Copy code
fun calculateData(){
   
  val list = comingFromServer()

   list.forEach{ shape->
      shape.circle?.let{
         // adding data 
      } 
      shape.rectangle?.let{
          // adding data 
      }
      shape.triangle?.let{
         // adding data 
      }
   }
}
I have few points in which I want your suggestion and best approach. I want to notify my view that my code is go inside the let of
shape.circle?
and
shape.rectangle?
or not . Also I want to notify my view that my code inside
shape.triangle?
. My List is this type of type of coming from server
Copy code
List(CircleData, RectangleData, TriangleData)
or
Copy code
List(CircleDataNull, RectangleDataNull, TriangleData)
I want to know, how can I to notify my view in best approach. What would you suugest to use MutableLiveData, MutableStateFlow or MutableSharedFlow. Please give me example which one is best. Thanks
t
Not sure what you’re trying to accomplish. Does each shape have a circle, rectangle, and triangle property on it? You say these inherit from shape but I also see properties on shape.
v
Hey @Tim Oltjenbruns I have list which is coming from server, inside that I have circle, rectangle and triangle data, so I am trying to separate them in 3 three form which is inherent from shape. My whole list would be come in Scenario 1
List(CircleData, RectangleData, TriangleData)....... Upto unlimited
or Scenario 2 `List(nu`ll`, nu`ll`, TriangleData).... Upto unlimited` My whole list would be scenario 1 or Scenario 2. But when Scenario 1 I need to notify my activity that I have all three view present in the list. And when Scenario 2 then I will notify the activity that I have only Triangle Data.
I want to choice which is best to use muablelist, mutableStateFlow or sharedFlow
t
Would filterIsInstance help?
v
@Tim Oltjenbruns can you please give me example?
t
@Vivek Modi of course.
val squares = listOf(Square(), Triangle(), Triangle()).filterIsInstance<Square>()
this gets you just the squares in this list.
Of course, null will not be a Square. So if you only get triangles, then the list of squares will be empty
v
okk got it thanks