From e66e07796449befecabba217a15a099b7a03d897 Mon Sep 17 00:00:00 2001 From: Pierre Date: Thu, 12 Apr 2018 14:07:36 +0200 Subject: [PATCH] Notification system ready. --- .../comunic/client/data/arrays/PostsList.java | 2 +- .../client/ui/activities/MainActivity.java | 22 ++- .../ui/fragments/NotificationsFragment.java | 22 ++- .../ui/fragments/SinglePostFragment.java | 169 ++++++++++++++++++ .../ui/listeners/onPostOpenListener.java | 19 ++ .../main/res/layout/fragment_single_post.xml | 11 ++ app/src/main/res/values/strings.xml | 1 + 7 files changed, 241 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/SinglePostFragment.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/onPostOpenListener.java create mode 100644 app/src/main/res/layout/fragment_single_post.xml diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/arrays/PostsList.java b/app/src/main/java/org/communiquons/android/comunic/client/data/arrays/PostsList.java index b8966b2..34859c1 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/arrays/PostsList.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/arrays/PostsList.java @@ -40,7 +40,7 @@ public class PostsList extends ArrayList { //Process the list of comments for(Comment comment : post.getComments_list()){ - if(ids.contains(comment.getUserID())) + if(!ids.contains(comment.getUserID())) ids.add(comment.getUserID()); } 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 659738e..3e75bfd 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 @@ -23,8 +23,10 @@ import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper; import org.communiquons.android.comunic.client.data.helpers.ConversationsListHelper; import org.communiquons.android.comunic.client.data.runnables.FriendRefreshLoopRunnable; import org.communiquons.android.comunic.client.data.services.NotificationsService; +import org.communiquons.android.comunic.client.ui.fragments.SinglePostFragment; import org.communiquons.android.comunic.client.ui.fragments.UserAccessDeniedFragment; import org.communiquons.android.comunic.client.ui.listeners.onOpenUsersPageListener; +import org.communiquons.android.comunic.client.ui.listeners.onPostOpenListener; import org.communiquons.android.comunic.client.ui.listeners.openConversationListener; import org.communiquons.android.comunic.client.ui.listeners.updateConversationListener; import org.communiquons.android.comunic.client.ui.utils.UiUtils; @@ -44,7 +46,7 @@ import org.communiquons.android.comunic.client.ui.fragments.UserPageFragment; * @author Pierre HUBERT */ public class MainActivity extends AppCompatActivity implements openConversationListener, - updateConversationListener, onOpenUsersPageListener { + updateConversationListener, onOpenUsersPageListener, onPostOpenListener { /** * Debug tag @@ -458,4 +460,22 @@ public class MainActivity extends AppCompatActivity implements openConversationL transaction.addToBackStack(null); transaction.commit(); } + + @Override + public void onOpenPost(int postID) { + + //Prepare the arguments + Bundle arguments = new Bundle(); + arguments.putInt(SinglePostFragment.ARGUMENT_POST_ID, postID); + + //Create the fragment + SinglePostFragment singlePostFragment = new SinglePostFragment(); + singlePostFragment.setArguments(arguments); + + //Perform the transaction + FragmentTransaction transaction = getFragmentManager().beginTransaction(); + transaction.addToBackStack(null); + transaction.replace(R.id.main_fragment, singlePostFragment); + transaction.commit(); + } } 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 dcc34b5..378a08d 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 @@ -27,6 +27,7 @@ import org.communiquons.android.comunic.client.data.models.Notif; import org.communiquons.android.comunic.client.ui.activities.MainActivity; import org.communiquons.android.comunic.client.ui.adapters.NotificationsAdapter; import org.communiquons.android.comunic.client.ui.listeners.onOpenUsersPageListener; +import org.communiquons.android.comunic.client.ui.listeners.onPostOpenListener; /** * Notifications fragment @@ -78,6 +79,11 @@ public class NotificationsFragment extends Fragment implements View.OnCreateCont */ private onOpenUsersPageListener mUserPageOpener; + /** + * Post open listener + */ + private onPostOpenListener mOpenPostListener; + @Override public void onAttach(Context context) { super.onAttach(context); @@ -129,6 +135,9 @@ public class NotificationsFragment extends Fragment implements View.OnCreateCont //Get user page opener mUserPageOpener = (onOpenUsersPageListener) getActivity(); + //Get post opener + mOpenPostListener = (onPostOpenListener) getActivity(); + //Check if it is required to fetch the list of notifications if(mNotificationsList == null){ @@ -331,12 +340,12 @@ public class NotificationsFragment extends Fragment implements View.OnCreateCont @Override public void onItemClick(AdapterView parent, View view, int position, long id) { - //Delete the notification - //deleteNotification(position); - //Perform notification action Notif notif = mNotificationsList.get(position); + //Delete the notification + deleteNotification(position); + //For friendship request if(notif.getOn_elem_type() == NotifElemType.FRIEND_REQUEST){ @@ -345,5 +354,12 @@ public class NotificationsFragment extends Fragment implements View.OnCreateCont } + //If the notification is targeting a post + if(notif.getOn_elem_type() == NotifElemType.POST){ + + //Open the post + mOpenPostListener.onOpenPost(notif.getOn_elem_id()); + + } } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/SinglePostFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/SinglePostFragment.java new file mode 100644 index 0000000..8c27d44 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/SinglePostFragment.java @@ -0,0 +1,169 @@ +package org.communiquons.android.comunic.client.ui.fragments; + +import android.app.Fragment; +import android.app.FragmentTransaction; +import android.content.Context; +import android.os.AsyncTask; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.util.ArrayMap; +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.arrays.PostsList; +import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper; +import org.communiquons.android.comunic.client.data.helpers.PostsHelper; +import org.communiquons.android.comunic.client.data.models.Post; +import org.communiquons.android.comunic.client.data.models.UserInfo; + +/** + * Single post fragment + * + * This fragment allows to display a single post + * + * @author Pierre HUBERT + * Created by pierre on 4/12/18. + */ + +public class SinglePostFragment extends Fragment { + + /** + * The name of the argument that contains the ID of the post to open + */ + public static final String ARGUMENT_POST_ID = "post_id"; + + /** + * The ID of the post + */ + private int mPostID = 0; + + /** + * Information about the post + */ + private Post mPost; + + /** + * Post list that contains only a single post + */ + private PostsList mPostsList; + + /** + * Information about the related users + */ + private ArrayMap mUserInfo; + + /** + * Post helper + */ + private PostsHelper mPostsHelper; + + /** + * Get user information helper + */ + private GetUsersHelper mGetUserHelper; + + @Override + public void onAttach(Context context) { + super.onAttach(context); + + //Create post helper + mPostsHelper = new PostsHelper(context); + + //Create get user helper + mGetUserHelper = new GetUsersHelper(context); + } + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + //Get post ID + mPostID = getArguments().getInt(ARGUMENT_POST_ID); + } + + @Nullable + @Override + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { + return inflater.inflate(R.layout.fragment_single_post, container, false); + } + + @Override + public void onResume() { + super.onResume(); + + //Check if the fragment contains information about the post + if(mPost == null || mUserInfo == null){ + getPostInfo(); + } + else + onGotPostInfo(); + } + + /** + * Get information about the post and its related users + */ + private void getPostInfo(){ + + //Perform the request in the background + new AsyncTask(){ + + @Override + protected Void doInBackground(Integer... params) { + + //Intend to get information about the post + mPost = mPostsHelper.getSingle(params[0]); + + if(mPost != null) { + mPostsList = new PostsList(); + mPostsList.add(mPost); + mUserInfo = mGetUserHelper.getMultiple(mPostsList.getUsersId()); + } + + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + + if(getActivity() == null) + return; + + onGotPostInfo(); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mPostID); + + } + + /** + * This method is triggered once we got informations about the post + */ + private void onGotPostInfo(){ + + //Check if we did not get post information + if(mPost == null){ + Toast.makeText(getActivity(), R.string.err_get_post_info, Toast.LENGTH_SHORT).show(); + return; + } + + //Check if we could not get user information + if(mUserInfo == null){ + Toast.makeText(getActivity(), R.string.err_get_users_info, Toast.LENGTH_SHORT).show(); + return; + } + + //Apply the post fragment + PostsListFragment postsListFragment = new PostsListFragment(); + postsListFragment.setPostsList(mPostsList); + postsListFragment.setUsersInfos(mUserInfo); + + //Create and commit a transaction + FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); + transaction.replace(R.id.posts_list_target, postsListFragment); + transaction.commit(); + + + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/onPostOpenListener.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/onPostOpenListener.java new file mode 100644 index 0000000..4c5ae57 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/onPostOpenListener.java @@ -0,0 +1,19 @@ +package org.communiquons.android.comunic.client.ui.listeners; + +/** + * This interface must be implemented by all the activities that can open post + * + * @author Pierre HUBERT + * Created by pierre on 4/12/18. + */ + +public interface onPostOpenListener { + + /** + * This method is triggered when a request to open a single post is made + * + * @param postID The ID of the post to open + */ + void onOpenPost(int postID); + +} diff --git a/app/src/main/res/layout/fragment_single_post.xml b/app/src/main/res/layout/fragment_single_post.xml new file mode 100644 index 0000000..212b6a2 --- /dev/null +++ b/app/src/main/res/layout/fragment_single_post.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 29a87c5..29c0ba5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -172,4 +172,5 @@ Accept request Reject request An error occurred while retrieving friendship status! + An error occurred while trying to get post information!