View difference between Paste ID: c17ppn7L and CcZnHfQk
SHOW: | | - or go back to the newest paste.
1
package com.example.bishal.evader;
2
3
import android.app.ProgressDialog;
4
import android.content.Intent;
5
import android.support.annotation.NonNull;
6
import android.support.v7.app.AppCompatActivity;
7
import android.os.Bundle;
8
import android.text.TextUtils;
9
import android.view.View;
10
import android.widget.Button;
11
import android.widget.EditText;
12
import android.widget.TextView;
13
import android.widget.Toast;
14
15
import com.example.bishal.evader.Model.Users;
16
import com.example.bishal.evader.Prevalent.Prevalent;
17
import com.google.firebase.database.DataSnapshot;
18
import com.google.firebase.database.DatabaseError;
19
import com.google.firebase.database.DatabaseReference;
20
import com.google.firebase.database.FirebaseDatabase;
21
import com.google.firebase.database.ValueEventListener;
22
import com.rey.material.widget.CheckBox;
23
24
import io.paperdb.Paper;
25
26
public class LoginActivity extends AppCompatActivity
27
{
28
29
    private EditText InputPhoneNumber, InputPassword;
30
    private Button LoginButton;
31
    private ProgressDialog loadingBar;
32
    private TextView AdminLink, NotAdminLink;
33
34
    private String parentDbName = "Users";
35
    private CheckBox chkBoxRememberMe;
36
37
38
    @Override
39
    protected void onCreate(Bundle savedInstanceState)
40
    {
41
        super.onCreate(savedInstanceState);
42
        setContentView(R.layout.activity_login);
43
44
        LoginButton = (Button) findViewById(R.id.login_btn);
45
        InputPassword = (EditText) findViewById(R.id.login_password_input);
46
        InputPhoneNumber = (EditText) findViewById(R.id.login_phone_number_input);
47
        AdminLink = (TextView) findViewById(R.id.admin_panel_link);
48
        NotAdminLink = (TextView) findViewById(R.id.not_admin_panel_link);
49
50
        loadingBar = new ProgressDialog(this);
51
52
        chkBoxRememberMe = (CheckBox) findViewById(R.id.remember_me_chkb);
53
        Paper.init(this);
54
55
        LoginButton.setOnClickListener(new View.OnClickListener() {
56
            @Override
57
            public void onClick(View v)
58
            {
59
                LoginUser();
60
61
            }
62
        });
63
//admin and users buttons change accordingly if users or admin press them accidentally
64
        AdminLink.setOnClickListener(new View.OnClickListener() {
65
            @Override
66
            public void onClick(View v)
67
            {
68
                LoginButton.setText("Login Admin");
69
                AdminLink.setVisibility(View.INVISIBLE);
70
                NotAdminLink.setVisibility(View.VISIBLE);
71
                parentDbName = "Admins";
72
            }
73
        });
74
75
        NotAdminLink.setOnClickListener(new View.OnClickListener() {
76
            @Override
77
            public void onClick(View v)
78
            {
79
80
                LoginButton.setText("Login");
81
                AdminLink.setVisibility(View.VISIBLE);
82
                NotAdminLink.setVisibility(View.INVISIBLE);
83
                parentDbName = "Users";
84
85
            }
86
        });
87
88
    }
89
90
91
92
    private  void LoginUser()
93
    {
94
        String phone = InputPhoneNumber.getText().toString();
95
        String password = InputPassword.getText().toString();
96
97
          if (TextUtils.isEmpty(phone))
98
    {
99
        Toast.makeText(this, "Please type your phone number here...", Toast.LENGTH_SHORT).show();
100
    }
101
    else if (TextUtils.isEmpty(password))
102
    {
103
        Toast.makeText(this, "Please type your password here...", Toast.LENGTH_SHORT).show();
104
    }
105
    else
106
          {
107
              loadingBar.setTitle("Login Account");
108
              loadingBar.setMessage("Please wait as the credentials are being checked");
109
              loadingBar.setCanceledOnTouchOutside(false);
110
              loadingBar.show();
111
112
              AllowAccessToAccount(phone, password);
113
          }
114
    }
115
116
117
118
    private void AllowAccessToAccount(final String phone, final String password)
119
    {
120
//ifthe checkbox for remember me is ticked it will return value
121
        if (chkBoxRememberMe.isChecked())
122
        {
123
            Paper.book().write(Prevalent.UserPhoneKey, phone);
124
            Paper.book().write(Prevalent.UserPasswordKey, password);
125
        }
126
127
128
        final DatabaseReference RootRef;
129
        RootRef = FirebaseDatabase.getInstance().getReference();
130
131
        RootRef.addListenerForSingleValueEvent(new ValueEventListener() {
132
            @Override
133
            public void onDataChange(@NonNull DataSnapshot dataSnapshot)
134
            {
135
                if (dataSnapshot.child(parentDbName).child(phone).exists())
136
                {
137
                    Users usersData = dataSnapshot.child(parentDbName).child(phone).getValue(Users.class);
138
139
                    if (usersData.getPhone().equals(phone)) {
140
                        if (usersData.getPassword().equals(password)) {
141
142
                            if (parentDbName.equals("Admins")) {
143
                                Toast.makeText(LoginActivity.this, "Welcome Admin You are logged in", Toast.LENGTH_SHORT).show();
144
                                loadingBar.dismiss();
145
146
                                Intent intent = new Intent(LoginActivity.this, AdminCategoryActivity.class);
147
                                startActivity(intent);
148
                            } else if (parentDbName.equals("Users")) {
149
                                Toast.makeText(LoginActivity.this, "You are logged in", Toast.LENGTH_SHORT).show();
150
                                loadingBar.dismiss();
151
152
                                Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
153
                                Prevalent.currentOnlineUser = usersData;
154
                                startActivity(intent);
155
156
                            }
157
                        }
158
                        else
159
                        {
160
                            loadingBar.dismiss();
161
                            Toast.makeText(LoginActivity.this, "Your password is incorrect", Toast.LENGTH_SHORT).show();
162
                        }
163
                    }
164
                }
165
                else
166
                {
167
                    Toast.makeText(LoginActivity.this, "Account on this " + phone + " number is not in database", Toast.LENGTH_SHORT).show();
168
                    loadingBar.dismiss();
169
                    Toast.makeText(LoginActivity.this, "Please create an Account idiot", Toast.LENGTH_SHORT).show();
170
                }
171
            }
172
173
            @Override
174
            public void onCancelled(@NonNull DatabaseError databaseError)
175
            {
176
177
            }
178
        });
179
    }
180
}