/** This method is called when user taps "Start/Stop shopping" button */
  public void toggleShopping(View view) {
    /** Create map and fill it in with deep path multi write operations list */
    HashMap<String, Object> updatedUserData = new HashMap<String, Object>();
    String propertyToUpdate = Constants.FIREBASE_PROPERTY_USERS_SHOPPING + "/" + mEncodedEmail;

    /** If current user is already shopping, remove current user from usersShopping map */
    if (mShopping) {

      /* Add the value to update at the specified property for all lists */
      Utils.updateMapForAllWithValue(
          mSharedWithUsers,
          mListId,
          mShoppingList.getOwner(),
          updatedUserData,
          propertyToUpdate,
          null);

      /* Appends the timestamp changes for all lists */
      Utils.updateMapWithTimestampLastChanged(
          mSharedWithUsers, mListId, mShoppingList.getOwner(), updatedUserData);

      /* Do a deep-path update */
      mFirebaseRef.updateChildren(
          updatedUserData,
          new Firebase.CompletionListener() {
            @Override
            public void onComplete(FirebaseError firebaseError, Firebase firebase) {
              /* Updates the reversed timestamp */
              Utils.updateTimestampReversed(
                  firebaseError, LOG_TAG, mListId, mSharedWithUsers, mShoppingList.getOwner());
            }
          });
    } else {
      /**
       * If current user is not shopping, create map to represent User model add to usersShopping
       * map
       */
      HashMap<String, Object> currentUser =
          (HashMap<String, Object>) new ObjectMapper().convertValue(mCurrentUser, Map.class);

      /* Add the value to update at the specified property for all lists */
      Utils.updateMapForAllWithValue(
          mSharedWithUsers,
          mListId,
          mShoppingList.getOwner(),
          updatedUserData,
          propertyToUpdate,
          currentUser);

      /* Appends the timestamp changes for all lists */
      Utils.updateMapWithTimestampLastChanged(
          mSharedWithUsers, mListId, mShoppingList.getOwner(), updatedUserData);

      /* Do a deep-path update */
      mFirebaseRef.updateChildren(
          updatedUserData,
          new Firebase.CompletionListener() {
            @Override
            public void onComplete(FirebaseError firebaseError, Firebase firebase) {
              /* Updates the reversed timestamp */
              Utils.updateTimestampReversed(
                  firebaseError, LOG_TAG, mListId, mSharedWithUsers, mShoppingList.getOwner());
            }
          });
    }
  }
Пример #2
0
 /** Return true if currentUserEmail equals to shoppingList.owner() Return false otherwise */
 public static boolean checkIfOwner(ShoppingList shoppingList, String currentUserEmail) {
   return (shoppingList.getOwner() != null && shoppingList.getOwner().equals(currentUserEmail));
 }