niedziela wieczor

This commit is contained in:
mikgaw@st.amu.edu.pl 2023-12-03 19:44:35 +01:00
parent 1c1a418b88
commit 18855aae13
9 changed files with 254 additions and 106 deletions

View File

@ -2,8 +2,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
@ -30,7 +28,8 @@
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
android:exported="true"
android:theme="@style/Theme.Material3.Light.NoActionBar">
</activity>
</application>

View File

@ -4,6 +4,7 @@ import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
@ -20,34 +21,22 @@ import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import java.util.Objects;
public class Login extends AppCompatActivity {
private static final String SHARED_NAME_CREDENTIALS = "Credentials";
EditText editTextEmail, editTextPassword;
Button buttonLogin;
FirebaseAuth mAuth;
ProgressBar progressBar;
TextView textView;
// Checks if a user is already signed in
@Override
public void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser != null){
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mAuth = FirebaseAuth.getInstance();
editTextEmail = findViewById(R.id.username);
editTextPassword = findViewById(R.id.password);
buttonLogin = findViewById(R.id.btn_login);
@ -63,43 +52,59 @@ public class Login extends AppCompatActivity {
}
});
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progressBar.setVisibility(View.VISIBLE);
//progressBar.setVisibility(View.VISIBLE);
String email, password;
email = String.valueOf(editTextEmail.getText());
password = String.valueOf(editTextPassword.getText());
if(TextUtils.isEmpty(email)){
if (TextUtils.isEmpty(email)){
Toast.makeText(Login.this, "Enter email!", Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(password)){
if (TextUtils.isEmpty(password)){
Toast.makeText(Login.this, "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
if (!checkIfUserExists(email)){
Toast.makeText(Login.this, "No such username in database!", Toast.LENGTH_SHORT).show();
editTextPassword.setText("");
return;
}
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressBar.setVisibility(View.GONE);
if (task.isSuccessful()) {
Toast.makeText(getApplicationContext(), "Login Successful", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(Login.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
login(email, password);
//progressBar.setVisibility(View.GONE);
}
});
}
private boolean checkIfUserExists(String email){
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_CREDENTIALS, MODE_PRIVATE);
return sharedPreferences.contains("user_" + email);
}
private void login(String email, String password){
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_CREDENTIALS, MODE_PRIVATE);
String passwordFromData = sharedPreferences.getString("user_" + email, "err");
if (password.equals(passwordFromData)){
Toast.makeText(getApplicationContext(), "Login Successful", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("CURRENT_USER_EMAIL", email);
startActivity(intent);
finish();
}else {
Toast.makeText(getApplicationContext(), "Wrong credentials!", Toast.LENGTH_SHORT).show();
editTextPassword.setText("");
}
}
}

View File

@ -1,51 +1,106 @@
package com.example.bsm_notatnik;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class MainActivity extends AppCompatActivity {
FirebaseAuth auth;
Button logout_btn;
TextView textView;
//FirebaseUser user;
Button buttonLogout, buttonChangePassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//auth = FirebaseAuth.getInstance();
logout_btn = findViewById(R.id.logout_btn);
textView = findViewById(R.id.user_details);
Intent intent = getIntent();
String currentu_username = intent.getStringExtra("CURRENT_USER_EMAIL");
/*
user = auth.getCurrentUser();
buttonLogout = findViewById(R.id.btn_logout);
buttonChangePassword = findViewById(R.id.btn_change_password);
if (user == null){
Intent intent = new Intent(getApplicationContext(), Login.class);
startActivity(intent);
finish();
}else {
textView.setText(user.getEmail());
}*/
logout_btn.setOnClickListener(new View.OnClickListener() {
buttonLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//FirebaseAuth.getInstance().signOut();
Intent intent = new Intent(getApplicationContext(), Login.class);
startActivity(intent);
finish();
logOut();
}
});
buttonChangePassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showPasswordChangeDialog();
}
});
}
}
private void logOut(){
Toast.makeText(getApplicationContext(), "Logout Successful!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), Login.class);
startActivity(intent);
finish();
}
private void showPasswordChangeDialog(){
// Inflate the dialog layout
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.password_change_dialog, null);
// Create the AlertDialog builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialogView);
builder.setTitle("Change Password");
// Set up the positive (OK) button
builder.setPositiveButton("Change", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Handle password change logic here
EditText editTextNewPassword = dialogView.findViewById(R.id.editTextNewPassword);
EditText editTextConfirmPassword = dialogView.findViewById(R.id.editTextConfirmPassword);
String newPassword = editTextNewPassword.getText().toString();
String confirmPassword = editTextConfirmPassword.getText().toString();
// Perform password change validation and logic
if (newPassword.equals(confirmPassword)) {
// Passwords match, implement your password change logic here
} else {
// Passwords do not match, show an error message
// You can use a Toast or any other method to display the message
}
}
});
// Set up the negative (Cancel) button
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// User canceled the password change
}
});
// Create and show the AlertDialog
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}

View File

@ -2,7 +2,9 @@ package com.example.bsm_notatnik;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
@ -15,13 +17,11 @@ import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class Register extends AppCompatActivity {
private static final String SHARED_NAME_CREDENTIALS = "Credentials";
EditText editTextEmail, editTextPassword;
Button buttonReg;
ProgressBar progressBar;
@ -56,42 +56,76 @@ public class Register extends AppCompatActivity {
buttonReg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progressBar.setVisibility(View.VISIBLE);
//progressBar.setVisibility(View.VISIBLE);
String email, password;
email = String.valueOf(editTextEmail.getText());
password = String.valueOf(editTextPassword.getText());
if(TextUtils.isEmpty(email)){
//checks if email field is not empty
if (TextUtils.isEmpty(email)){
Toast.makeText(Register.this, "Enter email!", Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(password)){
//checks if password field is not empty
if (TextUtils.isEmpty(password)){
Toast.makeText(Register.this, "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
//checks if given username is already registered in database
if (checkIfUserExists(email)){
editTextEmail.setText("");
editTextPassword.setText("");
Toast.makeText(Register.this, "Account with this username already exists!", Toast.LENGTH_SHORT).show();
return;
}
//checks if email has correct format
if (!validateEmail(email)){
editTextPassword.setText("");
Toast.makeText(Register.this, "Email format not correct!", Toast.LENGTH_SHORT).show();
return;
}
//checks password wymagania
if (!validatePassword(password)){
Toast.makeText(Register.this, "Password to weak!", Toast.LENGTH_SHORT).show();
return;
}
// TUTAJ ROBIE USERA
saveNewUser(email, password);
Toast.makeText(Register.this, "Konto utworzone z email: " + email + " oraz hasłem: " + password, Toast.LENGTH_SHORT).show();
editTextEmail.setText("");
editTextPassword.setText("");
}
});
}
private boolean validateEmail(){
return true;
private void saveNewUser(String email, String password){
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_CREDENTIALS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("user_" + email, password);
editor.apply();
}
private boolean validatePassword(){
return true;
private boolean checkIfUserExists(String email){
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_CREDENTIALS, MODE_PRIVATE);
return sharedPreferences.contains("user_" + email);
}
private boolean validateEmail(String email){
final String EMAIL_PATTERN = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
private boolean validatePassword(String password){
final String PASSWORD_PATTERN = "^.{6,}$";
Pattern pattern = Pattern.compile(PASSWORD_PATTERN);
Matcher matcher = pattern.matcher(password);
return matcher.matches();
}
}

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/cardview_dark_background" />
<corners android:radius="8dp" />
</shape>

View File

@ -5,21 +5,45 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/user_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
/>
<Button
android:id="@+id/logout_btn"
android:layout_width="wrap_content"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/logout_btn_txt"
/>
android:orientation="horizontal"
android:padding="5dp">
<Button
android:id="@+id/btn_logout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginEnd="8dp"
android:padding="5dp"
android:background="@drawable/rectangular_button_background"
android:text="Log Out" />
<Button
android:id="@+id/btn_change_password"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginEnd="8dp"
android:padding="5dp"
android:background="@drawable/rectangular_button_background"
android:text="Change Password" />
<Button
android:id="@+id/btn_add_note"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:background="@drawable/rectangular_button_background"
android:text="Add Note" />
</LinearLayout>
</LinearLayout>

View File

@ -0,0 +1,28 @@
<!-- password_change_dialog.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/editTextNewPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter New Password"
android:inputType="textPassword"/>
<EditText
android:id="@+id/editTextConfirmPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Confirm New Password"
android:inputType="textPassword"/>
<Button
android:id="@+id/btnChangePassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change Password"/>
</LinearLayout>

View File

@ -1,6 +1,6 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.BSMnotatnik" parent="Theme.Material3.DayNight.NoActionBar">
<style name="Base.Theme.BSMnotatnik" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Customize your dark theme here. -->
<!-- <item name="colorPrimary">@color/my_dark_primary</item> -->
</style>

View File

@ -1,9 +1,7 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.BSMnotatnik" parent="Theme.Material3.DayNight.NoActionBar">
<!-- Customize your light theme here. -->
<!-- <item name="colorPrimary">@color/my_light_primary</item> -->
<style name="Base.Theme.BSMnotatnik" parent="Theme.AppCompat.Light.NoActionBar">
</style>
<style name="Theme.BSMnotatnik" parent="Base.Theme.BSMnotatnik" />
</resources>