Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve map style url from .well-known #5176

Merged
merged 2 commits into from
Feb 8, 2022
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
1 change: 1 addition & 0 deletions changelog.d/5175.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Retrieve map style url from .well-known
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ class LocationPreviewFragment @Inject constructor(

mapView = WeakReference(views.mapView)
views.mapView.onCreate(savedInstanceState)
views.mapView.initialize(urlMapProvider.mapUrl)
loadPinDrawable()

lifecycleScope.launchWhenCreated {
views.mapView.initialize(urlMapProvider.getMapUrl())
loadPinDrawable()
}
}

override fun onResume() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isGone
import androidx.lifecycle.lifecycleScope
import com.airbnb.mvrx.fragmentViewModel
import com.airbnb.mvrx.withState
import com.google.android.material.dialog.MaterialAlertDialogBuilder
Expand Down Expand Up @@ -53,7 +54,10 @@ class LocationSharingFragment @Inject constructor(

mapView = WeakReference(views.mapView)
views.mapView.onCreate(savedInstanceState)
views.mapView.initialize(urlMapProvider.mapUrl)

lifecycleScope.launchWhenCreated {
views.mapView.initialize(urlMapProvider.getMapUrl())
}

views.shareLocationContainer.debouncedClicks {
viewModel.handle(LocationSharingAction.OnShareLocation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,31 @@ package im.vector.app.features.location
import im.vector.app.BuildConfig
import im.vector.app.core.resources.LocaleProvider
import im.vector.app.core.resources.isRTL
import im.vector.app.features.raw.wellknown.getElementWellknown
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.raw.RawService
import org.matrix.android.sdk.api.session.Session
import javax.inject.Inject

class UrlMapProvider @Inject constructor(
private val localeProvider: LocaleProvider
private val localeProvider: LocaleProvider,
private val session: Session,
private val rawService: RawService
) {
private val keyParam = "?key=${BuildConfig.mapTilerKey}"

// This is static so no need for a fun
val mapUrl = buildString {
private val fallbackMapUrl = buildString {
append(MAP_BASE_URL)
append(keyParam)
}

suspend fun getMapUrl(): String {
Copy link
Contributor

Choose a reason for hiding this comment

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

small nit: this might be easier to parse if we separate the fallback with its own variable

private val fallbackMapUrl = buildString {
    append(MAP_BASE_URL)
    append(keyParam)
}

suspend fun getMapUrl(): String {
    val upstreamMapUrl = tryOrNull { rawService.getElementWellknown(session.sessionParams) }
            ?.mapTileServerConfig
            ?.mapStyleUrl
    return upstreamMapUrl ?: fallbackMapUrl
}

what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Much easier to read! I will commit, thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

val upstreamMapUrl = tryOrNull { rawService.getElementWellknown(session.sessionParams) }
?.mapTileServerConfig
?.mapStyleUrl
return upstreamMapUrl ?: fallbackMapUrl
}

fun buildStaticMapUrl(locationData: LocationData,
Copy link
Member

Choose a reason for hiding this comment

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

I think this fun should also be impacted by the url set up in the .well-known file, since AFAIU, it can contain another API key.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch.

zoom: Double,
width: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ data class ElementWellKnown(
val elementE2E: E2EWellKnownConfig? = null,

@Json(name = "im.vector.riot.e2ee")
val riotE2E: E2EWellKnownConfig? = null
val riotE2E: E2EWellKnownConfig? = null,

@Json(name = "org.matrix.msc3488.tile_server")
val mapTileServerConfig: MapTileServerConfig? = null
)

@JsonClass(generateAdapter = true)
Expand All @@ -53,3 +56,9 @@ data class WellKnownPreferredConfig(
@Json(name = "preferredDomain")
val preferredDomain: String? = null
)

@JsonClass(generateAdapter = true)
data class MapTileServerConfig(
@Json(name = "map_style_url")
val mapStyleUrl: String? = null
)