Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://teamtreehouse.com/library/build-a-selfdestructing-message-android-app/sending-messages/selecting-recipients
- /* 6.1
- So we have figured out how to take a picture.
- Well we have photos and videos in our app, but they're trapped on a device.
- Now we need to give the user a way to send them to people,
- and then figure out how to upload the messages to our backend on parse. */
- // There are two things we need to send a message. One is a photo or video send, which we now have.
- // And the other is people to send it to.
- // Let's add an activity that will start for the user as soon as they take or select a photo or video.
- New > Activity > RecipientsActivity
- // Add this to activity_recipients.xml
- <!-- 6.1 if list is empty: show this textview -->
- <TextView
- android:id="@android:id/empty"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/empty_recipients_list_message"/>
- //Now we need to retrieve our list of friends from our backend.
- // Wait a minute though, haven't we done that already?
- // Aha, yes, we can copy and paste some code from the friends fragment.
- // grab everything inside the onResume() { } and we'll fix the errors one by one.
- // then paste it below onCreate() method in RecipientsActivity.java
- // copy over the variables too:
- public static final String TAG = FriendsFragment.class.getSimpleName();
- protected List<ParseUser> mFriends;
- protected ParseRelation<ParseUser> mFriendsRelation;
- protected ParseUser mCurrentUser;
- //enable choosing multiple recipients
- getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
- // 6.2 Adding a send button
- /*
- We need to track everybody that's selected as a recipient.
- And then when we need to display a Send button when at least one friend has been selected.
- 1) Tracking the people selected is easy.
- Thanks to the default list implementation we're using.
- 2) Since our whole screen is a list.
- We'll add the send button in the ActionBar, which is a good place for anyhow since it's used for actions.
- But we'll hide the button when
- nobody is selected and just show it when at least one friend is selected.
- 3) The ListView keeps track of items that are selected.
- It's that same check property on each item that we saw in the EditFriendsActivity.
- We can loop through the list to see who was checked
- */
- protected MenuItem mSendMenuItem; //*
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_recipients, menu);
- mSendMenuItem = menu.getItem(0); //6.2 .getItem('position in the menu') //since we only have one item, its going to be position zero*
- return true;
- }
- //-------------------
- //6.2 make send button visible: by changing the android:visible="false" in menu_recipients.xml to true
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- super.onListItemClick(l, v, position, id); //the ListView is passed in as an "l" parameter
- //6.2 if there is an item Checked/Selected:
- if (l.getCheckedItemCount() > 0) { //getCheckedItemCount() gives us the number of items checked/selected in the listview
- //the send button becomes visible
- mSendMenuItem.setVisible(true); //*
- }
- //when you deselect all the recipients (0), the send button becomes invisible
- else {
- mSendMenuItem.setVisible(false); //*
- }
- }
- //-------------------------------------------------------------------------------------------------------------------
- //6.4 Creating the Message (ParseObject)
- //Create the ParseObject
- //create getRecipientsId helper method to get the id of the selected Recipients in order to send to those recipients
- //The keys (which Ben unnecessarily put into ParseConstants.java
- //Lesson 4.2: Just like Strings.xml is to string resources, ParseConstants class is to Parse constants like "username"
- public final class ParseConstants {
- //Class name //Class begins with "CapitalLetter"
- public static final String CLASS_MESSAGES = "Messages"; //6.4
- //Field names //Fields begin with "lowerCase"
- public static final String KEY_USERNAME = "username";
- public static final String KEY_FRIENDS_RELATION = "friendsRelation";
- //6.4 What do we need to store for a message?
- // We need to know which image or video is tied to this message.
- // We need to know the recipient
- // And we need to know who created the message.
- // So the recipients will know who sent it. So let's add keys for these pieces of data.
- public static final String KEY_RECIPIENT_IDS = "recipientIds";
- public static final String KEY_SENDER_ID = "senderId";
- public static final String KEY_SENDER_NAME = "senderName";
- public static final String KEY_FILE = "file";
- public static final String KEY_FILE_TYPE = "fileType";
- }
- // The code in RecipientsActivity.java
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_send) {
- //6.4 Send Message
- ParseObject message = createMessage();
- // send(message);
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- //6.2 make send button visible: by changing the android:visible="false" in menu_recipients.xml to true
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- super.onListItemClick(l, v, position, id); //the ListView is passed in as an "l" parameter
- //6.2 if there is an item Checked/Selected:
- if (l.getCheckedItemCount() > 0) { //getCheckedItemCount() gives us the number of items checked/selected in the listview
- //the send button becomes visible
- mSendMenuItem.setVisible(true); //*
- }
- //when you deselect all the recipients (0), the send button becomes invisible
- else {
- mSendMenuItem.setVisible(false); //*
- }
- }
- //6.4 create Message ParseObject
- protected ParseObject createMessage() {
- ParseObject message = new ParseObject(ParseConstants.CLASS_MESSAGES); //6.4 create new ParseObject
- //add data to the message (using JSON "keys")
- message.put(ParseConstants.KEY_SENDER_ID, ParseUser.getCurrentUser().getObjectId()); //Sender's ID
- message.put(ParseConstants.KEY_SENDER_NAME, ParseUser.getCurrentUser().getUsername()); //Sender's Username
- message.put(ParseConstants.KEY_RECIPIENT_IDS, getRecipientIds()); //create a method called getRecipientIds() that gets the id of the selected Recipients
- return message;
- }
- protected ArrayList<String> getRecipientIds() {
- ArrayList<String> recipientIds = new ArrayList<String>();
- for (int i = 0; i < getListView().getCount(); i++){ //loops through the whole list
- if (getListView().isItemChecked(i)) { //isItemChecked? if it is:
- recipientIds.add(mFriends.get(i).getObjectId()); //add to the array list the Id of the user at the current position
- }
- }
- return recipientIds; //return the result //simple.
- }
- }
- //6.6 Adding file to the message part 1
- /*
- The ParseFile
- ParseFile lets you store application files in the cloud that would otherwise be too large or cumbersome to fit into a regular ParseObject. The most common use case is storing images but you can also use it for documents, videos, music, and any other binary data (up to 10 megabytes).
- Getting started with ParseFile is easy. First, you'll need to have the data in byte[] form and then create a ParseFile with it. In this example, we'll just use a string:
- */
- byte[] data = "Working at Parse is great!".getBytes();
- ParseFile file = new ParseFile("resume.txt", data);
- // for more info: https://parse.com/docs/android/guide#files-the-parsefile
- //6.7 Adding file to the message part 2
- /*
- Our message is coming along, but now we need to
- create the parse file object and attach that to our message.
- We will use the URI to get the file on the device, we'll convert that
- file to a byte array, and then we'll convert the byte array to a parse file.
- */
- Get the apache library
- Get the FileHelper.java and ImageResizer.java by saving them with "save link as" from RAW .java files on GitHub and dragging and dropping them into the video.
- //6.9 Sending the Message
- just add a few lines of simple code.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement