Fix loading states for username editing.

This commit is contained in:
Greyson Parrelli
2023-08-25 17:04:10 -04:00
parent 84b4d69913
commit 2b1136ea02
3 changed files with 20 additions and 32 deletions

View File

@@ -49,7 +49,6 @@ public class UsernameEditFragment extends LoggingFragment {
private UsernameEditViewModel viewModel; private UsernameEditViewModel viewModel;
private UsernameEditFragmentBinding binding; private UsernameEditFragmentBinding binding;
private ImageView suffixProgress;
private LifecycleDisposable lifecycleDisposable; private LifecycleDisposable lifecycleDisposable;
private UsernameEditFragmentArgs args; private UsernameEditFragmentArgs args;
@@ -128,23 +127,10 @@ public class UsernameEditFragment extends LoggingFragment {
pipe.setBounds(0, 0, (int) DimensionUnit.DP.toPixels(1f), (int) DimensionUnit.DP.toPixels(20f)); pipe.setBounds(0, 0, (int) DimensionUnit.DP.toPixels(1f), (int) DimensionUnit.DP.toPixels(20f));
suffixTextView.setCompoundDrawablesRelative(pipe, null, null, null); suffixTextView.setCompoundDrawablesRelative(pipe, null, null, null);
LinearLayout suffixParent = (LinearLayout) suffixTextView.getParent();
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
ViewUtil.setLeftMargin(suffixTextView, (int) DimensionUnit.DP.toPixels(16f)); ViewUtil.setLeftMargin(suffixTextView, (int) DimensionUnit.DP.toPixels(16f));
binding.usernameTextWrapper.getSuffixTextView().setCompoundDrawablePadding((int) DimensionUnit.DP.toPixels(16f)); binding.usernameTextWrapper.getSuffixTextView().setCompoundDrawablePadding((int) DimensionUnit.DP.toPixels(16f));
layoutParams.topMargin = suffixTextView.getPaddingTop();
layoutParams.bottomMargin = suffixTextView.getPaddingBottom();
layoutParams.setMarginEnd(suffixTextView.getPaddingEnd());
suffixProgress = new ImageView(requireContext());
suffixProgress.setImageDrawable(getInProgressDrawable());
suffixProgress.setContentDescription(getString(R.string.load_more_header__loading));
suffixProgress.setVisibility(View.GONE);
suffixParent.addView(suffixProgress, 0, layoutParams);
suffixTextView.setOnClickListener(this::onLearnMore); suffixTextView.setOnClickListener(this::onLearnMore);
} }
@@ -152,7 +138,6 @@ public class UsernameEditFragment extends LoggingFragment {
public void onDestroyView() { public void onDestroyView() {
super.onDestroyView(); super.onDestroyView();
binding = null; binding = null;
suffixProgress = null;
} }
private void onLearnMore(@Nullable View unused) { private void onLearnMore(@Nullable View unused) {
@@ -216,10 +201,14 @@ public class UsernameEditFragment extends LoggingFragment {
} }
private void presentSummary(@NonNull UsernameState usernameState) { private void presentSummary(@NonNull UsernameState usernameState) {
if (usernameState.getUsername() != null || usernameState instanceof UsernameState.Loading) { if (usernameState.getUsername() != null) {
binding.summary.setText(usernameState.getUsername()); binding.summary.setText(usernameState.getUsername());
binding.summary.setAlpha(1f);
} else if (usernameState instanceof UsernameState.Loading) {
binding.summary.setAlpha(0.5f);
} else { } else {
binding.summary.setText(R.string.UsernameEditFragment__choose_your_username); binding.summary.setText(R.string.UsernameEditFragment__choose_your_username);
binding.summary.setAlpha(1f);
} }
} }
@@ -304,25 +293,12 @@ public class UsernameEditFragment extends LoggingFragment {
boolean isInProgress = usernameState.isInProgress(); boolean isInProgress = usernameState.isInProgress();
if (isInProgress) { if (isInProgress) {
suffixProgress.setVisibility(View.VISIBLE); binding.suffixProgress.setVisibility(View.VISIBLE);
} else { } else {
suffixProgress.setVisibility(View.GONE); binding.suffixProgress.setVisibility(View.GONE);
} }
} }
private IndeterminateDrawable<CircularProgressIndicatorSpec> getInProgressDrawable() {
CircularProgressIndicatorSpec spec = new CircularProgressIndicatorSpec(requireContext(), null);
spec.indicatorInset = 0;
spec.indicatorSize = (int) DimensionUnit.DP.toPixels(16f);
spec.trackColor = ContextCompat.getColor(requireContext(), R.color.signal_colorOnSurfaceVariant);
spec.trackThickness = (int) DimensionUnit.DP.toPixels(1f);
IndeterminateDrawable<CircularProgressIndicatorSpec> drawable = IndeterminateDrawable.createCircularDrawable(requireContext(), spec);
drawable.setBounds(0, 0, spec.indicatorSize, spec.indicatorSize);
return drawable;
}
private void onEvent(@NonNull UsernameEditViewModel.Event event) { private void onEvent(@NonNull UsernameEditViewModel.Event event) {
switch (event) { switch (event) {
case SUBMIT_SUCCESS: case SUBMIT_SUCCESS:

View File

@@ -69,6 +69,19 @@
</EditText> </EditText>
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.progressindicator.CircularProgressIndicator
android:id="@+id/suffix_progress"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="10dp"
app:layout_constraintEnd_toEndOf="@id/username_text_wrapper"
app:layout_constraintBottom_toBottomOf="@id/username_text_wrapper"
app:indicatorSize="16dp"
app:trackThickness="1dp"
app:trackColor="@color/transparent"
app:indicatorColor="@color/signal_colorOnSurfaceVariant"/>
<TextView <TextView
android:id="@+id/username_error" android:id="@+id/username_error"

View File

@@ -2,7 +2,6 @@ package org.thoughtcrime.securesms.util
import org.junit.Test import org.junit.Test
import org.thoughtcrime.securesms.assertIs import org.thoughtcrime.securesms.assertIs
import org.thoughtcrime.securesms.assertIsNotNull
import org.thoughtcrime.securesms.assertIsNull import org.thoughtcrime.securesms.assertIsNull
import org.thoughtcrime.securesms.util.UsernameUtil.checkUsername import org.thoughtcrime.securesms.util.UsernameUtil.checkUsername