Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

2.02 The Detail Screen - Solution #15

Open
wants to merge 1 commit into
base: 2.01_the_detail_screen-quiz
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ 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;


/**
* Constants for bundles, extras and shared preferences keys
*/
public static final String KEY_LAYOUT_RESOURCE = "LAYOUT_RESOURCE";


}