package org.thoughtcrime.securesms.avatar.text import androidx.lifecycle.LiveData import androidx.lifecycle.Transformations import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import org.thoughtcrime.securesms.avatar.Avatar import org.thoughtcrime.securesms.avatar.Avatars import org.thoughtcrime.securesms.util.livedata.Store class TextAvatarCreationViewModel(initialText: Avatar.Text) : ViewModel() { private val store = Store(TextAvatarCreationState(initialText)) val state: LiveData = Transformations.distinctUntilChanged(store.stateLiveData) fun setColor(colorPair: Avatars.ColorPair) { store.update { it.copy(currentAvatar = it.currentAvatar.copy(color = colorPair)) } } fun setText(text: String) { store.update { if (it.currentAvatar.text == text) { it } else { it.copy(currentAvatar = it.currentAvatar.copy(text = text)) } } } fun getCurrentAvatar(): Avatar.Text { return store.state.currentAvatar } class Factory(private val initialText: Avatar.Text) : ViewModelProvider.Factory { override fun create(modelClass: Class): T { return requireNotNull(modelClass.cast(TextAvatarCreationViewModel(initialText))) } } }