From b8f1b98c7476941de044670205e5e04c23d3189a Mon Sep 17 00:00:00 2001 From: Alex Hart Date: Fri, 4 Jun 2021 09:50:45 -0300 Subject: [PATCH] Use user avatar or avatar color for bubble on wallpaper fragment. --- .../wallpaper/ChatWallpaperFragment.java | 12 ++++------ .../wallpaper/ChatWallpaperViewModel.java | 23 ++++++++++++++---- .../wallpaper/WallpaperPreviewPortrait.kt | 24 +++++++++++++++++++ .../res/layout/chat_wallpaper_fragment.xml | 2 +- 4 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/org/thoughtcrime/securesms/wallpaper/WallpaperPreviewPortrait.kt diff --git a/app/src/main/java/org/thoughtcrime/securesms/wallpaper/ChatWallpaperFragment.java b/app/src/main/java/org/thoughtcrime/securesms/wallpaper/ChatWallpaperFragment.java index 1c75c0426a..9aa3de891e 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/wallpaper/ChatWallpaperFragment.java +++ b/app/src/main/java/org/thoughtcrime/securesms/wallpaper/ChatWallpaperFragment.java @@ -1,11 +1,7 @@ package org.thoughtcrime.securesms.wallpaper; import android.content.res.ColorStateList; -import android.graphics.Color; -import android.graphics.PorterDuff; -import android.graphics.PorterDuffColorFilter; import android.graphics.drawable.Drawable; -import android.os.Build; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.LayoutInflater; @@ -29,6 +25,7 @@ import androidx.navigation.Navigation; import com.google.android.material.dialog.MaterialAlertDialogBuilder; import org.thoughtcrime.securesms.R; +import org.thoughtcrime.securesms.components.AvatarImageView; import org.thoughtcrime.securesms.conversation.colors.ColorizerView; import org.thoughtcrime.securesms.util.DisplayMetricsUtil; import org.thoughtcrime.securesms.util.Projection; @@ -52,7 +49,7 @@ public class ChatWallpaperFragment extends Fragment { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { viewModel = ViewModelProviders.of(requireActivity()).get(ChatWallpaperViewModel.class); - ImageView portrait = view.findViewById(R.id.chat_wallpaper_preview_top_bar_portrait); + AvatarImageView portrait = view.findViewById(R.id.chat_wallpaper_preview_top_bar_portrait); Toolbar toolbar = view.findViewById(R.id.toolbar); ImageView chatWallpaperPreview = view.findViewById(R.id.chat_wallpaper_preview_background); View setWallpaper = view.findViewById(R.id.chat_wallpaper_set_wallpaper); @@ -74,6 +71,9 @@ public class ChatWallpaperFragment extends Fragment { forceAspectRatioToScreenByAdjustingHeight(chatWallpaperPreview); + viewModel.getWallpaperPreviewPortrait().observe(getViewLifecycleOwner(), + wallpaperPreviewPortrait -> wallpaperPreviewPortrait.applyToAvatarImageView(portrait)); + viewModel.getCurrentWallpaper().observe(getViewLifecycleOwner(), wallpaper -> { if (wallpaper.isPresent()) { wallpaper.get().loadInto(chatWallpaperPreview); @@ -187,8 +187,6 @@ public class ChatWallpaperFragment extends Fragment { Drawable colorCircle = chatColors.asCircle(); colorCircle.setBounds(0, 0, ViewUtil.dpToPx(16), ViewUtil.dpToPx(16)); TextViewCompat.setCompoundDrawablesRelative(setChatColor, null, null, colorCircle, null); - - portrait.setImageDrawable(chatColors.asCircle()); }); sentBubble.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> viewModel.refreshChatColors()); diff --git a/app/src/main/java/org/thoughtcrime/securesms/wallpaper/ChatWallpaperViewModel.java b/app/src/main/java/org/thoughtcrime/securesms/wallpaper/ChatWallpaperViewModel.java index 1ccdcc3ca1..e6d1fb7f76 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/wallpaper/ChatWallpaperViewModel.java +++ b/app/src/main/java/org/thoughtcrime/securesms/wallpaper/ChatWallpaperViewModel.java @@ -10,12 +10,14 @@ import androidx.lifecycle.ViewModelProvider; import com.annimon.stream.Stream; +import org.thoughtcrime.securesms.conversation.colors.AvatarColor; import org.thoughtcrime.securesms.conversation.colors.ChatColors; import org.thoughtcrime.securesms.keyvalue.SignalStore; import org.thoughtcrime.securesms.recipients.LiveRecipient; import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.recipients.RecipientForeverObserver; import org.thoughtcrime.securesms.recipients.RecipientId; +import org.thoughtcrime.securesms.util.DefaultValueLiveData; import org.thoughtcrime.securesms.util.MappingModel; import org.thoughtcrime.securesms.util.livedata.LiveDataUtil; import org.whispersystems.libsignal.util.guava.Optional; @@ -34,6 +36,7 @@ public class ChatWallpaperViewModel extends ViewModel { private final RecipientId recipientId; private final LiveRecipient liveRecipient; private final RecipientForeverObserver recipientObserver = r -> refreshChatColors(); + private final LiveData wallpaperPreviewPortrait; private ChatWallpaperViewModel(@Nullable RecipientId recipientId) { this.recipientId = recipientId; @@ -46,8 +49,16 @@ public class ChatWallpaperViewModel extends ViewModel { if (recipientId != null) { liveRecipient = Recipient.live(recipientId); liveRecipient.observeForever(recipientObserver); + wallpaperPreviewPortrait = Transformations.map(liveRecipient.getLiveData(), recipient -> { + if (recipient.getContactPhoto() != null) { + return new WallpaperPreviewPortrait.ContactPhoto(recipient); + } else { + return new WallpaperPreviewPortrait.SolidColor(recipient.getAvatarColor()); + } + }); } else { - liveRecipient = null; + liveRecipient = null; + wallpaperPreviewPortrait = new DefaultValueLiveData<>(new WallpaperPreviewPortrait.SolidColor(AvatarColor.ULTRAMARINE)); } } @@ -117,15 +128,19 @@ public class ChatWallpaperViewModel extends ViewModel { @NonNull LiveData> getCurrentWallpaper() { return wallpaper; } - + @NonNull LiveData getCurrentChatColors() { return chatColors; } + @NonNull LiveData getWallpaperPreviewPortrait() { + return wallpaperPreviewPortrait; + } + @NonNull LiveData>> getWallpapers() { return LiveDataUtil.combineLatest(builtins, dimInDarkTheme, (wallpapers, dimInDarkMode) -> - Stream.of(wallpapers) - .map(paper -> ChatWallpaperFactory.updateWithDimming(paper, dimInDarkMode ? ChatWallpaper.FIXED_DIM_LEVEL_FOR_DARK_THEME : 0f)) + Stream.of(wallpapers) + .map(paper -> ChatWallpaperFactory.updateWithDimming(paper, dimInDarkMode ? ChatWallpaper.FIXED_DIM_LEVEL_FOR_DARK_THEME : 0f)) .>map(ChatWallpaperSelectionMappingModel::new).toList() ); } diff --git a/app/src/main/java/org/thoughtcrime/securesms/wallpaper/WallpaperPreviewPortrait.kt b/app/src/main/java/org/thoughtcrime/securesms/wallpaper/WallpaperPreviewPortrait.kt new file mode 100644 index 0000000000..6f44930d11 --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/wallpaper/WallpaperPreviewPortrait.kt @@ -0,0 +1,24 @@ +package org.thoughtcrime.securesms.wallpaper + +import org.thoughtcrime.securesms.R +import org.thoughtcrime.securesms.components.AvatarImageView +import org.thoughtcrime.securesms.conversation.colors.AvatarColor +import org.thoughtcrime.securesms.recipients.Recipient + +sealed class WallpaperPreviewPortrait { + class ContactPhoto(private val recipient: Recipient) : WallpaperPreviewPortrait() { + override fun applyToAvatarImageView(avatarImageView: AvatarImageView) { + avatarImageView.setAvatar(recipient) + avatarImageView.colorFilter = null + } + } + + class SolidColor(private val avatarColor: AvatarColor) : WallpaperPreviewPortrait() { + override fun applyToAvatarImageView(avatarImageView: AvatarImageView) { + avatarImageView.setImageResource(R.drawable.circle_tintable) + avatarImageView.setColorFilter(avatarColor.colorInt()) + } + } + + abstract fun applyToAvatarImageView(avatarImageView: AvatarImageView) +} diff --git a/app/src/main/res/layout/chat_wallpaper_fragment.xml b/app/src/main/res/layout/chat_wallpaper_fragment.xml index 3343f0c965..7aaf851c0d 100644 --- a/app/src/main/res/layout/chat_wallpaper_fragment.xml +++ b/app/src/main/res/layout/chat_wallpaper_fragment.xml @@ -83,7 +83,7 @@ app:srcCompat="@drawable/ic_arrow_left_24" app:tint="@color/core_white" /> -