-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShopInfoEditActivity.java
180 lines (145 loc) · 6.8 KB
/
ShopInfoEditActivity.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
package com.ikai.unitshop;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
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.ValueEventListener;
import java.util.HashMap;
import java.util.Map;
public class ShopInfoEditActivity extends AppCompatActivity implements View.OnClickListener {
private EditText shopNameEdit, mobNumberEdit, shopAddressEdit, pinCodeEdit;
private Seller sellerDetails;
private DatabaseReference mUserRef;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop_info_edit);
// Get reference to progress dialog.
progressDialog = new ProgressDialog(this);
// Get reference to all edit texts.
shopNameEdit = findViewById(R.id.shop_name_edit_text);
mobNumberEdit = findViewById(R.id.mobile_number_edit_text);
shopAddressEdit = findViewById(R.id.shop_address_edit_text);
pinCodeEdit = findViewById(R.id.pin_code_edit_text);
// Get reference to firebase database reference.
mUserRef = FirebaseDatabase.getInstance()
.getReference()
.child("UNiTSellers")
.child(FirebaseAuth.getInstance().getUid());
// Fill previous values into their respective field.
fillPreviousValues();
// Get reference to save and cancel button and set on click listener.
Button saveButton = findViewById(R.id.save_button);
Button cancelButton = findViewById(R.id.cancel_button);
saveButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);
}
// Helper function to fill previous values to their respective fields.
private void fillPreviousValues() {
// Get values from firebase database and fill respective field.
mUserRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot != null) {
sellerDetails = dataSnapshot.getValue(Seller.class);
if (sellerDetails != null) {
// Set the values.
shopNameEdit.setText(sellerDetails.shopName);
mobNumberEdit.setText(sellerDetails.sellerMobNumber);
shopAddressEdit.setText(sellerDetails.shopAddress);
pinCodeEdit.setText(sellerDetails.shopPinCode);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onClick(View view) {
int id = view.getId();
// Check for save button click.
if (id == R.id.save_button) {
// Show loading dialog while updating the seller data.
showLoadingDialogue("Saving the data...");
// Get everything from edit text and save it to database.
// First create a map and insert all data in it.
Map<String, Object> newSellerData = new HashMap<>();
newSellerData.put("shopName", shopNameEdit.getText().toString());
newSellerData.put("sellerMobNumber", mobNumberEdit.getText().toString());
newSellerData.put("shopAddress", shopAddressEdit.getText().toString());
newSellerData.put("shopPinCode", pinCodeEdit.getText().toString());
// Now update the database atomically.
mUserRef.updateChildren(newSellerData, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError,
DatabaseReference databaseReference) {
// Dismiss the progress dialog if showing and finish this activity.
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
// Check whether there is some error or not. If yes then show it to
// seller and if no then simply exit.
if (databaseError == null) {
finish();
} else {
// Show error why updation is canceled / failed.
showDatabaseFetchError(databaseError);
}
}
});
}
// Check for cancel button click.
if (id == R.id.cancel_button) {
// Simply finish this activity.
finish();
}
}
// Helper function to show error occurred during database fetching.
private void showDatabaseFetchError(DatabaseError databaseError) {
switch (databaseError.getCode()) {
case DatabaseError.DISCONNECTED:
showErrorMessageDialogue("Error in saving the data. " +
"You are disconnected. Please try again in a little bit");
break;
case DatabaseError.NETWORK_ERROR:
showErrorMessageDialogue("Error in saving tha data. " +
"Please make sure you have internet connection.");
break;
case DatabaseError.UNAVAILABLE:
showErrorMessageDialogue("Error in saving the data. " +
"Server is unavailable. We are sorry for that. Try " +
"again in a little bit");
break;
default:
showErrorMessageDialogue("Error in saving the data. " +
"Some unknown error has just happened. Please try " +
"again in a little bit");
}
}
// Helper function to show the error message.
private void showErrorMessageDialogue(final String errorMessage) {
ErrorMessageDialogue msgDialog = new ErrorMessageDialogue();
Bundle bundle = new Bundle();
bundle.putString("Message", errorMessage);
msgDialog.setArguments(bundle);
msgDialog.show(getSupportFragmentManager(), "Message");
}
// Helper function to show the loading dialogue.
private void showLoadingDialogue(final String loadingMessage) {
progressDialog.setMessage(loadingMessage);
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(false);
progressDialog.show();
}
}