-
Notifications
You must be signed in to change notification settings - Fork 74
/
RNPromptFragment.java
179 lines (152 loc) · 6.22 KB
/
RNPromptFragment.java
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package im.shimo.react.prompt;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.widget.EditText;
import androidx.appcompat.app.AlertDialog;
import javax.annotation.Nullable;
public class RNPromptFragment extends DialogFragment implements DialogInterface.OnClickListener {
/* package */ static final String ARG_TITLE = "title";
/* package */ static final String ARG_MESSAGE = "message";
/* package */ static final String ARG_BUTTON_POSITIVE = "button_positive";
/* package */ static final String ARG_BUTTON_NEGATIVE = "button_negative";
/* package */ static final String ARG_BUTTON_NEUTRAL = "button_neutral";
/* package */ static final String ARG_ITEMS = "items";
/* package */ static final String ARG_TYPE = "type";
/* package */ static final String ARG_STYLE = "style";
/* package */ static final String ARG_DEFAULT_VALUE = "defaultValue";
/* package */ static final String ARG_PLACEHOLDER = "placeholder";
private EditText mInputText;
public enum PromptTypes {
TYPE_DEFAULT("default"),
PLAIN_TEXT("plain-text"),
SECURE_TEXT("secure-text"),
NUMERIC("numeric"),
EMAIL_ADDRESS("email-address"),
PHONE_PAD("phone-pad");
private final String mName;
PromptTypes(final String name) {
mName = name;
}
@Override
public String toString() {
return mName;
}
}
private
@Nullable
RNPromptModule.PromptFragmentListener mListener;
public RNPromptFragment() {
mListener = null;
}
public void setListener(@Nullable RNPromptModule.PromptFragmentListener listener) {
mListener = listener;
}
public Dialog createDialog(Context activityContext, Bundle arguments) {
AlertDialog.Builder builder;
String style = arguments.containsKey(ARG_STYLE) ? arguments.getString(ARG_STYLE) : "default";
style = style != null ? style : "default";
// AlertDialog style
switch (style) {
case "shimo":
builder = new AlertDialog.Builder(activityContext, R.style.ShimoAlertDialogStyle);
break;
default:
builder = new AlertDialog.Builder(activityContext);
}
builder.setTitle(arguments.getString(ARG_TITLE));
if (arguments.containsKey(ARG_BUTTON_POSITIVE)) {
builder.setPositiveButton(arguments.getString(ARG_BUTTON_POSITIVE), this);
}
if (arguments.containsKey(ARG_BUTTON_NEGATIVE)) {
builder.setNegativeButton(arguments.getString(ARG_BUTTON_NEGATIVE), this);
}
if (arguments.containsKey(ARG_BUTTON_NEUTRAL)) {
builder.setNeutralButton(arguments.getString(ARG_BUTTON_NEUTRAL), this);
}
// if both message and items are set, Android will only show the message
// and ignore the items argument entirely
if (arguments.containsKey(ARG_MESSAGE)) {
builder.setMessage(arguments.getString(ARG_MESSAGE));
}
if (arguments.containsKey(ARG_ITEMS)) {
builder.setItems(arguments.getCharSequenceArray(ARG_ITEMS), this);
}
AlertDialog alertDialog = builder.create();
// input style
LayoutInflater inflater = LayoutInflater.from(activityContext);
final EditText input;
switch (style) {
case "shimo":
input = (EditText) inflater.inflate(R.layout.edit_text, null);
break;
default:
input = new EditText(activityContext);
}
// input type
int type = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
if (arguments.containsKey(ARG_TYPE)) {
String typeString = arguments.getString(ARG_TYPE);
if (typeString != null) {
switch (typeString) {
case "secure-text":
type = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;
break;
case "numeric":
type = InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_NUMBER;
break;
case "email-address":
type = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
break;
case "phone-pad":
type = InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_PHONE;
break;
case "plain-text":
default:
type = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
}
}
}
input.setInputType(type);
if (arguments.containsKey(ARG_DEFAULT_VALUE)) {
String defaultValue = arguments.getString(ARG_DEFAULT_VALUE);
if (defaultValue != null) {
input.setText(defaultValue);
int textLength = input.getText().length();
input.setSelection(textLength, textLength);
}
}
if (arguments.containsKey(ARG_PLACEHOLDER)) {
input.setHint(arguments.getString(ARG_PLACEHOLDER));
}
alertDialog.setView(input, 50, 15, 50, 0);
mInputText = input;
return alertDialog;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = this.createDialog(getActivity(), getArguments());
if (mInputText.requestFocus()) {
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
return dialog;
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (mListener != null) {
mListener.onConfirm(which, mInputText.getText().toString());
}
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
if (mListener != null) {
mListener.onDismiss(dialog);
}
}
}