Disallow 'visually empty' profile names.
This commit is contained in:
parent
b6b499d865
commit
cb81a9f783
2 changed files with 35 additions and 1 deletions
|
@ -12,6 +12,7 @@ import androidx.lifecycle.ViewModelProvider;
|
|||
|
||||
import org.thoughtcrime.securesms.groups.GroupId;
|
||||
import org.thoughtcrime.securesms.profiles.ProfileName;
|
||||
import org.thoughtcrime.securesms.util.StringUtil;
|
||||
import org.thoughtcrime.securesms.util.livedata.LiveDataPair;
|
||||
import org.whispersystems.libsignal.util.guava.Optional;
|
||||
|
||||
|
@ -27,7 +28,7 @@ class EditProfileViewModel extends ViewModel {
|
|||
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 LiveData<Boolean> isFormValid = Transformations.map(givenName, name -> !StringUtil.isVisuallyEmpty(name));
|
||||
private final EditProfileRepository repository;
|
||||
private final GroupId groupId;
|
||||
|
||||
|
|
|
@ -3,10 +3,17 @@ package org.thoughtcrime.securesms.util;
|
|||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.google.android.collect.Sets;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Set;
|
||||
|
||||
public final class StringUtil {
|
||||
|
||||
private static final Set<Character> WHITESPACE = Sets.newHashSet('\u200E', // left-to-right mark
|
||||
'\u200F', // right-to-left mark
|
||||
'\u2007'); // figure space
|
||||
|
||||
private StringUtil() {
|
||||
}
|
||||
|
||||
|
@ -28,4 +35,30 @@ public final class StringUtil {
|
|||
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return True if the string is empty, or if it contains nothing but whitespace characters.
|
||||
* Accounts for various unicode whitespace characters.
|
||||
*/
|
||||
public static boolean isVisuallyEmpty(@Nullable String value) {
|
||||
if (value == null || value.length() == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (int i = 0; i < value.length(); i++) {
|
||||
if (!isVisuallyEmpty(value.charAt(i))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return True if the character is invisible or whitespace. Accounts for various unicode
|
||||
* whitespace characters.
|
||||
*/
|
||||
public static boolean isVisuallyEmpty(char c) {
|
||||
return Character.isWhitespace(c) || WHITESPACE.contains(c);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue