Skip to main content

Overview

The PhraseRepository class handles CRUD operations for user phrases stored in Firebase Firestore. Each user’s phrases are stored in a subcollection under their user document.

Class Definition

class PhraseRepository(
    private val db: FirebaseFirestore = FirebaseFirestore.getInstance(),
    private val auth: FirebaseAuth = FirebaseAuth.getInstance()
)
db
FirebaseFirestore
default:"FirebaseFirestore.getInstance()"
Firestore database instance for storing and retrieving phrases
auth
FirebaseAuth
default:"FirebaseAuth.getInstance()"
Firebase Authentication instance to get the current user ID

Private Methods

uid

Retrieves the current user’s ID.
private fun uid(): String
This method throws IllegalStateException if no user is currently authenticated.

col

Returns a reference to the current user’s phrases collection.
private fun col(): CollectionReference
The collection path is: users/{uid}/phrases

Public Methods

add

Adds a new phrase to the current user’s collection.
suspend fun add(text: String)
text
String
required
The phrase text to save
Example:
val phraseRepository = PhraseRepository()
try {
    phraseRepository.add("Hello world")
    // Phrase added successfully
} catch (e: IllegalStateException) {
    // No active session
} catch (e: Exception) {
    // Handle error
}
The method automatically sets the createdAt timestamp to the current system time in milliseconds.

get

Retrieves all phrases for the current user, ordered by creation date (newest first).
suspend fun get(): List<Phrase>
List<Phrase>
List<Phrase>
A list of all user phrases, ordered by creation date in descending order
Example:
val phraseRepository = PhraseRepository()
try {
    val phrases = phraseRepository.get()
    phrases.forEach { phrase ->
        println("${phrase.id}: ${phrase.text}")
    }
} catch (e: IllegalStateException) {
    // No active session
} catch (e: Exception) {
    // Handle error
}

update

Updates the text of an existing phrase.
suspend fun update(id: String, newText: String)
id
String
required
The unique identifier of the phrase to update
newText
String
required
The new text for the phrase
Example:
val phraseRepository = PhraseRepository()
try {
    phraseRepository.update("phraseId123", "Updated phrase text")
    // Phrase updated successfully
} catch (e: IllegalStateException) {
    // No active session
} catch (e: Exception) {
    // Handle error
}

delete

Deletes a phrase from the user’s collection.
suspend fun delete(id: String)
id
String
required
The unique identifier of the phrase to delete
Example:
val phraseRepository = PhraseRepository()
try {
    phraseRepository.delete("phraseId123")
    // Phrase deleted successfully
} catch (e: IllegalStateException) {
    // No active session
} catch (e: Exception) {
    // Handle error
}

Error Handling

All methods may throw IllegalStateException with the message “No hay sesión activa” (No active session) if called when no user is authenticated.

Firestore Structure

Phrases are stored with the following structure:
users/
  {uid}/
    phrases/
      {phraseId}/
        text: String
        createdAt: Long

Source Location

com.demodogo.ev_sum_2.data.repositories.PhraseRepository

Build docs developers (and LLMs) love