Skip to main content

Overview

The Phrase data class represents a text phrase saved by a user in the EV Sum 2 application. Each phrase has a unique identifier, text content, and creation timestamp.

Class Definition

data class Phrase(
    val id: String = "",
    val text: String = "",
    val createdAtMillis: Long = 0L
)

Properties

id
String
default:"\"\""
The unique identifier for the phrase, typically assigned by Firestore
text
String
default:"\"\""
The actual text content of the phrase
createdAtMillis
Long
default:"0L"
The creation timestamp in milliseconds since epoch (Unix timestamp)

Usage Examples

Creating a Phrase instance

val phrase = Phrase(
    id = "phrase123",
    text = "Hello world",
    createdAtMillis = System.currentTimeMillis()
)

Creating from Firestore document

val documentSnapshot = // ... from Firestore
val phrase = Phrase(
    id = documentSnapshot.id,
    text = documentSnapshot.getString("text") ?: "",
    createdAtMillis = documentSnapshot.getLong("createdAt") ?: 0L
)

Default instance

// Creates an empty phrase with default values
val emptyPhrase = Phrase()

Data Class Features

As a Kotlin data class, Phrase automatically provides:
  • equals(): Compares two phrases based on their properties
  • hashCode(): Generates hash code based on properties
  • toString(): Returns a string representation of the phrase
  • copy(): Creates a copy with optionally modified properties
  • componentN(): Enables destructuring declarations

Copy with modifications

val phrase = Phrase(
    id = "phrase123",
    text = "Original text",
    createdAtMillis = 1234567890L
)

// Update only the text
val updatedPhrase = phrase.copy(text = "Updated text")

Destructuring

val phrase = Phrase(
    id = "phrase123",
    text = "Hello world",
    createdAtMillis = 1234567890L
)

val (id, text, timestamp) = phrase
println("Phrase ID: $id")
println("Text: $text")
println("Created at: $timestamp")

Timestamp Formatting

Converting to readable date

import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

fun Phrase.formattedDate(pattern: String = "dd/MM/yyyy HH:mm"): String {
    val dateFormat = SimpleDateFormat(pattern, Locale.getDefault())
    return dateFormat.format(Date(createdAtMillis))
}

// Usage
val phrase = Phrase(
    id = "phrase123",
    text = "Hello world",
    createdAtMillis = System.currentTimeMillis()
)
println("Created: ${phrase.formattedDate()}")

Relative time

import java.util.concurrent.TimeUnit

fun Phrase.timeAgo(): String {
    val now = System.currentTimeMillis()
    val diff = now - createdAtMillis
    
    return when {
        diff < TimeUnit.MINUTES.toMillis(1) -> "Just now"
        diff < TimeUnit.HOURS.toMillis(1) -> {
            "${TimeUnit.MILLISECONDS.toMinutes(diff)} minutes ago"
        }
        diff < TimeUnit.DAYS.toMillis(1) -> {
            "${TimeUnit.MILLISECONDS.toHours(diff)} hours ago"
        }
        else -> "${TimeUnit.MILLISECONDS.toDays(diff)} days ago"
    }
}

// Usage
println(phrase.timeAgo()) // "5 minutes ago"

Validation

fun Phrase.isValid(): Boolean {
    return id.isNotBlank() && text.isNotBlank() && createdAtMillis > 0
}

fun Phrase.isEmpty(): Boolean {
    return text.isBlank()
}

// Usage
val phrase = Phrase(id = "phrase123", text = "Hello", createdAtMillis = 123456L)
if (phrase.isValid()) {
    // Proceed with phrase operations
}

Integration with LazyColumn

@Composable
fun PhraseList(phrases: List<Phrase>) {
    LazyColumn {
        items(phrases) { phrase ->
            PhraseItem(
                text = phrase.text,
                timestamp = phrase.formattedDate(),
                onClick = { /* Handle click */ }
            )
        }
    }
}

Sorting

// Sort by newest first
val sortedByNewest = phrases.sortedByDescending { it.createdAtMillis }

// Sort by oldest first
val sortedByOldest = phrases.sortedBy { it.createdAtMillis }

// Sort alphabetically by text
val sortedByText = phrases.sortedBy { it.text }

Source Location

com.demodogo.ev_sum_2.domain.models.Phrase

Build docs developers (and LLMs) love