1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
package it.niedermann.nextcloud.deck.ui.card.comments;
import static it.niedermann.nextcloud.deck.ui.branding.BrandingUtil.applyBrandToEditTextInputLayout;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import java.util.Objects;
import it.niedermann.nextcloud.deck.R;
import it.niedermann.nextcloud.deck.databinding.DialogAddCommentBinding;
import it.niedermann.nextcloud.deck.ui.branding.BrandedDialogFragment;
public class CardCommentsEditDialogFragment extends BrandedDialogFragment {
private static final String BUNDLE_KEY_COMMENT_ID = "commentId";
private static final String BUNDLE_KEY_COMMENT_MESSAGE = "commentMessage";
private CommentEditedListener addCommentListener;
private DialogAddCommentBinding binding;
private Bundle args;
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
args = getArguments();
if (args == null || !args.containsKey(BUNDLE_KEY_COMMENT_ID)) {
throw new IllegalArgumentException("Please provide at least local comment id");
}
if (getParentFragment() instanceof CommentEditedListener) {
this.addCommentListener = (CommentEditedListener) getParentFragment();
} else if (context instanceof CommentEditedListener) {
this.addCommentListener = (CommentEditedListener) context;
} else {
throw new ClassCastException("Context or parent fragment must implement " + CommentEditedListener.class.getCanonicalName());
}
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
binding = DialogAddCommentBinding.inflate(requireActivity().getLayoutInflater());
return new MaterialAlertDialogBuilder(requireActivity())
.setView(binding.getRoot())
.setTitle(R.string.simple_comment)
.setNeutralButton(android.R.string.cancel, null)
.setPositiveButton(R.string.simple_update, (dialog, which) -> addCommentListener.onCommentEdited(requireArguments().getLong(BUNDLE_KEY_COMMENT_ID), binding.input.getText().toString()))
.create();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (args.containsKey(BUNDLE_KEY_COMMENT_MESSAGE)) {
binding.input.setText(args.getString(BUNDLE_KEY_COMMENT_MESSAGE));
}
binding.input.requestFocus();
Objects.requireNonNull(requireDialog().getWindow()).setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onDestroy() {
super.onDestroy();
this.binding = null;
}
public static DialogFragment newInstance(@NonNull Long commentLocalId, String message) {
final var fragment = new CardCommentsEditDialogFragment();
final var args = new Bundle();
args.putLong(BUNDLE_KEY_COMMENT_ID, commentLocalId);
args.putString(BUNDLE_KEY_COMMENT_MESSAGE, message);
fragment.setArguments(args);
return fragment;
}
@Override
public void applyBrand(int mainColor) {
applyBrandToEditTextInputLayout(mainColor, binding.inputWrapper);
}
}
|