Skip to content

Commit

Permalink
Merge pull request #105 from CMPUT301W21T03/user-profile
Browse files Browse the repository at this point in the history
User profile
  • Loading branch information
giancarlopernudisegura authored Mar 19, 2021
2 parents 132e424 + 43f8a5b commit 451a5cb
Show file tree
Hide file tree
Showing 12 changed files with 349 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import android.app.SearchManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.Preference;
import android.util.Log;
import android.view.View;
import android.view.Menu;
Expand Down Expand Up @@ -50,6 +52,7 @@
import androidx.appcompat.widget.Toolbar;

import java.util.UUID;
import java.util.prefs.Preferences;

/**
* Role/Pattern:
Expand All @@ -70,12 +73,14 @@ public class NavigationActivity extends AppCompatActivity implements

private Screen currentScreen;
public Fragment currentFragment;
public final User loggedUser = new User();
public User loggedUser;
public Experiment currentExperiment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = getSharedPreferences("experiment_automata", MODE_PRIVATE);
loggedUser = new User(preferences);
setContentView(R.layout.activity_navigation);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.experiment_automata.UserInformation;

import android.content.SharedPreferences;

/**
* Role/Pattern:
* The contact information of a user.
Expand All @@ -12,6 +14,8 @@ public class ContactInformation {
private String name;
private String email;
private String phone;
private SharedPreferences preferences;
boolean editable;

/**
* Creates a new contact information.
Expand All @@ -26,6 +30,19 @@ public ContactInformation(String name, String email, String phone) {
this.name = name;
this.email = email;
this.phone = phone;
this.editable = false;
}

/**
* Creates new contact information for local user on device
* @param preferences
*/
public ContactInformation(SharedPreferences preferences) {
this.name = preferences.getString("userName", "name");
this.email = preferences.getString("userEmail", "email");
this.phone = preferences.getString("userPhone", "phone");
this.preferences = preferences;
this.editable = true;
}

/**
Expand All @@ -43,7 +60,12 @@ public String getName() {
* The contact's new name
*/
public void setName(String name) {
this.name = name;
if (this.editable) {
this.name = name;
SharedPreferences.Editor editor = preferences.edit();
editor.putString("userName", name);
editor.apply();
}
}

/**
Expand All @@ -61,7 +83,12 @@ public String getEmail() {
* The contact's new email address
*/
public void setEmail(String email) {
this.email = email;
if (this.editable) {
this.email = email;
SharedPreferences.Editor editor = preferences.edit();
editor.putString("userName", name);
editor.apply();
}
}

/**
Expand All @@ -79,6 +106,23 @@ public String getPhone() {
* The contact's new phone number
*/
public void setPhone(String phone) {
this.phone = phone;
if (this.editable) {
this.phone = phone;
SharedPreferences.Editor editor = preferences.edit();
editor.putString("userName", name);
editor.apply();
}
}

/**
* Set all contact information variables
* @param name
* @param email
* @param phone
*/
public void setAll(String name, String email, String phone) {
this.setName(name);
this.setEmail(email);
this.setPhone(phone);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.example.experiment_automata.UserInformation;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import androidx.annotation.NonNull;

import com.example.experiment_automata.R;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.DocumentReference;
Expand All @@ -14,6 +18,9 @@
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.prefs.Preferences;

import static android.content.Context.MODE_PRIVATE;

/**
* Role/Pattern:
Expand All @@ -35,22 +42,15 @@ public class User {

/**
* Creates the user. Assigns a user id automatically.
* @param info
* @param preferences
* the ContactInformation object containing the information for the user
*/
public User(ContactInformation info) {
userId = UUID.randomUUID();//generates a random UUID
this.info = info;
updateFirestore();
}

/**
* Creates the stub user class
*/
public User() {
userId = UUID.fromString(DEFAULT_UUID_STRING);//hard code UUID for stub to defaultUUIDString
this.info = new ContactInformation("Individual",
"example@ualberta.ca", "780-555-1234");
public User(SharedPreferences preferences) {
this.userId = UUID.fromString(preferences.getString("userId", UUID.randomUUID().toString()));
SharedPreferences.Editor editor = preferences.edit();
editor.putString("userId", userId.toString());
editor.apply();
this.info = new ContactInformation(preferences);
this.ownedExperiments = new ArrayList<>();
this.subscribedExperiments = new ArrayList<>();
}
Expand Down Expand Up @@ -90,9 +90,9 @@ public static User getInstance(UUID id) {
*/
protected void updateFirestore() {
Map<String, String> userInfo = new HashMap<String, String>();
userInfo.put("name", info.getName());
userInfo.put("email", info.getEmail());
userInfo.put("phone", info.getPhone());
userInfo.put("name", this.info.getName());
userInfo.put("email", this.info.getEmail());
userInfo.put("phone", this.info.getPhone());

FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("users").document(this.userId.toString())
Expand Down Expand Up @@ -121,6 +121,15 @@ public UUID getUserId()
return this.userId;
}

/**
* Get the user's information
* @return
* The user's contact information object
*/
public ContactInformation getInfo() {
return this.info;
}

/**
* Get a collection of all the owned experiment IDs.
* @return
Expand All @@ -140,11 +149,4 @@ public UUID getUserId()
* @param experimentId
*/
public void addExperiment(UUID experimentId) { ownedExperiments.add(experimentId); }

/**
* Returns the information of the user.
* @return
* The contact information of the user
*/
public ContactInformation getInfo() { return info; }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.experiment_automata.ui.profile;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -12,23 +13,49 @@
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.experiment_automata.NavigationActivity;
import com.example.experiment_automata.R;
import com.example.experiment_automata.ui.profile.ProfileViewModel;
import com.example.experiment_automata.UserInformation.ContactInformation;
import com.example.experiment_automata.UserInformation.User;

import static android.content.Context.MODE_PRIVATE;

public class ProfileFragment extends Fragment {

private ProfileViewModel profileViewModel;

public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
NavigationActivity parentActivity = (NavigationActivity) getActivity();
User user = parentActivity.loggedUser;
ContactInformation userInfo = user.getInfo();
profileViewModel =
new ViewModelProvider(this).get(ProfileViewModel.class);
View root = inflater.inflate(R.layout.fragment_profile, container, false);
final TextView textView = root.findViewById(R.id.text_profile);
profileViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
final TextView idView = root.findViewById(R.id.profile_userid);
idView.setText(user.getUserId().toString());
final TextView nameView = root.findViewById(R.id.profile_username);
final TextView emailView = root.findViewById(R.id.profile_email);
final TextView phoneView = root.findViewById(R.id.profile_phone);
nameView.setText(userInfo.getName());
emailView.setText(userInfo.getEmail());
phoneView.setText(userInfo.getPhone());
profileViewModel.getName().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
nameView.setText(s);
}
});
profileViewModel.getEmail().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
emailView.setText(s);
}
});
profileViewModel.getPhone().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
phoneView.setText(s);
}
});
return root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,36 @@
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.experiment_automata.UserInformation.User;

import java.util.UUID;

public class ProfileViewModel extends ViewModel {

private MutableLiveData<String> mText;
private User user;
private MutableLiveData<String> username;
private MutableLiveData<String> email;
private MutableLiveData<String> phone;

public ProfileViewModel() {
mText = new MutableLiveData<>();
mText.setValue("This is profile fragment");
//user = new User();
username = new MutableLiveData<>();
// username.setValue(user.getInfo().getName());
email = new MutableLiveData<>();
// email.setValue(user.getInfo().getPhone());
phone = new MutableLiveData<>();
// phone.setValue(user.getInfo().getEmail());
}

public LiveData<String> getName() {
return username;
}

public LiveData<String> getEmail() {
return email;
}

public LiveData<String> getText() {
return mText;
public LiveData<String> getPhone() {
return phone;
}
}
10 changes: 10 additions & 0 deletions code/app/src/main/res/drawable/ic_baseline_email.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M20,4L4,4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2zM20,8l-8,5 -8,-5L4,6l8,5 8,-5v2z"/>
</vector>
10 changes: 10 additions & 0 deletions code/app/src/main/res/drawable/ic_baseline_phone.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M6.62,10.79c1.44,2.83 3.76,5.14 6.59,6.59l2.2,-2.2c0.27,-0.27 0.67,-0.36 1.02,-0.24 1.12,0.37 2.33,0.57 3.57,0.57 0.55,0 1,0.45 1,1V20c0,0.55 -0.45,1 -1,1 -9.39,0 -17,-7.61 -17,-17 0,-0.55 0.45,-1 1,-1h3.5c0.55,0 1,0.45 1,1 0,1.25 0.2,2.45 0.57,3.57 0.11,0.35 0.03,0.74 -0.25,1.02l-2.2,2.2z"/>
</vector>
Loading

0 comments on commit 451a5cb

Please sign in to comment.