From 703142df5b6bcc24bafdcad30f98f5f07fb5372c Mon Sep 17 00:00:00 2001 From: Pierre Date: Tue, 2 Jan 2018 12:20:21 +0100 Subject: [PATCH] Can add conversation members --- .../client/data/UsersInfo/GetUsersHelper.java | 20 +++ .../UsersInfo/UsersAsysncInfoAdapter.java | 100 +++++++++++ .../fragments/UpdateConversationFragment.java | 155 +++++++++++++++++- .../layout/fragment_update_conversation.xml | 9 +- app/src/main/res/values/strings.xml | 2 + 5 files changed, 283 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersAsysncInfoAdapter.java diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/GetUsersHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/GetUsersHelper.java index 9fc7221..f67daaf 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/GetUsersHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/GetUsersHelper.java @@ -108,6 +108,26 @@ public class GetUsersHelper { } + /** + * Get the list of missing users ID in a set of users informations + * + * @param IDs The reference IDs list + * @param usersInfo Informations about the users + * @return + */ + public static ArrayList get_missing_ids(@NonNull ArrayList IDs, + @NonNull ArrayMap usersInfo){ + ArrayList missingIds = new ArrayList<>(); + + //Process the list of IDs + for(int user_id : IDs){ + if(!usersInfo.containsKey(user_id)) + missingIds.add(user_id); + } + + return missingIds; + } + /** * Get information about multiple users from the database or from the server * diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersAsysncInfoAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersAsysncInfoAdapter.java new file mode 100644 index 0000000..76f7fc8 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersAsysncInfoAdapter.java @@ -0,0 +1,100 @@ +package org.communiquons.android.comunic.client.data.UsersInfo; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.util.ArrayMap; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; + + +import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.data.ImageLoad.ImageLoadManager; +import org.communiquons.android.comunic.client.data.utils.UiUtils; + +import java.util.ArrayList; + +/** + * User async infos adapter + * + * Similar to UsersBasicAdapter, but the information about users can be given after the users + * themselve + * + * @author Pierre HUBERT + * Created by pierre on 1/2/18. + */ + +public class UsersAsysncInfoAdapter extends ArrayAdapter { + + /** + * Informations about the members of the conversation + */ + private ArrayMap usersInfos; + + /** + * Constructor + * @param context The context of the application + * @param IDs The list of IDs of users + * @param usersInfos Informations about the users (can be updated asynchronously with the list + * of users ID) + */ + public UsersAsysncInfoAdapter(Context context, @NonNull ArrayList IDs, + @NonNull ArrayMap usersInfos){ + super(context, 0, IDs); + + //Save user information array map + this.usersInfos = usersInfos; + } + + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + + if(convertView == null) + convertView = LayoutInflater.from(getContext()) + .inflate(R.layout.user_basic_adapter_item, parent, false); + + //Get the views + ImageView account_image = convertView.findViewById(R.id.user_account_image); + TextView account_name = convertView.findViewById(R.id.user_name); + + //Empty the entry + ImageLoadManager.remove(account_image); + account_image.setImageDrawable(UiUtils.getDrawable(getContext(), + R.drawable.default_account_image)); + account_name.setText(""); + + //Get user ID + int userID = getItem(position); + + //Check if we go user informations + if(usersInfos.containsKey(userID)){ + + UserInfo user = usersInfos.get(userID); + + account_name.setText(user.getDisplayFullName()); + ImageLoadManager.load(getContext(), user.getAcountImageURL(), account_image); + } + + return convertView; + } + + /** + * Get the ID of a position + * + * This method is overriden in order to remove the @Nullable tag + * + * @param position The position of the item + * @return The ID of the personn or -1 in case of failure + */ + @NonNull + @Override + public Integer getItem(int position) { + Integer ID = super.getItem(position); + return ID == null ? -1 : ID; + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/UpdateConversationFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/UpdateConversationFragment.java index 335d052..ca0f99b 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/UpdateConversationFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/UpdateConversationFragment.java @@ -3,20 +3,29 @@ package org.communiquons.android.comunic.client.fragments; import android.app.Activity; import android.app.Fragment; import android.content.Intent; +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.Button; import android.widget.CheckBox; import android.widget.ListView; +import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import org.communiquons.android.comunic.client.MainActivity; import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.SearchUserActivity; +import org.communiquons.android.comunic.client.data.DatabaseHelper; +import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersHelper; +import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo; +import org.communiquons.android.comunic.client.data.UsersInfo.UsersAsysncInfoAdapter; + +import java.util.ArrayList; /** * Create and / or update a conversation fragment @@ -62,6 +71,42 @@ public class UpdateConversationFragment extends Fragment { */ private Button submitButton; + /** + * Loading progress bar + */ + private ProgressBar progressBar; + + /** + * Users members ID list + */ + private ArrayList membersID = null; + + /** + * Members informations + */ + private ArrayMap membersInfo = null; + + /** + * Members list adapter + */ + private UsersAsysncInfoAdapter membersAdapter; + + /** + * Users information helper + */ + private GetUsersHelper usersHelper; + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + //Get database helper instance + DatabaseHelper dbHelper = DatabaseHelper.getInstance(getActivity()); + + //Get User helper + usersHelper = new GetUsersHelper(getActivity(), dbHelper); + } + @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { @@ -72,7 +117,8 @@ public class UpdateConversationFragment extends Fragment { public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - //Get the view + //Get the views + progressBar = view.findViewById(R.id.progress_bar); nameView = view.findViewById(R.id.fragment_update_conversation_name); followCheckbox = view.findViewById(R.id.fragment_update_conversation_follow); membersList = view.findViewById(R.id.fragment_update_conversation_members); @@ -86,6 +132,10 @@ public class UpdateConversationFragment extends Fragment { requestAddMember(); } }); + + //Initialize the form + init_form(); + } @Override @@ -115,7 +165,108 @@ public class UpdateConversationFragment extends Fragment { //Check if it is a success if(resultCode == Activity.RESULT_OK){ - Toast.makeText(getActivity(), "Request success", Toast.LENGTH_SHORT).show(); + + switch(requestCode){ + + case FIND_USER_ID_INTENT: + addMemberID(Integer.decode(data.getData().getQueryParameter("userID"))); + } + } } + + /** + * Initialize the form + */ + private void init_form(){ + + //Hide progress bar + set_progressbar_visibility(false); + + //Initialize the list of members + membersID = new ArrayList<>(); + membersInfo = new ArrayMap<>(); + membersAdapter = new UsersAsysncInfoAdapter(getActivity(), membersID, membersInfo); + membersList.setAdapter(membersAdapter); + } + + /** + * Add a member to the list, specified by its ID + * + * @param memberID The ID of the member to add + */ + private void addMemberID(int memberID){ + + //Check if the member is already on the list + if(membersID.contains(memberID)) { + Toast.makeText(getActivity(), R.string.err_add_member_double, Toast.LENGTH_SHORT).show(); + return; + } + + //Push the member into the list + membersID.add(memberID); + membersAdapter.notifyDataSetChanged(); + + //Refresh members information + refresh_members_information(); + } + + /** + * Refresh members informations + * + * Fetch informations about the users for which we don't know anything yet + */ + private void refresh_members_information(){ + + //Get the list of the required IDs + final ArrayList missingIDs = GetUsersHelper.get_missing_ids(membersID, membersInfo); + + //Continue only if required + if(missingIDs.size() == 0) + return; //Do nothing + + //Search informations about them + new AsyncTask>(){ + + @Override + protected ArrayMap doInBackground(Void... params) { + return usersHelper.getMultiple(missingIDs); + } + + @Override + protected void onPostExecute(ArrayMap result) { + append_new_members_info(result); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + + } + + /** + * Append new information about members + * + * @param usersInfo Information about the members + */ + private void append_new_members_info(@Nullable ArrayMap usersInfo){ + + //Check for errors + if(usersInfo == null){ + Toast.makeText(getActivity(), R.string.err_get_users_info, Toast.LENGTH_SHORT).show(); + return; + } + + //Add the list of user information + membersInfo.putAll(usersInfo); + + //Notify data set update + membersAdapter.notifyDataSetChanged(); + } + + /** + * Update progressbar visibility + * + * @param visible TRUE to make the progressbar visible + */ + private void set_progressbar_visibility(boolean visible){ + progressBar.setVisibility(visible ? View.VISIBLE : View.GONE); + } } diff --git a/app/src/main/res/layout/fragment_update_conversation.xml b/app/src/main/res/layout/fragment_update_conversation.xml index f940462..def33a1 100644 --- a/app/src/main/res/layout/fragment_update_conversation.xml +++ b/app/src/main/res/layout/fragment_update_conversation.xml @@ -3,6 +3,13 @@ android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8b1457e..739dbbc 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -82,4 +82,6 @@ Search user User name Could not search user ! + This user is already a member of the conversation! + Could not get information about users !