|
@@ -1,7 +1,11 @@
|
|
|
package com.example.chatapp;
|
|
|
|
|
|
+import android.app.Activity;
|
|
|
import android.content.Intent;
|
|
|
+import android.graphics.Bitmap;
|
|
|
+import android.net.Uri;
|
|
|
import android.os.Bundle;
|
|
|
+import android.provider.MediaStore;
|
|
|
import android.view.View;
|
|
|
import android.widget.Button;
|
|
|
import android.widget.EditText;
|
|
@@ -9,25 +13,37 @@ import android.widget.TextView;
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
+import androidx.annotation.Nullable;
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
+import androidx.appcompat.widget.Toolbar;
|
|
|
|
|
|
+import com.example.chatapp.model.User;
|
|
|
import com.google.android.gms.tasks.OnCompleteListener;
|
|
|
+import com.google.android.gms.tasks.OnSuccessListener;
|
|
|
import com.google.android.gms.tasks.Task;
|
|
|
import com.google.firebase.auth.AuthResult;
|
|
|
import com.google.firebase.auth.FirebaseAuth;
|
|
|
import com.google.firebase.auth.FirebaseUser;
|
|
|
import com.google.firebase.database.DatabaseReference;
|
|
|
import com.google.firebase.database.FirebaseDatabase;
|
|
|
+import com.google.firebase.storage.FirebaseStorage;
|
|
|
+import com.google.firebase.storage.StorageReference;
|
|
|
+import com.google.firebase.storage.UploadTask;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+import de.hdodenhof.circleimageview.CircleImageView;
|
|
|
|
|
|
public class RegisterActivity extends AppCompatActivity {
|
|
|
|
|
|
- private Button registerButton;
|
|
|
+ private Button registerButton, selectPhotoButton;
|
|
|
private TextView signInButton;
|
|
|
private EditText usernameInput;
|
|
|
private EditText emailInput;
|
|
|
private EditText passwordInput;
|
|
|
+ private CircleImageView photoProfileRegUser;
|
|
|
|
|
|
private FirebaseAuth firebaseAuth;
|
|
|
private DatabaseReference reference;
|
|
@@ -37,6 +53,23 @@ public class RegisterActivity extends AppCompatActivity {
|
|
|
super.onCreate(savedInstanceState);
|
|
|
setContentView(R.layout.activity_register);
|
|
|
|
|
|
+ Toolbar toolbar = findViewById(R.id.toolbarRegistration);
|
|
|
+ setSupportActionBar(toolbar);
|
|
|
+ getSupportActionBar().setTitle("Registration");
|
|
|
+ getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
+
|
|
|
+ photoProfileRegUser = findViewById(R.id.selectPhotoImageViewRegister);
|
|
|
+
|
|
|
+ selectPhotoButton = findViewById(R.id.selectPhotoButton);
|
|
|
+ selectPhotoButton.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ Intent intent = new Intent(Intent.ACTION_PICK);
|
|
|
+ intent.setType("image/");
|
|
|
+ startActivityForResult(intent, 0);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
firebaseAuth = FirebaseAuth.getInstance();
|
|
|
|
|
|
usernameInput = findViewById(R.id.usernameInputRegister);
|
|
@@ -47,7 +80,7 @@ public class RegisterActivity extends AppCompatActivity {
|
|
|
registerButton.setOnClickListener(new View.OnClickListener() {
|
|
|
@Override
|
|
|
public void onClick(View v) {
|
|
|
- registration(usernameInput.getText().toString(), emailInput.getText().toString(), passwordInput.getText().toString());
|
|
|
+ performRegister(usernameInput.getText().toString(), emailInput.getText().toString(), passwordInput.getText().toString());
|
|
|
}
|
|
|
});
|
|
|
|
|
@@ -61,49 +94,88 @@ public class RegisterActivity extends AppCompatActivity {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ private Uri selectedPhotoUri = null;
|
|
|
|
|
|
- // method for registration new user - add user in database
|
|
|
- private void registration(final String username, String email, String password) {
|
|
|
+ @Override
|
|
|
+ protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
|
|
+ super.onActivityResult(requestCode, resultCode, data);
|
|
|
+
|
|
|
+ if (requestCode == 0 && resultCode == Activity.RESULT_OK && data != null) {
|
|
|
+ selectedPhotoUri = data.getData();
|
|
|
+ try {
|
|
|
+ Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedPhotoUri);
|
|
|
+ photoProfileRegUser.setImageBitmap(bitmap);
|
|
|
+ selectPhotoButton.setAlpha(0f);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ // method for registration new user - add user in database
|
|
|
+ private void performRegister(final String username, String email, String password) {
|
|
|
if (username.equals("") || email.equals("") || password.equals("")) {
|
|
|
Toast.makeText(this, "Empty field", Toast.LENGTH_SHORT).show();
|
|
|
} else if (password.length() < 6) {
|
|
|
Toast.makeText(this, "Password shorter than 6 characters", Toast.LENGTH_SHORT).show();
|
|
|
- } else {
|
|
|
- firebaseAuth.createUserWithEmailAndPassword(email, password)
|
|
|
- .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
|
|
|
- @Override
|
|
|
- public void onComplete(@NonNull Task<AuthResult> task) {
|
|
|
- if (task.isSuccessful()) {
|
|
|
- FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
|
|
|
- String userId = firebaseUser.getUid();
|
|
|
-
|
|
|
- // get reference for add new user
|
|
|
- reference = FirebaseDatabase.getInstance().getReference("Users").child(userId);
|
|
|
-
|
|
|
- HashMap<String, String> hashMap = new HashMap<>();
|
|
|
- hashMap.put("id", userId);
|
|
|
- hashMap.put("username", username);
|
|
|
- hashMap.put("imageUrl", "default");
|
|
|
-
|
|
|
- // set new user in database
|
|
|
- reference.setValue(hashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
|
|
|
- @Override
|
|
|
- public void onComplete(@NonNull Task<Void> task) {
|
|
|
- if (task.isSuccessful()) {
|
|
|
- Toast.makeText(RegisterActivity.this, "Welcome", Toast.LENGTH_SHORT).show();
|
|
|
- Intent intent = new Intent(getApplicationContext(), MainActivity.class);
|
|
|
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
- startActivity(intent);
|
|
|
- finish();
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- } else {
|
|
|
- Toast.makeText(RegisterActivity.this, "You can`t register with this email or password", Toast.LENGTH_SHORT).show();
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
+ return;
|
|
|
}
|
|
|
+
|
|
|
+ firebaseAuth.createUserWithEmailAndPassword(email, password)
|
|
|
+ .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
|
|
|
+ @Override
|
|
|
+ public void onComplete(@NonNull Task<AuthResult> task) {
|
|
|
+ if (task.isSuccessful()) {
|
|
|
+
|
|
|
+ Toast.makeText(RegisterActivity.this, "Successfully created user", Toast.LENGTH_LONG).show();
|
|
|
+ uploadImageToFirebaseStorage(username);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ Toast.makeText(RegisterActivity.this, "You can`t register with this email or password", Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void uploadImageToFirebaseStorage(final String username) {
|
|
|
+ if (selectedPhotoUri == null) return;
|
|
|
+
|
|
|
+ String fileName = UUID.randomUUID().toString();
|
|
|
+ final StorageReference ref = FirebaseStorage.getInstance().getReference("/images/" + fileName);
|
|
|
+ ref.putFile(selectedPhotoUri)
|
|
|
+ .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
|
|
|
+ @Override
|
|
|
+ public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
|
|
|
+ ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
|
|
|
+ @Override
|
|
|
+ public void onSuccess(Uri uri) {
|
|
|
+ saveUserToFirebaseDatabase(uri.toString(), username);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveUserToFirebaseDatabase(String profileImageUrl, String username) {
|
|
|
+ String uid = firebaseAuth.getUid();
|
|
|
+
|
|
|
+ // get reference for add new user
|
|
|
+ reference = FirebaseDatabase.getInstance().getReference("Users/" + uid);
|
|
|
+
|
|
|
+ User user = new User(uid, username, profileImageUrl);
|
|
|
+
|
|
|
+ // set new user in database
|
|
|
+ reference.setValue(user)
|
|
|
+ .addOnSuccessListener(new OnSuccessListener<Void>() {
|
|
|
+ @Override
|
|
|
+ public void onSuccess(Void aVoid) {
|
|
|
+ Toast.makeText(RegisterActivity.this, "Welcome", Toast.LENGTH_SHORT).show();
|
|
|
+ Intent intent = new Intent(getApplicationContext(), MainActivity.class);
|
|
|
+ intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ startActivity(intent);
|
|
|
+ finish();
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
}
|