Home > Software design >  Add value after date is end in list kotlin
Add value after date is end in list kotlin

Time:10-12

hey I am working to create a list of type INCOMING, OUTGOING and TIME through enum class. My data class is populated through api call and filled according to that in the list. I want to modify to add TIME enum value whenever date ends the current one.

fun main() {
    val list = mutableListOf<Conversation>(
        Conversation(ConversationType.INCOMING.value, Sender(1, "2021/10/12")),
        Conversation(ConversationType.INCOMING.value, Sender(2, "2021/10/12")),
        Conversation(ConversationType.OUTGOING.value, Sender(3, "2021/10/11")),
        Conversation(ConversationType.OUTGOING.value, Sender(4, "2021/10/11")),
        Conversation(ConversationType.OUTGOING.value, Sender(5, "2021/10/11")),
        Conversation(ConversationType.OUTGOING.value, Sender(6, "2021/10/09")),
        Conversation(ConversationType.INCOMING.value, Sender(7, "2021/10/09")),
        Conversation(ConversationType.INCOMING.value, Sender(8, "2021/10/09")),
        Conversation(ConversationType.OUTGOING.value, Sender(9, "2021/10/09")),
        Conversation(ConversationType.OUTGOING.value, Sender(10, "2021/10/08")),
        Conversation(ConversationType.OUTGOING.value, Sender(11, "2021/10/07"))
    )
    val previousDate: String? = null
    list.forEach { conversation ->
        val date = conversation.sender?.date
        if (!previousDate.equals(date)) {
            println(date)
            previousDate = date
        }
        ... need to add **TIME** ENUM value ...
    }
}

ConversationType

enum class ConversationType(val value: Int) {
    INCOMING(1),
    OUTGOING(2),
    TIME(0);
}

Conversation

data class Conversation(
    val type: Int? = null,
    val sender: Sender? = null
)

Sender

data class Sender(
    val id: Int? = null,
    val date: String? = null
)

For example

lets say 2021/10/12 this is the date first we need to search and this will present in index 0 and 1 after that I need to add

Conversation(ConversationType.TIME.value, Sender(null, "2021/10/12"))

Now List will look like this

    Conversation(ConversationType.INCOMING.value, Sender(1, "2021/10/12")),
    Conversation(ConversationType.INCOMING.value, Sender(2, "2021/10/12")),
    Conversation(ConversationType.TIME.value, Sender(null, "2021/10/12")),
    Conversation(ConversationType.OUTGOING.value, Sender(3, "2021/10/11")),
    Conversation(ConversationType.OUTGOING.value, Sender(4, "2021/10/11")),
    Conversation(ConversationType.OUTGOING.value, Sender(5, "2021/10/11")),
    Conversation(ConversationType.OUTGOING.value, Sender(6, "2021/10/09")),
    Conversation(ConversationType.INCOMING.value, Sender(7, "2021/10/09")),
    Conversation(ConversationType.INCOMING.value, Sender(8, "2021/10/09")),
    Conversation(ConversationType.OUTGOING.value, Sender(9, "2021/10/09")),
    Conversation(ConversationType.OUTGOING.value, Sender(10, "2021/10/08")),
    Conversation(ConversationType.OUTGOING.value, Sender(11, "2021/10/07"))

Now we go to 2021/10/11 on index 3,4 and 5 so after that i need to add this

Conversation(ConversationType.TIME.value, Sender(null, "2021/10/11"))

My List will be

    Conversation(ConversationType.INCOMING.value, Sender(1, "2021/10/12")),
    Conversation(ConversationType.INCOMING.value, Sender(2, "2021/10/12")),
    Conversation(ConversationType.TIME.value, Sender(null, "2021/10/12")),
    Conversation(ConversationType.OUTGOING.value, Sender(3, "2021/10/11")),
    Conversation(ConversationType.OUTGOING.value, Sender(4, "2021/10/11")),
    Conversation(ConversationType.OUTGOING.value, Sender(5, "2021/10/11")),
    Conversation(ConversationType.TIME.value, Sender(null, "2021/10/11"))
    Conversation(ConversationType.OUTGOING.value, Sender(6, "2021/10/09")),
    Conversation(ConversationType.INCOMING.value, Sender(7, "2021/10/09")),
    Conversation(ConversationType.INCOMING.value, Sender(8, "2021/10/09")),
    Conversation(ConversationType.OUTGOING.value, Sender(9, "2021/10/09")),
    Conversation(ConversationType.OUTGOING.value, Sender(10, "2021/10/08")),
    Conversation(ConversationType.OUTGOING.value, Sender(11, "2021/10/07"))

Like this i need to add values in my list. So how can i achieved this in my list. I need to do inside foreach loop. Sorry for my wrong english. Thanks Everyone.

CodePudding user response:

As I understand what you want, You need something like this:

var position = -1
var previousDate: String? = null
list.forEachIndexed { index, conversation ->
    val date = conversation.sender?.date
    if (date != previousDate) {
        println(date)
        previousDate = date
    }
    position = index
}
list.add(index, Conversation(ConversationType.TIME.value, Sender(null, "2021/10/11")))

Enjoy!

CodePudding user response:

Since you want to add items to the list while you're iterating it, you need to iterate the indices so you can insert items as you go. Since the list is growing as you go, you need to use a while loop instead of a for loop with the original indices.

var previousDate: String? = null
var i = 0
while (i < list.size) {
    val newDate = list[i].date
    if (newDate != previousDate && previousDate != null) {
        list.add(i  , Conversation(ConversationType.TIME.value, Sender(null, previousDate)))
    }
    previousDate = newDate
    i  
}

To explain my suggestion about simplifying using sealed interfaces, here is how I would redesign. I would replace your above classes with:

sealed interface Conversation {
    val date: String
}

sealed interface ConversationMessage: Conversation {
    val senderId: Int
}

data class IncomingConversation(
    override val date: String,
    override val senderId: Int
): ConversationMessage

data class OutgoingConversation(
    override val date: String,
    override val senderId: Int
): ConversationMessage

data class TimeStampConversation(override val date: String): Conversation

Then you don't have to worry about anything being null, you don't have to unpack enum values, and all the constructors are much easier to use. You can use when statements with is checks to determine which view holder to work with in your RecyclerView.

  • Related