Advertisement
yo2man

6 Sending Messages Ribbit Treehouse lesson

Sep 8th, 2015
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.79 KB | None | 0 0
  1. https://teamtreehouse.com/library/build-a-selfdestructing-message-android-app/sending-messages/selecting-recipients
  2.  
  3. /* 6.1
  4. So we have figured out how to take a picture.
  5. Well we have photos and videos in our app, but they're trapped on a device.
  6. Now we need to give the user a way to send them to people,
  7. and then figure out how to upload the messages to our backend on parse. */
  8.  
  9. // There are two things we need to send a message. One is a photo or video send, which we now have.
  10. // And the other is people to send it to.
  11.  
  12. // Let's add an activity that will start for the user as soon as they take or select a photo or video.
  13. New > Activity > RecipientsActivity
  14.  
  15.  
  16. // Add this to activity_recipients.xml
  17. <!-- 6.1 if list is empty: show this textview -->
  18. <TextView
  19.      android:id="@android:id/empty"
  20.      android:layout_width="match_parent"
  21.      android:layout_height="wrap_content"
  22.      android:text="@string/empty_recipients_list_message"/>
  23.  
  24. //Now we need to retrieve our list of friends from our backend.
  25. // Wait a minute though, haven't we done that already?
  26. // Aha, yes, we can copy and paste some code from the friends fragment.
  27. // grab everything inside the onResume() { } and we'll fix the errors one by one.
  28.  
  29. // then paste it below onCreate() method in RecipientsActivity.java
  30.  
  31. // copy over the variables too:
  32.  
  33. public static final String TAG = FriendsFragment.class.getSimpleName();
  34. protected List<ParseUser> mFriends;
  35. protected ParseRelation<ParseUser> mFriendsRelation;
  36. protected ParseUser mCurrentUser;
  37.  
  38.  
  39. //enable choosing multiple recipients
  40. getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  41.  
  42. // 6.2 Adding a send button
  43. /*
  44. We need to track everybody that's selected as a recipient.
  45. And then when we need to display a Send button when at least one friend has been selected.
  46.  
  47. 1) Tracking the people selected is easy.
  48. Thanks to the default list implementation we're using.
  49.  
  50. 2) Since our whole screen is a list.
  51. We'll add the send button in the ActionBar, which is a good place for anyhow since it's used for actions.
  52. But we'll hide the button when
  53. nobody is selected and just show it when at least one friend is selected.
  54.  
  55. 3) The ListView keeps track of items that are selected.
  56. It's that same check property on each item that we saw in the EditFriendsActivity.
  57. We can loop through the list to see who was checked
  58. */
  59.  
  60.  
  61.     protected MenuItem mSendMenuItem; //*
  62.  
  63.   @Override
  64.     public boolean onCreateOptionsMenu(Menu menu) {
  65.         // Inflate the menu; this adds items to the action bar if it is present.
  66.         getMenuInflater().inflate(R.menu.menu_recipients, menu);
  67.         mSendMenuItem = menu.getItem(0); //6.2  .getItem('position in the menu') //since we only have one item, its going to be position zero*
  68.         return true;
  69.     }
  70.  
  71. //-------------------
  72.  
  73.     //6.2 make send button visible: by changing the android:visible="false" in menu_recipients.xml to true
  74.     @Override
  75.     protected void onListItemClick(ListView l, View v, int position, long id) {
  76.         super.onListItemClick(l, v, position, id); //the ListView is passed in as an "l" parameter
  77.  
  78.         //6.2 if there is an item Checked/Selected:
  79.         if (l.getCheckedItemCount() > 0) { //getCheckedItemCount() gives us the number of items checked/selected in the listview
  80.             //the send button becomes visible
  81.             mSendMenuItem.setVisible(true); //*
  82.         }
  83.             //when you deselect all the recipients (0), the send button becomes invisible
  84.         else {
  85.             mSendMenuItem.setVisible(false); //*
  86.         }
  87.  
  88.  
  89.     }
  90.  
  91. //-------------------------------------------------------------------------------------------------------------------
  92.  
  93. //6.4 Creating the Message (ParseObject)
  94. //Create the ParseObject
  95. //create getRecipientsId helper method to get the id of the selected Recipients in order to send to those recipients
  96.  
  97.  
  98.  
  99. //The keys (which Ben unnecessarily put into ParseConstants.java
  100. //Lesson 4.2: Just like Strings.xml is to string resources, ParseConstants class is to Parse constants like "username"
  101. public final class ParseConstants {
  102.     //Class name //Class begins with "CapitalLetter"
  103.     public static final String CLASS_MESSAGES = "Messages"; //6.4
  104.  
  105.     //Field names //Fields begin with "lowerCase"
  106.     public static final String KEY_USERNAME = "username";
  107.     public static final String KEY_FRIENDS_RELATION = "friendsRelation";
  108.  
  109.     //6.4 What do we need to store for a message?
  110.     // We need to know which image or video is tied to this message.
  111.     // We need to know the recipient
  112.     // And we need to know who created the message.
  113.     // So the recipients will know who sent it. So let's add keys for these pieces of data.
  114.     public static final String KEY_RECIPIENT_IDS = "recipientIds";
  115.     public static final String KEY_SENDER_ID = "senderId";
  116.     public static final String KEY_SENDER_NAME = "senderName";
  117.     public static final String KEY_FILE = "file";
  118.     public static final String KEY_FILE_TYPE = "fileType";
  119.  
  120. }
  121.  
  122.  
  123. // The code in RecipientsActivity.java
  124.  
  125.     @Override
  126.     public boolean onOptionsItemSelected(MenuItem item) {
  127.         // Handle action bar item clicks here. The action bar will
  128.         // automatically handle clicks on the Home/Up button, so long
  129.         // as you specify a parent activity in AndroidManifest.xml.
  130.         int id = item.getItemId();
  131.  
  132.         //noinspection SimplifiableIfStatement
  133.         if (id == R.id.action_send) {
  134.             //6.4 Send Message
  135.             ParseObject message = createMessage();
  136.             // send(message);
  137.             return true;
  138.         }
  139.  
  140.         return super.onOptionsItemSelected(item);
  141.     }
  142.  
  143.     //6.2 make send button visible: by changing the android:visible="false" in menu_recipients.xml to true
  144.     @Override
  145.     protected void onListItemClick(ListView l, View v, int position, long id) {
  146.         super.onListItemClick(l, v, position, id); //the ListView is passed in as an "l" parameter
  147.  
  148.         //6.2 if there is an item Checked/Selected:
  149.         if (l.getCheckedItemCount() > 0) { //getCheckedItemCount() gives us the number of items checked/selected in the listview
  150.             //the send button becomes visible
  151.             mSendMenuItem.setVisible(true); //*
  152.         }
  153.             //when you deselect all the recipients (0), the send button becomes invisible
  154.         else {
  155.             mSendMenuItem.setVisible(false); //*
  156.         }
  157.  
  158.  
  159.     }
  160.  
  161.     //6.4 create Message ParseObject
  162.     protected ParseObject createMessage() {
  163.         ParseObject message = new ParseObject(ParseConstants.CLASS_MESSAGES); //6.4 create new ParseObject
  164.         //add data to the message (using JSON "keys")
  165.         message.put(ParseConstants.KEY_SENDER_ID, ParseUser.getCurrentUser().getObjectId()); //Sender's ID
  166.         message.put(ParseConstants.KEY_SENDER_NAME, ParseUser.getCurrentUser().getUsername()); //Sender's Username
  167.         message.put(ParseConstants.KEY_RECIPIENT_IDS, getRecipientIds()); //create a method called getRecipientIds() that gets the id of the selected Recipients
  168.  
  169.         return message;
  170.     }
  171.  
  172.     protected ArrayList<String> getRecipientIds() {
  173.         ArrayList<String> recipientIds = new ArrayList<String>();
  174.         for (int i = 0; i < getListView().getCount(); i++){ //loops through the whole list
  175.             if (getListView().isItemChecked(i)) { //isItemChecked? if it is:
  176.                 recipientIds.add(mFriends.get(i).getObjectId()); //add to the array list the Id of the user at the current position
  177.             }
  178.         }
  179.         return recipientIds; //return the result //simple.
  180.     }
  181. }
  182.  
  183. //6.6 Adding file to the message part 1
  184. /*
  185. The ParseFile
  186.  
  187. 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).
  188.  
  189. 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:
  190. */
  191. byte[] data = "Working at Parse is great!".getBytes();
  192. ParseFile file = new ParseFile("resume.txt", data);
  193.  
  194. // for more info: https://parse.com/docs/android/guide#files-the-parsefile
  195.  
  196. //6.7 Adding file to the message part 2
  197. /*
  198. Our message is coming along, but now we need to
  199. create the parse file object and attach that to our message.
  200. We will use the URI to get the file on the device, we'll convert that
  201. file to a byte array, and then we'll convert the byte array to a parse file.
  202. */
  203. Get the apache library
  204. 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.
  205.  
  206. //6.9 Sending the Message
  207. just add a few lines of simple code.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement