Skip to content

Commit 34a9f32

Browse files
Builder Pattern and Listeners Added
1 parent fd42236 commit 34a9f32

File tree

4 files changed

+227
-156
lines changed

4 files changed

+227
-156
lines changed

selectionalertdialog/src/main/java/com/drapps/selectionalertdialog/MulitpleSelectionAdapter.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import android.view.ViewGroup;
99
import android.widget.CheckBox;
1010

11+
import com.drapps.selectionalertdialog.MultiSelection;
12+
import com.drapps.selectionalertdialog.MultiSelectionListener;
13+
import com.drapps.selectionalertdialog.R;
14+
1115
import java.util.ArrayList;
1216
import java.util.Arrays;
1317
import java.util.List;
@@ -65,14 +69,20 @@ public void onBindViewHolder(final MulitpleSelectionHolder holder, final int pos
6569
}
6670
holder.line.setBackgroundColor(color);
6771
} catch (Exception e) {
68-
multiSelectionListener.onMultiDialogError(e.toString(), tag);
72+
if (multiSelectionListener != null) {
73+
74+
multiSelectionListener.onMultiDialogError(e.toString(), tag);
75+
}
6976
}
7077
}
7178
if (textColor != 0) {
7279
try {
7380
holder.checkBox.setTextColor(ColorStateList.valueOf(textColor));
7481
} catch (Exception e) {
75-
multiSelectionListener.onMultiDialogError(e.toString(), tag);
82+
if (multiSelectionListener != null) {
83+
84+
multiSelectionListener.onMultiDialogError(e.toString(), tag);
85+
}
7686
}
7787
}
7888

@@ -113,7 +123,10 @@ public boolean checkExist(String dataString) {
113123
}
114124
}
115125
} else {
116-
multiSelectionListener.onMultiDialogError("List is null or empty", tag);
126+
if (multiSelectionListener != null) {
127+
128+
multiSelectionListener.onMultiDialogError("List is null or empty", tag);
129+
}
117130
}
118131

119132

selectionalertdialog/src/main/java/com/drapps/selectionalertdialog/MultiSelectionDialog.java

Lines changed: 99 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@
2424
import androidx.recyclerview.widget.LinearLayoutManager;
2525
import androidx.recyclerview.widget.RecyclerView;
2626

27+
import android.app.Dialog;
28+
import android.content.Context;
29+
import android.content.res.ColorStateList;
30+
import android.os.Bundle;
31+
import android.util.Log;
32+
import android.view.LayoutInflater;
33+
import android.view.View;
34+
import android.widget.ImageView;
35+
import android.widget.LinearLayout;
36+
import android.widget.TextView;
37+
38+
import java.util.ArrayList;
39+
40+
import androidx.annotation.Nullable;
41+
import androidx.appcompat.app.AppCompatActivity;
42+
import androidx.recyclerview.widget.LinearLayoutManager;
43+
import androidx.recyclerview.widget.RecyclerView;
44+
2745
public class MultiSelectionDialog extends AppCompatActivity {
2846

2947
Dialog dialog;
@@ -32,10 +50,9 @@ public class MultiSelectionDialog extends AppCompatActivity {
3250
private ArrayList<MultiSelection> multiList = new ArrayList<>();
3351
private ArrayList<MultiSelection> temp_data_list = new ArrayList<>();
3452
private String headerTitle = "Select";
35-
private Boolean isSearchEnabled = false;
3653
MulitpleSelectionAdapter dialogAdapter;
3754
private String currentField = "", currentValue = "", currentPosition = "", tag = "", hintText = "Search here";
38-
private int headerColor,textColor;
55+
private int headerColor, textColor;
3956
MultiSelectionListener multiSelectionListener;
4057

4158

@@ -45,17 +62,10 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
4562
setContentView(R.layout.single_selection_dialog);
4663
}
4764

48-
public MultiSelectionDialog(MultiSelectionListener mMultiSelectionListener, Context mContext, String mTag) {
49-
this.multiSelectionListener = mMultiSelectionListener;
50-
this.context = mContext;
51-
this.tag = mTag;
52-
}
53-
54-
55-
public void setContent(ArrayList<String> contentProvide) {
56-
list = contentProvide;
65+
private MultiSelectionDialog(Builder builder) {
66+
context = builder.context;
67+
list = builder.list;
5768
temp_data_list.clear();
58-
5969
if (list != null && list.size() > 0) {
6070
for (int i = 0; i < list.size(); i++) {
6171
MultiSelection multiSelection = new MultiSelection();
@@ -64,35 +74,22 @@ public void setContent(ArrayList<String> contentProvide) {
6474
temp_data_list.add(multiSelection);
6575
}
6676
}
67-
68-
}
69-
70-
public void setTitle(String mTitle) {
71-
if (mTitle != null && !mTitle.equals("")) {
72-
this.headerTitle = mTitle;
73-
} else {
74-
this.headerTitle = "Select";
75-
}
77+
headerTitle = builder.headerTitle;
78+
tag = builder.tag;
79+
hintText = builder.hintText;
80+
currentField = builder.currentField;
81+
headerColor = builder.headerColor;
82+
textColor = builder.textColor;
83+
multiSelectionListener = builder.multiSelectionListener;
84+
Log.d("TAG--", headerTitle);
7685
}
7786

78-
public void setTextColor(int color){
79-
this.textColor = color;
80-
}
81-
// public void enableSearch(Boolean value, String hint) {
82-
// isSearchEnabled = value;
83-
// hintText = hint;
84-
// }
85-
86-
public void setColor(int color) {
87-
headerColor = color;
88-
}
8987

9088
public void setSelectedFields(String selectedField) {
9189
currentField = selectedField;
9290
}
9391

9492
public void show() {
95-
//Custom pop up dialog for selecting options
9693
try {
9794
dialog = new Dialog(context);
9895
final View convertView = LayoutInflater.from(context).inflate(R.layout.multi_selection_dialog, null);
@@ -103,28 +100,15 @@ public void show() {
103100
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context);
104101
recyclerView.setLayoutManager(layoutManager);
105102
LinearLayout header = convertView.findViewById(R.id.linear_multi_dialog);
106-
// final EditText etSearch = convertView.findViewById(R.id.et_search_single_selection);
107103
tvTitle.setText(headerTitle);
108-
//
109-
// if (isSearchEnabled) {
110-
// etSearch.setVisibility(View.VISIBLE);
111-
// } else {
112-
// etSearch.setVisibility(View.GONE);
113-
// }
104+
114105
if (headerColor != 0) {
115106
try {
116107
header.setBackgroundColor(headerColor);
117-
ColorStateList colorStateList = ColorStateList.valueOf(headerColor);
118-
// ViewCompat.setBackgroundTintList(etSearch, colorStateList);
119108
} catch (Exception e) {
120109
e.printStackTrace();
121110
}
122111
}
123-
//
124-
// if (hintText != null && !hintText.equals("")) {
125-
// etSearch.setHint(hintText);
126-
// }
127-
128112

129113
recyclerView.addOnItemTouchListener(
130114
new RecyclerItemClickListener(context, recyclerView,
@@ -156,53 +140,28 @@ public void onClick(View view) {
156140
assetValue = getAssetsValue.substring(0, getAssetsValue.length() - 1);
157141
}
158142
}
159-
multiSelectionListener.onMultiDialogItemsSelected(assetValue, tag, temp_string_list);
143+
if (multiSelectionListener != null) {
144+
multiSelectionListener.onMultiDialogItemsSelected(assetValue, tag, temp_string_list);
145+
}
160146
} else {
161-
multiSelectionListener.onMultiDialogError("List is null or empty", tag);
147+
if (multiSelectionListener != null) {
148+
multiSelectionListener.onMultiDialogError("List is null or empty", tag);
149+
}
162150
}
163151

164152
dialog.dismiss();
165153

166154
}
167155
});
168156

169-
// etSearch.addTextChangedListener(new TextWatcher() {
170-
// @Override
171-
// public void beforeTextChanged(CharSequence s, int start, int count, int after) {
172-
// }
173-
//
174-
// @Override
175-
// public void onTextChanged(CharSequence s, int start, int before, int count) {
176-
//
177-
// if (etSearch.getText().toString().equals("")) {
178-
// if (list != null && list.size() > 0) {
179-
// dialogAdapter = new SingleSelectionAdapter(list, context, currentField, headerColor);
180-
// dialogAdapter.notifyDataSetChanged();
181-
// recyclerView.setAdapter(dialogAdapter);
182-
// } else {
183-
// singleSelectionListener.onDialogError("List is empty or null", tag);
184-
// }
185-
//
186-
//
187-
// } else {
188-
// getSearch(etSearch.getText().toString(), recyclerView);
189-
// }
190-
//
191-
// }
192-
//
193-
// @Override
194-
// public void afterTextChanged(Editable s) {
195-
//
196-
// }
197-
//// });
198-
//
199157
if (list != null && list.size() > 0) {
200-
dialogAdapter = new MulitpleSelectionAdapter(multiSelectionListener,multiList, currentField,tag, context, headerColor,textColor);
158+
dialogAdapter = new MulitpleSelectionAdapter(multiSelectionListener, multiList, currentField, tag, context, headerColor, textColor);
201159
recyclerView.setAdapter(dialogAdapter);
202160
dialog.show();
203-
} else {
161+
} else { if (multiSelectionListener!=null){
162+
204163
multiSelectionListener.onMultiDialogError("List is null or empty", tag);
205-
}
164+
}}
206165

207166
dialog.show();
208167
} catch (Exception e) {
@@ -211,29 +170,64 @@ public void onClick(View view) {
211170

212171
}
213172

214-
// private void getSearch(String search, RecyclerView recyclerView) {
215-
// ArrayList<String> temp_list = new ArrayList<>();
216-
// if (list != null && list.size() > 0) {
217-
// for (int i = 0; i < list.size(); i++) {
218-
// if (list.get(i).toLowerCase().contains(search.toLowerCase())) {
219-
// temp_list.add(list.get(i));
220-
//
221-
// }
222-
// }
223-
// } else {
224-
// singleSelectionListener.onDialogError("List is empty or null", tag);
225-
//
226-
// }
227-
//
228-
// temp_data_list = new ArrayList<>();
229-
// temp_data_list.addAll(temp_list);
230-
// dialogAdapter = new SingleSelectionAdapter(temp_data_list, context, currentField, headerColor);
231-
//
232-
// dialogAdapter.notifyDataSetChanged();
233-
// recyclerView.setAdapter(dialogAdapter);
234-
//
235-
//
236-
// }
237173

174+
public static class Builder {
175+
//required
176+
177+
//optional
178+
179+
Dialog dialog;
180+
private Context context;
181+
private ArrayList<String> list = new ArrayList<>();
182+
private String headerTitle = "Select";
183+
private String currentField = "", tag = "", hintText = "Search here";
184+
private int headerColor, textColor;
185+
MultiSelectionListener multiSelectionListener;
186+
187+
public Builder(Context ctx, String tag) {
188+
this.context = ctx;
189+
this.tag = tag;
190+
}
191+
192+
public Builder setContent(ArrayList<String> contentProvide) {
193+
this.list = contentProvide;
194+
return this;
195+
}
196+
197+
public Builder setTitle(String value) {
198+
if (value != null && !value.equals("")) {
199+
this.headerTitle = value;
200+
} else {
201+
this.headerTitle = "Select";
202+
}
203+
return this;
204+
}
205+
206+
public Builder setTextColor(int color) {
207+
this.textColor = color;
208+
return this;
209+
}
210+
211+
212+
public Builder setColor(int color) {
213+
headerColor = color;
214+
return this;
215+
}
216+
217+
public Builder setListener(MultiSelectionListener listener) {
218+
multiSelectionListener = listener;
219+
return this;
220+
}
221+
222+
223+
public Builder setSelectedField(String selectedField) {
224+
currentField = selectedField;
225+
return this;
226+
}
227+
228+
public MultiSelectionDialog build() {
229+
return new MultiSelectionDialog(this);
230+
}
231+
}
238232

239233
}

selectionalertdialog/src/main/java/com/drapps/selectionalertdialog/SingleSelectionAdapter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,20 @@ public void onBindViewHolder(SingleSelectionHolder holder, int position) {
6161
}
6262
holder.line.setBackgroundColor(color);
6363
} catch (Exception e) {
64-
singleSelectionListener.onDialogError(e.toString(), tag);
64+
if (singleSelectionListener != null) {
6565

66+
singleSelectionListener.onDialogError(e.toString(), tag);
67+
}
6668
}
6769
}
6870
if (color != 0) {
6971
try {
7072
holder.radioButton.setTextColor(ColorStateList.valueOf(textColor));
7173
} catch (Exception e) {
72-
singleSelectionListener.onDialogError(e.toString(), tag);
74+
if (singleSelectionListener != null) {
75+
76+
singleSelectionListener.onDialogError(e.toString(), tag);
77+
}
7378
}
7479
}
7580
if (dataList.get(position).equals(currentField)) {

0 commit comments

Comments
 (0)