4
4
5
5
import android .annotation .SuppressLint ;
6
6
import android .content .Context ;
7
+ import android .content .DialogInterface ;
7
8
import android .content .Intent ;
8
9
import android .util .Log ;
9
10
import android .view .LayoutInflater ;
10
11
import android .view .View ;
11
12
import android .view .ViewGroup ;
13
+ import android .widget .Button ;
12
14
import android .widget .ImageView ;
13
15
import android .widget .RelativeLayout ;
14
16
import android .widget .TextView ;
15
17
import android .widget .Toast ;
16
18
17
19
import androidx .annotation .NonNull ;
20
+ import androidx .appcompat .app .AlertDialog ;
18
21
import androidx .cardview .widget .CardView ;
19
22
import androidx .recyclerview .widget .RecyclerView ;
20
23
import androidx .transition .TransitionManager ;
@@ -29,9 +32,11 @@ public class BookRecViewAdapter extends RecyclerView.Adapter<BookRecViewAdapter.
29
32
30
33
private ArrayList <Book > books = new ArrayList <>();
31
34
private Context mContext ;
35
+ private String parentActivity ;
32
36
33
- public BookRecViewAdapter (Context mContext ) {
37
+ public BookRecViewAdapter (Context mContext , String parentActivity ) {
34
38
this .mContext = mContext ;
39
+ this .parentActivity = parentActivity ;
35
40
}
36
41
37
42
@ NonNull
@@ -70,6 +75,127 @@ public void onClick(View v) {
70
75
TransitionManager .beginDelayedTransition (holder .parent );
71
76
holder .expandedRelLayout .setVisibility (View .VISIBLE );
72
77
holder .downArrow .setVisibility (View .GONE );
78
+ String removedBookName = books .get (position ).getName ();
79
+
80
+ if (parentActivity .equals ("allBooks" )) {
81
+ holder .btnDelete .setVisibility (View .GONE );
82
+ } else if (parentActivity .equals ("alreadyReadBook" )) {
83
+ holder .btnDelete .setVisibility (View .VISIBLE );
84
+ // String removedBookName = books.get(position).getName();
85
+ holder .btnDelete .setOnClickListener (new View .OnClickListener () {
86
+ @ Override
87
+ public void onClick (View v ) {
88
+ AlertDialog .Builder builder = new AlertDialog .Builder (mContext );
89
+ builder .setMessage ("Are you sure you want to delete " + removedBookName );
90
+ builder .setPositiveButton ("Yes" , new DialogInterface .OnClickListener () {
91
+ @ Override
92
+ public void onClick (DialogInterface dialog , int which ) {
93
+ if (Utils .getInstance ().removeFromAlreadyRead (books .get (position ))) {
94
+ Toast .makeText (mContext , removedBookName + " Removed!" , Toast .LENGTH_SHORT ).show ();
95
+ notifyDataSetChanged ();
96
+ } else {
97
+ Toast .makeText (mContext , "Something wrong happened,\n Please try again!" , Toast .LENGTH_SHORT ).show ();
98
+ }
99
+ }
100
+ });
101
+ builder .setNegativeButton ("No" , new DialogInterface .OnClickListener () {
102
+ @ Override
103
+ public void onClick (DialogInterface dialog , int which ) {
104
+ //
105
+ }
106
+ });
107
+
108
+ builder .create ().show ();
109
+ }
110
+ });
111
+ } else if (parentActivity .equals ("wantToReadBooks" )) {
112
+ holder .btnDelete .setVisibility (View .VISIBLE );
113
+ // String removedBookName = books.get(position).getName();
114
+ holder .btnDelete .setOnClickListener (new View .OnClickListener () {
115
+ @ Override
116
+ public void onClick (View v ) {
117
+ AlertDialog .Builder builder = new AlertDialog .Builder (mContext );
118
+ builder .setMessage ("Are you sure you want to delete " + removedBookName );
119
+ builder .setPositiveButton ("Yes" , new DialogInterface .OnClickListener () {
120
+ @ Override
121
+ public void onClick (DialogInterface dialog , int which ) {
122
+ if (Utils .getInstance ().removeFromWantToRead (books .get (position ))) {
123
+ Toast .makeText (mContext , removedBookName + " Removed!" , Toast .LENGTH_SHORT ).show ();
124
+ notifyDataSetChanged ();
125
+ } else {
126
+ Toast .makeText (mContext , "Something wrong happened,\n Please try again!" , Toast .LENGTH_SHORT ).show ();
127
+ }
128
+ }
129
+ });
130
+ builder .setNegativeButton ("No" , new DialogInterface .OnClickListener () {
131
+ @ Override
132
+ public void onClick (DialogInterface dialog , int which ) {
133
+ //
134
+ }
135
+ });
136
+
137
+ builder .create ().show ();
138
+ }
139
+ });
140
+ } else if (parentActivity .equals (("currentlyReadingBooks" ))) {
141
+ holder .btnDelete .setVisibility (View .VISIBLE );
142
+ // String removedBookName = books.get(position).getName();
143
+ holder .btnDelete .setOnClickListener (new View .OnClickListener () {
144
+ @ Override
145
+ public void onClick (View v ) {
146
+ AlertDialog .Builder builder = new AlertDialog .Builder (mContext );
147
+ builder .setMessage ("Are you sure you want to delete " + removedBookName );
148
+ builder .setPositiveButton ("Yes" , new DialogInterface .OnClickListener () {
149
+ @ Override
150
+ public void onClick (DialogInterface dialog , int which ) {
151
+ if (Utils .getInstance ().removeFromCurrentlyReading (books .get (position ))) {
152
+ Toast .makeText (mContext , removedBookName + " Removed!" , Toast .LENGTH_SHORT ).show ();
153
+ notifyDataSetChanged ();
154
+ } else {
155
+ Toast .makeText (mContext , "Something wrong happened,\n Please try again!" , Toast .LENGTH_SHORT ).show ();
156
+ }
157
+ }
158
+ });
159
+ builder .setNegativeButton ("No" , new DialogInterface .OnClickListener () {
160
+ @ Override
161
+ public void onClick (DialogInterface dialog , int which ) {
162
+ //
163
+ }
164
+ });
165
+
166
+ builder .create ().show ();
167
+ }
168
+ });
169
+ } else {
170
+ holder .btnDelete .setVisibility (View .VISIBLE );
171
+ // String removedBookName = books.get(position).getName();
172
+ holder .btnDelete .setOnClickListener (new View .OnClickListener () {
173
+ @ Override
174
+ public void onClick (View v ) {
175
+ AlertDialog .Builder builder = new AlertDialog .Builder (mContext );
176
+ builder .setMessage ("Are you sure you want to delete " + removedBookName );
177
+ builder .setPositiveButton ("Yes" , new DialogInterface .OnClickListener () {
178
+ @ Override
179
+ public void onClick (DialogInterface dialog , int which ) {
180
+ if (Utils .getInstance ().removeFromFavourite (books .get (position ))) {
181
+ Toast .makeText (mContext , removedBookName + " Removed!" , Toast .LENGTH_SHORT ).show ();
182
+ notifyDataSetChanged ();
183
+ } else {
184
+ Toast .makeText (mContext , "Something wrong happened,\n Please try again!" , Toast .LENGTH_SHORT ).show ();
185
+ }
186
+ }
187
+ });
188
+ builder .setNegativeButton ("No" , new DialogInterface .OnClickListener () {
189
+ @ Override
190
+ public void onClick (DialogInterface dialog , int which ) {
191
+ //
192
+ }
193
+ });
194
+
195
+ builder .create ().show ();
196
+ }
197
+ });
198
+ }
73
199
} else {
74
200
TransitionManager .beginDelayedTransition (holder .parent );
75
201
holder .expandedRelLayout .setVisibility (View .GONE );
@@ -95,6 +221,7 @@ public class ViewHolder extends RecyclerView.ViewHolder {
95
221
private ImageView downArrow , upArrow ;
96
222
private RelativeLayout expandedRelLayout ;
97
223
private TextView txtAuthor , txtDescription ;
224
+ private TextView btnDelete ;
98
225
99
226
public ViewHolder (@ NonNull View itemView ) {
100
227
super (itemView );
@@ -109,6 +236,7 @@ public ViewHolder(@NonNull View itemView) {
109
236
expandedRelLayout = itemView .findViewById (R .id .expandedRelLayout );
110
237
txtAuthor = itemView .findViewById (R .id .txtAuthor );
111
238
txtDescription = itemView .findViewById (R .id .shortDesc );
239
+ btnDelete = itemView .findViewById (R .id .btnDelete );
112
240
113
241
// set click listener
114
242
downArrow .setOnClickListener (new View .OnClickListener () {
0 commit comments