Can delete notifications.

This commit is contained in:
Pierre 2018-04-11 16:29:18 +02:00
parent 711afa46e2
commit 39135ef637
4 changed files with 105 additions and 1 deletions

View File

@ -71,6 +71,29 @@ public class NotificationsHelper {
} }
} }
/**
* Mark a notification as seen
*
* @param notifID The ID of the target notification
* @return TRUE in case of success / FALSE else
*/
public boolean markSeen(int notifID){
//Perform a request on the server
APIRequestParameters params = new APIRequestParameters(mContext, "notifications/mark_seen");
params.addInt("notifID", notifID);
//Try to send the request to the server
try {
APIResponse response = new APIRequestHelper().exec(params);
return response.getResponse_code() == 200;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** /**
* Intend to delete the entire list of notifications * Intend to delete the entire list of notifications
* *

View File

@ -7,9 +7,13 @@ import android.content.DialogInterface;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.view.ContextMenu;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView; import android.widget.ListView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.Toast; import android.widget.Toast;
@ -28,7 +32,7 @@ import org.communiquons.android.comunic.client.ui.adapters.NotificationsAdapter;
* Created by pierre on 4/1/18. * Created by pierre on 4/1/18.
*/ */
public class NotificationsFragment extends Fragment { public class NotificationsFragment extends Fragment implements View.OnCreateContextMenuListener {
/** /**
* Notifications helper * Notifications helper
@ -244,5 +248,71 @@ public class NotificationsFragment extends Fragment {
//Create notification adapter //Create notification adapter
mNotificationsAdapter = new NotificationsAdapter(getActivity(), mNotificationsList); mNotificationsAdapter = new NotificationsAdapter(getActivity(), mNotificationsList);
mNotificationsListView.setAdapter(mNotificationsAdapter); mNotificationsListView.setAdapter(mNotificationsAdapter);
//Set context menu creator
mNotificationsListView.setOnCreateContextMenuListener(this);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
//Check the context menu is targeting the list view
if(v != mNotificationsListView)
return;
//Inflate the menu
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.menu_notification_actions, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
//Fetch source item
AdapterView.AdapterContextMenuInfo src
= (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
//Check if the action is to delete the notification
if(item.getItemId() == R.id.action_delete){
deleteNotification(src.position);
return true;
}
return super.onContextItemSelected(item);
}
/**
* Delete the notification located at a specified position
*
* @param pos The position of the notification to delete
*/
private void deleteNotification(int pos){
//Get the ID of the notification
int notifID = mNotificationsList.get(pos).getId();
//Delete the notification from the list
mNotificationsList.remove(pos);
mNotificationsAdapter.notifyDataSetChanged();
//Delete the notification from the server
new AsyncTask<Integer, Void, Boolean>(){
@Override
protected Boolean doInBackground(Integer... params) {
return mNotificationsHelper.markSeen(params[0]);
}
@Override
protected void onPostExecute(Boolean aBoolean) {
if(getActivity() == null)
return;
//Check for errors
if(!aBoolean)
Toast.makeText(getActivity(), R.string.err_delete_notification,
Toast.LENGTH_SHORT).show();
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, notifID);
} }
} }

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Delete the notification -->
<item
android:id="@+id/action_delete"
android:title="@string/action_delete_notification" />
</menu>

View File

@ -164,4 +164,6 @@
<string name="notif_posted_comment">posted a comment</string> <string name="notif_posted_comment">posted a comment</string>
<string name="notif_on_creator_page">on his / her page</string> <string name="notif_on_creator_page">on his / her page</string>
<string name="notif_on_user_page">on %1$s\'s page</string> <string name="notif_on_user_page">on %1$s\'s page</string>
<string name="action_delete_notification">Delete</string>
<string name="err_delete_notification">An error occurred while trying to delete the notification!</string>
</resources> </resources>