That fuck shit the fascists are using
at master 186 lines 6.8 kB view raw
1package org.tm.archive.wallpaper; 2 3import androidx.annotation.NonNull; 4import androidx.annotation.Nullable; 5import androidx.lifecycle.LiveData; 6import androidx.lifecycle.MutableLiveData; 7import androidx.lifecycle.Transformations; 8import androidx.lifecycle.ViewModel; 9import androidx.lifecycle.ViewModelProvider; 10 11import com.annimon.stream.Stream; 12 13import org.tm.archive.conversation.colors.AvatarColor; 14import org.tm.archive.conversation.colors.ChatColors; 15import org.tm.archive.keyvalue.SignalStore; 16import org.tm.archive.recipients.LiveRecipient; 17import org.tm.archive.recipients.Recipient; 18import org.tm.archive.recipients.RecipientForeverObserver; 19import org.tm.archive.recipients.RecipientId; 20import org.tm.archive.util.DefaultValueLiveData; 21import org.tm.archive.util.adapter.mapping.MappingModel; 22import org.tm.archive.util.livedata.LiveDataUtil; 23 24import java.util.List; 25import java.util.Objects; 26import java.util.Optional; 27 28public class ChatWallpaperViewModel extends ViewModel { 29 30 private final ChatWallpaperRepository repository = new ChatWallpaperRepository(); 31 private final MutableLiveData<Optional<ChatWallpaper>> wallpaper = new MutableLiveData<>(); 32 private final MutableLiveData<List<ChatWallpaper>> builtins = new MutableLiveData<>(); 33 private final MutableLiveData<Boolean> dimInDarkTheme = new MutableLiveData<>(); 34 private final MutableLiveData<Boolean> enableWallpaperControls = new MutableLiveData<>(); 35 private final MutableLiveData<ChatColors> chatColors = new MutableLiveData<>(); 36 private final RecipientId recipientId; 37 private final LiveRecipient liveRecipient; 38 private final RecipientForeverObserver recipientObserver = r -> refreshChatColors(); 39 private final LiveData<WallpaperPreviewPortrait> wallpaperPreviewPortrait; 40 41 private ChatWallpaperViewModel(@Nullable RecipientId recipientId) { 42 this.recipientId = recipientId; 43 44 ChatWallpaper currentWallpaper = repository.getCurrentWallpaper(recipientId); 45 dimInDarkTheme.setValue(currentWallpaper == null || currentWallpaper.getDimLevelForDarkTheme() > 0f); 46 enableWallpaperControls.setValue(hasClearableWallpaper()); 47 wallpaper.setValue(Optional.ofNullable(currentWallpaper)); 48 49 if (recipientId != null) { 50 liveRecipient = Recipient.live(recipientId); 51 liveRecipient.observeForever(recipientObserver); 52 wallpaperPreviewPortrait = Transformations.map(liveRecipient.getLiveData(), recipient -> { 53 if (recipient.getContactPhoto() != null) { 54 return new WallpaperPreviewPortrait.ContactPhoto(recipient); 55 } else { 56 return new WallpaperPreviewPortrait.SolidColor(recipient.getAvatarColor()); 57 } 58 }); 59 } else { 60 liveRecipient = null; 61 wallpaperPreviewPortrait = new DefaultValueLiveData<>(new WallpaperPreviewPortrait.SolidColor(AvatarColor.A100)); 62 } 63 } 64 65 @Override 66 protected void onCleared() { 67 if (liveRecipient != null) { 68 liveRecipient.removeForeverObserver(recipientObserver); 69 } 70 } 71 72 void refreshWallpaper() { 73 repository.getAllWallpaper(builtins::postValue); 74 } 75 76 void refreshChatColors() { 77 chatColors.postValue(repository.getCurrentChatColors(recipientId)); 78 } 79 80 void setDimInDarkTheme(boolean shouldDimInDarkTheme) { 81 dimInDarkTheme.setValue(shouldDimInDarkTheme); 82 83 Optional<ChatWallpaper> wallpaper = this.wallpaper.getValue(); 84 if (wallpaper.isPresent()) { 85 repository.setDimInDarkTheme(recipientId, shouldDimInDarkTheme); 86 } 87 } 88 89 void setWallpaper(@Nullable ChatWallpaper chatWallpaper) { 90 wallpaper.setValue(Optional.ofNullable(chatWallpaper)); 91 } 92 93 void saveWallpaperSelection() { 94 Optional<ChatWallpaper> wallpaper = this.wallpaper.getValue(); 95 boolean dimInDarkTheme = this.dimInDarkTheme.getValue(); 96 97 if (!wallpaper.isPresent()) { 98 repository.saveWallpaper(recipientId, null, this::refreshChatColors); 99 100 if (recipientId != null) { 101 ChatWallpaper globalWallpaper = SignalStore.wallpaper().getWallpaper(); 102 103 this.wallpaper.setValue(Optional.ofNullable(globalWallpaper)); 104 this.dimInDarkTheme.setValue(globalWallpaper == null || globalWallpaper.getDimLevelForDarkTheme() > 0); 105 } 106 107 enableWallpaperControls.setValue(false); 108 return; 109 } else { 110 enableWallpaperControls.setValue(true); 111 } 112 113 Optional<ChatWallpaper> updated = wallpaper.map(paper -> ChatWallpaperFactory.updateWithDimming(paper, dimInDarkTheme ? ChatWallpaper.FIXED_DIM_LEVEL_FOR_DARK_THEME : 0f)); 114 115 if (updated.isPresent()) { 116 repository.saveWallpaper(recipientId, updated.get(), this::refreshChatColors); 117 } 118 } 119 120 void resetAllWallpaper() { 121 repository.resetAllWallpaper(this::refreshChatColors); 122 } 123 124 @Nullable RecipientId getRecipientId() { 125 return recipientId; 126 } 127 128 @NonNull LiveData<Optional<ChatWallpaper>> getCurrentWallpaper() { 129 return wallpaper; 130 } 131 132 @NonNull LiveData<ChatColors> getCurrentChatColors() { 133 return chatColors; 134 } 135 136 @NonNull LiveData<WallpaperPreviewPortrait> getWallpaperPreviewPortrait() { 137 return wallpaperPreviewPortrait; 138 } 139 140 @NonNull LiveData<List<MappingModel<?>>> getWallpapers() { 141 return LiveDataUtil.combineLatest(builtins, dimInDarkTheme, (wallpapers, dimInDarkMode) -> 142 Stream.of(wallpapers) 143 .map(paper -> ChatWallpaperFactory.updateWithDimming(paper, dimInDarkMode ? ChatWallpaper.FIXED_DIM_LEVEL_FOR_DARK_THEME : 0f)) 144 .<MappingModel<?>>map(ChatWallpaperSelectionMappingModel::new).toList() 145 ); 146 } 147 148 @NonNull LiveData<Boolean> getDimInDarkTheme() { 149 return dimInDarkTheme; 150 } 151 152 @NonNull LiveData<Boolean> getEnableWallpaperControls() { 153 return enableWallpaperControls; 154 } 155 156 boolean isGlobal() { 157 return recipientId == null; 158 } 159 160 void clearChatColor() { 161 repository.clearChatColor(recipientId, this::refreshChatColors); 162 } 163 164 private boolean hasClearableWallpaper() { 165 return (isGlobal() && SignalStore.wallpaper().hasWallpaperSet()) || 166 (recipientId != null && Recipient.live(recipientId).get().hasOwnWallpaper()); 167 } 168 169 public void resetAllChatColors() { 170 repository.resetAllChatColors(this::refreshChatColors); 171 } 172 173 public static class Factory implements ViewModelProvider.Factory { 174 175 private final RecipientId recipientId; 176 177 public Factory(@Nullable RecipientId recipientId) { 178 this.recipientId = recipientId; 179 } 180 181 @Override 182 public @NonNull <T extends ViewModel> T create(@NonNull Class<T> modelClass) { 183 return Objects.requireNonNull(modelClass.cast(new ChatWallpaperViewModel(recipientId))); 184 } 185 } 186}