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

View File

@ -4,6 +4,7 @@ import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; 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.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser; import com.google.firebase.auth.FirebaseUser;
import java.util.Objects;
public class Login extends AppCompatActivity { public class Login extends AppCompatActivity {
private static final String SHARED_NAME_CREDENTIALS = "Credentials";
EditText editTextEmail, editTextPassword; EditText editTextEmail, editTextPassword;
Button buttonLogin; Button buttonLogin;
FirebaseAuth mAuth;
ProgressBar progressBar; ProgressBar progressBar;
TextView textView; 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 @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login); setContentView(R.layout.activity_login);
mAuth = FirebaseAuth.getInstance();
editTextEmail = findViewById(R.id.username); editTextEmail = findViewById(R.id.username);
editTextPassword = findViewById(R.id.password); editTextPassword = findViewById(R.id.password);
buttonLogin = findViewById(R.id.btn_login); buttonLogin = findViewById(R.id.btn_login);
@ -63,43 +52,59 @@ public class Login extends AppCompatActivity {
} }
}); });
buttonLogin.setOnClickListener(new View.OnClickListener() { buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
progressBar.setVisibility(View.VISIBLE); //progressBar.setVisibility(View.VISIBLE);
String email, password; String email, password;
email = String.valueOf(editTextEmail.getText()); email = String.valueOf(editTextEmail.getText());
password = String.valueOf(editTextPassword.getText()); password = String.valueOf(editTextPassword.getText());
if(TextUtils.isEmpty(email)){ if (TextUtils.isEmpty(email)){
Toast.makeText(Login.this, "Enter email!", Toast.LENGTH_SHORT).show(); Toast.makeText(Login.this, "Enter email!", Toast.LENGTH_SHORT).show();
return; return;
} }
if(TextUtils.isEmpty(password)){ if (TextUtils.isEmpty(password)){
Toast.makeText(Login.this, "Enter password!", Toast.LENGTH_SHORT).show(); Toast.makeText(Login.this, "Enter password!", Toast.LENGTH_SHORT).show();
return; return;
} }
if (!checkIfUserExists(email)){
Toast.makeText(Login.this, "No such username in database!", Toast.LENGTH_SHORT).show();
editTextPassword.setText("");
return;
}
login(email, password);
mAuth.signInWithEmailAndPassword(email, password) //progressBar.setVisibility(View.GONE);
.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();
}
}
});
} }
}); });
} }
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; package com.example.bsm_notatnik;
import android.content.DialogInterface;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; 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 { public class MainActivity extends AppCompatActivity {
FirebaseAuth auth; Button buttonLogout, buttonChangePassword;
Button logout_btn;
TextView textView;
//FirebaseUser user;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
//auth = FirebaseAuth.getInstance(); Intent intent = getIntent();
logout_btn = findViewById(R.id.logout_btn); String currentu_username = intent.getStringExtra("CURRENT_USER_EMAIL");
textView = findViewById(R.id.user_details);
/* buttonLogout = findViewById(R.id.btn_logout);
user = auth.getCurrentUser(); 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 @Override
public void onClick(View view) { public void onClick(View view) {
//FirebaseAuth.getInstance().signOut(); logOut();
Intent intent = new Intent(getApplicationContext(), Login.class);
startActivity(intent);
finish();
} }
}); });
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 androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.View; import android.view.View;
@ -15,13 +17,11 @@ import android.widget.Toast;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class Register extends AppCompatActivity { public class Register extends AppCompatActivity {
private static final String SHARED_NAME_CREDENTIALS = "Credentials";
EditText editTextEmail, editTextPassword; EditText editTextEmail, editTextPassword;
Button buttonReg; Button buttonReg;
ProgressBar progressBar; ProgressBar progressBar;
@ -56,42 +56,76 @@ public class Register extends AppCompatActivity {
buttonReg.setOnClickListener(new View.OnClickListener() { buttonReg.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
progressBar.setVisibility(View.VISIBLE); //progressBar.setVisibility(View.VISIBLE);
String email, password; String email, password;
email = String.valueOf(editTextEmail.getText()); email = String.valueOf(editTextEmail.getText());
password = String.valueOf(editTextPassword.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(); Toast.makeText(Register.this, "Enter email!", Toast.LENGTH_SHORT).show();
return; 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(); Toast.makeText(Register.this, "Enter password!", Toast.LENGTH_SHORT).show();
return; 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(); Toast.makeText(Register.this, "Konto utworzone z email: " + email + " oraz hasłem: " + password, Toast.LENGTH_SHORT).show();
editTextEmail.setText("");
editTextPassword.setText("");
} }
}); });
} }
private boolean validateEmail(){ private void saveNewUser(String email, String password){
return true; SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME_CREDENTIALS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("user_" + email, password);
editor.apply();
} }
private boolean validatePassword(){ private boolean checkIfUserExists(String email){
return true; 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_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity"> tools:context=".MainActivity">
<TextView
android:id="@+id/user_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
/>
<Button <LinearLayout
android:id="@+id/logout_btn" android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content" 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> </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"> <resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. --> <!-- 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. --> <!-- Customize your dark theme here. -->
<!-- <item name="colorPrimary">@color/my_dark_primary</item> --> <!-- <item name="colorPrimary">@color/my_dark_primary</item> -->
</style> </style>

View File

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