Skip to content

Commit

Permalink
[23125] Working real time indicator in the occupancy indicator in the…
Browse files Browse the repository at this point in the history
… list

# Conflicts:
#	TripKitAndroidUI/src/main/res/values/strings.xml
  • Loading branch information
sg-jsonjuliane committed Jan 16, 2025
1 parent 2782e31 commit 8485fe8
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ class ServiceDetailFragment : BaseTripKitFragment() {
mapContributor.setStop(stop)
mapContributor.setService(timetableEntry)
}

(mapContributor as? TimetableMapContributor)?.let { contributor ->
contributor.formattedElapsedTime.observe(viewLifecycleOwner) { formattedTime ->
// Handle the formatted time in the fragment
handleFormattedElapsedTime(formattedTime)
}
}
}

private fun handleFormattedElapsedTime(formattedTime: String) {
viewModel.apply {
lastUpdatedText.set(formattedTime)
}
}

class Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class ServiceDetailViewModel @Inject constructor(

val showOccupancyInfo = ObservableBoolean(false)

val lastUpdatedText = ObservableField<String>()

val itemBinding = ItemBinding.of<ServiceDetailItemViewModel>(
BR.viewModel,
R.layout.service_detail_fragment_list_item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModelProviders
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
Expand Down Expand Up @@ -81,6 +83,9 @@ class TimetableMapContributor(val fragment: Fragment) : TripKitMapContributor {
@Inject
lateinit var serviceStopCalloutAdapter: ServiceStopInfoWindowAdapter

private val _formattedElapsedTime = MutableLiveData<String>()
val formattedElapsedTime: LiveData<String> get() = _formattedElapsedTime

private var mStop: ScheduledStop? = null
private var service: TimetableEntry? = null
private var realTimeVehicleMarker: Marker? = null
Expand Down Expand Up @@ -365,14 +370,18 @@ class TimetableMapContributor(val fragment: Fragment) : TripKitMapContributor {
val currentTimeMillis = System.currentTimeMillis()
val lastUpdateTimeMillis = realTimeVehicle.lastUpdateTime // Already in milliseconds
val ageInSeconds =
((currentTimeMillis - lastUpdateTimeMillis) / 1000).coerceAtLeast(1) // Start from 1 second
((currentTimeMillis - lastUpdateTimeMillis) / 1000) // Start from 1 second

// Calculate age factor and fade level
val ageFactor = calculateAgeFactor(ageInSeconds)
val fadeLevel = calculateFadeFromAgeFactor(ageFactor)

// Update marker opacity and snippet
updateMarkerOpacity(marker, fadeLevel)

// Post the formatted elapsed time
_formattedElapsedTime.postValue(formatElapsedTime(ageInSeconds))

marker.snippet = formatElapsedTime(ageInSeconds, realTimeVehicle)

if (ageFactor < 0.1f) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,26 +169,21 @@ object MapUtils {
* Formats elapsed time into a human-readable string (e.g., "15 seconds ago" or "1 minute and 15 seconds ago").
*
* @param ageInSeconds The age of the data in seconds.
* @param vehicle The RealTimeVehicle object to include its label in the message.
* @param vehicle Optional RealTimeVehicle object. If provided, its label will be included in the message.
* @return The formatted elapsed time string.
*/
@SuppressLint("DefaultLocale")
fun formatElapsedTime(ageInSeconds: Long, vehicle: RealTimeVehicle): String {
fun formatElapsedTime(ageInSeconds: Long, vehicle: RealTimeVehicle? = null): String {
val prefix = vehicle?.label?.let { "Vehicle $it updated" } ?: "Last updated:"
return if (ageInSeconds < 60) {
"Vehicle ${vehicle.label} updated ${formatTimeUnit(ageInSeconds, "second")} ago"
"$prefix ${formatTimeUnit(ageInSeconds, "second")} ago"
} else {
val minutes = ageInSeconds / 60
val seconds = ageInSeconds % 60

if (seconds == 0L) {
"Vehicle ${vehicle.label} updated ${formatTimeUnit(minutes, "minute")} ago"
"$prefix ${formatTimeUnit(minutes, "minute")} ago"
} else {
"Vehicle ${vehicle.label} updated ${
formatTimeUnit(
minutes,
"minute"
)
} and ${formatTimeUnit(seconds, "second")} ago"
"$prefix ${formatTimeUnit(minutes, "minute")} and ${formatTimeUnit(seconds, "second")} ago"
}
}
}
Expand All @@ -201,7 +196,7 @@ object MapUtils {
* @return A formatted string with singular or plural unit (e.g., "1 second", "15 seconds").
*/
private fun formatTimeUnit(value: Long, unit: String): String {
return "$value $unit${if (value != 1L) "s" else ""}"
return "$value $unit${if (value > 1L) "s" else ""}"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@
app:layout_constraintTop_toTopOf="@+id/occupancyView"
tools:text="Label O" />

<TextView
android:id="@+id/tvLastUpdatedLabel"
style="@style/TextAppearance.MaterialComponents.Caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{viewModel.lastUpdatedText}"
android:textColor="@color/black1"
android:layout_marginTop="4dp"
app:layout_constraintStart_toStartOf="@id/occupancyLabel"
app:layout_constraintTop_toBottomOf="@+id/occupancyLabel"
tools:text="Label O" />

<ImageView
android:id="@+id/expandButton"
android:layout_width="@dimen/icon_medium"
Expand Down
1 change: 1 addition & 0 deletions TripKitAndroidUI/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -619,5 +619,6 @@ We aggregate the anonymised data and provide it to researchers, regulators, and
<string name="confirmation_allow_background_location_title">Background location access</string>
<string name="confirmation_allow_background_location_message">"To enable background location, we'll redirect you to the app settings. Once there, go to: Permissions > Location > Allow all the time, then try again."</string>
<string name="terms_of_use">Terms of Use</string>
<string name="label_last_updated">Last Updated:</string>
<string name="route_not_supported">Routing from %s to %s is not yet supported</string>
</resources>
6 changes: 6 additions & 0 deletions TripKitAndroidUI/src/main/res/xml/service_detail_motion.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
android:alpha="0"
app:visibilityMode="ignore" />
</Constraint>
<Constraint android:id="@+id/tvLastUpdatedLabel">
<PropertySet android:visibility="gone" />
</Constraint>
<Constraint
android:id="@+id/occupancyView"
app:visibilityMode="ignore">
Expand Down Expand Up @@ -72,6 +75,9 @@
<Constraint android:id="@+id/occupancyLabel">
<PropertySet android:alpha="1" />
</Constraint>
<Constraint android:id="@+id/tvLastUpdatedLabel">
<PropertySet android:visibility="visible" />
</Constraint>
<Constraint android:id="@+id/occupancyView">
<Layout
android:layout_width="@dimen/service_detail_icon_size"
Expand Down

0 comments on commit 8485fe8

Please sign in to comment.