diff --git a/app/src/main/java/com/udacity/firebase/shoppinglistplusplus/ui/activeListDetails/ActiveListDetailsActivity.java b/app/src/main/java/com/udacity/firebase/shoppinglistplusplus/ui/activeListDetails/ActiveListDetailsActivity.java index 6102478..d08f1ca 100755 --- a/app/src/main/java/com/udacity/firebase/shoppinglistplusplus/ui/activeListDetails/ActiveListDetailsActivity.java +++ b/app/src/main/java/com/udacity/firebase/shoppinglistplusplus/ui/activeListDetails/ActiveListDetailsActivity.java @@ -3,42 +3,88 @@ import android.app.DialogFragment; import android.os.Bundle; import android.support.v7.widget.Toolbar; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; +import com.firebase.client.DataSnapshot; +import com.firebase.client.Firebase; +import com.firebase.client.FirebaseError; +import com.firebase.client.ValueEventListener; import com.udacity.firebase.shoppinglistplusplus.R; import com.udacity.firebase.shoppinglistplusplus.model.ShoppingList; import com.udacity.firebase.shoppinglistplusplus.ui.BaseActivity; +import com.udacity.firebase.shoppinglistplusplus.utils.Constants; /** * Represents the details screen for the selected shopping list */ public class ActiveListDetailsActivity extends BaseActivity { private static final String LOG_TAG = ActiveListDetailsActivity.class.getSimpleName(); + private Firebase mActiveListRef; private ListView mListView; private ShoppingList mShoppingList; + @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_active_list_details); + /** + * Create Firebase references + */ + mActiveListRef = new Firebase(Constants.FIREBASE_URL_ACTIVE_LIST); + + /** * Link layout elements from XML and setup the toolbar */ initializeScreen(); - // TODO Add the Firebase code here that will set the appropriate title for the - // detail screen using setTitle and passing in the name of the current - // shopping list. You might want to save this shopping list as well. - // You can but the invalidateOptionsMenu call inside of the same block of code. - // If the shopping list doesn't exist, close the activity using finish() + /** + * Save the most recent version of current shopping list into mShoppingList instance + * variable an update the UI to match the current list. + */ + mActiveListRef.addValueEventListener(new ValueEventListener() { + @Override + public void onDataChange(DataSnapshot snapshot) { + + /** + * Saving the most recent version of current shopping list into mShoppingList if present + * finish() the activity if the list is null (list was removed or unshared by it's owner + * while current user is in the list details activity) + */ + ShoppingList shoppingList = snapshot.getValue(ShoppingList.class); + + if (shoppingList == null) { + finish(); + /** + * Make sure to call return, otherwise the rest of the method will execute, + * even after calling finish. + */ + return; + } + mShoppingList = shoppingList; + + /* Calling invalidateOptionsMenu causes onCreateOptionsMenu to be called */ + invalidateOptionsMenu(); + + /* Set title appropriately. */ + setTitle(shoppingList.getListName()); + } + + @Override + public void onCancelled(FirebaseError firebaseError) { + Log.e(LOG_TAG, + getString(R.string.log_error_the_read_failed) + + firebaseError.getMessage()); + } + }); - /* Calling invalidateOptionsMenu causes onCreateOptionsMenu to be called */ - invalidateOptionsMenu(); /** * Set up click listeners for interaction. diff --git a/app/src/main/java/com/udacity/firebase/shoppinglistplusplus/ui/activeLists/AddListDialogFragment.java b/app/src/main/java/com/udacity/firebase/shoppinglistplusplus/ui/activeLists/AddListDialogFragment.java index 02d6c80..eb5a2b3 100644 --- a/app/src/main/java/com/udacity/firebase/shoppinglistplusplus/ui/activeLists/AddListDialogFragment.java +++ b/app/src/main/java/com/udacity/firebase/shoppinglistplusplus/ui/activeLists/AddListDialogFragment.java @@ -103,7 +103,7 @@ public void addShoppingList() { // Go to the "activeList" child node of the root node. // This will create the node for you if it doesn't already exist. // Then using the setValue menu it will serialize the ShoppingList POJO - ref.child("activeList").setValue(currentList); + ref.child(Constants.FIREBASE_LOCATION_ACTIVE_LIST).setValue(currentList); } } diff --git a/app/src/main/java/com/udacity/firebase/shoppinglistplusplus/ui/activeLists/ShoppingListsFragment.java b/app/src/main/java/com/udacity/firebase/shoppinglistplusplus/ui/activeLists/ShoppingListsFragment.java index f6e4ecd..346316c 100755 --- a/app/src/main/java/com/udacity/firebase/shoppinglistplusplus/ui/activeLists/ShoppingListsFragment.java +++ b/app/src/main/java/com/udacity/firebase/shoppinglistplusplus/ui/activeLists/ShoppingListsFragment.java @@ -1,6 +1,7 @@ package com.udacity.firebase.shoppinglistplusplus.ui.activeLists; +import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; @@ -16,6 +17,7 @@ import com.firebase.client.ValueEventListener; import com.udacity.firebase.shoppinglistplusplus.R; import com.udacity.firebase.shoppinglistplusplus.model.ShoppingList; +import com.udacity.firebase.shoppinglistplusplus.ui.activeListDetails.ActiveListDetailsActivity; import com.udacity.firebase.shoppinglistplusplus.utils.Constants; import com.udacity.firebase.shoppinglistplusplus.utils.Utils; @@ -75,7 +77,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, /** * Create Firebase references */ - Firebase refListName = new Firebase(Constants.FIREBASE_URL).child("activeList"); + Firebase refListName = new Firebase(Constants.FIREBASE_URL).child(Constants.FIREBASE_LOCATION_ACTIVE_LIST); /** * Add ValueEventListeners to Firebase references @@ -121,8 +123,14 @@ public void onItemClick(AdapterView parent, View view, int position, long id) } }); - // TODO Add an OnClick listener here so that when the user clicks on the - // mTextViewListName it opens up an instance of ActiveListDetailsActivity + mTextViewListName.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + /* Starts an active showing the details for the selected list */ + Intent intent = new Intent(getActivity(), ActiveListDetailsActivity.class); + startActivity(intent); + } + }); return rootView; } diff --git a/app/src/main/java/com/udacity/firebase/shoppinglistplusplus/utils/Constants.java b/app/src/main/java/com/udacity/firebase/shoppinglistplusplus/utils/Constants.java index 09b8d1f..65d1d94 100755 --- a/app/src/main/java/com/udacity/firebase/shoppinglistplusplus/utils/Constants.java +++ b/app/src/main/java/com/udacity/firebase/shoppinglistplusplus/utils/Constants.java @@ -11,21 +11,20 @@ public final class Constants { * Constants related to locations in Firebase, such as the name of the node * where active lists are stored (ie "activeLists") */ - + public static final String FIREBASE_LOCATION_ACTIVE_LIST = "activeList"; /** * Constants for Firebase object properties */ public static final String FIREBASE_PROPERTY_LIST_NAME = "listName"; public static final String FIREBASE_PROPERTY_TIMESTAMP = "timestamp"; - // TODO Consider adding and using a constant here for the key where your lists are stored - + /** * Constants for Firebase URL */ public static final String FIREBASE_URL = BuildConfig.UNIQUE_FIREBASE_ROOT_URL; - // TODO Consider adding a constant for the URL for your lists + public static final String FIREBASE_URL_ACTIVE_LIST = FIREBASE_URL + "/" + FIREBASE_LOCATION_ACTIVE_LIST; /** @@ -33,4 +32,5 @@ public final class Constants { */ public static final String KEY_LAYOUT_RESOURCE = "LAYOUT_RESOURCE"; + }