Skip to content

Commit aa805dc

Browse files
committed
Mutualizing some code
1 parent df2c3e7 commit aa805dc

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

vector/src/main/java/im/vector/app/features/location/MapTilerMapView.kt

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package im.vector.app.features.location
1818

1919
import android.content.Context
2020
import android.content.res.TypedArray
21+
import android.graphics.drawable.Drawable
2122
import android.util.AttributeSet
2223
import android.view.Gravity
2324
import android.widget.ImageView
@@ -170,12 +171,7 @@ class MapTilerMapView @JvmOverloads constructor(
170171
}
171172

172173
val pinDrawable = state.pinDrawable ?: userLocationDrawable
173-
pinDrawable?.let { drawable ->
174-
if (!safeMapRefs.style.isFullyLoaded ||
175-
safeMapRefs.style.getImage(state.pinId) == null) {
176-
safeMapRefs.style.addImage(state.pinId, drawable.toBitmap())
177-
}
178-
}
174+
addImageToMapStyle(pinDrawable, state.pinId, safeMapRefs)
179175

180176
safeMapRefs.symbolManager.deleteAll()
181177
state.pinLocationData?.let { locationData ->
@@ -185,30 +181,35 @@ class MapTilerMapView @JvmOverloads constructor(
185181
}
186182

187183
if (pinDrawable != null && state.showPin) {
188-
safeMapRefs.symbolManager.create(
189-
SymbolOptions()
190-
.withLatLng(LatLng(locationData.latitude, locationData.longitude))
191-
.withIconImage(state.pinId)
192-
.withIconAnchor(Property.ICON_ANCHOR_BOTTOM)
193-
)
184+
createSymbol(locationData, state.pinId, safeMapRefs)
194185
}
195186
}
196187

197188
state.userLocationData?.let { locationData ->
198-
if (!safeMapRefs.style.isFullyLoaded || safeMapRefs.style.getImage(USER_PIN_ID) == null) {
199-
userLocationDrawable?.let { drawable ->
200-
safeMapRefs.style.addImage(USER_PIN_ID, drawable.toBitmap())
201-
}
189+
addImageToMapStyle(userLocationDrawable, USER_PIN_ID, safeMapRefs)
190+
if (userLocationDrawable != null) {
191+
createSymbol(locationData, USER_PIN_ID, safeMapRefs)
202192
}
203-
safeMapRefs.symbolManager.create(
204-
SymbolOptions()
205-
.withLatLng(LatLng(locationData.latitude, locationData.longitude))
206-
.withIconImage(USER_PIN_ID)
207-
.withIconAnchor(Property.ICON_ANCHOR_BOTTOM)
208-
)
209193
}
210194
}
211195

196+
private fun addImageToMapStyle(image: Drawable?, imageId: String, mapRefs: MapRefs) {
197+
image?.let { drawable ->
198+
if (!mapRefs.style.isFullyLoaded || mapRefs.style.getImage(imageId) == null) {
199+
mapRefs.style.addImage(imageId, drawable.toBitmap())
200+
}
201+
}
202+
}
203+
204+
private fun createSymbol(locationData: LocationData, imageId: String, mapRefs: MapRefs) {
205+
mapRefs.symbolManager.create(
206+
SymbolOptions()
207+
.withLatLng(LatLng(locationData.latitude, locationData.longitude))
208+
.withIconImage(imageId)
209+
.withIconAnchor(Property.ICON_ANCHOR_BOTTOM)
210+
)
211+
}
212+
212213
fun zoomToLocation(locationData: LocationData) {
213214
Timber.d("## Location: zoomToLocation")
214215
mapRefs?.map?.zoomToLocation(locationData)

0 commit comments

Comments
 (0)