Hi guys, I want to iterate with next item value an...
# codereview
k
Hi guys, I want to iterate with next item value and made a logic on my current value. I found
windowed
for my use case. I tried some code and which is working fine. Is their any possibility to improve this code?
Copy 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 logic
More code in here
Copy code
private 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
)
Copy code
data 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)
}
k
I feel there could be improvements in the whole logic, but for some micro-improvements: 1. This:
Copy code
.onEach { window: List<NewMovieItem> ->
            val currentItem = window.first()
            val nextItem = window.last()
can be replaced with the following, using destructuring:
Copy code
.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
?)
k
Thanks for guidance. Yeah I'll change what you have mention in 1. point
In 2 point, I am getting
getSeatCount
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.
yeah 3 point is typo. Sorry for that
k
Sorry I hadn't read the code properly regarding the seatCount. However, I don't understand why you're calculating the seatCount for the current movie based on the screen number of the next movie.
k
My code is just basic example. Real code is looks similar like that but values are different. I want to iterate one by one. In my
currentItem
I want to make a condition based on my
nextItem
value.