From fcf31fdae1e0df98aa14d5a4dd4544a340962749 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 1 Dec 2018 11:41:06 +0100 Subject: [PATCH] Display group name --- .../client/data/helpers/GroupsHelper.java | 82 ++++++++++-- .../client/data/models/AdvancedGroupInfo.java | 58 +++++++++ .../asynctasks/GetGroupAdvancedInfoTask.java | 22 ++++ .../groups/GroupPageMainFragment.java | 117 ++++++++++++++++++ .../res/layout/layout_group_main_page.xml | 54 ++++++++ app/src/main/res/values/colors.xml | 2 +- app/src/main/res/values/strings.xml | 1 + 7 files changed, 322 insertions(+), 14 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/models/AdvancedGroupInfo.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/GetGroupAdvancedInfoTask.java create mode 100644 app/src/main/res/layout/layout_group_main_page.xml 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 index 6340c21..5b4752f 100644 --- 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 @@ -11,6 +11,7 @@ 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.AdvancedGroupInfo; import org.communiquons.android.comunic.client.data.models.GroupInfo; import org.json.JSONArray; import org.json.JSONException; @@ -161,6 +162,32 @@ public class GroupsHelper extends BaseHelper { } } + /** + * Get advanced information about a group + * + * @param groupID Target group to get information about + * @return Information about the group / null in case of failure + */ + @Nullable + public AdvancedGroupInfo getAdvancedInformation(int groupID){ + APIRequest request = new APIRequest(getContext(), "groups/get_advanced_info"); + request.addInt("id", groupID); + + try { + + //Execute request and get result + JSONObject object = new APIRequestHelper().exec(request).getJSONObject(); + return parse_advanced_group_info(object); + + } catch (Exception e) { + + //Could not execute request + e.printStackTrace(); + return null; + + } + } + /** * Send a group membership request * @@ -225,30 +252,59 @@ public class GroupsHelper extends BaseHelper { } } + /** + * Overloaded method (see below) + */ + private GroupInfo parse_group_info(@NonNull JSONObject object) throws JSONException { + return parse_group_info(object, new GroupInfo()); + } + /** * Parse group information into GroupInfo object * * @param object The object to parse + * @param group Group object to complete with group information * @return Generated group object * @throws JSONException In case of failure */ - private GroupInfo parse_group_info(@NonNull JSONObject object) throws JSONException { + private GroupInfo parse_group_info(@NonNull JSONObject object, GroupInfo group) throws JSONException { - GroupInfo info = new GroupInfo(); + group.setId(object.getInt("id")); + group.setName(object.getString("name")); + group.setIcon_url(object.getString("icon_url")); + group.setNumber_members(object.getInt("number_members")); + group.setMembershipLevel(parse_membership_level(object.getString("membership"))); + group.setVisibility(parse_group_visibility(object.getString("visibility"))); + group.setRegistrationLevel(parse_group_registration_level(object.getString("registration_level"))); + group.setPostCreationLevel(parse_post_creation_level(object.getString("posts_level"))); + group.setVirtualDirectory(object.getString("virtual_directory")); + group.setFollowing(object.getBoolean("following")); - 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 group; - return info; + } + /** + * Parse advanced group information into AdvancedGroupInfo object + * + * @param object The object to parse + * @return Generated group information object + * @throws JSONException In case of failure + */ + private AdvancedGroupInfo parse_advanced_group_info(@NonNull JSONObject object) throws JSONException { + + AdvancedGroupInfo group = new AdvancedGroupInfo(); + + //Parse basic group information + parse_group_info(object, group); + + group.setTime_create(object.getInt("time_create")); + group.setUrl(object.getString("url")); + group.setDescription(object.getString("description")); + group.setNumber_likes(object.getInt("number_likes")); + group.setIs_liking(object.getBoolean("is_liking")); + + return group; } /** diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/models/AdvancedGroupInfo.java b/app/src/main/java/org/communiquons/android/comunic/client/data/models/AdvancedGroupInfo.java new file mode 100644 index 0000000..a1566ee --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/models/AdvancedGroupInfo.java @@ -0,0 +1,58 @@ +package org.communiquons.android.comunic.client.data.models; + +/** + * Advanced group information + * + * Contains more information about group than GroupInfo + * + * @author Pierre HUBERT + */ +public class AdvancedGroupInfo extends GroupInfo { + + //Private fields + private int time_create; + private String url; + private String description; + private int number_likes; + private boolean is_liking; + + public int getTime_create() { + return time_create; + } + + public void setTime_create(int time_create) { + this.time_create = time_create; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public int getNumber_likes() { + return number_likes; + } + + public void setNumber_likes(int number_likes) { + this.number_likes = number_likes; + } + + public boolean isIs_liking() { + return is_liking; + } + + public void setIs_liking(boolean is_liking) { + this.is_liking = is_liking; + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/GetGroupAdvancedInfoTask.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/GetGroupAdvancedInfoTask.java new file mode 100644 index 0000000..298bb77 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/GetGroupAdvancedInfoTask.java @@ -0,0 +1,22 @@ +package org.communiquons.android.comunic.client.ui.asynctasks; + +import android.content.Context; + +import org.communiquons.android.comunic.client.data.helpers.GroupsHelper; +import org.communiquons.android.comunic.client.data.models.AdvancedGroupInfo; + +/** + * This task is used to get a group advanced information + * + * @author Pierre HUBERT + */ +public class GetGroupAdvancedInfoTask extends SafeAsyncTask { + public GetGroupAdvancedInfoTask(Context context) { + super(context); + } + + @Override + protected AdvancedGroupInfo doInBackground(Integer... integers) { + return new GroupsHelper(getContext()).getAdvancedInformation(integers[0]); + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/GroupPageMainFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/GroupPageMainFragment.java index e52028b..6a3a430 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/GroupPageMainFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/groups/GroupPageMainFragment.java @@ -1,5 +1,23 @@ package org.communiquons.android.comunic.client.ui.fragments.groups; +import android.app.AlertDialog; +import android.os.AsyncTask; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; +import android.widget.Toast; + +import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.data.models.AdvancedGroupInfo; +import org.communiquons.android.comunic.client.ui.asynctasks.GetGroupAdvancedInfoTask; +import org.communiquons.android.comunic.client.ui.asynctasks.SafeAsyncTask; +import org.communiquons.android.comunic.client.ui.utils.UiUtils; +import org.communiquons.android.comunic.client.ui.views.GroupImageView; + /** * Group main page * @@ -17,5 +35,104 @@ public class GroupPageMainFragment extends AbstractGroupFragment { */ public static final String ARGUMENT_GROUP_ID = "group_id"; + /** + * Target group ID + */ + private int mGroupID; + /** + * Target group advanced information + */ + private AdvancedGroupInfo mAdvancedGroupInfo; + + /** + * Loading dialog + */ + private AlertDialog mLoadingDialog; + + /** + * UI views + */ + private GroupImageView mGroupImage; + private TextView mGroupName; + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + return inflater.inflate(R.layout.layout_group_main_page, container, false); + } + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + //Get views + mGroupImage = view.findViewById(R.id.groupImageView); + mGroupName = view.findViewById(R.id.groupName); + } + + @Override + public void onStart() { + super.onStart(); + + //Get group ID + assert getArguments() != null; + mGroupID = getArguments().getInt(ARGUMENT_GROUP_ID); + + if(mAdvancedGroupInfo == null) + loadGroupInformation(); + else + applyGroupInfo(); + } + + /** + * Download group information + */ + private void loadGroupInformation(){ + + //Display a loading dialog + mLoadingDialog = UiUtils.create_loading_dialog(getActivity()); + + getTasksManager().unsetSpecificTasks(GetGroupAdvancedInfoTask.class); + + GetGroupAdvancedInfoTask task = new GetGroupAdvancedInfoTask(getActivity()); + task.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener() { + @Override + public void OnPostExecute(@Nullable AdvancedGroupInfo advancedGroupInfo) { + onLoadGroupAdvancedInfoCallback(advancedGroupInfo); + } + }); + task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mGroupID); + getTasksManager().addTask(task); + } + + /** + * Get group advanced information callback + * + * @param info Information about the target group + */ + private void onLoadGroupAdvancedInfoCallback(@Nullable AdvancedGroupInfo info){ + + //Hide loading dialog + mLoadingDialog.dismiss(); + + if(info == null) { + Toast.makeText(getActivity(), R.string.err_get_group_info, Toast.LENGTH_SHORT).show(); + return; + } + + mAdvancedGroupInfo = info; + applyGroupInfo(); + } + + /** + * Apply previously downloaded group information + */ + private void applyGroupInfo(){ + + //Apply main group information + mGroupName.setText(mAdvancedGroupInfo.getDisplayName()); + mGroupImage.setGroup(mAdvancedGroupInfo); + + } } diff --git a/app/src/main/res/layout/layout_group_main_page.xml b/app/src/main/res/layout/layout_group_main_page.xml new file mode 100644 index 0000000..89977f0 --- /dev/null +++ b/app/src/main/res/layout/layout_group_main_page.xml @@ -0,0 +1,54 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index efba63e..def4d1c 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -1,6 +1,6 @@ - #3F51B5 + #3f51b5 #303F9F #ff4081 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 27bd27d..9984480 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -311,4 +311,5 @@ Cancel Leave Could not update group membership! + Could not get group information!