Added Profile page
- Changed layout of profile page - made it so we can edit phone and email on profile page - can take photos or use gallery to change profile photo TODO: - change theme of app to stay consistent with desktop app - change text so it uses String.xml values -add more info to profile if needed -give pets their own profile -connect to backend when its ready
This commit is contained in:
@@ -46,6 +46,16 @@
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<provider
|
||||
android:name="androidx.core.content.FileProvider"
|
||||
android:authorities="${applicationId}.fileprovider"
|
||||
android:exported="false"
|
||||
android:grantUriPermissions="true">
|
||||
<meta-data
|
||||
android:name="android.support.FILE_PROVIDER_PATHS"
|
||||
android:resource="@xml/file_paths" />
|
||||
</provider>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@@ -1,24 +1,252 @@
|
||||
package com.example.petstoremobile.fragments;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.activity.result.contract.ActivityResultContracts;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.core.content.FileProvider;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import android.provider.MediaStore;
|
||||
import android.text.InputType;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.example.petstoremobile.R;
|
||||
import com.example.petstoremobile.activities.MainActivity;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class ProfileFragment extends Fragment {
|
||||
|
||||
//initialize the view/controls
|
||||
private ImageView imgProfile;
|
||||
private TextView tvProfileName, tvProfileEmail, tvProfilePhone, tvProfileRole;
|
||||
private Button btnChangePhoto, btnEditEmail, btnEditPhone, btnLogout;
|
||||
private Uri photoUri;
|
||||
|
||||
//TODO: Create functionality for profile fragment
|
||||
//Initialize the launchers for camera and gallery
|
||||
private ActivityResultLauncher<Intent> galleryLauncher;
|
||||
private ActivityResultLauncher<Uri> cameraLauncher;
|
||||
private ActivityResultLauncher<String> permissionLauncher;
|
||||
|
||||
//Called when the fragment is created, sets up the launchers is set profile image
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
// Launcher to open gallery to select profile image
|
||||
galleryLauncher = registerForActivityResult(
|
||||
//open gallery
|
||||
new ActivityResultContracts.StartActivityForResult(),
|
||||
result -> {
|
||||
//if the user selects an image and its not null
|
||||
if (result.getResultCode() == Activity.RESULT_OK
|
||||
&& result.getData() != null) {
|
||||
//get the selected image and set the image to the profile
|
||||
Uri selectedImage = result.getData().getData();
|
||||
imgProfile.setImageURI(selectedImage);
|
||||
//TODO: SAVE CHANGED PHOTO TO DATABASE
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// Launcher for camera to open and capture profile image
|
||||
cameraLauncher = registerForActivityResult(
|
||||
//open camera
|
||||
new ActivityResultContracts.TakePicture(),
|
||||
success -> {
|
||||
//if a photo is taken set the image profile to it otherwise do nothing
|
||||
if (success) {
|
||||
//Clear the old image and set the new one
|
||||
imgProfile.setImageURI(null);
|
||||
imgProfile.setImageURI(photoUri);
|
||||
//TODO: SAVE CHANGED PHOTO TO DATABASE
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// Launcher to request camera permission
|
||||
permissionLauncher = registerForActivityResult(
|
||||
//ask user for camera permission
|
||||
new ActivityResultContracts.RequestPermission(),
|
||||
granted -> {
|
||||
//if the permission is granted launch the camera
|
||||
if (granted) {
|
||||
launchCamera();
|
||||
}
|
||||
else {
|
||||
//if the permission is denied then tell the user to grant it
|
||||
new AlertDialog.Builder(requireContext())
|
||||
.setTitle("Permission Permission Required")
|
||||
.setMessage("Please grant camera permission to use this feature")
|
||||
.setPositiveButton("Open Settings", (dialog, which) ->{
|
||||
//open the settings page to grant the permission when they click open settings
|
||||
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
||||
intent.setData(Uri.fromParts("package", requireContext().getPackageName(), null));
|
||||
startActivity(intent);
|
||||
})
|
||||
//close the dialog when the user clicks cancel
|
||||
.setNegativeButton("Cancel", null)
|
||||
.show();
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
//TODO: MAKE PROFILE VIEW DISPLAY PROFILE DATA FROM DATABASE
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.fragment_profile, container, false);
|
||||
|
||||
//get all the controls from the view
|
||||
imgProfile = view.findViewById(R.id.imgProfile);
|
||||
tvProfileName = view.findViewById(R.id.tvProfileName);
|
||||
tvProfileEmail = view.findViewById(R.id.tvProfileEmail);
|
||||
tvProfilePhone = view.findViewById(R.id.tvProfilePhone);
|
||||
tvProfileRole = view.findViewById(R.id.tvProfileRole);
|
||||
btnChangePhoto = view.findViewById(R.id.btnChangePhoto);
|
||||
btnEditEmail = view.findViewById(R.id.btnEditEmail);
|
||||
btnEditPhone = view.findViewById(R.id.btnEditPhone);
|
||||
btnLogout = view.findViewById(R.id.btnLogout);
|
||||
|
||||
//Set up listeners for the buttons
|
||||
//Change photo button
|
||||
btnChangePhoto.setOnClickListener(v -> {
|
||||
//Show alert dialog to user to select from gallery or camera
|
||||
new AlertDialog.Builder(requireContext())
|
||||
.setTitle("Change Profile Photo")
|
||||
//set the options for the alert dialog
|
||||
.setItems(new String[]{"Take Photo", "Choose from Gallery"}, (dialog, which) -> {
|
||||
if (which == 0) {
|
||||
// Choose Camera
|
||||
//Checks if the user has granted the camera permission already
|
||||
if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
|
||||
//if the permission is already granted then launch the camera
|
||||
launchCamera();
|
||||
} else {
|
||||
//otherwise request the permission
|
||||
permissionLauncher.launch(Manifest.permission.CAMERA);
|
||||
}
|
||||
} else {
|
||||
// Choose Gallery
|
||||
Intent intent = new Intent(Intent.ACTION_PICK,
|
||||
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
|
||||
galleryLauncher.launch(intent);
|
||||
}
|
||||
})
|
||||
.show();
|
||||
});
|
||||
|
||||
//Edit email button
|
||||
//When clicked open a dialog to change email
|
||||
btnEditEmail.setOnClickListener(v -> {
|
||||
//Make a text field for the user to enter the new email
|
||||
EditText input = new EditText(requireContext());
|
||||
input.setPadding(30,30,30,30);
|
||||
input.setText(tvProfileEmail.getText().toString());
|
||||
|
||||
//set input type to email
|
||||
input.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
|
||||
|
||||
|
||||
//Show alert dialog to user to enter new email
|
||||
new AlertDialog.Builder(requireContext())
|
||||
.setTitle("Edit Email")
|
||||
.setView(input)
|
||||
.setPositiveButton("Save", (dialog, which) -> {
|
||||
String newEmail = input.getText().toString();
|
||||
//if the new value is a valid email then set the email to the new value
|
||||
if (android.util.Patterns.EMAIL_ADDRESS.matcher(newEmail).matches()) {
|
||||
tvProfileEmail.setText(newEmail);
|
||||
//TODO: UPDATE THE EMAIL IN DATABASE
|
||||
}
|
||||
else {
|
||||
//tell the user to email is invalid
|
||||
new AlertDialog.Builder(requireContext())
|
||||
.setTitle("Error")
|
||||
.setMessage("Email is invalid")
|
||||
.setPositiveButton("OK", null)
|
||||
.show();
|
||||
}
|
||||
})
|
||||
.setNegativeButton("Cancel", null)
|
||||
.show();
|
||||
});
|
||||
|
||||
//Edit phone button
|
||||
//When clicked open a dialog to change phone
|
||||
btnEditPhone.setOnClickListener(v -> {
|
||||
//Make a text field for the user to enter the new email
|
||||
EditText input = new EditText(requireContext());
|
||||
input.setPadding(30,30,30,30);
|
||||
input.setText(tvProfilePhone.getText().toString());
|
||||
|
||||
//set input type to phone number
|
||||
input.setInputType(InputType.TYPE_CLASS_PHONE);
|
||||
|
||||
//add canada phone number formatting to input (XXX) XXX-XXXX
|
||||
input.addTextChangedListener(new android.telephony.PhoneNumberFormattingTextWatcher("CA"));
|
||||
input.setFilters(new android.text.InputFilter[]{new android.text.InputFilter.LengthFilter(14)});
|
||||
|
||||
|
||||
//Show alert dialog to user to enter new phone
|
||||
new AlertDialog.Builder(requireContext())
|
||||
.setTitle("Edit Phone Number")
|
||||
.setView(input)
|
||||
.setPositiveButton("Save", (dialog, which) -> {
|
||||
String newPhone = input.getText().toString();
|
||||
//if the new value is format: (XXX) XXX-XXXX then set the phone to the new value
|
||||
if (newPhone.matches("\\(\\d{3}\\) \\d{3}-\\d{4}")) { //TODO MAKE VALIDATION CLASS INSTEAD FOR THIS
|
||||
tvProfilePhone.setText(newPhone);
|
||||
//TODO: UPDATE PHONE IN DATABASE
|
||||
}
|
||||
else {
|
||||
//tell the user to email cannot be empty
|
||||
new AlertDialog.Builder(requireContext())
|
||||
.setTitle("Error")
|
||||
.setMessage("Phone number is invalid. Format: (XXX) XXX-XXXX")
|
||||
.setPositiveButton("OK", null)
|
||||
.show();
|
||||
}
|
||||
})
|
||||
.setNegativeButton("Cancel", null)
|
||||
.show();
|
||||
});
|
||||
|
||||
//Logout button
|
||||
btnLogout.setOnClickListener(v -> {
|
||||
//get the intent to the main activity and clear the back stack so the back button won't allow the user to go back to the previous screen
|
||||
Intent intent = new Intent(getActivity(), MainActivity.class);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
//start the activity to go to login page and finish the current activity
|
||||
startActivity(intent);
|
||||
requireActivity().finish();
|
||||
});
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
//Helper function create a file in the cache directory to store the photo in then launch the camera to capture the photo
|
||||
private void launchCamera() {
|
||||
//create a file in the cache directory to store the photo in
|
||||
File photoFile = new File(requireContext().getCacheDir(), "profile_photo.jpg");
|
||||
//get the uri for the file made
|
||||
photoUri = FileProvider.getUriForFile(requireContext(), requireContext().getPackageName() + ".fileprovider", photoFile);
|
||||
//launch the camera to capture the photo and save the photo to photoUri
|
||||
cameraLauncher.launch(photoUri);
|
||||
}
|
||||
}
|
||||
8
app/src/main/res/drawable/circle_image.xml
Normal file
8
app/src/main/res/drawable/circle_image.xml
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="oval">
|
||||
<solid android:color="#E0E0E0"/>
|
||||
<stroke
|
||||
android:width="2dp"
|
||||
android:color="#BDBDBD"/>
|
||||
</shape>
|
||||
9
app/src/main/res/drawable/rounded_card.xml
Normal file
9
app/src/main/res/drawable/rounded_card.xml
Normal file
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="rectangle">
|
||||
<solid android:color="#FFFFFF"/>
|
||||
<corners android:radius="12dp"/>
|
||||
<stroke
|
||||
android:width="1dp"
|
||||
android:color="#EEEEEE"/>
|
||||
</shape>
|
||||
@@ -1,88 +1,183 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout 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"
|
||||
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="#F5F5F5"
|
||||
android:gravity="center"
|
||||
android:orientation="vertical"
|
||||
android:padding="24dp">
|
||||
android:background="#F5F5F5">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/imgProfile"
|
||||
android:layout_width="189dp"
|
||||
android:layout_height="162dp"
|
||||
android:layout_marginBottom="20dp"
|
||||
android:src="@drawable/placeholder" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnChangePhoto"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Change Photo"
|
||||
android:layout_marginBottom="24dp"/>
|
||||
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvProfileName"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="4dp"
|
||||
android:text="Name"
|
||||
android:textColor="#000000"
|
||||
android:textSize="26sp"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvProfileRole"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="24dp"
|
||||
android:text="Staff Role"
|
||||
android:textColor="#2196F3"
|
||||
android:textSize="16sp" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="4dp"
|
||||
android:text="Email"
|
||||
android:textColor="#888888"
|
||||
android:textSize="12sp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvProfileEmail"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="16dp"
|
||||
android:text="Example@example.com"
|
||||
android:textColor="#000000"
|
||||
android:textSize="16sp" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="4dp"
|
||||
android:text="Phone"
|
||||
android:textColor="#888888"
|
||||
android:textSize="12sp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvProfilePhone"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="32dp"
|
||||
android:text="123-123-1234"
|
||||
android:textColor="#000000"
|
||||
android:textSize="16sp" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnLogout"
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:backgroundTint="#F44336"
|
||||
android:text="Log Out"
|
||||
android:textColor="#FFFFFF" />
|
||||
android:orientation="vertical"
|
||||
android:padding="24dp"
|
||||
android:gravity="center_horizontal">
|
||||
|
||||
</LinearLayout>
|
||||
<ImageView
|
||||
android:id="@+id/imgProfile"
|
||||
android:layout_width="160dp"
|
||||
android:layout_height="161dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:background="@drawable/circle_image"
|
||||
android:clipToOutline="true"
|
||||
android:scaleType="centerCrop"
|
||||
android:src="@drawable/placeholder" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnChangePhoto"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Change Photo"
|
||||
style="@style/Widget.Material3.Button.TextButton"
|
||||
android:layout_marginBottom="8dp"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvProfileName"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="20dp"
|
||||
android:text="First Last"
|
||||
android:textColor="#000000"
|
||||
android:textSize="24sp"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:background="@drawable/rounded_card"
|
||||
android:padding="16dp"
|
||||
android:layout_marginBottom="16dp">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_vertical"
|
||||
android:paddingBottom="12dp">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Email"
|
||||
android:textSize="12sp"
|
||||
android:textColor="#888888"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvProfileEmail"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="email@example.com"
|
||||
android:textColor="#000000"
|
||||
android:textSize="16sp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnEditEmail"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Edit"
|
||||
style="@style/Widget.Material3.Button.TextButton"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<View
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="1dp"
|
||||
android:background="#F0F0F0"/>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_vertical"
|
||||
android:paddingTop="12dp"
|
||||
android:paddingBottom="12dp">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Phone"
|
||||
android:textSize="12sp"
|
||||
android:textColor="#888888"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvProfilePhone"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="111-222-3333"
|
||||
android:textSize="16sp"
|
||||
android:textColor="#000000"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnEditPhone"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Edit"
|
||||
style="@style/Widget.Material3.Button.TextButton"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<View
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="1dp"
|
||||
android:background="#F0F0F0"/>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_vertical"
|
||||
android:paddingTop="12dp">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Role"
|
||||
android:textSize="12sp"
|
||||
android:textColor="#888888"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvProfileRole"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Manager"
|
||||
android:textSize="16sp"
|
||||
android:textColor="#2196F3"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnLogout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Log Out"
|
||||
android:backgroundTint="#F44336"
|
||||
android:textColor="#FFFFFF"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</ScrollView>
|
||||
4
app/src/main/res/xml/file_paths.xml
Normal file
4
app/src/main/res/xml/file_paths.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<paths>
|
||||
<cache-path name="my_cache_images" path="." />
|
||||
</paths>
|
||||
Reference in New Issue
Block a user