-
-
Notifications
You must be signed in to change notification settings - Fork 177
/
ProfileActivity.java
executable file
·284 lines (237 loc) · 13.5 KB
/
ProfileActivity.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package com.android.iunoob.bloodbank.activities;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.android.iunoob.bloodbank.R;
import com.android.iunoob.bloodbank.viewmodels.UserData;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
public class ProfileActivity extends AppCompatActivity {
private EditText inputemail, inputpassword, retypePassword, fullName, address, contact;
private FirebaseAuth mAuth;
private Button btnSignup;
private ProgressDialog pd;
private Spinner gender, bloodgroup, division;
private boolean isUpdate = false;
private DatabaseReference db_ref, donor_ref;
private FirebaseDatabase db_User;
private CheckBox isDonor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pd = new ProgressDialog(this);
pd.setMessage("Loading...");
pd.setCancelable(true);
pd.setCanceledOnTouchOutside(false);
pd.show();
setContentView(R.layout.activity_profile);
db_User = FirebaseDatabase.getInstance();
db_ref = db_User.getReference("users");
donor_ref = db_User.getReference("donors");
mAuth = FirebaseAuth.getInstance();
inputemail = findViewById(R.id.input_userEmail);
inputpassword = findViewById(R.id.input_password);
retypePassword = findViewById(R.id.input_password_confirm);
fullName = findViewById(R.id.input_fullName);
gender = findViewById(R.id.gender);
address = findViewById(R.id.inputAddress);
division = findViewById(R.id.inputDivision);
bloodgroup = findViewById(R.id.inputBloodGroup);
contact = findViewById(R.id.inputMobile);
isDonor = findViewById(R.id.checkbox);
btnSignup = findViewById(R.id.button_register);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (mAuth.getCurrentUser() != null) {
inputemail.setVisibility(View.GONE);
inputpassword.setVisibility(View.GONE);
retypePassword.setVisibility(View.GONE);
btnSignup.setText("Update Profile");
pd.dismiss();
/// getActionBar().setTitle("Profile");
getSupportActionBar().setTitle("Profile");
findViewById(R.id.image_logo).setVisibility(View.GONE);
isUpdate = true;
Query Profile = db_ref.child(mAuth.getCurrentUser().getUid());
Profile.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
UserData userData = dataSnapshot.getValue(UserData.class);
if (userData != null) {
pd.show();
fullName.setText(userData.getName());
gender.setSelection(userData.getGender());
address.setText(userData.getAddress());
contact.setText(userData.getContact());
bloodgroup.setSelection(userData.getBloodGroup());
division.setSelection(userData.getDivision());
Query donor = donor_ref.child(division.getSelectedItem().toString())
.child(bloodgroup.getSelectedItem().toString())
.child(mAuth.getCurrentUser().getUid());
donor.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists())
{
isDonor.setChecked(true);
isDonor.setText("Unmark this to leave from donors");
}
else
{
Toast.makeText(ProfileActivity.this, "Your are not a donor! Be a donor and save life by donating blood.",
Toast.LENGTH_LONG).show();
}
pd.dismiss();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("User", databaseError.getMessage());
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("User", databaseError.getMessage());
}
});
} else pd.dismiss();
btnSignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String email = inputemail.getText().toString();
final String password = inputpassword.getText().toString();
final String ConfirmPassword = retypePassword.getText().toString();
final String Name = fullName.getText().toString();
final int Gender = gender.getSelectedItemPosition();
final String Contact = contact.getText().toString();
final int BloodGroup = bloodgroup.getSelectedItemPosition();
final String Address = address.getText().toString();
final int Division = division.getSelectedItemPosition();
final String blood = bloodgroup.getSelectedItem().toString();
final String div = division.getSelectedItem().toString();
try {
if (Name.length() <= 2) {
ShowError("Name");
fullName.requestFocusFromTouch();
} else if (Contact.length() < 11) {
ShowError("Contact Number");
contact.requestFocusFromTouch();
} else if (Address.length() <= 2) {
ShowError("Address");
address.requestFocusFromTouch();
} else {
if (!isUpdate) {
if (email.length() == 0) {
ShowError("Email ID");
inputemail.requestFocusFromTouch();
} else if (password.length() <= 5) {
ShowError("Password");
inputpassword.requestFocusFromTouch();
} else if (password.compareTo(ConfirmPassword) != 0) {
Toast.makeText(ProfileActivity.this, "Password did not match!", Toast.LENGTH_LONG)
.show();
retypePassword.requestFocusFromTouch();
} else {
pd.show();
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(ProfileActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(ProfileActivity.this, "Registration failed! try agian.", Toast.LENGTH_LONG)
.show();
Log.v("error", task.getException().getMessage());
} else {
String id = mAuth.getCurrentUser().getUid();
db_ref.child(id).child("Name").setValue(Name);
db_ref.child(id).child("Gender").setValue(Gender);
db_ref.child(id).child("Contact").setValue(Contact);
db_ref.child(id).child("BloodGroup").setValue(BloodGroup);
db_ref.child(id).child("Address").setValue(Address);
db_ref.child(id).child("Division").setValue(Division);
if(isDonor.isChecked())
{
donor_ref.child(div).child(blood).child(id).child("UID").setValue(id).toString();
donor_ref.child(div).child(blood).child(id).child("LastDonate").setValue("Don't donate yet!");
donor_ref.child(div).child(blood).child(id).child("TotalDonate").setValue(0);
donor_ref.child(div).child(blood).child(id).child("Name").setValue(Name);
donor_ref.child(div).child(blood).child(id).child("Contact").setValue(Contact);
donor_ref.child(div).child(blood).child(id).child("Address").setValue(Address);
}
Toast.makeText(getApplicationContext(), "Welcome, your account has been created!", Toast.LENGTH_LONG)
.show();
Intent intent = new Intent(ProfileActivity.this, Dashboard.class);
startActivity(intent);
finish();
}
pd.dismiss();
}
});
}
} else {
String id = mAuth.getCurrentUser().getUid();
db_ref.child(id).child("Name").setValue(Name);
db_ref.child(id).child("Gender").setValue(Gender);
db_ref.child(id).child("Contact").setValue(Contact);
db_ref.child(id).child("BloodGroup").setValue(BloodGroup);
db_ref.child(id).child("Address").setValue(Address);
db_ref.child(id).child("Division").setValue(Division);
if(isDonor.isChecked())
{
donor_ref.child(div).child(blood).child(id).child("UID").setValue(id).toString();
donor_ref.child(div).child(blood).child(id).child("LastDonate").setValue("Don't donate yet!");
donor_ref.child(div).child(blood).child(id).child("TotalDonate").setValue(0);
donor_ref.child(div).child(blood).child(id).child("Name").setValue(Name);
donor_ref.child(div).child(blood).child(id).child("Contact").setValue(Contact);
donor_ref.child(div).child(blood).child(id).child("Address").setValue(Address);
}
else
{
donor_ref.child(div).child(blood).child(id).removeValue();
}
Toast.makeText(getApplicationContext(), "Your account has been updated!", Toast.LENGTH_LONG)
.show();
Intent intent = new Intent(ProfileActivity.this, Dashboard.class);
startActivity(intent);
finish();
}
pd.dismiss();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void ShowError(String error) {
Toast.makeText(ProfileActivity.this, "Please, Enter a valid "+error,
Toast.LENGTH_LONG).show();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
}