Advertisement
yo2man

8. Self-Destruction

Oct 12th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.05 KB | None | 0 0
  1. // Lesson 8: Self-Destruction
  2. //Lesson 8.1 set timer that sends the ViewImageActivity back to inbox after 10 seconds //simple
  3.  
  4. public class ViewImageActivity extends AppCompatActivity {
  5.  
  6.         //Lesson 8.1 declare a timer for the self-destruction
  7.         Timer timer = new Timer();
  8.         timer.schedule(new TimerTask() {
  9.             @Override
  10.             public void run() {
  11.                 //put the code to go back to the inbox after timer is done
  12.                 finish();
  13.             }
  14.         }, 10 * 1000); // ( , miliseconds[10*1000miliseconds = 10 seconds])
  15.     }
  16.  
  17.  
  18. //Lesson 8.3 Maintaining Scroll position in ListView
  19.  
  20. public class InboxFragment extends ListFragment {
  21.  
  22.     protected List<ParseObject> mMessages; //[*M*]
  23.  
  24.  
  25.     @Override
  26.     //3.6 copied over from MainActivityy
  27.     //onCreateView = when fragment is drawn for the first time. And although similar to onCreate, is NOT onCreate. Simple.
  28.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  29.         View rootView = inflater.inflate(R.layout.fragment_inbox, container, false); //this inflates the layout, acts like setContentView() except for Fragments instead of for Activities
  30.         // (1st Parameter passes in layout that's used for the fragment,
  31.         // 2nd parameter is the container where the fragment will be displayed[the viewPager from MainActivity],
  32.         // 3rd parameter should be 'false' whenever we are adding a fragment to an activity in code) //Simple.
  33.  
  34.         return rootView;
  35.     }
  36.  
  37.     //7.1  We'll want the inbox refreshed every time it's displayed.
  38.     // So we'll add code inside the onResume method.
  39.     @Override
  40.     public void onResume() {
  41.         //We are going to query the message class/table but we only want messages that match our User ID //We only want to get messages sent to us
  42.         super.onResume();
  43.  
  44.         getActivity().setProgressBarIndeterminateVisibility(true);
  45.  
  46.         ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(ParseConstants.CLASS_MESSAGES);
  47.  
  48.         // "where" clause: (if you are familiar with SQL, you'll know this), it desognates how to search through a collection
  49.         // we want to find all messages, WHERE(.whereEqualTo()) our current user ID is of of the recipients.
  50.         query.whereEqualTo(ParseConstants.KEY_RECIPIENT_IDS, ParseUser.getCurrentUser().getObjectId()); //ParseUser.getCurrentUser().getObjectId() = gets the current user's ID
  51.         query.addDescendingOrder(ParseConstants.KEY_CREATED_AT);   //make the most recent messages at the top
  52.         query.findInBackground(new FindCallback<ParseObject>() { //runs the query in the background
  53.             @Override
  54.             public void done(List<ParseObject> messages, ParseException e) {
  55.                 getActivity().setProgressBarIndeterminate(false); //dismiss the progressbar indicator
  56.  
  57.                 if (e == null) { //if exceptions == null:
  58.                     //We found messages!
  59.                     mMessages = messages; //[*M*]
  60.  
  61.                     // copied over from FriendsFragment.java
  62.                     String[] usernames = new String[mMessages.size()];
  63.                     int i = 0;
  64.                     for (ParseObject message : mMessages) {
  65.                         usernames[i] = message.getString(ParseConstants.KEY_SENDER_NAME);
  66.                         i++;
  67.                     }
  68.                     //ArrayAdapter to adapt to the list*
  69.                     /*ArrayAdapter<String> adapter = new ArrayAdapter<String>(
  70.                             getListView().getContext(),
  71.                             android.R.layout.simple_list_item_1,
  72.                             usernames);*/
  73.  
  74.                     //^swap out array adapter for the one we created (MessageAdapter.java):
  75.  
  76.             //LESSON 8.3 LESSON HERE:
  77.                     if (getListView().getAdapter() == null) { // if (list view has an adapter or not)
  78.                     // Lesson 8.3 Make it so when we go back to inbox, it doesn't recreate the adapter but just reuses it if the adapter is already created
  79.                     //so when you hit back from ViewImageActivity, it won't jump straight to the top.
  80.                         MessageAdapter adapter = new MessageAdapter(getListView().getContext(), mMessages);
  81.                         setListAdapter(adapter);
  82.                     } else {
  83.                         //refill adapter
  84.                         ((MessageAdapter)getListView().getAdapter()).refill(mMessages);
  85.                     }
  86.                 }
  87.  
  88.             }
  89.         });
  90.     }
  91.  
  92.  
  93. // Lesson 8.4 Deleting the messages.
  94. // we can delete them from our Parse backend once they are viewed.
  95. // We want the messages to be available until all recipients have viewed them though.
  96. // So we'll start by deleting each recipient from the list of recipients.
  97. // Then, when the last one is deleted, we can delete the whole message and the file itself
  98.  
  99. //Lesson 8.4 Delete the message!
  100.         List<String> ids = message.getList(ParseConstants.KEY_RECIPIENT_IDS); //check the recipients. //return a list of recipient IDs.
  101.  
  102.         //if there are more than one recipient, only remove this recipient from the message and leave the backend alone.
  103.         //so other recipients can still view the message.
  104.         if(ids.size() == 1){ //if (there are only 1 recipient id left[meaning its the last recipient])
  105.             //delete the message from backend
  106.             message.deleteInBackground();
  107.         } else { //otherwise
  108.             // remove the recipient and save
  109.  
  110.         }.
  111.  
  112. // Lesson 8.6 Deleting Partial data for one Recipient
  113. //Lesson 8.4 Delete the message!
  114.         List<String> ids = message.getList(ParseConstants.KEY_RECIPIENT_IDS); //check the recipients. //return a list of recipient IDs.
  115.  
  116.         //if there are more than one recipient, only remove this recipient from the message and leave the backend alone.
  117.         //so other recipients can still view the message.
  118.         if(ids.size() == 1){ //if (there are only 1 recipient id left[meaning its the last recipient])
  119.             //delete the message from backend
  120.             message.deleteInBackground();
  121.         } else { //otherwise
  122.             //Lesson 8.6  remove the recipient and save
  123.             ids.remove(ParseUser.getCurrentUser().getObjectId()); // ids.remove("ObjectId of the current User");
  124.             // Now this line only removes the User ID locally.^^^
  125.             // As usual, we need to let the back end know that we made the change too.
  126.             // The way parse handles edits like this is
  127.             // to download the data, make changes locally, and then save the new version of the data,
  128.             // just like we did when we were editing friend relationships.
  129.  
  130.  
  131.             //So we need to update the RecipientsArray to our Message Parse Object.
  132.             ArrayList<String> idsToRemove = new ArrayList<String>();
  133.             idsToRemove.add(ParseUser.getCurrentUser().getObjectId());
  134.  
  135.             message.removeAll(ParseConstants.KEY_RECIPIENT_IDS, idsToRemove);
  136.             message.saveInBackground();//save it
  137.  
  138.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement