KotlinLeaner
05/25/2023, 11:37 AMwindowed
for my use case. I tried some code and which is working fine. Is their any possibility to improve this code?
private fun simpleWindowed(unFilteredMovieList: List<NewMovieItem>) {
val filterMovieList = mutableListOf<FilterMovieItem>()
val otherMovieType = mutableListOf<FilterMovieItem>()
unFilteredMovieList
.windowed(size = 2, step = 1, partialWindows = true)
.onEach { window: List<NewMovieItem> ->
val currentItem = window.first()
val nextItem = window.last()
val currentFilterItem = filterItem(currentItem)
if (currentFilterItem != null) {
var seatCount = 0
val screenNumber = nextItem.screenNumber
if (currentItem != nextItem && !screenNumber.isNullOrEmpty()) {
seatCount = getSeatCount(screenNumber)
}
filterMovieList.add(currentFilterItem.copy(seatCount = seatCount))
if (currentFilterItem.type == "other") {
otherMovieType.add(currentFilterItem)
}
}
}
filterMovieList.onEach { println(it) }
println("---------------")
otherMovieType.onEach { println(it) }
}
More code in thread to understand the basic logicKotlinLeaner
05/25/2023, 11:38 AMprivate fun filterItem(item: NewMovieItem): FilterMovieItem? {
return if (!item.name.isNullOrEmpty() && !item.type.isNullOrEmpty() && item.isReleaseNow != null) {
FilterMovieItem(item.name, item.type, item.isReleaseNow, 0)
} else {
null
}
}
private fun getSeatCount(screenNumber: String): Int {
return when (screenNumber) {
"Screen 1" -> {
10
}
"Screen 2" -> {
6
}
else -> {
0
}
}
}
data class FilterMovieItem(
val name: String, val type: String, val isReleasedInThreader: Boolean, val seatCount: Int
)
KotlinLeaner
05/25/2023, 11:38 AMdata class BollywoodMovie(
val year: String? = null, val movieList: List<NewMovieItem>? = null
)
data class NewMovieItem(
val name: String? = null,
val type: String? = null,
val isReleaseNow: Boolean? = null,
val screenNumber: String? = null
)
fun main() {
val movieList = listOf(
NewMovieItem("Kisi Ka Bhai Kisi Ki Jaan", "action", true, "Screen 1"),
NewMovieItem("Pathaan", "action", true, ""),
NewMovieItem("Tu Jhoothi Main Makkaar", "romance"),
NewMovieItem("r", "", true, "Screen 3"),
NewMovieItem("Dasara", "romance", false, "Screen 1"),
NewMovieItem("Kuttey", "comedy", false, "Screen 2"),
NewMovieItem("Selfiee", "comedy", true, "Screen 3"),
NewMovieItem("xyz", "other", false, "Screen 1"),
NewMovieItem("ABC 3", "dance", false, "Screen 1"),
)
val unfilteredList = BollywoodMovie("2023", movieList)
val unFilteredMovieList = unfilteredList.movieList.orEmpty()
simpleWindowed(unFilteredMovieList)
}
Klitos Kyriacou
05/25/2023, 2:48 PM.onEach { window: List<NewMovieItem> ->
val currentItem = window.first()
val nextItem = window.last()
can be replaced with the following, using destructuring:
.onEach { (currentItem, nextItem) ->
Also, I find it more usual to see forEach
rather than onEach
- the latter is the same except that it also returns the same object, so if you don't need to do any further operations on it, it's more usual to use forEach
.
2. What's the purpose of currentFilterItem.copy(seatCount = seatCount)
? Why not just currentFilterItem
?
3. Is this a typo? isReleasedInThreader
-> isReleasedInTheater
? (or isReleasedInCinema
?)KotlinLeaner
05/25/2023, 2:50 PMKotlinLeaner
05/25/2023, 2:52 PMgetSeatCount
value and store value in different variable called seatCount
. I want seatCount
value in currentFilterItem
so that's why I used copy
method to change that value.KotlinLeaner
05/25/2023, 2:53 PMKlitos Kyriacou
05/25/2023, 3:05 PMKotlinLeaner
05/25/2023, 3:13 PMcurrentItem
I want to make a condition based on my nextItem
value.