-
Notifications
You must be signed in to change notification settings - Fork 0
/
Register.java
205 lines (179 loc) · 8.08 KB
/
Register.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
package com.example.fbans.projecthm;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.fbans.projecthm.utils.Constants;
import com.example.fbans.projecthm.utils.SharedPreference;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class Register extends AppCompatActivity {
EditText name,mobileno, password,age;
RadioGroup gendergroup;
RadioButton male, female;
Spinner spinner;
Button register, signin;
String gender;
String spinnerValue;
boolean noconn = false;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
if(SharedPreference.getInstance(this).isLoggedIn()) {
SharedPreferences sharedPreferences = this.getSharedPreferences("hospmanagepref", Context.MODE_PRIVATE);
String temp = sharedPreferences.getString("keytype",null);
if (temp.isEmpty() ){
Toast.makeText(this,"Welcome",Toast.LENGTH_LONG).show();
}else {
Intent intent = new Intent(Register.this,MainActivity.class);
intent.putExtra("spinnerValue",temp);
startActivity(intent);
finish();
}
}
name = findViewById(R.id.name);
mobileno = findViewById(R.id.mobileno);
password = findViewById(R.id.password);
age = findViewById(R.id.age);
gendergroup = findViewById(R.id.radioGroup);
register = findViewById(R.id.register);
signin = findViewById(R.id.signin);
male = findViewById(R.id.radioButton);
female = findViewById(R.id.radioButton2);
spinner = findViewById(R.id.spinnerReg);
progressDialog = new ProgressDialog(this);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinnerValue = spinner.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isNetworkAvailable()){
registerUser();
}
else {
noconn=true;
new AlertDialog.Builder(Register.this)
.setTitle("Closing the App")
.setMessage("No Internet Connection,check your settings")
.setPositiveButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.show();
}
}
});
signin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
startActivity(new Intent(Register.this,LoginActivity.class));
}
});
}
private void registerUser(){
final String uname = name.getText().toString().trim();
final String pwd = password.getText().toString().trim();
final String mno = mobileno.getText().toString().trim();
final String usage = age.getText().toString().trim();
if (name.getText().toString().equals("")|| name.getText().toString().length() < 3)
Toast.makeText(getApplicationContext(), "Invalid name", Toast.LENGTH_SHORT).show();
else if(!(name.getText().toString().matches("^[a-zA-Z\\s]*$")))
Toast.makeText(getApplicationContext(), "Invalid name", Toast.LENGTH_SHORT).show();
else if (mobileno.getText().toString().equals("") || mobileno.getText().toString().length() != 10)
Toast.makeText(getApplicationContext(), "Invalid phoneno", Toast.LENGTH_SHORT).show();
else if (password.getText().toString().equals("") || password.getText().toString().length() < 6)
Toast.makeText(getApplicationContext(), "Password should atleast contain 4 characters", Toast.LENGTH_SHORT).show();
else if (age.getText().toString().equals(""))
Toast.makeText(getApplicationContext(), "Enter a age", Toast.LENGTH_SHORT).show();
else if (male.isChecked() || female.isChecked()) {
if(male.isChecked())
gender ="male";
if (female.isChecked())
gender ="female";
}
// else {
progressDialog.setMessage("Registering..");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.REG_URL, new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.getString("re").equalsIgnoreCase("success !")){
Intent intent = new Intent(Register.this,MainActivity.class);
intent.putExtra("spinnerValue",spinnerValue);
startActivity(intent);
finish();
}else {
Toast.makeText(getApplicationContext(),jsonObject.getString("re"),Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
progressDialog.dismiss();
Log.i("Volley Error",volleyError.getMessage());
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String ,String> params = new HashMap<>();
params.put("f1",spinnerValue);
params.put("f2",uname);
params.put("f3",mno);
params.put("f4",pwd);
params.put("f5",usage);
params.put("f6",gender);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
//}
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
}