From 9a2520efd521c57662708cc4bc58df7e8722a81a Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 1 Aug 2018 09:29:21 +0200 Subject: [PATCH] Can get and display information about groups in notification fragment. --- .../client/data/arrays/NotifsList.java | 47 ++++ .../data/enums/GroupPostsCreationLevel.java | 11 + .../data/enums/GroupRegistrationLevel.java | 12 + .../client/data/enums/GroupVisibility.java | 12 + .../data/enums/GroupsMembershipLevels.java | 17 ++ .../client/data/helpers/GroupsHelper.java | 220 ++++++++++++++++++ .../comunic/client/data/models/GroupInfo.java | 119 ++++++++++ .../client/data/utils/NotifsUtils.java | 10 +- .../ui/adapters/NotificationsAdapter.java | 14 +- .../ui/fragments/NotificationsFragment.java | 15 +- 10 files changed, 469 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/enums/GroupPostsCreationLevel.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/enums/GroupRegistrationLevel.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/enums/GroupVisibility.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/enums/GroupsMembershipLevels.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/helpers/GroupsHelper.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/models/GroupInfo.java diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/arrays/NotifsList.java b/app/src/main/java/org/communiquons/android/comunic/client/data/arrays/NotifsList.java index 2a78ef4..43c5e0c 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/arrays/NotifsList.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/arrays/NotifsList.java @@ -3,6 +3,7 @@ package org.communiquons.android.comunic.client.data.arrays; import android.support.annotation.Nullable; import android.util.ArrayMap; +import org.communiquons.android.comunic.client.data.models.GroupInfo; import org.communiquons.android.comunic.client.data.models.UserInfo; import org.communiquons.android.comunic.client.data.enums.NotifElemType; import org.communiquons.android.comunic.client.data.models.Notif; @@ -23,6 +24,11 @@ public class NotifsList extends ArrayList { */ private ArrayMap mUsersInfo; + /** + * Information about the groups related to the notifications + */ + private ArrayMap mGroupsInfo; + /** * Get and return the IDs of the users related to the notifications * @@ -62,6 +68,29 @@ public class NotifsList extends ArrayList { return IDs; } + /** + * Get and return the IDs of the groups related to the notificatoin + * + * @return The ID of the groups related to the notification + */ + public ArrayList getGroupsID(){ + ArrayList IDs = new ArrayList<>(); + + for(Notif notif : this){ + + if(notif.getOn_elem_type() == NotifElemType.GROUP_PAGE + && !IDs.contains(notif.getOn_elem_id())) + IDs.add(notif.getOn_elem_id()); + + if(notif.getFrom_container_type() == NotifElemType.GROUP_PAGE + && !IDs.contains(notif.getFrom_container_id())) + IDs.add(notif.getFrom_container_id()); + + } + + return IDs; + } + /** * Set information about the users related to the notifications * @@ -80,4 +109,22 @@ public class NotifsList extends ArrayList { public ArrayMap getUsersInfo() { return mUsersInfo; } + + /** + * Get information about the related groups + * + * @return Information about the related groups + */ + public ArrayMap getGroupsInfo() { + return mGroupsInfo; + } + + /** + * Set information about the related groups + * + * @param mGroupsInfo Information about the groups + */ + public void setGroupsInfo(ArrayMap mGroupsInfo) { + this.mGroupsInfo = mGroupsInfo; + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/enums/GroupPostsCreationLevel.java b/app/src/main/java/org/communiquons/android/comunic/client/data/enums/GroupPostsCreationLevel.java new file mode 100644 index 0000000..2981eee --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/enums/GroupPostsCreationLevel.java @@ -0,0 +1,11 @@ +package org.communiquons.android.comunic.client.data.enums; + +/** + * Posts creations levels + * + * @author Pierre HUBERT + */ +public enum GroupPostsCreationLevel { + MODERATORS, + MEMBERS +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/enums/GroupRegistrationLevel.java b/app/src/main/java/org/communiquons/android/comunic/client/data/enums/GroupRegistrationLevel.java new file mode 100644 index 0000000..e5bedc3 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/enums/GroupRegistrationLevel.java @@ -0,0 +1,12 @@ +package org.communiquons.android.comunic.client.data.enums; + +/** + * Group registration levels + * + * @author Pierre HUBERT + */ +public enum GroupRegistrationLevel { + OPEN, + MODERATED, + CLOSED +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/enums/GroupVisibility.java b/app/src/main/java/org/communiquons/android/comunic/client/data/enums/GroupVisibility.java new file mode 100644 index 0000000..5d82f15 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/enums/GroupVisibility.java @@ -0,0 +1,12 @@ +package org.communiquons.android.comunic.client.data.enums; + +/** + * Groups visibility levels + * + * @author Pierre HUBERT + */ +public enum GroupVisibility { + OPEN, + PRIVATE, + SECRETE +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/enums/GroupsMembershipLevels.java b/app/src/main/java/org/communiquons/android/comunic/client/data/enums/GroupsMembershipLevels.java new file mode 100644 index 0000000..b33db54 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/enums/GroupsMembershipLevels.java @@ -0,0 +1,17 @@ +package org.communiquons.android.comunic.client.data.enums; + +/** + * Groups membership levels + * + * @author Pierre HUBERT + */ +public enum GroupsMembershipLevels { + + ADMINISTRATOR, + MODERATOR, + MEMBER, + INVITED, + PENDING, + VISITOR + +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/GroupsHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/GroupsHelper.java new file mode 100644 index 0000000..9c799ad --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/GroupsHelper.java @@ -0,0 +1,220 @@ +package org.communiquons.android.comunic.client.data.helpers; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.util.ArrayMap; + +import org.communiquons.android.comunic.client.data.enums.GroupPostsCreationLevel; +import org.communiquons.android.comunic.client.data.enums.GroupRegistrationLevel; +import org.communiquons.android.comunic.client.data.enums.GroupVisibility; +import org.communiquons.android.comunic.client.data.enums.GroupsMembershipLevels; +import org.communiquons.android.comunic.client.data.models.APIRequest; +import org.communiquons.android.comunic.client.data.models.APIResponse; +import org.communiquons.android.comunic.client.data.models.GroupInfo; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; + +/** + * Groups helper + */ +public class GroupsHelper extends BaseHelper { + + /** + * Groups constructor + * + * @param context The context of the application + */ + public GroupsHelper(Context context) { + super(context); + } + + + /** + * Get information about multiple groups + * + * @param IDs The ID of the target groups + * @return Information about the related groups + */ + @Nullable + public ArrayMap getInfoMultiple(ArrayList IDs){ + return downloadMultiple(IDs); + } + + /** + * Download information of multiple groups from the server + * + * @param IDs The IDs of the groups to get + * @return Information about the groups / null in case of failure + */ + @Nullable + private ArrayMap downloadMultiple(ArrayList IDs){ + + //Make a request over the server + APIRequest request = new APIRequest(getContext(), "groups/get_multiple_info"); + + //Process the list of groups to get + StringBuilder reqList = new StringBuilder(); + for(Integer id : IDs) { + reqList.append(id); + reqList.append(","); + } + request.addString("list", reqList.toString()); + + + try { + + APIResponse response = new APIRequestHelper().exec(request); + JSONObject object = response.getJSONObject(); + ArrayMap list = new ArrayMap<>(); + + if(object == null) return null; + + //Parse the list of keys + for(Integer id : IDs){ + + //Get raw information about the group + JSONObject info = object.getJSONObject(id + ""); + + //Check if we did not get anything + if(info == null) + return null; + + list.put(id, parse_group_info(info)); + } + + return list; + + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * Parse group information into GroupInfo object + * + * @param object The object to parse + * @return Generated group object + * @throws JSONException In case of failure + */ + private GroupInfo parse_group_info(@NonNull JSONObject object) throws JSONException { + + GroupInfo info = new GroupInfo(); + + info.setId(object.getInt("id")); + info.setName(object.getString("name")); + info.setIcon_url(object.getString("icon_url")); + info.setNumber_members(object.getInt("number_members")); + info.setMembershipLevel(parse_membership_level(object.getString("membership"))); + info.setVisibility(parse_group_visibility(object.getString("visibility"))); + info.setRegistrationLevel(parse_group_registration_level(object.getString("registration_level"))); + info.setPostCreationLevel(parse_post_creation_level(object.getString("posts_level"))); + info.setVirtualDirectory(object.getString("virtual_directory")); + info.setFollowing(object.getBoolean("following")); + + return info; + + } + + /** + * Parse membership level + * + * @param level Membership level to parse as string + * @return Equivalent membership level + */ + private GroupsMembershipLevels parse_membership_level(String level){ + switch (level){ + + case "administrator": + return GroupsMembershipLevels.ADMINISTRATOR; + + case "moderator": + return GroupsMembershipLevels.MODERATOR; + + case "member": + return GroupsMembershipLevels.MEMBER; + + case "invited": + return GroupsMembershipLevels.INVITED; + + case "pending": + return GroupsMembershipLevels.PENDING; + + case "visitor": + return GroupsMembershipLevels.VISITOR; + + default: + throw new RuntimeException("Unsupported membership level: " + level); + } + } + + /** + * Parse group visibility level + * + * @param level The level to parse + * @return Equivalent visibility level + */ + private GroupVisibility parse_group_visibility(String level){ + switch (level){ + + case "open": + return GroupVisibility.OPEN; + + case "private": + return GroupVisibility.PRIVATE; + + case "secrete": + return GroupVisibility.SECRETE; + + default: + throw new RuntimeException("Unsupported group visibility level: " + level); + } + } + + /** + * Parse group registration level + * + * @param level The level to parse + * @return Equivalent registration level + */ + private GroupRegistrationLevel parse_group_registration_level(String level){ + switch (level){ + + case "open": + return GroupRegistrationLevel.OPEN; + + case "moderated": + return GroupRegistrationLevel.MODERATED; + + case "closed": + return GroupRegistrationLevel.CLOSED; + + default: + throw new RuntimeException("Unsupported group registration level: " + level); + } + } + + /** + * Parse post creation level + * + * @param level The level to parse + * @return Equivalent post creation post level + */ + private GroupPostsCreationLevel parse_post_creation_level(String level){ + switch (level){ + + case "moderators": + return GroupPostsCreationLevel.MODERATORS; + + case "members": + return GroupPostsCreationLevel.MEMBERS; + + + default: + throw new RuntimeException("Unsupported group post creation level: " + level); + } + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/models/GroupInfo.java b/app/src/main/java/org/communiquons/android/comunic/client/data/models/GroupInfo.java new file mode 100644 index 0000000..a72fc55 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/models/GroupInfo.java @@ -0,0 +1,119 @@ +package org.communiquons.android.comunic.client.data.models; + +import android.support.annotation.Nullable; + +import org.communiquons.android.comunic.client.data.enums.GroupRegistrationLevel; +import org.communiquons.android.comunic.client.data.enums.GroupVisibility; +import org.communiquons.android.comunic.client.data.enums.GroupsMembershipLevels; +import org.communiquons.android.comunic.client.data.enums.GroupPostsCreationLevel; + +/** + * Group information base model + * + * @author Pierre HUBERT + */ +public class GroupInfo { + + //Private fields + private int id; + private String name; + private String icon_url; + private int number_members; + private GroupsMembershipLevels membershipLevel; + private GroupVisibility visibility; + private GroupRegistrationLevel registrationLevel; + private GroupPostsCreationLevel postCreationLevel; + private String virtualDirectory; + private boolean following; + + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getIcon_url() { + return icon_url; + } + + public void setIcon_url(String icon_url) { + this.icon_url = icon_url; + } + + public int getNumber_members() { + return number_members; + } + + public void setNumber_members(int number_members) { + this.number_members = number_members; + } + + public GroupsMembershipLevels getMembershipLevel() { + return membershipLevel; + } + + public void setMembershipLevel(GroupsMembershipLevels membershipLevel) { + this.membershipLevel = membershipLevel; + } + + public GroupVisibility getVisibility() { + return visibility; + } + + public void setVisibility(GroupVisibility visibility) { + this.visibility = visibility; + } + + public GroupRegistrationLevel getRegistrationLevel() { + return registrationLevel; + } + + public void setRegistrationLevel(GroupRegistrationLevel registrationLevel) { + this.registrationLevel = registrationLevel; + } + + public GroupPostsCreationLevel getPostCreationLevel() { + return postCreationLevel; + } + + public void setPostCreationLevel(GroupPostsCreationLevel creationLevel) { + this.postCreationLevel = creationLevel; + } + + @Nullable + public String getVirtualDirectory() { + return virtualDirectory; + } + + public boolean hasVirtualDirectory(){ + return virtualDirectory != null; + } + + public void setVirtualDirectory(@Nullable String virtualDirectory) { + this.virtualDirectory = virtualDirectory; + + if(virtualDirectory != null){ + if(virtualDirectory.equals("null")) + this.virtualDirectory = null; + } + } + + public boolean isFollowing() { + return following; + } + + public void setFollowing(boolean following) { + this.following = following; + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/NotifsUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/NotifsUtils.java index 4fbe671..3a0e9e1 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/NotifsUtils.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/NotifsUtils.java @@ -6,10 +6,13 @@ import android.util.ArrayMap; import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.data.enums.NotifElemType; import org.communiquons.android.comunic.client.data.enums.NotificationTypes; +import org.communiquons.android.comunic.client.data.models.GroupInfo; import org.communiquons.android.comunic.client.data.models.Notif; import org.communiquons.android.comunic.client.data.models.UserInfo; import org.communiquons.android.comunic.client.ui.utils.UiUtils; +import java.security.acl.Group; + /** * Notifications utilities * @@ -25,10 +28,12 @@ public class NotifsUtils { * @param context The context of the application * @param notif The target notification * @param userInfos Information about the user of the notification + * @param groupsInfo Information about related groups * @return The message associated to the notification */ public static String getNotificationMessage(Context context, Notif notif, - ArrayMap userInfos){ + ArrayMap userInfos, + ArrayMap groupsInfo){ //First, put the name of the user String message = userInfos.get(notif.getFrom_user_id()).getDisplayFullName(); @@ -74,7 +79,8 @@ public class NotifsUtils { //Group page else if(notif.getFrom_container_type() == NotifElemType.GROUP_PAGE){ - message += UiUtils.getString(context, R.string.notif_on_group_page); + message += UiUtils.getString(context, R.string.notif_on_group_page, + groupsInfo.get(notif.getFrom_container_id()).getName()); } //Return the message diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/NotificationsAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/NotificationsAdapter.java index 21b58a9..402fa97 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/NotificationsAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/NotificationsAdapter.java @@ -13,12 +13,15 @@ import android.widget.TextView; import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.data.helpers.ImageLoadHelper; +import org.communiquons.android.comunic.client.data.models.GroupInfo; import org.communiquons.android.comunic.client.data.models.UserInfo; import org.communiquons.android.comunic.client.data.models.Notif; import org.communiquons.android.comunic.client.data.arrays.NotifsList; import org.communiquons.android.comunic.client.data.utils.NotifsUtils; import org.communiquons.android.comunic.client.data.utils.Utilities; +import java.lang.reflect.Array; + /** * Notifications list adapter * @@ -38,6 +41,11 @@ public class NotificationsAdapter extends ArrayAdapter{ */ private ArrayMap mUsersInfo; + /** + * Information about the groups related to the notifications + */ + private ArrayMap mGroupsInfo; + /** * Public adapter constructor * @@ -47,8 +55,9 @@ public class NotificationsAdapter extends ArrayAdapter{ public NotificationsAdapter(Context context, NotifsList list){ super(context, 0, list); - //Save user information + //Save users and groups information mUsersInfo = list.getUsersInfo(); + mGroupsInfo = list.getGroupsInfo(); mUtils = new Utilities(context); } @@ -75,7 +84,8 @@ public class NotificationsAdapter extends ArrayAdapter{ //Update the message of the notification TextView message = convertView.findViewById(R.id.notification_message); - message.setText(NotifsUtils.getNotificationMessage(getContext(), notif, mUsersInfo)); + message.setText(NotifsUtils.getNotificationMessage(getContext(), notif, + mUsersInfo, mGroupsInfo)); //Update the date of the notification TextView date = convertView.findViewById(R.id.notification_date); 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 5948792..d828ee0 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 @@ -22,6 +22,7 @@ import android.widget.Toast; import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.data.enums.NotifElemType; import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper; +import org.communiquons.android.comunic.client.data.helpers.GroupsHelper; import org.communiquons.android.comunic.client.data.helpers.NotificationsHelper; import org.communiquons.android.comunic.client.data.arrays.NotifsList; import org.communiquons.android.comunic.client.data.models.Notif; @@ -50,6 +51,11 @@ public class NotificationsFragment extends Fragment implements View.OnCreateCont */ private GetUsersHelper mUsersInfoHelper; + /** + * Groups heper + */ + private GroupsHelper mGroupsHelper; + /** * Notifications list */ @@ -94,11 +100,10 @@ public class NotificationsFragment extends Fragment implements View.OnCreateCont public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - //Create notifications helper + //Initialize helpers mNotificationsHelper = new NotificationsHelper(getActivity()); - - //Create get users helper mUsersInfoHelper = new GetUsersHelper(getActivity()); + mGroupsHelper = new GroupsHelper(getActivity()); } @Nullable @@ -228,8 +233,10 @@ public class NotificationsFragment extends Fragment implements View.OnCreateCont NotifsList list = mNotificationsHelper.getListUnread(); //If we got the list of notifications, fetch users information - if(list != null) + if(list != null) { list.setUsersInfo(mUsersInfoHelper.getMultiple(list.getUsersID())); + list.setGroupsInfo(mGroupsHelper.getInfoMultiple(list.getGroupsID())); + } return list;