Add some polish to the groups V2 manager UI.

This commit is contained in:
Alex Hart
2020-05-07 10:39:40 -03:00
parent 88a40be901
commit b191341c57
25 changed files with 445 additions and 220 deletions

View File

@@ -9,16 +9,14 @@ import androidx.annotation.VisibleForTesting;
import com.annimon.stream.Stream;
import org.thoughtcrime.securesms.util.StringUtil;
import org.thoughtcrime.securesms.util.cjkv.CJKVUtil;
import org.whispersystems.signalservice.api.crypto.ProfileCipher;
import java.nio.charset.StandardCharsets;
public final class ProfileName implements Parcelable {
public static final ProfileName EMPTY = new ProfileName("", "");
private static final int MAX_PART_LENGTH = (ProfileCipher.NAME_PADDED_LENGTH - 1) / 2;
public static final ProfileName EMPTY = new ProfileName("", "");
public static final int MAX_PART_LENGTH = (ProfileCipher.NAME_PADDED_LENGTH - 1) / 2;
private final String givenName;
private final String familyName;
@@ -94,31 +92,12 @@ public final class ProfileName implements Parcelable {
givenName = givenName == null ? "" : givenName;
familyName = familyName == null ? "" : familyName;
givenName = trimToFit(givenName .trim());
familyName = trimToFit(familyName.trim());
givenName = StringUtil.trimToFit(givenName.trim(), ProfileName.MAX_PART_LENGTH);
familyName = StringUtil.trimToFit(familyName.trim(), ProfileName.MAX_PART_LENGTH);
return new ProfileName(givenName, familyName);
}
/**
* Trims a name string to fit into the byte length requirement.
*/
public static @NonNull String trimToFit(@Nullable String name) {
if (name == null) return "";
// At least one byte per char, so shorten string to reduce loop
if (name.length() > ProfileName.MAX_PART_LENGTH) {
name = name.substring(0, ProfileName.MAX_PART_LENGTH);
}
// Remove one char at a time until fits in byte allowance
while (name.getBytes(StandardCharsets.UTF_8).length > ProfileName.MAX_PART_LENGTH) {
name = name.substring(0, name.length() - 1);
}
return name;
}
private static @NonNull String getJoinedName(@NonNull String givenName, @NonNull String familyName) {
if (givenName.isEmpty() && familyName.isEmpty()) return "";
else if (givenName.isEmpty()) return familyName;

View File

@@ -46,6 +46,7 @@ import org.thoughtcrime.securesms.profiles.ProfileName;
import org.thoughtcrime.securesms.providers.BlobProvider;
import org.thoughtcrime.securesms.registration.RegistrationUtil;
import org.thoughtcrime.securesms.util.FeatureFlags;
import org.thoughtcrime.securesms.util.StringUtil;
import org.thoughtcrime.securesms.util.concurrent.SimpleTask;
import org.thoughtcrime.securesms.util.text.AfterTextChanged;
import org.whispersystems.libsignal.util.guava.Optional;
@@ -66,6 +67,7 @@ public class EditProfileFragment extends Fragment {
private static final String TAG = Log.tag(EditProfileFragment.class);
private static final String AVATAR_STATE = "avatar";
private static final short REQUEST_CODE_SELECT_AVATAR = 31726;
private static final int MAX_GROUP_NAME_LENGTH = 32;
private Toolbar toolbar;
private View title;
@@ -130,7 +132,9 @@ public class EditProfileFragment extends Fragment {
initializeProfileName();
initializeUsername();
requireActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
if (groupId == null) {
requireActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
}
}
@Override
@@ -234,7 +238,7 @@ public class EditProfileFragment extends Fragment {
.execute());
this.givenName .addTextChangedListener(new AfterTextChanged(s -> {
trimInPlace(s);
trimInPlace(s, isEditingGroup);
viewModel.setGivenName(s.toString());
}));
@@ -248,7 +252,7 @@ public class EditProfileFragment extends Fragment {
view.<ImageView>findViewById(R.id.avatar_placeholder).setImageResource(R.drawable.ic_group_outline_40);
} else {
this.familyName.addTextChangedListener(new AfterTextChanged(s -> {
trimInPlace(s);
trimInPlace(s, false);
viewModel.setFamilyName(s.toString());
}));
}
@@ -389,8 +393,10 @@ public class EditProfileFragment extends Fragment {
animation.start();
}
private static void trimInPlace(Editable s) {
int trimmedLength = ProfileName.trimToFit(s.toString()).length();
private static void trimInPlace(Editable s, boolean isGroup) {
int trimmedLength = isGroup ? StringUtil.trimToFit(s.toString(), MAX_GROUP_NAME_LENGTH).length()
: StringUtil.trimToFit(s.toString(), ProfileName.MAX_PART_LENGTH).length();
if (s.length() > trimmedLength) {
s.delete(trimmedLength, s.length());
}