Material 3: Simplify title and category dialogs

Signed-off-by: Stefan Niedermann <info@niedermann.it>
This commit is contained in:
Stefan Niedermann 2022-11-04 12:56:23 +01:00
parent 9e8cf5df0a
commit 7ee5b61587
8 changed files with 57 additions and 93 deletions

View file

@ -10,9 +10,9 @@ import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import java.util.ArrayList;
import java.util.Collection;
@ -21,13 +21,11 @@ import java.util.Objects;
import java.util.stream.Collectors;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.BrandedAlertDialogBuilder;
import it.niedermann.owncloud.notes.branding.BrandedDialogFragment;
import it.niedermann.owncloud.notes.databinding.DialogChooseAccountBinding;
import it.niedermann.owncloud.notes.persistence.entity.Account;
import it.niedermann.owncloud.notes.persistence.entity.Note;
import it.niedermann.owncloud.notes.shared.account.AccountChooserAdapter;
import it.niedermann.owncloud.notes.shared.account.AccountChooserViewHolder;
/**
* A {@link DialogFragment} which provides an {@link Account} chooser that hides the current {@link Account}.
@ -75,7 +73,7 @@ public class AccountPickerDialogFragment extends BrandedDialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final var dialogBuilder = new BrandedAlertDialogBuilder(requireActivity())
final var dialogBuilder = new MaterialAlertDialogBuilder(requireActivity())
.setTitle(R.string.simple_move)
.setNegativeButton(android.R.string.cancel, null);

View file

@ -1,50 +0,0 @@
package it.niedermann.owncloud.notes.branding;
import static it.niedermann.owncloud.notes.branding.BrandingUtil.getSecondaryForegroundColorDependingOnTheme;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.Button;
import androidx.annotation.CallSuper;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
public class BrandedAlertDialogBuilder extends MaterialAlertDialogBuilder implements Branded {
protected AlertDialog dialog;
public BrandedAlertDialogBuilder(Context context) {
super(context);
}
@NonNull
@Override
public AlertDialog create() {
this.dialog = super.create();
@NonNull final var context = getContext();
@ColorInt final int mainColor = BrandingUtil.readBrandMainColor(context);
@ColorInt final int textColor = BrandingUtil.readBrandTextColor(context);
applyBrand(mainColor, textColor);
dialog.setOnShowListener(dialog -> applyBrand(mainColor, textColor));
return dialog;
}
@CallSuper
@Override
public void applyBrand(int mainColor, int textColor) {
final var buttons = new Button[3];
buttons[0] = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
buttons[1] = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
buttons[2] = dialog.getButton(DialogInterface.BUTTON_NEUTRAL);
for (final var button : buttons) {
if (button != null) {
button.setTextColor(getSecondaryForegroundColorDependingOnTheme(button.getContext(), mainColor));
}
}
}
}

View file

@ -1,26 +0,0 @@
package it.niedermann.owncloud.notes.branding;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.Button;
import androidx.annotation.CallSuper;
import it.niedermann.owncloud.notes.R;
public class BrandedDeleteAlertDialogBuilder extends BrandedAlertDialogBuilder {
public BrandedDeleteAlertDialogBuilder(Context context) {
super(context);
}
@CallSuper
@Override
public void applyBrand(int mainColor, int textColor) {
super.applyBrand(mainColor, textColor);
final var positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
if (positiveButton != null) {
positiveButton.setTextColor(getContext().getResources().getColor(R.color.bg_attention));
}
}
}

View file

@ -0,0 +1,37 @@
package it.niedermann.owncloud.notes.branding;
import android.content.Context;
import android.content.DialogInterface;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import it.niedermann.owncloud.notes.R;
public class DeleteAlertDialogBuilder extends MaterialAlertDialogBuilder {
protected AlertDialog dialog;
public DeleteAlertDialogBuilder(Context context) {
super(context);
}
@NonNull
@Override
public AlertDialog create() {
this.dialog = super.create();
applyBrand();
dialog.setOnShowListener(dialog -> applyBrand());
return dialog;
}
public void applyBrand() {
final var positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
if (positiveButton != null) {
positiveButton.setTextColor(ContextCompat.getColor(getContext(), R.color.danger));
}
}
}

View file

@ -13,14 +13,14 @@ import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModelProvider;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import java.util.List;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.BrandedAlertDialogBuilder;
import it.niedermann.owncloud.notes.branding.BrandedDialogFragment;
import it.niedermann.owncloud.notes.branding.BrandingUtil;
import it.niedermann.owncloud.notes.databinding.DialogChangeCategoryBinding;
@ -150,7 +150,7 @@ public class CategoryDialogFragment extends BrandedDialogFragment {
}
});
return new BrandedAlertDialogBuilder(getActivity())
return new MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.change_category_title)
.setView(dialogView)
.setCancelable(true)

View file

@ -57,7 +57,7 @@ public class EditTitleDialogFragment extends BrandedDialogFragment {
binding.title.setText(oldTitle);
}
return new MaterialAlertDialogBuilder(requireActivity())
return new MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.change_note_title)
.setView(dialogView)
.setCancelable(true)

View file

@ -18,6 +18,7 @@ import androidx.annotation.Px;
import androidx.annotation.StringRes;
import androidx.lifecycle.ViewModelProvider;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.nextcloud.android.sso.AccountImporter;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
@ -27,8 +28,7 @@ import java.util.function.Function;
import it.niedermann.owncloud.notes.LockedActivity;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.BrandedAlertDialogBuilder;
import it.niedermann.owncloud.notes.branding.BrandedDeleteAlertDialogBuilder;
import it.niedermann.owncloud.notes.branding.DeleteAlertDialogBuilder;
import it.niedermann.owncloud.notes.databinding.ActivityManageAccountsBinding;
import it.niedermann.owncloud.notes.exception.ExceptionDialogFragment;
import it.niedermann.owncloud.notes.persistence.NotesRepository;
@ -91,7 +91,7 @@ public class ManageAccountsActivity extends LockedActivity implements IManageAcc
public void onSuccess(Long unsynchronizedChangesCount) {
runOnUiThread(() -> {
if (unsynchronizedChangesCount > 0) {
new BrandedDeleteAlertDialogBuilder(ManageAccountsActivity.this)
new DeleteAlertDialogBuilder(ManageAccountsActivity.this)
.setTitle(getString(R.string.remove_account, accountToDelete.getUserName()))
.setMessage(getResources().getQuantityString(R.plurals.remove_account_message, (int) unsynchronizedChangesCount.longValue(), accountToDelete.getAccountName(), unsynchronizedChangesCount))
.setNeutralButton(android.R.string.cancel, null)
@ -134,7 +134,7 @@ public class ManageAccountsActivity extends LockedActivity implements IManageAcc
final var repository = NotesRepository.getInstance(getApplicationContext());
final var editText = new EditText(this);
final var wrapper = createDialogViewWrapper();
final var dialog = new BrandedAlertDialogBuilder(this)
final var dialog = new MaterialAlertDialogBuilder(this)
.setTitle(title)
.setMessage(message)
.setView(wrapper)

View file

@ -4,13 +4,18 @@
style="@style/textInputLayoutStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/spacer_2x">
android:hint="@string/change_note_title"
android:padding="?attr/dialogPreferredPadding">
<EditText
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/change_note_title"
android:imeOptions="actionDone"
android:importantForAutofill="no"
android:inputType="text" />
android:inputType="text">
<requestFocus />
</com.google.android.material.textfield.TextInputEditText>
</com.google.android.material.textfield.TextInputLayout>