1+ package com .example .mealerapp ;
2+
3+ import androidx .appcompat .app .AppCompatActivity ;
4+
5+ import android .content .Intent ;
6+ import android .os .Bundle ;
7+
8+
9+
10+ import java .util .ArrayList ;
11+
12+ import android .view .View ;
13+ import android .widget .ArrayAdapter ;
14+ import android .widget .Button ;
15+ import android .widget .TextView ;
16+
17+ import androidx .annotation .NonNull ;
18+
19+ import com .google .firebase .database .DataSnapshot ;
20+ import com .google .firebase .database .DatabaseError ;
21+ import com .google .firebase .database .DatabaseReference ;
22+ import com .google .firebase .database .FirebaseDatabase ;
23+ import com .google .firebase .database .ValueEventListener ;
24+ import android .widget .EditText ;
25+ import android .widget .Spinner ;
26+
27+ public class EditMenu extends AppCompatActivity {
28+ //private String mealName;
29+ DatabaseReference databaseReference ;
30+ EditText mealNameInput ;
31+
32+ @ Override
33+ protected void onCreate (Bundle savedInstanceState ) {
34+ super .onCreate (savedInstanceState );
35+ setContentView (R .layout .activity_edit_menu );
36+
37+ // get email from MainLogin
38+ String email = MainLogin .emailWithCommas ;
39+
40+ // get meal name (capture text)
41+ EditText mealNameInput = (EditText ) findViewById (R .id .mealNameInput );
42+
43+
44+ ArrayList <String > menu = new ArrayList <>();
45+ ArrayList <String > status = new ArrayList <>();
46+
47+
48+
49+
50+ Button addMeal = (Button ) findViewById (R .id .addMeal );
51+ addMeal .setOnClickListener (new View .OnClickListener () {
52+ @ Override
53+ public void onClick (View v ) {
54+
55+ /// ***** ON CLICk STARTS ////
56+
57+
58+ // !!!!!!!!!!!!!!!!!!! make sure they can't add empty meal //
59+
60+ String mealName = mealNameInput .getText ().toString ();
61+ if (!mealName .isEmpty ()) {
62+ databaseReference = FirebaseDatabase .getInstance ().getReference ("users" );
63+ databaseReference .child (email ).child ("menu" ).addValueEventListener (new ValueEventListener () {
64+
65+ @ Override
66+ public void onDataChange (@ NonNull DataSnapshot snapshot ) {
67+ menu .clear ();
68+ for (DataSnapshot item : snapshot .getChildren ()) {
69+
70+ String meal = item .getKey ().toString ();
71+ menu .add (meal );
72+
73+ }
74+ }
75+
76+ @ Override
77+ public void onCancelled (@ NonNull DatabaseError error ) {
78+
79+ }
80+ });
81+
82+ /// ******* ON CLICK ENDS ///////
83+
84+
85+ /*
86+ * add the meal to the menu*/
87+
88+ if (menu .contains (mealName )) {
89+ // set textView error
90+ mealNameInput .setError ("Meal already exists in menu" );
91+
92+ } else {
93+ databaseReference .child (email ).child ("menu" ).child (mealName ).setValue (false );
94+ mealNameInput .getText ().clear ();
95+ }
96+
97+
98+ } else {
99+ mealNameInput .setError ("Please enter a meal name" );
100+ }
101+
102+
103+ }
104+ });
105+
106+
107+ // REMOVING //
108+ Button removeMeal = (Button ) findViewById (R .id .button5 );
109+
110+
111+ removeMeal .setOnClickListener (new View .OnClickListener () {
112+
113+
114+
115+ @ Override
116+ public void onClick (View v ) {
117+ // ONCLICK STARTS
118+
119+
120+ String mealName = mealNameInput .getText ().toString ();
121+
122+ if (!mealName .isEmpty ()) {
123+ databaseReference = FirebaseDatabase .getInstance ().getReference ("users" );
124+ databaseReference .child (email ).child ("menu" ).addValueEventListener (new ValueEventListener () {
125+
126+
127+
128+ // VALUE EVENT LISTENER STARTS //
129+
130+ @ Override
131+ public void onDataChange (@ NonNull DataSnapshot snapshot ) {
132+ menu .clear ();
133+ status .clear ();
134+ for (DataSnapshot item : snapshot .getChildren ()) {
135+
136+ String meal = item .getKey ().toString ();
137+ String s = item .getValue ().toString ();
138+ if (!menu .contains (meal )) {
139+ menu .add (meal );
140+ status .add (s );
141+ }
142+
143+
144+
145+ }
146+
147+ }
148+
149+ @ Override
150+ public void onCancelled (@ NonNull DatabaseError error ) {
151+
152+ }
153+
154+ // VALUE EVENT LISTENER STOPS //
155+ });
156+
157+
158+
159+ // if you can't find meal in array, then raise error
160+ // if meal is set to false, remove and clear text
161+ // if meal is set to true, set error and clear text
162+
163+ if (menu .isEmpty ()) {
164+ mealNameInput .setError ("Still connecting to firebase. Try again in 1 second" );
165+ } else {
166+
167+ if (menu .contains (mealName ) ) {
168+
169+ int index = menu .indexOf (mealName );
170+ if (status .get (index ).equals ("true" )) {
171+
172+ mealNameInput .getText ().clear ();
173+ mealNameInput .setError ("Cannot delete an offered meal" );
174+ } else {
175+ // remove the item
176+
177+ databaseReference .child (email ).child ("menu" ).child (mealName ).removeValue ();
178+ mealNameInput .getText ().clear ();
179+
180+ }
181+
182+ } else {
183+ mealNameInput .getText ().clear ();
184+ mealNameInput .setError ("Meal not in menu" );
185+ }
186+
187+ }
188+
189+
190+ } else {
191+ mealNameInput .setError ("Please enter a meal name" );
192+ }
193+
194+
195+
196+
197+
198+
199+ }
200+ // ONCLICK ENDS
201+ });
202+
203+ /*
204+ * home button
205+ * */
206+ Button returnHome = (Button ) findViewById (R .id .goHomePage );
207+ returnHome .setOnClickListener (new View .OnClickListener () {
208+ @ Override
209+ public void onClick (View view ) {
210+ openHomePage ();
211+ }
212+ });
213+
214+
215+ }
216+
217+ public void openHomePage (){
218+ Intent intent = new Intent (this , CookSuccessfulLogin .class );
219+ startActivity (intent );
220+ }
221+ }
0 commit comments