From 6972622bc6a685a2598c8bd9a7cbcd7f4b8c4b72 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Mon, 1 Aug 2022 14:45:08 +0200 Subject: [PATCH] Handling map loading error in timeline --- .../stylable_map_loading_error_view.xml | 8 ++++ .../timeline/item/AbsMessageLocationItem.kt | 12 ++++-- .../features/location/MapLoadingErrorView.kt | 39 +++++++++++++++++-- .../location/MapLoadingErrorViewState.kt | 21 ++++++++++ .../res/drawable/ic_location_pin_failed.xml | 9 ----- .../item_timeline_event_location_stub.xml | 23 +++++------ .../res/layout/view_map_loading_error.xml | 16 ++++++-- 7 files changed, 94 insertions(+), 34 deletions(-) create mode 100644 library/ui-styles/src/main/res/values/stylable_map_loading_error_view.xml create mode 100644 vector/src/main/java/im/vector/app/features/location/MapLoadingErrorViewState.kt delete mode 100644 vector/src/main/res/drawable/ic_location_pin_failed.xml diff --git a/library/ui-styles/src/main/res/values/stylable_map_loading_error_view.xml b/library/ui-styles/src/main/res/values/stylable_map_loading_error_view.xml new file mode 100644 index 0000000000..911167e52a --- /dev/null +++ b/library/ui-styles/src/main/res/values/stylable_map_loading_error_view.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/AbsMessageLocationItem.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/AbsMessageLocationItem.kt index b7790b8a30..4903b8c8cf 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/AbsMessageLocationItem.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/AbsMessageLocationItem.kt @@ -36,6 +36,8 @@ import im.vector.app.core.utils.DimensionConverter import im.vector.app.features.home.room.detail.timeline.helper.LocationPinProvider import im.vector.app.features.home.room.detail.timeline.style.TimelineMessageLayout import im.vector.app.features.home.room.detail.timeline.style.granularRoundedCorners +import im.vector.app.features.location.MapLoadingErrorView +import im.vector.app.features.location.MapLoadingErrorViewState abstract class AbsMessageLocationItem( @LayoutRes layoutId: Int = R.layout.item_timeline_event_base @@ -86,8 +88,10 @@ abstract class AbsMessageLocationItem( target: Target?, isFirstResource: Boolean ): Boolean { - holder.staticMapPinImageView.setImageResource(R.drawable.ic_location_pin_failed) - holder.staticMapErrorTextView.isVisible = true + holder.staticMapPinImageView.setImageDrawable(null) + holder.staticMapLoadingErrorView.isVisible = true + val mapErrorViewState = MapLoadingErrorViewState(imageCornerTransformation) + holder.staticMapLoadingErrorView.render(mapErrorViewState) holder.staticMapCopyrightTextView.isVisible = false return false } @@ -103,7 +107,7 @@ abstract class AbsMessageLocationItem( // we are not using Glide since it does not display it correctly when there is no user photo holder.staticMapPinImageView.setImageDrawable(pinDrawable) } - holder.staticMapErrorTextView.isVisible = false + holder.staticMapLoadingErrorView.isVisible = false holder.staticMapCopyrightTextView.isVisible = true return false } @@ -115,7 +119,7 @@ abstract class AbsMessageLocationItem( abstract class Holder(@IdRes stubId: Int) : AbsMessageItem.Holder(stubId) { val staticMapImageView by bind(R.id.staticMapImageView) val staticMapPinImageView by bind(R.id.staticMapPinImageView) - val staticMapErrorTextView by bind(R.id.staticMapErrorTextView) + val staticMapLoadingErrorView by bind(R.id.staticMapLoadingError) val staticMapCopyrightTextView by bind(R.id.staticMapCopyrightTextView) } } diff --git a/vector/src/main/java/im/vector/app/features/location/MapLoadingErrorView.kt b/vector/src/main/java/im/vector/app/features/location/MapLoadingErrorView.kt index 45b7dfd6d6..c82f1eb687 100644 --- a/vector/src/main/java/im/vector/app/features/location/MapLoadingErrorView.kt +++ b/vector/src/main/java/im/vector/app/features/location/MapLoadingErrorView.kt @@ -17,10 +17,16 @@ package im.vector.app.features.location import android.content.Context +import android.content.res.TypedArray +import android.graphics.drawable.ColorDrawable import android.util.AttributeSet import android.view.LayoutInflater import androidx.constraintlayout.widget.ConstraintLayout +import androidx.core.content.res.use +import im.vector.app.R +import im.vector.app.core.glide.GlideApp import im.vector.app.databinding.ViewMapLoadingErrorBinding +import im.vector.app.features.themes.ThemeUtils /** * Custom view to display an error when map fails to load. @@ -29,10 +35,35 @@ class MapLoadingErrorView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : ConstraintLayout(context, attrs, defStyleAttr) { + private val binding = ViewMapLoadingErrorBinding.inflate( + LayoutInflater.from(context), + this + ) + init { - ViewMapLoadingErrorBinding.inflate( - LayoutInflater.from(context), - this - ) + context.obtainStyledAttributes( + attrs, + R.styleable.MapLoadingErrorView, + 0, + 0 + ).use { + setErrorDescription(it) + } + } + + private fun setErrorDescription(typedArray: TypedArray) { + val description = typedArray.getString(R.styleable.MapLoadingErrorView_mapErrorDescription) + if(description.isNullOrEmpty()) { + binding.mapLoadingErrorDescription.setText(R.string.location_share_loading_map_error) + } else { + binding.mapLoadingErrorDescription.text = description + } + } + + fun render(mapLoadingErrorViewState: MapLoadingErrorViewState) { + GlideApp.with(binding.mapLoadingErrorBackground) + .load(ColorDrawable(ThemeUtils.getColor(context, R.attr.vctr_system))) + .transform(mapLoadingErrorViewState.backgroundTransformation) + .into(binding.mapLoadingErrorBackground) } } diff --git a/vector/src/main/java/im/vector/app/features/location/MapLoadingErrorViewState.kt b/vector/src/main/java/im/vector/app/features/location/MapLoadingErrorViewState.kt new file mode 100644 index 0000000000..8098d26a12 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/location/MapLoadingErrorViewState.kt @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.location + +import com.bumptech.glide.load.resource.bitmap.BitmapTransformation + +data class MapLoadingErrorViewState(val backgroundTransformation: BitmapTransformation) diff --git a/vector/src/main/res/drawable/ic_location_pin_failed.xml b/vector/src/main/res/drawable/ic_location_pin_failed.xml deleted file mode 100644 index 250d048836..0000000000 --- a/vector/src/main/res/drawable/ic_location_pin_failed.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - diff --git a/vector/src/main/res/layout/item_timeline_event_location_stub.xml b/vector/src/main/res/layout/item_timeline_event_location_stub.xml index e4c87dab71..3eef4a4084 100644 --- a/vector/src/main/res/layout/item_timeline_event_location_stub.xml +++ b/vector/src/main/res/layout/item_timeline_event_location_stub.xml @@ -29,21 +29,16 @@ app:layout_constraintTop_toTopOf="@id/staticMapImageView" app:layout_constraintVertical_bias="1.0" /> - + app:layout_constraintBottom_toTopOf="@id/locationLiveRunningBanner" + app:layout_constraintEnd_toEndOf="@id/staticMapImageView" + app:layout_constraintStart_toStartOf="@id/staticMapImageView" + app:layout_constraintTop_toTopOf="@id/staticMapImageView" + app:mapErrorDescription="@string/location_timeline_failed_to_load_map" /> + + + android:src="@drawable/ic_warning_badge" + tools:ignore="ContentDescription" />