Skip to content

Commit

Permalink
Merge pull request #166 from skedgo/task/14628-regions-json-default-city
Browse files Browse the repository at this point in the history
[14628] Redo handling for regions json default city
  • Loading branch information
sg-jsonjuliane authored Dec 13, 2024
2 parents 9c86259 + f3b4f52 commit 313ec16
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.SharedPreferences
import com.skedgo.tripkit.camera.CachingDateTimeOfMapCameraPositionRepository
import com.skedgo.tripkit.camera.LastCameraPositionRepository
import com.skedgo.tripkit.data.regions.RegionService
import com.skedgo.tripkit.ui.data.cameraposition.CachingDateTimeOfMapCameraPositionRepositoryImpl
import com.skedgo.tripkit.ui.data.cameraposition.LastCameraPositionRepositoryImpl
import dagger.Module
Expand All @@ -13,11 +14,12 @@ import java.util.Locale
@Module
class CameraPositionDataModule {
@Provides
fun lastCameraPositionRepository(context: Context): LastCameraPositionRepository =
fun lastCameraPositionRepository(context: Context, regionService: RegionService): LastCameraPositionRepository =
LastCameraPositionRepositoryImpl(
context.resources,
getMapPrefs(context),
Locale.getDefault()
Locale.getDefault(),
regionService
)

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.content.SharedPreferences
import android.content.res.Resources
import com.skedgo.tripkit.camera.LastCameraPositionRepository
import com.skedgo.tripkit.camera.MapCameraPosition
import com.skedgo.tripkit.common.model.region.Region
import com.skedgo.tripkit.data.regions.RegionService
import com.skedgo.tripkit.ui.data.R
import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers
Expand All @@ -22,7 +24,8 @@ internal const val LON_US = -97.3880223557353f
open class LastCameraPositionRepositoryImpl(
private val resources: Resources,
private val preferences: SharedPreferences,
private val locale: Locale
private val locale: Locale,
private val regionService: RegionService
) : LastCameraPositionRepository {
override fun putMapCameraPosition(mapCameraPosition: MapCameraPosition?): Observable<MapCameraPosition?> {
// FIXME: Remove nullity of `mapCameraPosition`.
Expand Down Expand Up @@ -72,12 +75,54 @@ open class LastCameraPositionRepositoryImpl(
.subscribeOn(Schedulers.computation())

private fun getDefaultMapCameraPositionSync(): MapCameraPosition {
return if (resources.getBoolean(R.bool.trip_kit_map_override_default_latlon)) {
getOverriddenMapCameraPosition()
return if (!resources.getBoolean(R.bool.trip_kit_map_override_default_latlon)) {
// First, try to get a matching region based on the city name
val matchingRegion = executeWithCityString(resources.getString(R.string.default_city)).blockingFirst()
if (matchingRegion != null) {
// Return the map position based on the matching city's lat/lng
val matchingCity = matchingRegion.cities?.firstOrNull {
it.name?.contains(resources.getString(R.string.default_city), ignoreCase = true) == true
}
// Fetch zoom from resources, falling back to default if empty
var zoom = resources.getString(R.string.trip_kit_map_default_zoom)
if (zoom.isEmpty()) zoom = "3"
// Return the map position based on the matching city's lat/lng and zoom
MapCameraPosition(
matchingCity?.lat ?: LAT_US.toDouble(),
matchingCity?.lon ?: LON_US.toDouble(),
zoom.toFloat(),
0f,
0f
)
} else {
// If no match, fallback to overridden position
getOverriddenMapCameraPosition()
}
} else {
getMapCameraPositionByLocaleSync()
}
}
// Method to get the single region that matches the city name closest
private fun executeWithCityString(defaultCity: String): Observable<Region> {
return regionService.getRegionsAsync()
.flatMap { regions ->
val matchingRegion = regions.firstOrNull { region ->
region.hasCityMatching(defaultCity)
}
if (matchingRegion != null) {
Observable.just(matchingRegion)
} else {
// Fallback to getOverriddenMapCameraPosition()
Observable.empty()
}
}
}
// Extension function to check if the region has a city that matches the city name
private fun Region.hasCityMatching(cityName: String): Boolean {
return cities?.firstOrNull {
it.name?.contains(cityName, ignoreCase = true) == true
} != null
}

override fun getDefaultMapCameraPosition(): Observable<MapCameraPosition> =
Observable.fromCallable { getDefaultMapCameraPositionSync() }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="default_city">Las Vegas, NV, USA</string>
</resources>

0 comments on commit 313ec16

Please sign in to comment.