Connect GV2 title and avatar updates and prevent no-change avatar updates.

This commit is contained in:
Alan Evans
2020-05-07 14:33:27 -03:00
committed by Alex Hart
parent 959718618f
commit 8084822f16
7 changed files with 103 additions and 79 deletions

View File

@@ -15,7 +15,7 @@ interface EditProfileRepository {
void getCurrentDisplayName(@NonNull Consumer<String> displayNameConsumer);
void uploadProfile(@NonNull ProfileName profileName, @NonNull String displayName, @Nullable byte[] avatar, @NonNull Consumer<UploadResult> uploadResultConsumer);
void uploadProfile(@NonNull ProfileName profileName, @Nullable String displayName, @Nullable byte[] avatar, boolean avatarChanged, @NonNull Consumer<UploadResult> uploadResultConsumer);
void getCurrentUsername(@NonNull Consumer<Optional<String>> callback);

View File

@@ -1,7 +1,5 @@
package org.thoughtcrime.securesms.profiles.edit;
import android.view.animation.Transformation;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -17,6 +15,8 @@ import org.thoughtcrime.securesms.profiles.ProfileName;
import org.thoughtcrime.securesms.util.livedata.LiveDataPair;
import org.whispersystems.libsignal.util.guava.Optional;
import java.util.Objects;
class EditProfileViewModel extends ViewModel {
private final MutableLiveData<String> givenName = new MutableLiveData<>();
@@ -24,7 +24,9 @@ class EditProfileViewModel extends ViewModel {
private final LiveData<ProfileName> internalProfileName = Transformations.map(new LiveDataPair<>(givenName, familyName),
pair -> ProfileName.fromParts(pair.first(), pair.second()));
private final MutableLiveData<byte[]> internalAvatar = new MutableLiveData<>();
private final MutableLiveData<byte[]> originalAvatar = new MutableLiveData<>();
private final MutableLiveData<Optional<String>> internalUsername = new MutableLiveData<>();
private final MutableLiveData<String> originalDisplayName = new MutableLiveData<>();
private final LiveData<Boolean> isFormValid = Transformations.map(givenName, name -> !name.isEmpty());
private final EditProfileRepository repository;
private final GroupId groupId;
@@ -37,7 +39,10 @@ class EditProfileViewModel extends ViewModel {
if (!hasInstanceState) {
if (groupId != null) {
repository.getCurrentDisplayName(givenName::setValue);
repository.getCurrentDisplayName(value -> {
givenName.setValue(value);
originalDisplayName.setValue(value);
});
} else {
repository.getCurrentProfileName(name -> {
givenName.setValue(name.getGivenName());
@@ -45,7 +50,10 @@ class EditProfileViewModel extends ViewModel {
});
}
repository.getCurrentAvatar(internalAvatar::setValue);
repository.getCurrentAvatar(value -> {
internalAvatar.setValue(value);
originalAvatar.setValue(value);
});
}
}
@@ -110,7 +118,15 @@ class EditProfileViewModel extends ViewModel {
return;
}
repository.uploadProfile(profileName, displayName, internalAvatar.getValue(), uploadResultConsumer);
byte[] oldAvatar = originalAvatar.getValue();
byte[] newAvatar = internalAvatar.getValue();
String oldDisplayName = isGroup() ? originalDisplayName.getValue() : null;
repository.uploadProfile(profileName,
Objects.equals(oldDisplayName, displayName) ? null : displayName,
newAvatar,
oldAvatar != newAvatar,
uploadResultConsumer);
}
static class Factory implements ViewModelProvider.Factory {

View File

@@ -8,8 +8,12 @@ import androidx.annotation.WorkerThread;
import androidx.core.util.Consumer;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.groups.GroupChangeBusyException;
import org.thoughtcrime.securesms.groups.GroupChangeFailedException;
import org.thoughtcrime.securesms.groups.GroupId;
import org.thoughtcrime.securesms.groups.GroupInsufficientRightsException;
import org.thoughtcrime.securesms.groups.GroupManager;
import org.thoughtcrime.securesms.groups.GroupNotAMemberException;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.profiles.AvatarHelper;
import org.thoughtcrime.securesms.profiles.ProfileName;
@@ -18,7 +22,6 @@ import org.thoughtcrime.securesms.recipients.RecipientId;
import org.thoughtcrime.securesms.util.Util;
import org.thoughtcrime.securesms.util.concurrent.SimpleTask;
import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.util.InvalidNumberException;
import java.io.IOException;
@@ -64,16 +67,17 @@ class EditPushGroupProfileRepository implements EditProfileRepository {
@Override
public void uploadProfile(@NonNull ProfileName profileName,
@NonNull String displayName,
@Nullable String displayName,
@Nullable byte[] avatar,
boolean avatarChanged,
@NonNull Consumer<UploadResult> uploadResultConsumer)
{
SimpleTask.run(() -> {
try {
GroupManager.updateGroup(context, groupId, avatar, displayName);
GroupManager.updateGroup(context, groupId, avatar, avatarChanged, displayName);
return UploadResult.SUCCESS;
} catch (InvalidNumberException e) {
} catch (GroupChangeFailedException | GroupInsufficientRightsException | IOException | GroupNotAMemberException | GroupChangeBusyException e) {
return UploadResult.ERROR_IO;
}

View File

@@ -108,14 +108,16 @@ class EditSelfProfileRepository implements EditProfileRepository {
}
@Override
public void uploadProfile(@NonNull ProfileName profileName, @NonNull String displayName, @Nullable byte[] avatar, @NonNull Consumer<UploadResult> uploadResultConsumer) {
public void uploadProfile(@NonNull ProfileName profileName, @Nullable String displayName, @Nullable byte[] avatar, boolean avatarChanged, @NonNull Consumer<UploadResult> uploadResultConsumer) {
SimpleTask.run(() -> {
DatabaseFactory.getRecipientDatabase(context).setProfileName(Recipient.self().getId(), profileName);
try {
AvatarHelper.setAvatar(context, Recipient.self().getId(), avatar != null ? new ByteArrayInputStream(avatar) : null);
} catch (IOException e) {
return UploadResult.ERROR_IO;
if (avatarChanged) {
try {
AvatarHelper.setAvatar(context, Recipient.self().getId(), avatar != null ? new ByteArrayInputStream(avatar) : null);
} catch (IOException e) {
return UploadResult.ERROR_IO;
}
}
ApplicationDependencies.getJobManager()