2020-01-31 16:18:43
package com.xbsaykat.easy24vendorapp;
 
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
 
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
 
import com.github.ybq.android.spinkit.style.Wave;
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.ValueEventListener;
 
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
 
public class Registration extends AppCompatActivity {
    private Button Register, Login;
    private EditText editTextShopName, editTextnumber, editTextemail, editTextpassword, editTextPercentage;
    private EditText editTextShopUserPercentage,editTextpasswordrecheck;
    private String mobile, date, month, shopname, mail, password;
    SharedPreferenceRegistration sharedPreferenceRegistration;
    ProgressBar progressBar;
    FirebaseAuth auth = FirebaseAuth.getInstance();
    DatabaseReference mUsersRef, mAdminRef,mAdmin,mVendorID,mAd;
 
    ArrayAdapter<String> adapter;
    ArrayAdapter<String> adapter2;
    ArrayAdapter<String> adapter3;
    ArrayAdapter<String> adapter4;
 
    ArrayList<String> users = new ArrayList<String>();
    ArrayList<String> users2 = new ArrayList<String>();
    ArrayList<String> users3 = new ArrayList<String>();
    ArrayList<String> users4 = new ArrayList<String>();
    ArrayList<String> VendorNumber = new ArrayList<String>();
 
    Spinner spin, spin2, spin3, spin4;
 
    String Divition, Zilaplace, upPlace, ProCategory, Percentage,passwordcheck ;
 
    int StringPosition, StringPosition2, StringPosition3;
 
    DatabaseReference Lists = FirebaseDatabase.getInstance().getReference().child("location").child("Division");
    DatabaseReference Category = FirebaseDatabase.getInstance().getReference().child("zDiscountCategory");
 
    SharedPreferenceLogIn sharedPreferenceLogIn;
 
    int intID,count=0;
    String StringID;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registrationactivaty);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimary, this.getTheme()));
        } else {
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
 
        spin = (Spinner) findViewById(R.id.spinner1);
        spin2 = (Spinner) findViewById(R.id.spinner2);
        spin3 = (Spinner) findViewById(R.id.spinner3);
        spin4 = (Spinner) findViewById(R.id.spinner4);
 
        sharedPreferenceLogIn = new SharedPreferenceLogIn(getApplicationContext());
        sharedPreferenceRegistration = new SharedPreferenceRegistration(getApplicationContext());
        if (sharedPreferenceRegistration.readRegistrationString()) {
            Intent intent = new Intent(Registration.this, LogInActivaty.class);
            startActivity(intent);
            finish();
        }
        mVendorID=FirebaseDatabase.getInstance().getReference().child("VendorID");
 
 
 
       /* mVendorID.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    String refer = snapshot.getRef().getKey();
                    VendorNumber.add(refer);
                }
            }
 
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
 
            }
        });*/
 
 
 
 
        progressBar = findViewById(R.id.progress);
        Wave wave = new Wave();
        progressBar.setIndeterminateDrawable(wave);
 
        editTextShopName = findViewById(R.id.ShopName);
        editTextnumber = findViewById(R.id.number);
        editTextemail = findViewById(R.id.email);
        editTextpassword = findViewById(R.id.password);
        editTextPercentage = findViewById(R.id.ShopEasyPercentage);
        editTextShopUserPercentage=findViewById(R.id.ShopUserPercentage);
        editTextpasswordrecheck=findViewById(R.id.passwordrecheck);
 
        mUsersRef = FirebaseDatabase.getInstance().getReference().child("Vendors");
        mAdminRef = FirebaseDatabase.getInstance().getReference().child("VendorsRequest");
        mAdmin = FirebaseDatabase.getInstance().getReference().child("Admin");
        mAd = FirebaseDatabase.getInstance().getReference().child("Admin");
 
        date = String.valueOf(android.text.format.DateFormat.format("dd-MM-yyyy", new java.util.Date()));
        month = String.valueOf(android.text.format.DateFormat.format("MM-yyyy", new java.util.Date()));
 
        Register = findViewById(R.id.next);
        Login = findViewById(R.id.next2);
 
        Login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Registration.this, LogInActivaty.class);
                startActivity(intent);
                finish();
            }
        });
 
        Lists.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    String refer = snapshot.child("divname").getValue().toString();
                    users.add(refer);
                }
                adapter = new ArrayAdapter<String>(Registration.this, android.R.layout.simple_spinner_item, users);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spin.setAdapter(adapter);
                spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        if (position == 0) {
                            Toast.makeText(getApplicationContext(), parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
                        } else {
                            spin2.setVisibility(View.VISIBLE);
                            spin3.setVisibility(View.GONE);
                            Toast.makeText(getApplicationContext(), "Selected : " + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
                            Divition = parent.getItemAtPosition(position).toString();
                            StringPosition = position;
                            spin.setEnabled(false);
                            if (!spin.isEnabled()) {
                                ((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);
                            }
 
                            Lists.child(String.valueOf(StringPosition)).child("zila").addValueEventListener(new ValueEventListener() {
                                @Override
                                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                                        String refer = snapshot.child("zilaname").getValue().toString();
                                        users2.add(refer);
 
                                    }
                                    adapter2 = new ArrayAdapter<String>(Registration.this, android.R.layout.simple_spinner_item, users2);
                                    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                                    spin2.setAdapter(adapter2);
                                    spin2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                        @Override
                                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                            if (position == 0) {
                                                Toast.makeText(getApplicationContext(), parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
                                            } else {
                                                Toast.makeText(getApplicationContext(), "Selected : " + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
                                                spin2.setEnabled(false);
                                                if (!spin2.isEnabled()) {
                                                    ((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);
                                                }
                                                Zilaplace = parent.getItemAtPosition(position).toString();
                                                StringPosition2 = position;
                                                spin3.setVisibility(View.VISIBLE);
                                                Lists.child(String.valueOf(StringPosition)).child("zila").child(String.valueOf(StringPosition2)).child("upzila").addValueEventListener(new ValueEventListener() {
                                                    @Override
                                                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                                        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                                                            String refer = snapshot.getValue().toString();
                                                            users3.add(refer);
                                                        }
                                                        adapter3 = new ArrayAdapter<String>(Registration.this, android.R.layout.simple_spinner_item, users3);
                                                        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                                                        spin3.setAdapter(adapter3);
                                                        spin3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                                            @Override
                                                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                                                if (position == 0) {
                                                                    Toast.makeText(getApplicationContext(), parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
                                                                } else {
                                                                    Toast.makeText(getApplicationContext(), "Selected : " + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
                                                                    spin3.setEnabled(false);
                                                                    if (!spin3.isEnabled()) {
                                                                        ((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);
                                                                    }
                                                                    upPlace = parent.getItemAtPosition(position).toString();
                                                                    StringPosition3 = position;
                                                                    spin4.setVisibility(View.VISIBLE);
                                                                    Category.addValueEventListener(new ValueEventListener() {
                                                                        @Override
                                                                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                                                            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                                                                                String refer = snapshot.getValue().toString();
                                                                                users4.add(refer);
                                                                            }
                                                                            adapter4 = new ArrayAdapter<String>(Registration.this, android.R.layout.simple_spinner_item, users4);
                                                                            adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                                                                            spin4.setAdapter(adapter4);
                                                                            spin4.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                                                                @Override
                                                                                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                                                                    if (position == 0) {
                                                                                        Toast.makeText(getApplicationContext(), parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
                                                                                    } else {
                                                                                        Toast.makeText(getApplicationContext(), "Selected : " + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
                                                                                        ProCategory = parent.getItemAtPosition(position).toString();
 
                                                                                    }
                                                                                }
 
                                                                                @Override
                                                                                public void onNothingSelected(AdapterView<?> parent) {
 
                                                                                }
                                                                            });
 
                                                                        }
 
                                                                        @Override
                                                                        public void onCancelled(@NonNull DatabaseError databaseError) {
 
                                                                        }
                                                                    });
                                                                }
                                                            }
 
                                                            @Override
                                                            public void onNothingSelected(AdapterView<?> parent) {
 
                                                            }
                                                        });
 
                                                    }
 
                                                    @Override
                                                    public void onCancelled(@NonNull DatabaseError databaseError) {
 
                                                    }
                                                });
                                            }
                                        }
 
                                        @Override
                                        public void onNothingSelected(AdapterView<?> parent) {
 
                                        }
                                    });
 
                                }
 
                                @Override
                                public void onCancelled(@NonNull DatabaseError databaseError) {
 
                                }
                            });
                        }
                    }
 
                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {
 
                    }
                });
 
            }
 
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
 
            }
        });
 
 
        Register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                check();
            }
        });
    }
 
    private void check() {
 
 
        mobile = editTextnumber.getText().toString().trim();
        shopname = editTextShopName.getText().toString();
        mail = editTextemail.getText().toString();
        password = editTextpassword.getText().toString();
        passwordcheck = editTextpasswordrecheck.getText().toString();
        Percentage = editTextPercentage.getText().toString();
 
        if (editTextShopName.getText().toString().isEmpty()) {
            editTextShopName.setError("Enter Your Shop Name");
            editTextShopName.requestFocus();
        } else if (editTextemail.getText().toString().isEmpty()) {
            editTextemail.setError("Enter Your Email");
            editTextemail.requestFocus();
        } else if (!isValidEmail(mail)) {
            editTextemail.setError("Invalid Email");
            return;
        } else if (editTextpassword.getText().toString().isEmpty()|| editTextpasswordrecheck.getText().toString().isEmpty()) {
            editTextpassword.setError("Enter Your Password");
            editTextpassword.requestFocus();
        }else if (!(password).equals(passwordcheck)) {
            editTextpassword.setError("Password Mismatch");
            editTextpassword.requestFocus();
        } else if (Divition == null) {
            TextView tv = (TextView) spin.getSelectedView();
            tv.setError("বিভাগ নির্বাচন করুন");
        } else if (Zilaplace == null) {
            TextView tv = (TextView) spin2.getSelectedView();
            tv.setError("জেলা নির্বাচন করুন");
        } else if (upPlace == null) {
            TextView tv = (TextView) spin3.getSelectedView();
            tv.setError("স্থান নির্বাচন করুন");
        } else if (ProCategory == null) {
            TextView tv = (TextView) spin4.getSelectedView();
            tv.setError("Select Category");
        } else if (mobile.isEmpty()) {
            editTextnumber.setError("Enter Your Shop Valid number");
            editTextnumber.requestFocus();
            return;
        } else if (editTextPercentage.getText().toString().isEmpty()) {
            editTextPercentage.setError("Enter Easy24 Referral Bonus(%)");
            editTextPercentage.requestFocus();
            return;
        }else if (editTextShopUserPercentage.getText().toString().isEmpty()) {
            editTextShopUserPercentage.setError("Enter Easy24 User's Bonus(%)");
            editTextShopUserPercentage.requestFocus();
            return;
        } else {
            if (!(mobile.length() == 11)) {
                editTextnumber.setError("Enter 11 Digits Only");
                editTextnumber.requestFocus();
                return;
            } else {
                progressBar.setVisibility(View.VISIBLE);
 
                auth.createUserWithEmailAndPassword(mail, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (!task.isSuccessful()) {
                            Toast.makeText(Registration.this, "Email Already Registered!", Toast.LENGTH_SHORT).show();
                            progressBar.setVisibility(View.GONE);
                        } else {
 
                            auth.getCurrentUser().sendEmailVerification().addOnCompleteListener(Registration.this, new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if (task.isSuccessful()) {
                                        Toast.makeText(Registration.this, "Registration Complete! Verification email sent to " + auth.getCurrentUser().getEmail(), Toast.LENGTH_SHORT).show();
                                        mUsersRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("shopname").setValue(shopname);
                                        mUsersRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("mobile").setValue(mobile);
                                        mUsersRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("email").setValue(mail);
                                        mUsersRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("password").setValue(password);
                                        mUsersRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("joindate").setValue(date);
                                        mUsersRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("category").setValue(ProCategory);
                                        mUsersRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("divition").setValue(Divition);
                                        mUsersRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("zila").setValue(Zilaplace);
                                        mUsersRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("upzila").setValue(upPlace);
                                        mUsersRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("shopPercentage").setValue(Percentage);
                                        mUsersRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("confirm").setValue("0");
                                        mUsersRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("amount").setValue("0");
                                        mUsersRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("userPercentage").setValue(editTextShopUserPercentage.getText().toString());
                                        mUsersRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("key").setValue(FirebaseAuth.getInstance().getCurrentUser().getUid());
 
                                        mAdminRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("shopname").setValue(shopname);
                                        mAdminRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("mobile").setValue(mobile);
                                        mAdminRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("email").setValue(mail);
                                        mAdminRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("password").setValue(password);
                                        mAdminRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("joindate").setValue(date);
                                        mAdminRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("category").setValue(ProCategory);
                                        mAdminRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("divition").setValue(Divition);
                                        mAdminRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("zila").setValue(Zilaplace);
                                        mAdminRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("upzila").setValue(upPlace);
                                        mAdminRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("shopPercentage").setValue(Percentage);
                                        mAdminRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("confirm").setValue("0");
                                        mAdminRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("amount").setValue("0");
                                        mAdminRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("userPercentage").setValue(editTextShopUserPercentage.getText().toString());
                                        mAdminRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("key").setValue(FirebaseAuth.getInstance().getCurrentUser().getUid());
 
                                        sharedPreferenceRegistration.writeRegistrationinStatus(true);
                                        sharedPreferenceLogIn.writeLoginStatus(true);
                                        Next();
                                    }
                                }
                            });
                        }
 
                    }
                });
 
            }
        }
 
    }
 
    private boolean isValidEmail(String email) {
        String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
 
        Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }
 
    private void Next() {
        mAdmin.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                StringID=dataSnapshot.child("PresentID").getValue().toString();
                checkLim(StringID);
            }
 
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
 
            }
        });
 
        Next2();
 
 
    }
 
    private void Next2() {
 
        progressBar.setVisibility(View.GONE);
        Intent intent = new Intent(Registration.this, ApproveWaitPage.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        finish();
    }
 
    private void checkLim(String StringID) {
        if (count == 0){
            String i=StringID;
            int nu= Integer.parseInt(i);
            mAd.child("PresentID").setValue((nu+1)+"").toString();
            mUsersRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("ID").setValue((nu+1)+"").toString();
            mAdminRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("ID").setValue((nu+1)+"").toString();
 
        }
        count++;
 
    }
 
 
    @Override
    protected void onStart() {
        super.onStart();
        if (!isConnected(Registration.this)) buildDialog(Registration.this).show();
    }
 
    private boolean isConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netinfo = cm.getActiveNetworkInfo();
 
        if (netinfo != null && netinfo.isConnectedOrConnecting()) {
            NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
 
            if ((mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting()))
                return true;
            else return false;
        } else
            return false;
    }
 
    public android.app.AlertDialog.Builder buildDialog(Context c) {
 
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(c);
        builder.setTitle("No Internet Connection");
        builder.setMessage("You need to have Mobile Data or wifi to access this. Press ok to Exit");
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
 
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        builder.setCancelable(false);
        return builder;
    }
}
 
Invalid Email or Password