Skip to content

Commit

Permalink
Settings Activity setup
Browse files Browse the repository at this point in the history
  • Loading branch information
BukenyaLukman committed Jun 26, 2020
1 parent b9b68ba commit 82d89e3
Show file tree
Hide file tree
Showing 6 changed files with 338 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
implementation 'com.google.firebase:firebase-storage:19.1.1'
implementation 'com.google.firebase:firebase-auth:19.3.1'
implementation 'com.firebaseui:firebase-ui-database:3.2.2'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.github.bumptech.glide:glide:4.10.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'
implementation 'de.hdodenhof:circleimageview:3.0.1'
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ScoreActivity"></activity>
<activity android:name=".SettingsActivity"></activity>
<activity android:name=".ScoreActivity" />
<activity android:name=".InstructionsActivity" />
<activity android:name=".QuestionsActivity" />
<activity android:name=".CategoryActivity" />
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/example/nextmedia/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected void onCreate(Bundle savedInstanceState) {
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent categoryIntent = new Intent(MainActivity.this,CategoryActivity.class);
Intent categoryIntent = new Intent(MainActivity.this,SettingsActivity.class);
startActivity(categoryIntent);

}
Expand Down
244 changes: 244 additions & 0 deletions app/src/main/java/com/example/nextmedia/SettingsActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
package com.example.nextmedia;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
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 com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.squareup.picasso.Picasso;

import java.util.HashMap;

import de.hdodenhof.circleimageview.CircleImageView;

public class SettingsActivity extends AppCompatActivity {
private Button SendBtn,GoToTest;
private CircleImageView ProfileImage;
private EditText FullName,Department;
private TextView ChangeImage;
private String InputName,DepartmentName;
private Toolbar mToolbar;
private Uri ImageUri;
private String downloadUrl;

private static int GalleryPick = 1;
private ProgressDialog loadingBar;

private StorageReference userProfileImageRef;
private DatabaseReference userRef;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);


userRef = FirebaseDatabase.getInstance().getReference().child("Users");
userProfileImageRef = FirebaseStorage.getInstance().getReference().child("Profile Images");

mToolbar = findViewById(R.id.settings_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("Settings");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

SendBtn = findViewById(R.id.send_btn);
ProfileImage = findViewById(R.id.profile_image);
FullName = findViewById(R.id.user_full_name);
Department = findViewById(R.id.department);
ChangeImage = findViewById(R.id.change_profile);
GoToTest = findViewById(R.id.skip_to_test);
loadingBar = new ProgressDialog(this);

ChangeImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent,GalleryPick);
}
});

SendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SaveUserData();
}
});

retrieveUserInfo();

}


@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GalleryPick && resultCode==RESULT_OK && data != null){
ImageUri = data.getData();
ProfileImage.setImageURI(ImageUri);
}
}

private void SaveUserData(){
InputName = FullName.getText().toString();
DepartmentName = Department.getText().toString();

if(ImageUri == null){
userRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).hasChild("image")){
saveInfoOnlyWithoutImage();
}else{
Toast.makeText(SettingsActivity.this, "Please select image First", Toast.LENGTH_SHORT).show();
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}else if(InputName.equals("")){
Toast.makeText(this, "Full Name is Required", Toast.LENGTH_SHORT).show();
}else if(DepartmentName.equals("")){
Toast.makeText(this, "Department Name is required", Toast.LENGTH_SHORT).show();
}else{
loadingBar.setTitle("Account Settings");
loadingBar.setMessage("Please wait...");
loadingBar.setCanceledOnTouchOutside(false);
loadingBar.show();
final StorageReference filepath = userProfileImageRef
.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
final UploadTask uploadTask = filepath.putFile(ImageUri);
uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if(!task.isSuccessful()){
throw task.getException();
}
downloadUrl = filepath.getDownloadUrl().toString();
return filepath.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if(task.isSuccessful()){
downloadUrl = task.getResult().toString();
HashMap<String,Object> profileMap = new HashMap<>();
profileMap.put("uid",FirebaseAuth.getInstance().getCurrentUser().getUid());
profileMap.put("name",InputName);
profileMap.put("department",DepartmentName);
profileMap.put("image",downloadUrl);


userRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.updateChildren(profileMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Intent intent = new Intent(SettingsActivity.this,SettingsActivity.class);
startActivity(intent);
finish();
loadingBar.dismiss();
Toast.makeText(SettingsActivity.this, "Profile Settings Updated", Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
}
}

private void saveInfoOnlyWithoutImage(){
InputName = FullName.getText().toString();
DepartmentName = Department.getText().toString();

if(InputName.equals("")){
Toast.makeText(this, "Full Name is required", Toast.LENGTH_SHORT).show();
}else if(DepartmentName.equals("")){
Toast.makeText(this, "Department Name is Required", Toast.LENGTH_SHORT).show();
}else{
loadingBar.setTitle("Account Settings");
loadingBar.setMessage("Please wait...");
loadingBar.setCanceledOnTouchOutside(false);
loadingBar.show();

HashMap<String,Object> profileMap = new HashMap<>();
profileMap.put("uid",FirebaseAuth.getInstance().getCurrentUser().getUid());
profileMap.put("name",InputName);
profileMap.put("department",DepartmentName);

userRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.updateChildren(profileMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Intent intent = new Intent(SettingsActivity.this,SettingsActivity.class);
startActivity(intent);
finish();
loadingBar.dismiss();
Toast.makeText(SettingsActivity.this, "Profile Settings Updated", Toast.LENGTH_SHORT).show();
}
}
});
}
}

private void retrieveUserInfo(){
userRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
String imageDb = dataSnapshot.child("image").getValue().toString();
String nameBb = dataSnapshot.child("name").getValue().toString();
String department = dataSnapshot.child("department").getValue().toString();

FullName.setText(nameBb);
Department.setText(department);
Picasso.get().load(imageDb).placeholder(R.drawable.profile_image).into(ProfileImage);
GoToTest.setVisibility(View.VISIBLE);
GoToTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent goToTest = new Intent(SettingsActivity.this, CategoryActivity.class);
startActivity(goToTest);
finish();
}
});

}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}
}
89 changes: 89 additions & 0 deletions app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SettingsActivity">

<androidx.appcompat.widget.Toolbar
android:id="@+id/settings_toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:background="@color/colorPrimary"/>
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profile_image"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_below="@+id/settings_toolbar"
android:layout_centerInParent="true"
android:layout_marginTop="50dp"
android:src="@drawable/profile_image"/>
<TextView
android:id="@+id/change_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Image"
android:layout_below="@+id/profile_image"
android:layout_centerInParent="true"
android:layout_marginTop="15dp"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/colorAccent"/>
<EditText
android:id="@+id/user_full_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Full Name"
android:layout_marginTop="10dp"
android:textColor="@android:color/black"
android:textColorHint="@color/colorPrimary"
android:layout_below="@+id/change_profile"
android:padding="20sp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:background="@drawable/inputs"/>
<EditText
android:id="@+id/department"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Department"
android:textColor="@android:color/black"
android:textColorHint="@color/colorPrimary"
android:layout_below="@+id/user_full_name"
android:padding="20sp"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:background="@drawable/inputs"/>
<Button
android:id="@+id/skip_to_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/department"
android:layout_marginTop="40dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:text="Go To Test"
android:visibility="invisible"
android:textAllCaps="false"
android:background="@drawable/rounded_corners"
android:layout_marginBottom="20dp"
android:textColor="@android:color/white"
/>
<Button
android:id="@+id/send_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:text="SUBMIT"
android:background="@drawable/rounded_corners"
android:layout_marginBottom="20dp"
android:textColor="@android:color/white"
/>

</RelativeLayout>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0'
classpath 'com.google.gms:google-services:4.2.0'
classpath 'com.google.gms:google-services:4.0.0'


// NOTE: Do not place your application dependencies here; they belong
Expand Down

0 comments on commit 82d89e3

Please sign in to comment.