Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.android.polybazaar.chat.model.ChatRepository
import com.android.polybazaar.chat.model.ChatRules
import com.android.polybazaar.chat.model.Conversation
import com.android.polybazaar.chat.model.conversationId
import com.android.polybazaar.connectivity.model.ConnectivityRepository
import com.android.polybazaar.error.model.AppError
import com.android.polybazaar.error.model.toAppError
import com.android.polybazaar.listing.model.Listing
Expand All @@ -27,6 +28,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -142,6 +144,9 @@ private data class LoadedListing(
* [photoUploadScope]. Once the screen is closed, an upload that ends deletes its photo if it was
* taken with the camera, whether it was sent or not, as nothing can retry it any more.
*
* [connectivity] tells when the device is back online, to send again the photos that failed for
* want of a connection while the screen is open. Pass the app's single [ConnectivityRepository].
*
* The lender must only be routed here for a conversation that already exists: until then the
* security rules refuse them the conversation and its messages, which shows as a load error.
*/
Expand All @@ -155,6 +160,7 @@ class ConversationViewModel(
private val photoStorage: PhotoStorage,
private val savedStateHandle: SavedStateHandle,
private val uploadScope: CoroutineScope,
private val connectivity: ConnectivityRepository,
) : ViewModel() {

private val _uiState = MutableStateFlow(ConversationUiState(myUid = myUid))
Expand Down Expand Up @@ -197,6 +203,27 @@ class ConversationViewModel(
observeConversation()
observeMessages()
observeOtherUser()
retryPhotosWhenOnline()
}

/**
* Each time the device comes back online, sends again the photos that failed with
* [AppError.NETWORK]. Other failures, such as a refusal, would fail again, so they wait for the
* user's Retry.
*
* [ConnectivityRepository.isOnline] also gives its current value first, but no photo has been
* sent yet when the screen opens, so that first value sends nothing again.
*/
private fun retryPhotosWhenOnline() {
viewModelScope.launch {
connectivity.isOnline
.filter { online -> online }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: The KDoc says "each time the device comes back online", but the flow's first value also passes this filter, so opening the screen while online resends the photos that failed with a network error right away. That seems useful (for example photos restored after the app was killed), but could the comment say so, so it doesn't look accidental?

@Mamel0uk Mamel0uk Nov 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pending photos aren't restored after the app is killed, they're only in the ViewModel and start empty, so that first value never resends anything. I put that in the KDoc (e8bdbf4).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks for checking, the KDoc makes it clear now.

.collect {
_uiState.value.pendingPhotos
.filter { it.status == PendingPhotoStatus.FAILED && it.error == AppError.NETWORK }
.forEach { retryPhoto(it.messageId) }
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ object AppNavHostTags {
* folder than [photoStorage]: opening a form deletes the photos its folder holds for other forms.
* @param locationRepository reads the device's approximate location: to centre the Map tab on the
* renter, and to open the listing form's map picker where the lender is.
* @param connectivityRepository tells the listing forms whether the device is online.
* @param connectivityRepository tells the listing forms and the conversations whether the device is
* online.
*/
@Composable
fun AppNavHost(
Expand Down Expand Up @@ -535,6 +536,7 @@ fun AppNavHost(
photoStorage = chatPhotoStorage,
savedStateHandle = createSavedStateHandle(),
uploadScope = photoUploadScope,
connectivity = connectivityRepository,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import com.android.polybazaar.chat.viewmodel.ConversationUiState
import com.android.polybazaar.chat.viewmodel.ConversationViewModel
import com.android.polybazaar.chat.viewmodel.PendingPhoto
import com.android.polybazaar.chat.viewmodel.PendingPhotoStatus
import com.android.polybazaar.connectivity.model.FakeConnectivityRepository
import com.android.polybazaar.error.model.AppError
import com.android.polybazaar.listing.model.FakeListingRepository
import com.android.polybazaar.listing.model.FilePhotoStorage
Expand Down Expand Up @@ -243,6 +244,7 @@ class ConversationScreenTest {
photoStorage = photoStorage,
savedStateHandle = SavedStateHandle(),
uploadScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate),
connectivity = FakeConnectivityRepository(),
)
val launcherOwner =
object : ActivityResultRegistryOwner {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.android.polybazaar.chat.model.ChatRepository
import com.android.polybazaar.chat.model.ChatRules
import com.android.polybazaar.chat.model.Conversation
import com.android.polybazaar.chat.model.FakeChatRepository
import com.android.polybazaar.connectivity.model.FakeConnectivityRepository
import com.android.polybazaar.error.model.AppError
import com.android.polybazaar.error.model.AppException
import com.android.polybazaar.listing.model.FakeListingRepository
Expand Down Expand Up @@ -55,6 +56,7 @@ class ConversationViewModelTest {
private lateinit var profiles: FakeProfileRepository
private lateinit var listings: FakeListingRepository
private lateinit var photoStorage: FakePhotoStorage
private lateinit var connectivity: FakeConnectivityRepository
private lateinit var downloads: CountingDownloads

private val lender = User(uid = "lender", email = "", username = "Lena")
Expand Down Expand Up @@ -90,6 +92,7 @@ class ConversationViewModelTest {
profiles = FakeProfileRepository(listOf(lender, renter).associateBy { it.uid })
listings = FakeListingRepository(allListings = listOf(listing))
photoStorage = FakePhotoStorage()
connectivity = FakeConnectivityRepository()
}

@After
Expand Down Expand Up @@ -117,6 +120,7 @@ class ConversationViewModelTest {
photoStorage,
savedState,
uploadScope,
connectivity,
)

/** Creates the ViewModel and lets its loads finish. */
Expand Down Expand Up @@ -1756,6 +1760,78 @@ class ConversationViewModelTest {
assertEquals(listOf(conversation.id to "m1"), chat.deletedPhotoMessages)
}

@Test
fun backOnline_sendsANetworkFailureAgainWithTheSameId() {
connectivity.isOnline.value = false
val viewModel = failedPhotoSend(offline)
chat.sendFailure = null

connectivity.isOnline.value = true
advance()

assertEquals(listOf("m1", "m1"), chat.sentPhotos.map { it.messageId })
assertEquals(emptyList<PendingPhoto>(), viewModel.uiState.value.pendingPhotos)
}

@Test
fun backOnline_leavesARefusedPhotoForTheUsersRetry() {
connectivity.isOnline.value = false
val viewModel = failedPhotoSend(refused)

connectivity.isOnline.value = true
advance()

assertEquals(1, chat.sentPhotos.size)
assertEquals(AppError.PERMISSION_DENIED, viewModel.uiState.value.pendingPhotos.single().error)
}

@Test
fun backOnline_leavesAPhotoStillSendingAlone() {
connectivity.isOnline.value = false
val viewModel = inConversation()
chat.gate = CompletableDeferred()
viewModel.attachPhoto(galleryPhoto)
viewModel.send()
advance()

connectivity.isOnline.value = true
advance()

assertEquals(1, chat.sentPhotos.size)
assertEquals(
listOf(PendingPhotoStatus.UPLOADING),
viewModel.uiState.value.pendingPhotos.map { it.status },
)
}

@Test
fun stillOffline_sendsNothingAgain() {
connectivity.isOnline.value = false
val viewModel = failedPhotoSend(offline)

connectivity.isOnline.value = false
advance()

assertEquals(1, chat.sentPhotos.size)
assertEquals(PendingPhotoStatus.FAILED, viewModel.uiState.value.pendingPhotos.single().status)
}

@Test
fun backOnlineAgain_afterASecondNetworkFailure_sendsItAgain() {
connectivity.isOnline.value = false
failedPhotoSend(offline)
connectivity.isOnline.value = true
advance()
assertEquals(2, chat.sentPhotos.size)

connectivity.isOnline.value = false
advance()
connectivity.isOnline.value = true
advance()

assertEquals(listOf("m1", "m1", "m1"), chat.sentPhotos.map { it.messageId })
}

private val photoPath = "chats/drill_renter/m1.jpg"
private val photoBytes = byteArrayOf(1, 2, 3)
private val missing = AppException(AppError.NOT_FOUND)
Expand Down
Loading