2020-02-01 15:45:21
package com.xbsaykat.easy24vendorapp;
 
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
 
import com.github.ybq.android.spinkit.style.CubeGrid;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
 
import android.util.Log;
import android.view.View;
 
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.core.app.ShareCompat;
import androidx.core.view.GravityCompat;
import androidx.appcompat.app.ActionBarDrawerToggle;
 
import android.view.MenuItem;
 
import com.google.android.material.navigation.NavigationView;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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 androidx.drawerlayout.widget.DrawerLayout;
 
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
 
import android.view.Menu;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
 
import java.util.ArrayList;
 
public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    DatabaseReference mUsersRef,mUsersDiscount,mAdminDiscount,mReportDaily,mReportMonthly,mReportYearly;
    EditText editTextNumber,editTextAmonut;
    TextView Done;
    String date,month,year;
    String number,amount;
    ProgressBar progressBar;
    String mUsersDiscountkey;
    SharedPreferenceWait sharedPreferenceWait;
    String shoppercentage,userpercentage,shopname,ID,category,email,mobile,divition,zila,upzila,password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mUsersDiscount = FirebaseDatabase.getInstance().getReference().child("DiscountRequest");
        mAdminDiscount = FirebaseDatabase.getInstance().getReference().child("AdminDiscountRequest");
        mReportDaily = FirebaseDatabase.getInstance().getReference().child("AdminDiscountRequestReportDaily");
        mReportMonthly = FirebaseDatabase.getInstance().getReference().child("AdminDiscountRequestReportMonthly");
        mReportYearly = FirebaseDatabase.getInstance().getReference().child("AdminDiscountRequestReportYearly");
        mUsersRef = FirebaseDatabase.getInstance().getReference().child("Vendors").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
 
        progressBar = findViewById(R.id.progress);
        CubeGrid foldingCube = new CubeGrid();
        progressBar.setIndeterminateDrawable(foldingCube);
 
        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()));
        year = String.valueOf(android.text.format.DateFormat.format("yyyy", new java.util.Date()));
 
 
 
        sharedPreferenceWait = new SharedPreferenceWait(getApplicationContext());
        mUsersRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                String Confirm=dataSnapshot.child("confirm").getValue().toString();
                userpercentage=dataSnapshot.child("userPercentage").getValue().toString();
                shoppercentage=dataSnapshot.child("shopPercentage").getValue().toString();
                shopname=dataSnapshot.child("shopname").getValue().toString();
                ID=dataSnapshot.child("ID").getValue().toString();
                category=dataSnapshot.child("category").getValue().toString();
                email=dataSnapshot.child("email").getValue().toString();
                password=dataSnapshot.child("password").getValue().toString();
                mobile=dataSnapshot.child("mobile").getValue().toString();
                divition=dataSnapshot.child("divition").getValue().toString();
                zila=dataSnapshot.child("zila").getValue().toString();
                upzila=dataSnapshot.child("upzila").getValue().toString();
                if(Confirm.equals("2")){
                    SharedPreferenceWait sharedPreferenceWait=new SharedPreferenceWait(getApplicationContext());
                    sharedPreferenceWait.done(false);
                    finish();
                }
 
            }
 
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
 
            }
        });
        editTextAmonut=findViewById(R.id.amount);
        editTextNumber=findViewById(R.id.number);
        Done=findViewById(R.id.done);
 
        Done.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                number=editTextNumber.getText().toString().trim();
                amount=editTextAmonut.getText().toString().trim();
                progressBar.setVisibility(View.VISIBLE);
 
 
                FirebaseAuth.getInstance().signInWithEmailAndPassword( number+"@gmail.com", number).addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
 
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            FirebaseUser user = task.getResult().getUser();
 
                            Log.e("g", String.valueOf(user));
                            Log.e("g", String.valueOf(user.getUid()));
 
                            mUsersDiscountkey=mUsersDiscount.child(user.getUid()).push().getKey();
                            mUsersDiscount.child(user.getUid()).child(mUsersDiscountkey).child("Moneyrequest").setValue(amount);
                            mUsersDiscount.child(user.getUid()).child(mUsersDiscountkey).child("UserNumber").setValue(number);
                            mUsersDiscount.child(user.getUid()).child(mUsersDiscountkey).child("shopPercentage").setValue(shoppercentage);
                            mUsersDiscount.child(user.getUid()).child(mUsersDiscountkey).child("userPercentage").setValue(userpercentage);
                            mUsersDiscount.child(user.getUid()).child(mUsersDiscountkey).child("shopname").setValue(shopname);
                            mUsersDiscount.child(user.getUid()).child(mUsersDiscountkey).child("ID").setValue(ID);
                            mUsersDiscount.child(user.getUid()).child(mUsersDiscountkey).child("category").setValue(category);
                            mUsersDiscount.child(user.getUid()).child(mUsersDiscountkey).child("email").setValue(email);
                            mUsersDiscount.child(user.getUid()).child(mUsersDiscountkey).child("mobile").setValue(mobile);
                            mUsersDiscount.child(user.getUid()).child(mUsersDiscountkey).child("divition").setValue(divition);
                            mUsersDiscount.child(user.getUid()).child(mUsersDiscountkey).child("upzila").setValue(upzila);
                            mUsersDiscount.child(user.getUid()).child(mUsersDiscountkey).child("zila").setValue(zila);
                            mUsersDiscount.child(user.getUid()).child(mUsersDiscountkey).child("key").setValue(mUsersDiscountkey);
                            mUsersDiscount.child(user.getUid()).child(mUsersDiscountkey).child("shop_uid").setValue(user.getUid());
                            mUsersDiscount.child(user.getUid()).child(mUsersDiscountkey).child("date").setValue(date);
 
                            next();
                        }else{
                            Toast.makeText(MainActivity.this,"User Not Found!",Toast.LENGTH_LONG).show();
                            progressBar.setVisibility(View.GONE);
                            editTextAmonut.setText("");
                            editTextNumber.setText("");
                        }
 
 
                    }
 
                });
 
            }
        });
 
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        navigationView.setNavigationItemSelectedListener(this);
    }
 
    private void next() {
        number=editTextNumber.getText().toString().trim();
        amount=editTextAmonut.getText().toString().trim();
 
        FirebaseAuth.getInstance().signInWithEmailAndPassword( email, password).addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                FirebaseUser user = task.getResult().getUser();
 
                Log.e("g", String.valueOf(user));
                Log.e("g", String.valueOf(user.getUid()));
 
                mReportDaily.child(user.getUid()).child(date).child(mUsersDiscountkey).child("Moneyrequest").setValue(amount);
                mReportDaily.child(user.getUid()).child(date).child(mUsersDiscountkey).child("UserNumber").setValue(number);
                mReportDaily.child(user.getUid()).child(date).child(mUsersDiscountkey).child("shopPercentage").setValue(shoppercentage);
                mReportDaily.child(user.getUid()).child(date).child(mUsersDiscountkey).child("userPercentage").setValue(userpercentage);
                mReportDaily.child(user.getUid()).child(date).child(mUsersDiscountkey).child("shopname").setValue(shopname);
                mReportDaily.child(user.getUid()).child(date).child(mUsersDiscountkey).child("ID").setValue(ID);
                mReportDaily.child(user.getUid()).child(date).child(mUsersDiscountkey).child("category").setValue(category);
                mReportDaily.child(user.getUid()).child(date).child(mUsersDiscountkey).child("email").setValue(email);
                mReportDaily.child(user.getUid()).child(date).child(mUsersDiscountkey).child("mobile").setValue(mobile);
                mReportDaily.child(user.getUid()).child(date).child(mUsersDiscountkey).child("divition").setValue(divition);
                mReportDaily.child(user.getUid()).child(date).child(mUsersDiscountkey).child("upzila").setValue(upzila);
                mReportDaily.child(user.getUid()).child(date).child(mUsersDiscountkey).child("zila").setValue(zila);
                mReportDaily.child(user.getUid()).child(date).child(mUsersDiscountkey).child("key").setValue(mUsersDiscountkey);
                mReportDaily.child(user.getUid()).child(date).child(mUsersDiscountkey).child("shop_uid").setValue(user.getUid());
 
                mReportMonthly.child(user.getUid()).child(month).child(mUsersDiscountkey).child("Moneyrequest").setValue(amount);
                mReportMonthly.child(user.getUid()).child(month).child(mUsersDiscountkey).child("UserNumber").setValue(number);
                mReportMonthly.child(user.getUid()).child(month).child(mUsersDiscountkey).child("date").setValue(date);
                mReportMonthly.child(user.getUid()).child(month).child(mUsersDiscountkey).child("shopPercentage").setValue(shoppercentage);
                mReportMonthly.child(user.getUid()).child(month).child(mUsersDiscountkey).child("userPercentage").setValue(userpercentage);
                mReportMonthly.child(user.getUid()).child(month).child(mUsersDiscountkey).child("shopname").setValue(shopname);
                mReportMonthly.child(user.getUid()).child(month).child(mUsersDiscountkey).child("ID").setValue(ID);
                mReportMonthly.child(user.getUid()).child(month).child(mUsersDiscountkey).child("category").setValue(category);
                mReportMonthly.child(user.getUid()).child(month).child(mUsersDiscountkey).child("email").setValue(email);
                mReportMonthly.child(user.getUid()).child(month).child(mUsersDiscountkey).child("mobile").setValue(mobile);
                mReportMonthly.child(user.getUid()).child(month).child(mUsersDiscountkey).child("divition").setValue(divition);
                mReportMonthly.child(user.getUid()).child(month).child(mUsersDiscountkey).child("upzila").setValue(upzila);
                mReportMonthly.child(user.getUid()).child(month).child(mUsersDiscountkey).child("zila").setValue(zila);
                mReportMonthly.child(user.getUid()).child(month).child(mUsersDiscountkey).child("key").setValue(mUsersDiscountkey);
                mReportMonthly.child(user.getUid()).child(month).child(mUsersDiscountkey).child("shop_uid").setValue(user.getUid());
 
                mReportYearly.child(user.getUid()).child(year).child(mUsersDiscountkey).child("Moneyrequest").setValue(amount);
                mReportYearly.child(user.getUid()).child(year).child(mUsersDiscountkey).child("UserNumber").setValue(number);
                mReportYearly.child(user.getUid()).child(year).child(mUsersDiscountkey).child("date").setValue(date);
                mReportYearly.child(user.getUid()).child(year).child(mUsersDiscountkey).child("shopPercentage").setValue(shoppercentage);
                mReportYearly.child(user.getUid()).child(year).child(mUsersDiscountkey).child("userPercentage").setValue(userpercentage);
                mReportYearly.child(user.getUid()).child(year).child(mUsersDiscountkey).child("shopname").setValue(shopname);
                mReportYearly.child(user.getUid()).child(year).child(mUsersDiscountkey).child("ID").setValue(ID);
                mReportYearly.child(user.getUid()).child(year).child(mUsersDiscountkey).child("category").setValue(category);
                mReportYearly.child(user.getUid()).child(year).child(mUsersDiscountkey).child("email").setValue(email);
                mReportYearly.child(user.getUid()).child(year).child(mUsersDiscountkey).child("mobile").setValue(mobile);
                mReportYearly.child(user.getUid()).child(year).child(mUsersDiscountkey).child("divition").setValue(divition);
                mReportYearly.child(user.getUid()).child(year).child(mUsersDiscountkey).child("upzila").setValue(upzila);
                mReportYearly.child(user.getUid()).child(year).child(mUsersDiscountkey).child("zila").setValue(zila);
                mReportYearly.child(user.getUid()).child(year).child(mUsersDiscountkey).child("key").setValue(mUsersDiscountkey);
                mReportYearly.child(user.getUid()).child(year).child(mUsersDiscountkey).child("shop_uid").setValue(user.getUid());
 
 
 
 
                //sharedPreferenceWait.done(true);
 
 
 
 
                Intent intent = new Intent(MainActivity.this, ReportActivaty.class);
                progressBar.setVisibility(View.GONE);
                editTextAmonut.setText("");
                editTextNumber.setText("");
                startActivity(intent);
            }
        });
    }
 
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
 
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
 
        return super.onOptionsItemSelected(item);
    }
 
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
 
        if (id == R.id.nav_home) {
            Intent intent = new Intent(MainActivity.this, Coming.class);
            startActivity(intent);
        } else if (id == R.id.nav_gallery) {
 
        } else if (id == R.id.nav_slideshow) {
 
        } else if (id == R.id.nav_tools) {
 
        } else if (id == R.id.nav_share) {
            ShareCompat.IntentBuilder.from(MainActivity.this)
                    .setType("text/plain")
                    .setChooserTitle("Chooser title")
                    .setText("https://play.google.com/store/apps/details?id=com.xbsaykat.easy24vendorapp")
                    .startChooser();
 
        } else if (id == R.id.nav_send) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + MainActivity.this.getPackageName())));
 
        }
 
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
 
Invalid Email or Password