Hey I am working to create a list of type INCOMING...
# android
v
Hey I am working to create a list of type INCOMINGOUTGOING 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.
Copy code
fun main() {
    val list = mutableListOf(
        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.ASSIGN.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.ASSIGN.value, Sender(9, "2021/10/09")),
        Conversation(ConversationType.OUTGOING.value, Sender(10, "2021/10/08")),
        Conversation(ConversationType.OUTGOING.value, Sender(11, "2021/10/07"))
    )
    groupByDate(list)
}

private fun groupByDate(list: MutableList<Conversation>) {
    var previous: MessageKey? = null
    val result: MutableList<Message> = mutableListOf()

    list.iterator().forEach { item ->
        val messageKey = MessageKey(item.type)
         val date = item.sender.date
        if (previous == null ||
            previous != messageKey ||
            messageKey.type?.let {
                ConversationType.fromType(it)
            } == ConversationType.ASSIGN ||
          !result[result.size - 1].value[0].date.equals(date)
        ) {
            val messageGroup = Message(messageKey, mutableListOf(item.sender))
            result.add(messageGroup)
        } else {
            val lastMessageGroup = result[result.size - 1]
            lastMessageGroup.value.add(item.sender)
        }
        previous = messageKey
    }

    result.forEach {
        println(it)
    }
}
MessageKey.kt
Copy code
data class MessageKey(
    val type: Int? = null
)
Message
Copy code
data class Message(
    val key: MessageKey,
    val value: MutableList<Sender>
)
ConversationType
Copy code
enum class ConversationType(val value: Int) {
    INCOMING(1),
    OUTGOING(2),
    TIME(0),
    ASSIGN(3);

    companion object {
        private val mapByType = values().associateBy(ConversationType::value)
        fun fromType(type: Int): ConversationType? {
            return mapByType[type]
        }
    }
}
Conversation
Copy code
data class Conversation(
    val type: Int? = null,
    val sender: Sender
)
Sender
Copy code
data class Sender(
    val id: Int? = null,
    val date: String? = null
)
Output above printing list is
Copy code
Message(key=MessageKey(type=1), value=[Sender(id=1, date=2021/10/12), Sender(id=2, date=2021/10/12)])
Message(key=MessageKey(type=2), value=[Sender(id=3, date=2021/10/11), Sender(id=4, date=2021/10/11)])
Message(key=MessageKey(type=3), value=[Sender(id=5, date=2021/10/11)])
Message(key=MessageKey(type=2), value=[Sender(id=6, date=2021/10/09)])
Message(key=MessageKey(type=1), value=[Sender(id=7, date=2021/10/09), Sender(id=8, date=2021/10/09)])
Message(key=MessageKey(type=3), value=[Sender(id=9, date=2021/10/09)])
Message(key=MessageKey(type=2), value=[Sender(id=10, date=2021/10/08)])
Message(key=MessageKey(type=2), value=[Sender(id=11, date=2021/10/07)])
when I am grouping the list in groupByDate function i need to add date according to that ends. I added below expected output. I don't want to add more iterator or foreach extra. Is it possible to do inside groupByDate in for loop?. Thanks. Expected Output
Copy code
//Message(key=MessageKey(type=1), value=[Sender(id=1, date=2021/10/12), Sender(id=2, date=2021/10/12)])
//Message(key=MessageKey(type=0), value=[Sender=(date=2021/10/12....))
//Message(key=MessageKey(type=2), value=[Sender(id=3, date=2021/10/11), Sender(id=4, date=2021/10/11)])
//Message(key=MessageKey(type=3), value=[Sender(id=5, date=2021/10/11)])
//Message(key=MessageKey(type=0), value=[Sender=(date=2021/10/11....))
//Message(key=MessageKey(type=2), value=[Sender(id=6, date=2021/10/09)])
//Message(key=MessageKey(type=1), value=[Sender(id=7, date=2021/10/09), Sender(id=8, date=2021/10/09)])
//Message(key=MessageKey(type=3), value=[Sender(id=9, date=2021/10/09)])
//Message(key=MessageKey(type=0), value=[Sender=(date=2021/10/09....))
//Message(key=MessageKey(type=2), value=[Sender(id=10, date=2021/10/08)])
//Message(key=MessageKey(type=0), value=[Sender=(date=2021/10/08....))
//Message(key=MessageKey(type=2), value=[Sender(id=11, date=2021/10/07)])
//Message(key=MessageKey(type=0), value=[Sender=(date=2021/10/07....))
g
A lot of business stuff that we don't need to understand your Kotlin problem IMO. Can you try to resume that to a simple case with a list of 2 items not correlated to your business just to show what you're trying to do? As far as I can see, it's list edition (add/remove), a loop for an unknown reason, and a conversation type that can be deduced from the date, not sure what's the blocker there.
v
@Grégory Lureau i want to group message and according to type and store in list and I don't know how to do in efficent way
g
There was a video some days ago about the collections and diverse operators like
partition
, you should be able to find them somewhere in this slack. The explanations were graphical and well defined;
v
great thanks!! I'll try to look if i find that otherwise I'll ping you 😅
@Grégory Lureau i can't find that video link
v
ohh great thanks once again