Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ClsUser
- -------------
- /**
- * Created by ayalfarhat on 3/14/18.
- */
- public class ClsUser {
- String _id,password,role;
- public ClsUser() {
- }
- public ClsUser(String _id, String password) {
- this._id = _id;
- this.password = password;
- }
- }
- DonateActivity(login)
- ------------
- package com.example.ayalfarhat.mytestibmapp2;
- import android.annotation.SuppressLint;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.cloudant.client.api.ClientBuilder;
- import com.cloudant.client.api.CloudantClient;
- import com.cloudant.client.api.Database;
- import java.util.List;
- public class DonateActivity extends AppCompatActivity {
- //calling user table with correct info
- final String TEXT_API_KEY = "illednensadstublantoompa";
- final String TEXT_API_SECRET = "be61a9e14653f5deb88affac3dc255094e5a90ae";
- final String DB_USER_NAME = "4345d22e-d754-4ad3-8e8a-cf5f0a18158b-bluemix";
- Button btn_login, btnRegDon;
- final String DB_NAME_TEXT = "users";
- EditText input_email, input_password;
- Context context;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_donate);
- setPointer();
- }
- private void setPointer() {
- this.context = this;
- input_email = findViewById(R.id.input_email);
- input_password = findViewById(R.id.input_password);
- btn_login = findViewById(R.id.btn_login);
- btn_login.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String uName = input_email.getText().toString();
- String uPass = input_password.getText().toString();
- Log.e("onClick Login", "Check user");
- checkUser(uName, uPass);
- }
- });
- btnRegDon = findViewById(R.id.btnRegDon);
- btnRegDon.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- openRegDonateActivity();
- }
- });
- }
- @SuppressLint("StaticFieldLeak")
- private void checkUser(final String uName, final String uPass) {
- final ProgressDialog pd = new ProgressDialog(context);
- pd.setTitle("מאמת נתונים....");
- pd.show();
- Log.e("going to async", "now");
- new AsyncTask<Void, Void, Boolean>() {
- @Override
- protected Boolean doInBackground(Void... voids) {
- CloudantClient client = ClientBuilder.account(DB_USER_NAME)
- .username(TEXT_API_KEY)
- .password(TEXT_API_SECRET)
- .build();
- Database db = client.database(DB_NAME_TEXT, false);
- Log.e("in asynctask", "creating json string");
- String myJson = "{\n" +
- " \"selector\": {\n" +
- " \"_id\": \"" + uName + "\",\n" +
- " \"password\": \"" + uPass + "\"\n" +
- " }\n" +
- "}";
- List<UserCls> test = db.findByIndex(myJson, UserCls.class);
- Log.e("data sent", "now we wait");
- //we check the username and userpass exist and match
- if (test.isEmpty()) {
- return false;
- }
- /*
- //we start a new activity which depends on the "role" field
- String role = test.get(0).role;
- Log.e("not empty", "starting new activity, role is " + role);
- if (role.equals("donor")) {
- Log.e("start the role", "role is " + role);
- Intent myIntent = new Intent(context, DonateActivity.class);
- startActivity(myIntent);
- } else if (role.equals("runner")) {
- //todo: create runner activity somewhere and start it here
- // in the meantime we just use toast
- Log.e("connection", "connect runner, role is " + role);
- } else if (role.equals("clerk")) {
- Log.e("connection", "connect clerk, role is " + role);
- }
- */
- return true;
- }
- @Override
- protected void onPostExecute(Boolean aBoolean) {
- pd.dismiss();
- if (!aBoolean) //if the doInBackground returned false, that is if the List was empty (because there is no username with that password)
- {
- Log.e("test is empty", "no match");
- Toast.makeText(context, "Username or password incorrect", Toast.LENGTH_SHORT).show();
- }
- Intent intent = new Intent(context,DonorActivity1.class);
- startActivity(intent);
- }
- //return null;
- }.execute();
- }
- private void openRegDonateActivity() {
- Intent intentAbout = new Intent(this, RegDonate.class);
- startActivity(intentAbout);
- }
- }
- DonorCls
- -----------
- /**
- * Created by ayalfarhat on 3/14/18.
- */
- public class DonorCls {
- String _id, password, role;
- String uAddress, uMail, uMobile;
- public DonorCls(String _id, String password, String uAddress, String uMail, String uMobile) {
- this._id = _id;
- this.password = password;
- this.uAddress = uAddress;
- this.uMail = uMail;
- this.uMobile = uMobile;
- this.role = "donor";
- }
- }
- RegDonate (Regster)
- ------------
- package com.example.ayalfarhat.mytestibmapp2;
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.cloudant.client.api.ClientBuilder;
- import com.cloudant.client.api.CloudantClient;
- import com.cloudant.client.api.Database;
- import java.util.List;
- public class RegDonate extends AppCompatActivity {
- //final for getting access to users table.
- final String TEXT_API_KEY = "illednensadstublantoompa";
- final String TEXT_API_SECRET = "be61a9e14653f5deb88affac3dc255094e5a90ae";
- final String DB_USER_NAME = "4345d22e-d754-4ad3-8e8a-cf5f0a18158b-bluemix";
- final String DB_NAME_TEXT = "users";
- EditText userName, input_address, input_email, input_mobile, userPass1, userPass2;
- Button btnRegister, btnLoginBack;
- Context context;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_reg_donate);
- setPointer();
- }
- private void setPointer() {
- this.context = this;
- userName = findViewById(R.id.userName);
- input_address = findViewById(R.id.input_address);
- input_email = findViewById(R.id.input_email);
- input_mobile = findViewById(R.id.input_mobile);
- userPass1 = findViewById(R.id.userPass1);
- userPass2 = findViewById(R.id.userPass2);
- btnLoginBack = findViewById(R.id.btnLoginBack);
- btnLoginBack.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- goBack();
- }
- });
- btnRegister = findViewById(R.id.btnRegister);
- btnRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- checkPass();
- }
- });
- }
- private void checkPass() {
- //if passwords do not match
- if (!userPass1.getText().toString().equals(userPass2.getText().toString())) {
- Toast.makeText(context, "Passwords do not match", Toast.LENGTH_LONG).show();
- userPass1.setText("");
- userPass2.setText("");
- }
- //then we check if username is unique; if it is, this function will register him/her
- Log.e("onclickRegister", "find if username exists");
- userNameExists(userName.getText().toString());
- }
- private void goBack() {
- Intent backIntent = new Intent(this, DonateActivity.class);
- startActivity(backIntent);
- }
- //todo if does not work - change void to boolean
- @SuppressLint("StaticFieldLeak")
- private boolean userNameExists(final String s) { //s = userName
- Log.e("in userNameExists", "check in database");
- new AsyncTask<Void, Void, Boolean>() {
- @Override
- protected Boolean doInBackground(Void... voids) {
- CloudantClient client = ClientBuilder.account(DB_USER_NAME)
- .username(TEXT_API_KEY)
- .password(TEXT_API_SECRET)
- .build();
- Database db = client.database(DB_NAME_TEXT, false);
- if (db.contains(s)) { //s = userName
- return true;
- }
- return false;
- }
- @Override
- protected void onPostExecute(Boolean aVoid) { //if true = username exists, toast a message.
- if (aVoid) {
- Toast.makeText(context, "Username is taken, try another one", Toast.LENGTH_LONG).show();
- } else {
- //register user, and start a new intent
- Toast.makeText(context, "You are now registered", Toast.LENGTH_LONG).show();
- registerUser(s, userPass1.getText().toString(), input_address.getText().toString(),
- input_email.getText().toString(),
- input_mobile.getText().toString());
- }
- }
- }.execute();
- return true;
- }
- @SuppressLint("StaticFieldLeak")
- private void registerUser(final String uName, final String uPass, final String uAddress, final String uMail, final String uMobile) {
- new AsyncTask<Void, Void, Void>() {
- @Override
- protected Void doInBackground(Void... voids) {
- CloudantClient client = ClientBuilder.account(DB_USER_NAME)
- .username(TEXT_API_KEY)
- .password(TEXT_API_SECRET)
- .build();
- //return database.toString();
- Database db = client.database(DB_NAME_TEXT, false);
- //a java type that can be serialized to json
- DonorCls newDonor = new DonorCls(uName, uPass, uAddress, uMail, uMobile);
- db.save(newDonor);
- Log.e("newDonorRegisterUser", "doInBackGround: new donor was saved in database");
- //todo:check if this is the right place to put that line, if not then in onPostExecute
- registerDonor(uName, uPass, uAddress, uMail, uMobile);
- return null;
- }
- @Override
- protected void onPostExecute(Void aVoid) {
- Toast.makeText(context, "You were registered", Toast.LENGTH_SHORT).show();
- Intent intent = new Intent(context, DonorActivity1.class);
- startActivity(intent);
- }
- }.execute();
- }
- //now save details in "donor" database
- @SuppressLint("StaticFieldLeak")
- private void registerDonor(final String uName, final String uPass, final String uAddress, final String uMail, final String uMobile) {
- final String TEXT_API_KEY = "heyetonithorefurestureds";
- final String TEXT_API_SECRET = "a9ae36badadeede43ad0defda75f545801013ab9";
- final String DB_USER_NAME = "4345d22e-d754-4ad3-8e8a-cf5f0a18158b-bluemix";
- final String DB_NAME_TEXT = "donor";
- new AsyncTask<Void, Void, Void>() {
- @Override
- protected Void doInBackground(Void... voids) {
- CloudantClient client = ClientBuilder.account(DB_USER_NAME)
- .username(TEXT_API_KEY)
- .password(TEXT_API_SECRET)
- .build();
- //return database.toString();
- Database db = client.database(DB_NAME_TEXT, false);
- // A Java type that can be serialized to JSON
- DonorCls newDonor = new DonorCls(uName, uPass, uAddress, uMail, uMobile);
- db.save(newDonor);
- Log.e("newRegisterDonor", "doInBackground: cloudant data was saves..");
- return null;
- }
- }.execute();
- }
- }
- /* @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_reg_donate);
- }
- }
- */
- /*
- EditText userName, userPass1, userPass2,input_address,input_email,input_mobile;
- Button btnRegister;
- Context context;
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_reg_donate);
- setPointer();
- }
- private void setPointer() {
- this.context=this;
- userName = findViewById(R.id.userName1);
- userPass1 = findViewById(R.id.userPass1);
- userPass2 = findViewById(R.id.userPass2);
- btnRegister = findViewById(R.id.btnRegister);
- input_address = findViewById(R.id.input_address);
- input_email = findViewById(R.id.input_email);
- input_mobile = findViewById(R.id.input_mobile);
- btnRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (!userPass1.getText().toString().equals(userPass2.getText().toString())) {
- //if passwords dont match
- Toast.makeText(context, "Passwords don't match", Toast.LENGTH_SHORT).show();
- userPass1.setText("");
- userPass2.setText("");
- }
- else
- {
- //then we check if the username is unique and if it is this function will register him
- Log.e("onclickregister","going to find username");
- userNameExists(userName.getText().toString());
- }
- }
- });
- }
- @SuppressLint("StaticFieldLeak")
- private boolean userNameExists(final String s) {
- final String TEXT_API_KEY = "heyetonithorefurestureds";
- final String TEXT_API_SECRET = "a9ae36badadeede43ad0defda75f545801013ab9";
- final String DB_USER_NAME = "4345d22e-d754-4ad3-8e8a-cf5f0a18158b-bluemix";
- final String DB_NAME_TEXT = "donor";
- Log.e("in userNAmeexist","going to database");
- new AsyncTask<Void, Void, Boolean>() {
- @Override
- protected Boolean doInBackground(Void... voids) {
- CloudantClient client = ClientBuilder.account(DB_USER_NAME)
- .username(TEXT_API_KEY)
- .password(TEXT_API_SECRET)
- .build();
- Database db = client.database(DB_NAME_TEXT, false);
- if(db.contains(s))
- return true;
- return false;
- }
- @Override
- protected void onPostExecute(Boolean aVoid) {
- if (aVoid)
- {
- Toast.makeText(context, "Username already taken, try something else", Toast.LENGTH_SHORT).show();
- }
- else
- {
- //register user and start new intent
- Toast.makeText(context, "congrats!", Toast.LENGTH_SHORT).show();
- registerUser(s,userPass1.getText().toString(),s,userPass2.getText().toString(),);
- }
- }
- }.execute();
- return true;
- }
- @SuppressLint("StaticFieldLeak")
- public void registerUser(final String uName, final String uPass, final String address, final String email, final String phone)
- {
- final String TEXT_API_KEY = "heyetonithorefurestureds";
- final String TEXT_API_SECRET = "a9ae36badadeede43ad0defda75f545801013ab9";
- final String DB_USER_NAME = "4345d22e-d754-4ad3-8e8a-cf5f0a18158b-bluemix";
- final String DB_NAME_TEXT = "donor";
- new AsyncTask<Void, Void, Void>() {
- @Override
- protected Void doInBackground(Void... voids) {
- CloudantClient client = ClientBuilder.account(DB_USER_NAME)
- .username(TEXT_API_KEY)
- .password(TEXT_API_SECRET)
- .build();
- //return databases.toString();
- Database db = client.database(DB_NAME_TEXT, false);
- // A Java type that can be serialized to JSON
- RegDonate newDonor = new RegDonate(uName,uPass, address, email, phone);
- db.save(newDonor);
- Log.e("TAG", "doInBackground: cloudant data was saved.... " );
- return null;
- }
- @Override
- protected void onPostExecute(Void aVoid) {
- Toast.makeText(context, "You were registered", Toast.LENGTH_SHORT).show();
- //now starting the new activity
- Intent myIntent = new Intent(context,DonorActivity1.class);
- startActivity(myIntent);
- }
- }.execute();
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement