Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Lesson 8: Self-Destruction
- //Lesson 8.1 set timer that sends the ViewImageActivity back to inbox after 10 seconds //simple
- public class ViewImageActivity extends AppCompatActivity {
- //Lesson 8.1 declare a timer for the self-destruction
- Timer timer = new Timer();
- timer.schedule(new TimerTask() {
- @Override
- public void run() {
- //put the code to go back to the inbox after timer is done
- finish();
- }
- }, 10 * 1000); // ( , miliseconds[10*1000miliseconds = 10 seconds])
- }
- //Lesson 8.3 Maintaining Scroll position in ListView
- public class InboxFragment extends ListFragment {
- protected List<ParseObject> mMessages; //[*M*]
- @Override
- //3.6 copied over from MainActivityy
- //onCreateView = when fragment is drawn for the first time. And although similar to onCreate, is NOT onCreate. Simple.
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.fragment_inbox, container, false); //this inflates the layout, acts like setContentView() except for Fragments instead of for Activities
- // (1st Parameter passes in layout that's used for the fragment,
- // 2nd parameter is the container where the fragment will be displayed[the viewPager from MainActivity],
- // 3rd parameter should be 'false' whenever we are adding a fragment to an activity in code) //Simple.
- return rootView;
- }
- //7.1 We'll want the inbox refreshed every time it's displayed.
- // So we'll add code inside the onResume method.
- @Override
- public void onResume() {
- //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
- super.onResume();
- getActivity().setProgressBarIndeterminateVisibility(true);
- ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(ParseConstants.CLASS_MESSAGES);
- // "where" clause: (if you are familiar with SQL, you'll know this), it desognates how to search through a collection
- // we want to find all messages, WHERE(.whereEqualTo()) our current user ID is of of the recipients.
- query.whereEqualTo(ParseConstants.KEY_RECIPIENT_IDS, ParseUser.getCurrentUser().getObjectId()); //ParseUser.getCurrentUser().getObjectId() = gets the current user's ID
- query.addDescendingOrder(ParseConstants.KEY_CREATED_AT); //make the most recent messages at the top
- query.findInBackground(new FindCallback<ParseObject>() { //runs the query in the background
- @Override
- public void done(List<ParseObject> messages, ParseException e) {
- getActivity().setProgressBarIndeterminate(false); //dismiss the progressbar indicator
- if (e == null) { //if exceptions == null:
- //We found messages!
- mMessages = messages; //[*M*]
- // copied over from FriendsFragment.java
- String[] usernames = new String[mMessages.size()];
- int i = 0;
- for (ParseObject message : mMessages) {
- usernames[i] = message.getString(ParseConstants.KEY_SENDER_NAME);
- i++;
- }
- //ArrayAdapter to adapt to the list*
- /*ArrayAdapter<String> adapter = new ArrayAdapter<String>(
- getListView().getContext(),
- android.R.layout.simple_list_item_1,
- usernames);*/
- //^swap out array adapter for the one we created (MessageAdapter.java):
- //LESSON 8.3 LESSON HERE:
- if (getListView().getAdapter() == null) { // if (list view has an adapter or not)
- // 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
- //so when you hit back from ViewImageActivity, it won't jump straight to the top.
- MessageAdapter adapter = new MessageAdapter(getListView().getContext(), mMessages);
- setListAdapter(adapter);
- } else {
- //refill adapter
- ((MessageAdapter)getListView().getAdapter()).refill(mMessages);
- }
- }
- }
- });
- }
- // Lesson 8.4 Deleting the messages.
- // we can delete them from our Parse backend once they are viewed.
- // We want the messages to be available until all recipients have viewed them though.
- // So we'll start by deleting each recipient from the list of recipients.
- // Then, when the last one is deleted, we can delete the whole message and the file itself
- //Lesson 8.4 Delete the message!
- List<String> ids = message.getList(ParseConstants.KEY_RECIPIENT_IDS); //check the recipients. //return a list of recipient IDs.
- //if there are more than one recipient, only remove this recipient from the message and leave the backend alone.
- //so other recipients can still view the message.
- if(ids.size() == 1){ //if (there are only 1 recipient id left[meaning its the last recipient])
- //delete the message from backend
- message.deleteInBackground();
- } else { //otherwise
- // remove the recipient and save
- }.
- // Lesson 8.6 Deleting Partial data for one Recipient
- //Lesson 8.4 Delete the message!
- List<String> ids = message.getList(ParseConstants.KEY_RECIPIENT_IDS); //check the recipients. //return a list of recipient IDs.
- //if there are more than one recipient, only remove this recipient from the message and leave the backend alone.
- //so other recipients can still view the message.
- if(ids.size() == 1){ //if (there are only 1 recipient id left[meaning its the last recipient])
- //delete the message from backend
- message.deleteInBackground();
- } else { //otherwise
- //Lesson 8.6 remove the recipient and save
- ids.remove(ParseUser.getCurrentUser().getObjectId()); // ids.remove("ObjectId of the current User");
- // Now this line only removes the User ID locally.^^^
- // As usual, we need to let the back end know that we made the change too.
- // The way parse handles edits like this is
- // to download the data, make changes locally, and then save the new version of the data,
- // just like we did when we were editing friend relationships.
- //So we need to update the RecipientsArray to our Message Parse Object.
- ArrayList<String> idsToRemove = new ArrayList<String>();
- idsToRemove.add(ParseUser.getCurrentUser().getObjectId());
- message.removeAll(ParseConstants.KEY_RECIPIENT_IDS, idsToRemove);
- message.saveInBackground();//save it
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement