/* * Copyright (C) 2017 Moez Bhatti * * This file is part of QKSMS. * * QKSMS is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * QKSMS is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with QKSMS. If not, see . */ package dev.octoshrimpy.quik.model import io.realm.RealmList import io.realm.RealmObject import io.realm.annotations.Index import io.realm.annotations.PrimaryKey open class Conversation( @PrimaryKey var id: Long = 0, @Index var archived: Boolean = false, @Index var blocked: Boolean = false, @Index var pinned: Boolean = false, var recipients: RealmList = RealmList(), var lastMessage: Message? = null, var draft: String = "", var blockingClient: Int? = null, var blockReason: String? = null, var name: String = "" // For group chats, the user is allowed to set a custom title for the conversation ) : RealmObject() { val date: Long get() = lastMessage?.date ?: 0 val snippet: String? get() = lastMessage?.getSummary() val unread: Boolean get() = lastMessage?.read == false val me: Boolean get() = lastMessage?.isMe() == true fun getTitle(): String { return name.takeIf { it.isNotBlank() } ?: recipients.joinToString { recipient -> recipient.getDisplayName() } } }