Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
eca904a
feat(listing): name a location after the campus it is on
thanna23 Nov 17, 2026
98ad572
feat(listing): refuse a location with a blank name and trim it
thanna23 Nov 17, 2026
bc89ebd
feat(listing): set, rename and clear the location in the listing form
thanna23 Nov 17, 2026
8dcfa8d
docs(listing): update why an invalid stored location reads as none
thanna23 Nov 17, 2026
62728eb
feat(connectivity): know whether the device can reach the internet
thanna23 Nov 17, 2026
69be9ba
feat(listing): follow whether the device is online in the listing form
thanna23 Nov 17, 2026
03fc642
chore: merge main into feature/listing-location-form-state
thanna23 Nov 19, 2026
ef028f4
revert: stop following connectivity in the listing form
thanna23 Nov 19, 2026
3115d5f
revert: remove the connectivity repository from this PR
thanna23 Nov 19, 2026
4cfcb4e
docs(map): say the listing form keeps the approximate location on the…
thanna23 Nov 19, 2026
e9b4d50
style(map): format the LocationRepository KDoc with ktfmt
thanna23 Nov 19, 2026
a6742c5
fix(listing): flag a missing location name only once its field is left
thanna23 Nov 19, 2026
3b6f370
feat(connectivity): know whether the device can reach the internet
thanna23 Nov 17, 2026
c38df59
feat(listing): follow whether the device is online in the listing form
thanna23 Nov 17, 2026
fbbca9a
chore: merge main into feature/listing-connectivity
thanna23 Nov 19, 2026
d982d95
fix(connectivity): stay online when a previous default network is lost
thanna23 Nov 19, 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.test.core.app.ApplicationProvider
import com.android.polybazaar.auth.viewmodel.AuthViewModel
import com.android.polybazaar.booking.model.FakeBookingRepository
import com.android.polybazaar.chat.model.FakeChatRepository
import com.android.polybazaar.connectivity.model.FakeConnectivityRepository
import com.android.polybazaar.listing.model.FakeListingRepository
import com.android.polybazaar.listing.model.FilePhotoStorage
import com.android.polybazaar.map.model.FakeLocationRepository
Expand Down Expand Up @@ -59,6 +60,7 @@ abstract class PolyBazaarTestCase :
chatRepository = FakeChatRepository(),
bookingRepository = FakeBookingRepository(),
locationRepository = FakeLocationRepository(),
connectivityRepository = FakeConnectivityRepository(),
navController = rememberNavController(),
)
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
keeps it on the device, in its saved state, so the picker still opens there after the app
is killed. Without it the user picks their campus instead. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- To know when the device is offline, as choosing a listing's location on the map needs it. -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:allowBackup="true"
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/com/android/polybazaar/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import com.android.polybazaar.auth.model.FirebaseAuthRepository
import com.android.polybazaar.auth.viewmodel.AuthViewModel
import com.android.polybazaar.booking.model.FirebaseBookingRepository
import com.android.polybazaar.chat.model.FirebaseChatRepository
import com.android.polybazaar.connectivity.model.AndroidConnectivityRepository
import com.android.polybazaar.connectivity.model.ConnectivityRepository
import com.android.polybazaar.listing.model.FilePhotoStorage
import com.android.polybazaar.listing.model.FirebaseListingRepository
import com.android.polybazaar.map.model.FusedLocationRepository
Expand Down Expand Up @@ -70,9 +72,22 @@ class MainActivity : ComponentActivity() {
LocationServices.getFusedLocationProviderClient(applicationContext)
)
},
connectivityRepository = connectivityRepository(),
)
}
}
}
}

/** The app's [ConnectivityRepository], created on first use. */
private fun connectivityRepository(): ConnectivityRepository =
connectivity ?: AndroidConnectivityRepository(applicationContext).also { connectivity = it }

private companion object {
/**
* One for the whole process, never released: the listing forms' ViewModels outlive a rotated
* activity and keep using it, and its network callback is registered only once.
*/
var connectivity: ConnectivityRepository? = null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Co-authored-by: Claude Code (Claude Opus 5.5)
package com.android.polybazaar.connectivity.model

import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow

/** Whether the device can reach the internet, for features that need it (#339). */
interface ConnectivityRepository {
/** True while the device has a network that reaches the internet. */
val isOnline: StateFlow<Boolean>
}

/**
* A [ConnectivityRepository] that follows Android's default network.
*
* Its callback is never unregistered: create one per process, with the application context, and
* share it, so it lives as long as the app does.
*/
class AndroidConnectivityRepository(context: Context) : ConnectivityRepository {

private val connectivityManager =
context.applicationContext.getSystemService(ConnectivityManager::class.java)

private val _isOnline =
MutableStateFlow(
reachesInternet(
connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
)
)
override val isOnline: StateFlow<Boolean> = _isOnline.asStateFlow()

/** The default network the callback last reported, or null once it is lost. */
@Volatile private var defaultNetwork: Network? = connectivityManager.activeNetwork

init {
connectivityManager.registerDefaultNetworkCallback(
object : ConnectivityManager.NetworkCallback() {
// Also called right after a new default network becomes available.
override fun onCapabilitiesChanged(network: Network, capabilities: NetworkCapabilities) {
defaultNetwork = network
_isOnline.value = reachesInternet(capabilities)
}

// During a Wi-Fi/mobile handoff the old network can be lost after the new one became
// the default: only losing the current default means the device is offline.
override fun onLost(network: Network) {
Comment thread
adrimikh marked this conversation as resolved.
Resolved
if (network != defaultNetwork) return
defaultNetwork = null
_isOnline.value = false
}
}
)
}

/** Whether [capabilities] reach the internet: validated, so not stuck behind a login page. */
private fun reachesInternet(capabilities: NetworkCapabilities?): Boolean =
capabilities != null &&
capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) &&
capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.android.polybazaar.auth.model.AuthRules
import com.android.polybazaar.booking.model.upcomingDays
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.ItemListing
Expand Down Expand Up @@ -66,6 +67,8 @@ import kotlinx.coroutines.launch
* until a Retry succeeds.
* @property savedListing the saved listing, until the screen has handled it.
* @property saveError why the last save failed, or null.
* @property isOnline whether the device is online, which choosing a location on the map needs.
* Followed live, never saved with the form.
*/
data class ListingFormUiState(
val today: LocalDate,
Expand All @@ -90,6 +93,7 @@ data class ListingFormUiState(
val savedListing: Listing? = null,
val isSaving: Boolean = false,
val saveError: AppError? = null,
val isOnline: Boolean = true,
) {
/** Whether this form edits a saved listing rather than creating one. */
val isEditing: Boolean
Expand Down Expand Up @@ -187,6 +191,8 @@ private fun formFor(listing: Listing, today: LocalDate) =
* camera app is in front. Pass `createSavedStateHandle()` from the factory: a handle made by hand
* is not saved by Android.
* @param locationRepository finds the lender, to open the map picker where they are.
* @param connectivityRepository tells whether the device is online, see
* [ListingFormUiState.isOnline].
* @param today the current day, to leave past available days out of an edited listing.
*/
class ListingFormViewModel(
Expand All @@ -195,10 +201,16 @@ class ListingFormViewModel(
private val photoStorage: PhotoStorage,
private val savedStateHandle: SavedStateHandle,
private val locationRepository: LocationRepository,
connectivityRepository: ConnectivityRepository,
private val today: () -> LocalDate = LocalDate::now,
) : ViewModel() {

private val _uiState = MutableStateFlow(savedStateHandle.restoreForm(listingId, today()))
private val _uiState =
MutableStateFlow(
savedStateHandle
.restoreForm(listingId, today())
.copy(isOnline = connectivityRepository.isOnline.value)
)
val uiState: StateFlow<ListingFormUiState> = _uiState.asStateFlow()

/**
Expand Down Expand Up @@ -236,6 +248,11 @@ class ListingFormViewModel(
photoStorage.deleteAllExcept(_uiState.value.photos + listOfNotNull(pendingPhoto))
// A restored edit form holds the user's changes, which loading the listing would overwrite.
if (listingId != null && savedStateHandle.get<Boolean>(LOADED) != true) loadListing()
viewModelScope.launch {
connectivityRepository.isOnline.collect { online ->
_uiState.update { it.copy(isOnline = online) }
}
}
}

/**
Expand All @@ -253,7 +270,9 @@ class ListingFormViewModel(
.getListing(id)
.onSuccess { listing ->
savedStateHandle[LOADED] = true
updateState { formFor(listing, today()).copy(isLoading = true) }
updateState {
formFor(listing, today()).copy(isLoading = true, isOnline = it.isOnline)
}
}
.onFailure { exception ->
// Cancellation means the screen was closed: it is not an error to show.
Expand Down Expand Up @@ -447,7 +466,7 @@ class ListingFormViewModel(
val form =
if (state.isEditing) formFor(savedListing, today())
else ListingFormUiState(today = today())
form.copy(savedListing = savedListing)
form.copy(savedListing = savedListing, isOnline = it.isOnline)
}
} catch (exception: Exception) {
if (exception is CancellationException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import com.android.polybazaar.chat.ui.InboxScreen
import com.android.polybazaar.chat.viewmodel.ConversationViewModel
import com.android.polybazaar.chat.viewmodel.InboxViewModel
import com.android.polybazaar.chat.viewmodel.photoUploadScope
import com.android.polybazaar.connectivity.model.ConnectivityRepository
import com.android.polybazaar.error.model.AppError
import com.android.polybazaar.error.ui.message
import com.android.polybazaar.listing.model.FilePhotoStorage
Expand Down Expand Up @@ -121,6 +122,7 @@ object AppNavHostTags {
* @param editPhotoStorage keeps the photos added to the listing being edited. It must use another
* folder than [photoStorage]: opening a form deletes the photos its folder holds for other forms.
* @param locationRepository finds the lender, to open the listing form's map picker where they are.
* @param connectivityRepository tells the listing forms whether the device is online.
*/
@Composable
fun AppNavHost(
Expand All @@ -133,6 +135,7 @@ fun AppNavHost(
chatRepository: ChatRepository,
bookingRepository: BookingRepository,
locationRepository: LocationRepository,
connectivityRepository: ConnectivityRepository,
navController: NavHostController = rememberNavController(),
) {
val uiState by authViewModel.uiState.collectAsState()
Expand Down Expand Up @@ -304,6 +307,7 @@ fun AppNavHost(
photoStorage = photoStorage,
savedStateHandle = createSavedStateHandle(),
locationRepository = locationRepository,
connectivityRepository = connectivityRepository,
)
}
}
Expand Down Expand Up @@ -331,6 +335,7 @@ fun AppNavHost(
photoStorage = editPhotoStorage,
savedStateHandle = createSavedStateHandle(),
locationRepository = locationRepository,
connectivityRepository = connectivityRepository,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Co-authored-by: Claude Code (Claude Opus 5.5)
package com.android.polybazaar.connectivity.model

import kotlinx.coroutines.flow.MutableStateFlow

/** A [ConnectivityRepository] for tests: set [isOnline] to go offline or back online. */
class FakeConnectivityRepository(online: Boolean = true) : ConnectivityRepository {
override val isOnline = MutableStateFlow(online)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Co-authored-by: Claude Code (Claude Opus 5.5)
package com.android.polybazaar.connectivity.model

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Shadows.shadowOf
import org.robolectric.shadows.ShadowNetwork
import org.robolectric.shadows.ShadowNetworkCapabilities

/** [AndroidConnectivityRepository] on Robolectric's [ConnectivityManager]. */
@RunWith(AndroidJUnit4::class)
class AndroidConnectivityRepositoryTest {

private val context: Context = ApplicationProvider.getApplicationContext()
private val connectivityManager = context.getSystemService(ConnectivityManager::class.java)

private fun capabilities(vararg capabilities: Int): NetworkCapabilities =
ShadowNetworkCapabilities.newInstance().also { network ->
capabilities.forEach { shadowOf(network).addCapability(it) }
}

private val validated =
capabilities(
NetworkCapabilities.NET_CAPABILITY_INTERNET,
NetworkCapabilities.NET_CAPABILITY_VALIDATED,
)

/** Gives the active network [capabilities], then creates the repository. */
private fun repositoryOn(capabilities: NetworkCapabilities?): AndroidConnectivityRepository {
shadowOf(connectivityManager)
.setNetworkCapabilities(connectivityManager.activeNetwork, capabilities)
return AndroidConnectivityRepository(context)
}

@Test
fun startsOffline_withoutAValidatedInternetNetwork() {
// Behind a login page, the network has the internet capability but is not validated.
assertFalse(
repositoryOn(capabilities(NetworkCapabilities.NET_CAPABILITY_INTERNET)).isOnline.value
)
assertFalse(repositoryOn(null).isOnline.value)
}

@Test
fun followsTheDefaultNetwork() {
val repository = repositoryOn(validated)
val callback = shadowOf(connectivityManager).networkCallbacks.single()
val network = connectivityManager.activeNetwork!!
assertTrue(repository.isOnline.value)

callback.onLost(network)
assertFalse(repository.isOnline.value)

callback.onCapabilitiesChanged(network, validated)
assertTrue(repository.isOnline.value)
}

@Test
fun staysOnline_whenThePreviousNetworkIsLostAfterAHandoff() {
val repository = repositoryOn(validated)
val callback = shadowOf(connectivityManager).networkCallbacks.single()
val wifi = connectivityManager.activeNetwork!!
val mobile = ShadowNetwork.newInstance(shadowOf(wifi).netId + 1)

// Mobile becomes the default, validated, before Wi-Fi's loss arrives.
callback.onCapabilitiesChanged(mobile, validated)
callback.onLost(wifi)
assertTrue(repository.isOnline.value)

callback.onLost(mobile)
assertFalse(repository.isOnline.value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.polybazaar.R
import com.android.polybazaar.booking.ui.AvailabilityCalendarSheetTags
import com.android.polybazaar.booking.ui.AvailabilityCalendarTags
import com.android.polybazaar.connectivity.model.FakeConnectivityRepository
import com.android.polybazaar.listing.model.FilePhotoStorage
import com.android.polybazaar.listing.model.ItemListing
import com.android.polybazaar.listing.model.Listing
Expand Down Expand Up @@ -99,6 +100,7 @@ class CreateListingScreenTest {
createStorage(),
SavedStateHandle(),
FakeLocationRepository(),
FakeConnectivityRepository(),
today,
)
setContentWithRegistry(registry) {
Expand Down Expand Up @@ -887,6 +889,7 @@ class CreateListingScreenTest {
createStorage(),
SavedStateHandle(),
FakeLocationRepository(),
FakeConnectivityRepository(),
)
var createdCount = 0
val owner =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.polybazaar.R
import com.android.polybazaar.booking.model.formatRuns
import com.android.polybazaar.booking.model.groupIntoRuns
import com.android.polybazaar.connectivity.model.FakeConnectivityRepository
import com.android.polybazaar.listing.model.FakeListingRepository
import com.android.polybazaar.listing.model.FilePhotoStorage
import com.android.polybazaar.listing.model.ItemListing
Expand Down Expand Up @@ -99,6 +100,7 @@ class EditListingScreenTest {
FilePhotoStorage(context, FilePhotoStorage.EDIT_FORM),
SavedStateHandle(),
FakeLocationRepository(),
FakeConnectivityRepository(),
)
composeTestRule.setContent {
EditListingScreen(
Expand Down Expand Up @@ -343,6 +345,7 @@ class EditListingScreenTest {
FilePhotoStorage(context, FilePhotoStorage.EDIT_FORM),
SavedStateHandle(),
FakeLocationRepository(),
FakeConnectivityRepository(),
)
composeTestRule.setContent {
dispatcher = LocalOnBackPressedDispatcherOwner.current!!.onBackPressedDispatcher
Expand Down
Loading
Loading