Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
2e4ff12
refactor(chat): give the chat repository Storage and a photo compressor
Mamel0uk Nov 16, 2026
583764e
feat(chat): send a photo message
Mamel0uk Nov 16, 2026
8e9f5b0
feat(chat): reuse a photo already uploaded when retrying
Mamel0uk Nov 16, 2026
da1d4b1
feat(chat): fail a photo upload quickly when offline
Mamel0uk Nov 16, 2026
5615cfb
feat(chat): load a photo message
Mamel0uk Nov 16, 2026
9e81c6c
test(chat): cover sending a photo message
Mamel0uk Nov 16, 2026
c4963be
test(chat): cover a failed photo upload
Mamel0uk Nov 16, 2026
90243ce
test(chat): cover retrying a photo message
Mamel0uk Nov 16, 2026
b58f084
test(chat): cover a photo upload that times out
Mamel0uk Nov 16, 2026
dce6f25
test(chat): cover loading a photo message
Mamel0uk Nov 16, 2026
ab068e2
fix(chat): write a photo's message before uploading it
Mamel0uk Nov 17, 2026
96f3d61
fix(chat): compress a photo before sending its message
Mamel0uk Nov 18, 2026
a0c453d
fix(chat): report a photo that can't be read as not found
Mamel0uk Nov 18, 2026
cb3713f
docs(chat): say which rules order a photo message's writes
Mamel0uk Nov 18, 2026
fecfb37
fix(chat): stop loading a photo after 30 s offline
Mamel0uk Nov 18, 2026
c14b569
feat(chat): let the sender mark a photo message's photo as uploaded
Mamel0uk Nov 18, 2026
f232a90
feat(chat): read whether a message's photo is still uploading
Mamel0uk Nov 18, 2026
bff3b09
test(chat): cover reading a photo that is still uploading
Mamel0uk Nov 18, 2026
ff14e21
feat(chat): mark a photo message's photo uploaded after sending it
Mamel0uk Nov 18, 2026
ef67ab9
test(chat): cover marking a photo uploaded
Mamel0uk Nov 18, 2026
0f2e7f3
fix(chat): wait for a queued photo message instead of writing it again
Mamel0uk Nov 18, 2026
f5d1718
test(chat): cover retrying a photo whose message is still queued
Mamel0uk Nov 18, 2026
4ee4e3a
docs(chat): describe the pending photo and retries in sendPhoto
Mamel0uk Nov 18, 2026
cc55d42
fix(chat): wait for a queued photo message seen in the server read
Mamel0uk Nov 18, 2026
d3748b4
test(chat): cover retries that find a queued write on the server read
Mamel0uk Nov 18, 2026
f5d14c9
fix(chat): accept a refused photo mark once the server has it
Mamel0uk Nov 18, 2026
f9b1749
test(chat): cover a photo mark sent twice
Mamel0uk Nov 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/src/main/java/com/android/polybazaar/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ class MainActivity : ComponentActivity() {
remember { FilePhotoStorage(applicationContext, FilePhotoStorage.CREATE_FORM) },
editPhotoStorage =
remember { FilePhotoStorage(applicationContext, FilePhotoStorage.EDIT_FORM) },
chatRepository = remember { FirebaseChatRepository() },
chatRepository =
remember {
FirebaseChatRepository(JpegPhotoCompressor(applicationContext.contentResolver))
},
bookingRepository = remember { FirebaseBookingRepository() },
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import java.time.Instant
* @property text trimmed message text, or "" for a photo-only message.
* @property sentAt when the message was sent.
* @property imagePath Storage path of the message's photo, or null for a text message.
* @property photoPending true while the sender has not finished uploading the photo: it can't be
* downloaded yet. It stays true if the upload never ends, as when the sender's app was closed.
*/
data class ChatMessage(
val id: String,
val senderUid: String,
val text: String,
val sentAt: Instant,
val imagePath: String? = null,
val photoPending: Boolean = false,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Co-authored-by: Claude Code (Claude Opus 5.5)
package com.android.polybazaar.chat.model

import android.net.Uri
import kotlinx.coroutines.flow.Flow

interface ChatRepository {
Expand All @@ -22,6 +23,54 @@ interface ChatRepository {
onWriteFailure: (Exception) -> Unit = {},
)

/**
* Returns a new message ID for the conversation with this ID, without any network access. Keep it
* to retry a failed [sendPhoto] with the same ID.
*/
fun newMessageId(conversationId: String): String

/**
* Sends a photo message from the signed-in participant, and returns once the server has the
* message and the photo, so it needs a connection. The photo is compressed first. Storage only
* accepts the photo once its message is on the server, so the message and the conversation update
* are written next, with [ChatMessage.photoPending] true; then the photo is uploaded, and the
* message is marked as no longer pending. Until then, the other participant sees the message with
* its photo pending, which can't be downloaded yet.
*
* Throws when signed out (UNAUTHENTICATED), when offline or when the send takes too long (an
* error that maps to NETWORK with toAppError), when the server rejects a write (a Firestore
* error) or the upload (a Storage error), when the photo can't be read, as when it was deleted or
* is not an image (an AppException with NOT_FOUND), and IllegalArgumentException when the user is
* not a participant, when the conversation has no message yet (a photo cannot open a
* conversation), or when the caption is not sendable.
*
* Signed out, offline when the send starts, for an unreadable photo, or for an
* IllegalArgumentException, nothing is written. Any other failure can leave the message written,
* or queued on this device to be written once the connection is back, with its photo pending.
* Keep the [messageId] after a failure and retry with it: a retry waits for a queued message
* rather than writing it again, uploads the photo only if it is not stored yet, and marks it
* uploaded. A message whose send is never retried, as when the app is closed, stays pending.
*
* @param conversation the conversation from [observeConversation].
* @param messageId from [newMessageId]; pass the same one to retry a failed send.
* @param caption optional text sent with the photo; null or blank sends the photo alone, which
* previews as an empty lastMessageText.
*/
suspend fun sendPhoto(
conversation: Conversation,
messageId: String,
photo: Uri,
caption: String? = null,
)

/**
* Downloads the photo of a message, from its [ChatMessage.imagePath], as the signed-in user.
* Fails with PERMISSION_DENIED for a user who is not a participant of the conversation, with an
* AppException with NETWORK when it takes too long, as offline, and with a Storage error to map
* with toAppError otherwise.
*/
suspend fun loadMessagePhoto(imagePath: String): ByteArray

/**
* Marks the conversation with this ID as read by the signed-in user: sets only their lastReadAt
* to the server time. Like [sendMessage], the write is queued without waiting for the server, so
Expand Down
Loading
Loading