diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/notifications/NotificationsHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/notifications/NotificationsHelper.java index 4bb3541..83a0268 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/notifications/NotificationsHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/notifications/NotificationsHelper.java @@ -64,4 +64,25 @@ public class NotificationsHelper { } } + /** + * Intend to delete the entire list of notifications + * + * @return TRUE in case of success / FALSE else + */ + public boolean deleteAllNotifs(){ + + //Perform a request on the server + APIRequestParameters params = new APIRequestParameters(mContext, + "notifications/delete_all"); + + //Try to perform the request on the server + try { + APIResponse response = new APIRequest().exec(params); + return response.getResponse_code() == 200; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java index df19a2a..3b1e7bc 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java @@ -112,9 +112,9 @@ public class MainActivity extends AppCompatActivity //Initialize conversation list helper conversationsListHelper = new ConversationsListHelper(this, dbHelper); - //If it is the first time the application is launched, start the user friends tab + //If it is the first time the application is launched, open notifications fragment if(savedInstanceState == null){ - openFriendsFragment(); + openNotificationsFragment(); } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/NotificationsFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/NotificationsFragment.java index e1c3941..eb87bb0 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/NotificationsFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/NotificationsFragment.java @@ -1,13 +1,19 @@ package org.communiquons.android.comunic.client.ui.fragments; +import android.app.AlertDialog; import android.app.Fragment; +import android.content.Context; +import android.content.DialogInterface; +import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Toast; import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.data.notifications.NotificationsHelper; import org.communiquons.android.comunic.client.ui.activities.MainActivity; /** @@ -19,12 +25,44 @@ import org.communiquons.android.comunic.client.ui.activities.MainActivity; public class NotificationsFragment extends Fragment { + /** + * Notifications helper + */ + private NotificationsHelper mNotificationsHelper; + + /** + * Delete all the notifications button + */ + private View mDeleteNotificationsBtn; + + @Override + public void onAttach(Context context) { + super.onAttach(context); + + //Create notifications helper + mNotificationsHelper = new NotificationsHelper(context); + } + @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_notifications, container, false); } + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + //Delete all the notifications action + mDeleteNotificationsBtn = view.findViewById(R.id.delete_all_notif_btn); + mDeleteNotificationsBtn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + confirmNotificationsDeletion(); + } + }); + } + @Override public void onResume() { super.onResume(); @@ -36,4 +74,62 @@ public class NotificationsFragment extends Fragment { ((MainActivity) getActivity()) .setSelectedNavigationItem(R.id.main_bottom_navigation_notif); } + + /** + * Ask the user to confirm the deletion of all the notifications + */ + private void confirmNotificationsDeletion(){ + + //Create and display a confirmation dialog + new AlertDialog.Builder(getActivity()) + .setTitle(R.string.dialog_deleteallnotifs_title) + .setMessage(R.string.dialog_deleteallnotifs_message) + .setNegativeButton(R.string.dialog_deleteallnotifs_cancel, null) + + .setPositiveButton(R.string.dialog_deleteallnotifs_confirm, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + processAllNotificationsDeletion(); + } + }) + + .show(); + + } + + /** + * Do the deletion of all the notifications + */ + private void processAllNotificationsDeletion(){ + + //Perform the operation in the background + new AsyncTask(){ + + @Override + protected Boolean doInBackground(Void... params) { + return mNotificationsHelper.deleteAllNotifs(); + } + + @Override + protected void onPostExecute(Boolean result) { + + //Check if the activity has been destroyed + if(getActivity() == null) + return; + + //Check for error + if(!result) { + Toast.makeText(getActivity(), R.string.err_delete_all_notifs, + Toast.LENGTH_SHORT).show(); + return; + } + + //Success + Toast.makeText(getActivity(), R.string.success_delete_all_notifs, + Toast.LENGTH_SHORT).show(); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + + } } diff --git a/app/src/main/res/layout/fragment_notifications.xml b/app/src/main/res/layout/fragment_notifications.xml index 3509b84..c9f37a6 100644 --- a/app/src/main/res/layout/fragment_notifications.xml +++ b/app/src/main/res/layout/fragment_notifications.xml @@ -3,4 +3,26 @@ android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> + + + + + + + +