-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b9b68ba
commit 82d89e3
Showing
6 changed files
with
338 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
244 changes: 244 additions & 0 deletions
244
app/src/main/java/com/example/nextmedia/SettingsActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters