Skip to content

Commit

Permalink
Merge pull request #193 from Claudio5/replace-enum
Browse files Browse the repository at this point in the history
Replaced attributes enum by simply final values
  • Loading branch information
Claudio5 authored Dec 19, 2018
2 parents a6af057 + 281a5ef commit a0cb9c5
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 179 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package ch.epfl.sweng.SDP.auth;

import static ch.epfl.sweng.SDP.firebase.AccountAttributes.EMAIL;
import static ch.epfl.sweng.SDP.firebase.AccountAttributes.attributeToPath;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.firebase.database.DataSnapshot;

import ch.epfl.sweng.SDP.NoBackPressActivity;
import ch.epfl.sweng.SDP.R;
import ch.epfl.sweng.SDP.firebase.FbDatabase;
import ch.epfl.sweng.SDP.firebase.OnSuccessValueEventListener;
import ch.epfl.sweng.SDP.home.HomeActivity;
import ch.epfl.sweng.SDP.utils.GlideUtils;
import com.google.firebase.database.DataSnapshot;

import static ch.epfl.sweng.SDP.firebase.AccountAttributes.EMAIL;

/**
* Class representing the account creation page.
Expand All @@ -33,7 +34,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_account_creation);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

userEmail = getIntent().getStringExtra(attributeToPath(EMAIL));
userEmail = getIntent().getStringExtra(EMAIL);

usernameInput = findViewById(R.id.usernameInput);
usernameTaken = findViewById(R.id.usernameTaken);
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/ch/epfl/sweng/SDP/auth/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import static android.view.View.VISIBLE;
import static ch.epfl.sweng.SDP.firebase.AccountAttributes.EMAIL;
import static ch.epfl.sweng.SDP.firebase.AccountAttributes.attributeToPath;


/**
Expand Down Expand Up @@ -87,7 +86,7 @@ private void handleSuccessfulSignIn(IdpResponse response) {
// New user
Log.d(TAG, "New user");
Intent intent = new Intent(this, AccountCreationActivity.class);
intent.putExtra(attributeToPath(EMAIL), email);
intent.putExtra(EMAIL, email);
startActivity(intent);
finish();
} else {
Expand All @@ -106,7 +105,7 @@ public void onDataChange(@NonNull DataSnapshot snapshot) {
Log.d(TAG, "User signed in but not did not create an account");
Intent intent = new Intent(getApplicationContext(),
AccountCreationActivity.class);
intent.putExtra(attributeToPath(EMAIL), email);
intent.putExtra(EMAIL, email);
startActivity(intent);
finish();
}
Expand Down
61 changes: 18 additions & 43 deletions app/src/main/java/ch/epfl/sweng/SDP/firebase/AccountAttributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,24 @@
import ch.epfl.sweng.SDP.auth.Account;

/**
* Enum representing all the {@link Account}'s attributes.
* Class representing all the {@link Account}'s attributes.
*/
public enum AccountAttributes {
USER_ID, USERNAME, EMAIL, STARS, TROPHIES, LEAGUE, MATCHES_WON, MATCHES_TOTAL,
AVERAGE_RATING, MAX_TROPHIES, FRIENDS, STATUS, BOUGHT_ITEMS;
public final class AccountAttributes {

private AccountAttributes() {}

public static final String USER_ID = "userId";
public static final String USERNAME = "username";
public static final String EMAIL = "email";
public static final String STARS = "stars";
public static final String TROPHIES = "trophies";
public static final String LEAGUE = "currentLeague";
public static final String MATCHES_WON = "matchesWon";
public static final String MATCHES_TOTAL = "totalMatches";
public static final String AVERAGE_RATING = "averageRating";
public static final String MAX_TROPHIES = "maxTrophies";
public static final String FRIENDS = "friends";
public static final String STATUS = "online";
public static final String BOUGHT_ITEMS = "boughtItems";

/**
* Converts the given {@link AccountAttributes} to a string which can be used in a path (for a
* Firebase Database query, for example).
*
* @param accountAttribute the account attribute to convert
* @return a string representing the given attribute
*/
public static String attributeToPath(AccountAttributes accountAttribute) {
switch (accountAttribute) {
case USER_ID:
return "userId";
case USERNAME:
return "username";
case EMAIL:
return "email";
case STARS:
return "stars";
case TROPHIES:
return "trophies";
case LEAGUE:
return "currentLeague";
case MATCHES_WON:
return "matchesWon";
case MATCHES_TOTAL:
return "totalMatches";
case AVERAGE_RATING:
return "averageRating";
case MAX_TROPHIES:
return "maxTrophies";
case FRIENDS:
return "friends";
case STATUS:
return "online";
case BOUGHT_ITEMS:
return "boughtItems";
default:
throw new IllegalArgumentException("Unknown attribute");
}
}
}
90 changes: 40 additions & 50 deletions app/src/main/java/ch/epfl/sweng/SDP/firebase/FbDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
import static ch.epfl.sweng.SDP.firebase.AccountAttributes.FRIENDS;
import static ch.epfl.sweng.SDP.firebase.AccountAttributes.STATUS;
import static ch.epfl.sweng.SDP.firebase.AccountAttributes.USERNAME;
import static ch.epfl.sweng.SDP.firebase.AccountAttributes.attributeToPath;
import static ch.epfl.sweng.SDP.firebase.RoomAttributes.FINISHED;
import static ch.epfl.sweng.SDP.firebase.RoomAttributes.RANKING;
import static ch.epfl.sweng.SDP.firebase.RoomAttributes.UPLOAD_DRAWING;
import static ch.epfl.sweng.SDP.firebase.RoomAttributes.USERS;
import static ch.epfl.sweng.SDP.firebase.RoomAttributes.attributeToPath;
import static ch.epfl.sweng.SDP.utils.OnlineStatus.OFFLINE;
import static ch.epfl.sweng.SDP.utils.Preconditions.checkPrecondition;

Expand Down Expand Up @@ -80,47 +78,47 @@ public static void getUsers(ValueEventListener valueEventListener) {
* Gets an attribute from a given user in the database.
*
* @param userId id of the user to get the attribute from
* @param attribute enum to determine which attribute to get
* @param attribute which attribute to get
* @param valueEventListener listener to handle response
*/
public static void getAccountAttribute(String userId, AccountAttributes attribute,
public static void getAccountAttribute(String userId, String attribute,
ValueEventListener valueEventListener) {
getReference(constructUsersPath(userId, attributeToPath(attribute)))
getReference(constructUsersPath(userId, attribute))
.addListenerForSingleValueEvent(valueEventListener);
}

/**
* Modifies (or inserts) the value of a given attribute in the database.
*
* @param userId id of the user whose attribute to modify
* @param attribute enum to determine which attribute to modify
* @param attribute which attribute to modify
* @param newValue new value to be inserted for attribute
* @param completionListener listener to handle response
*/
public static void setAccountAttribute(
String userId, AccountAttributes attribute, Object newValue,
String userId, String attribute, Object newValue,
DatabaseReference.CompletionListener completionListener) {
getReference(constructUsersPath(userId, attributeToPath(attribute)))
getReference(constructUsersPath(userId, attribute))
.setValue(newValue, completionListener);
}

/**
* Same as {@link #setAccountAttribute}, but with a default completionListener.
*
* @param userId id of user whose attribute to modify
* @param attribute enum to determine which attribute to modify
* @param attribute which attribute to modify
* @param newValue new value to be inserted for attribute
*/
public static void setAccountAttribute(String userId,
AccountAttributes attribute, Object newValue) {
String attribute, Object newValue) {
setAccountAttribute(userId, attribute, newValue, createCompletionListener());
}

/**
* Removes an attribute from a given user.
*/
public static void removeAccountAttribute(String userId, AccountAttributes attribute) {
getReference(constructUsersPath(userId, attributeToPath(attribute))).removeValue();
public static void removeAccountAttribute(String userId, String attribute) {
getReference(constructUsersPath(userId, attribute)).removeValue();
}

/**
Expand All @@ -142,7 +140,7 @@ public static void getUserById(String userId, ValueEventListener valueEventListe
* @param valueEventListener action that should be taken after retrieving the user
*/
public static void getUserByUsername(String username, ValueEventListener valueEventListener) {
USERS_REFERENCE.orderByChild(attributeToPath(USERNAME)).equalTo(username)
USERS_REFERENCE.orderByChild(USERNAME).equalTo(username)
.addListenerForSingleValueEvent(valueEventListener);
}

Expand All @@ -154,7 +152,7 @@ public static void getUserByUsername(String username, ValueEventListener valueEv
* @param valueEventListener action that should be taken after retrieving the user
*/
public static void getUserByEmail(String email, ValueEventListener valueEventListener) {
USERS_REFERENCE.orderByChild(attributeToPath(EMAIL)).equalTo(email)
USERS_REFERENCE.orderByChild(EMAIL).equalTo(email)
.addListenerForSingleValueEvent(valueEventListener);
}

Expand All @@ -166,7 +164,7 @@ public static void getUserByEmail(String email, ValueEventListener valueEventLis
* @param valueEventListener action that should be taken after retrieving the friends
*/
public static void getAllFriends(String userId, ValueEventListener valueEventListener) {
getReference(constructUsersPath(userId, attributeToPath(FRIENDS)))
getReference(constructUsersPath(userId, FRIENDS))
.addListenerForSingleValueEvent(valueEventListener);
}

Expand All @@ -177,7 +175,7 @@ public static void getAllFriends(String userId, ValueEventListener valueEventLis
*/
public static void getFriend(String userId, String friendId,
ValueEventListener valueEventListener) {
getReference(constructUsersPath(userId, attributeToPath(FRIENDS), friendId))
getReference(constructUsersPath(userId, FRIENDS, friendId))
.addListenerForSingleValueEvent(valueEventListener);
}

Expand All @@ -189,7 +187,7 @@ public static void getFriend(String userId, String friendId,
* @param newValue new status of friendship
*/
public static void setFriendValue(String userId, String friendId, int newValue) {
getReference(constructUsersPath(userId, attributeToPath(FRIENDS), friendId))
getReference(constructUsersPath(userId, FRIENDS, friendId))
.setValue(newValue, createCompletionListener());
}

Expand All @@ -200,7 +198,7 @@ public static void setFriendValue(String userId, String friendId, int newValue)
* @param friendId id of friend to be removed
*/
public static void removeFriend(String userId, String friendId) {
getReference(constructUsersPath(userId, attributeToPath(FRIENDS), friendId))
getReference(constructUsersPath(userId, FRIENDS, friendId))
.removeValue(createCompletionListener());
}

Expand All @@ -211,7 +209,7 @@ public static void removeFriend(String userId, String friendId) {
* @param item item that will be inserted
*/
public static void setShopItemValue(String userId, ShopItem item) {
getReference(constructUsersPath(userId, attributeToPath(BOUGHT_ITEMS),
getReference(constructUsersPath(userId, BOUGHT_ITEMS,
item.getColorItem().toString()))
.setValue(item.getPriceItem(), createCompletionListener());
}
Expand All @@ -220,38 +218,38 @@ public static void setShopItemValue(String userId, ShopItem item) {
* Sets a listener to an attribute of a given user.
*
* @param userId id of user whose attribute will be observed
* @param attribute enum to determine which attribute to observe
* @param attribute which attribute to observe
* @param valueEventListener listener to add
*/
public static void setListenerToAccountAttribute(String userId, AccountAttributes attribute,
public static void setListenerToAccountAttribute(String userId, String attribute,
ValueEventListener valueEventListener) {
getReference(constructUsersPath(userId, attributeToPath(attribute)))
getReference(constructUsersPath(userId, attribute))
.addValueEventListener(valueEventListener);
}

/**
* Removes the listener to an attribute of a given user.
*
* @param userId id of user whose attribute won't be observed anymore
* @param attribute enum to determine which attribute's listener to remove
* @param attribute which attribute's listener to remove
* @param valueEventListener listener to remove
*/
public static void removeListenerFromAccountAttribute(
String userId, AccountAttributes attribute, ValueEventListener valueEventListener) {
getReference(constructUsersPath(userId, attributeToPath(attribute)))
String userId, String attribute, ValueEventListener valueEventListener) {
getReference(constructUsersPath(userId, attribute))
.removeEventListener(valueEventListener);
}

/**
* Gets an attribute from a given room in the database.
*
* @param roomId id of the room to get the attribute from
* @param attribute attribute to get
* @param attribute which attribute to get
* @param valueEventListener listener to run on completion
*/
public static void getRoomAttribute(String roomId, RoomAttributes attribute,
public static void getRoomAttribute(String roomId, String attribute,
ValueEventListener valueEventListener) {
getReference(constructRoomsPath(roomId, attributeToPath(attribute)))
getReference(constructRoomsPath(roomId, attribute))
.addListenerForSingleValueEvent(valueEventListener);
}

Expand All @@ -264,8 +262,8 @@ public static void getRoomAttribute(String roomId, RoomAttributes attribute,
* @param newValue associated to the user
*/
public static void setValueToUserInRoomAttribute(String roomId, String username,
RoomAttributes attribute, Object newValue) {
getReference(constructRoomsPath(roomId, attributeToPath(attribute), username))
String attribute, Object newValue) {
getReference(constructRoomsPath(roomId, attribute, username))
.setValue(newValue);
}

Expand All @@ -276,48 +274,40 @@ public static void setValueToUserInRoomAttribute(String roomId, String username,
* @param account user that should be deleted
*/
public static void removeUserFromRoom(String roomId, Account account) {
getReference(constructRoomsPath(roomId,
attributeToPath(USERS), account.getUserId()))
.removeValue();
getReference(constructRoomsPath(roomId,
attributeToPath(RANKING), account.getUsername()))
.removeValue();
getReference(constructRoomsPath(roomId,
attributeToPath(FINISHED), account.getUsername()))
.removeValue();
getReference(constructRoomsPath(roomId,
attributeToPath(UPLOAD_DRAWING), account.getUsername()))
getReference(constructRoomsPath(roomId, USERS, account.getUserId())).removeValue();
getReference(constructRoomsPath(roomId, RANKING, account.getUsername())).removeValue();
getReference(constructRoomsPath(roomId, FINISHED, account.getUsername())).removeValue();
getReference(constructRoomsPath(roomId, UPLOAD_DRAWING, account.getUsername()))
.removeValue();
}

/**
* Sets a listener to an attribute of a given room.
*
* @param roomId id of room whose attribute will be observed
* @param attribute enum to determine which attribute to observe
* @param attribute which attribute to observe
* @param valueEventListener listener to handle response
*/
public static void setListenerToRoomAttribute(String roomId, RoomAttributes attribute,
public static void setListenerToRoomAttribute(String roomId, String attribute,
ValueEventListener valueEventListener) {
getReference(constructRoomsPath(roomId, attributeToPath(attribute)))
getReference(constructRoomsPath(roomId, attribute))
.addValueEventListener(valueEventListener);
}

/**
* Removes a listener from an attribute of a given room.
*/
public static void removeListenerFromRoomAttribute(String roomId, RoomAttributes attribute,
public static void removeListenerFromRoomAttribute(String roomId, String attribute,
ValueEventListener valueEventListener) {
getReference(constructRoomsPath(roomId, attributeToPath(attribute)))
getReference(constructRoomsPath(roomId, attribute))
.removeEventListener(valueEventListener);
}

/**
* Returns the {@link DatabaseReference} of an attribute in a given room.
*/
public static DatabaseReference getRoomAttributeReference(String roomId,
RoomAttributes attribute) {
return getReference(constructRoomsPath(roomId, attributeToPath(attribute)));
public static DatabaseReference getRoomAttributeReference(String roomId, String attribute) {
return getReference(constructRoomsPath(roomId, attribute));
}

/**
Expand All @@ -326,7 +316,7 @@ public static DatabaseReference getRoomAttributeReference(String roomId,
* @param userId id of user whose online value will be set to offline upon disconnection
*/
public static void changeToOfflineOnDisconnect(String userId) {
FbDatabase.getReference(constructUsersPath(userId, attributeToPath(STATUS)))
FbDatabase.getReference(constructUsersPath(userId, STATUS))
.onDisconnect()
.setValue(OFFLINE.ordinal());
}
Expand Down
Loading

0 comments on commit a0cb9c5

Please sign in to comment.