mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-12-28 14:39:00 +00:00
Can add conversation members
This commit is contained in:
parent
1818db46ab
commit
703142df5b
@ -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<Integer> get_missing_ids(@NonNull ArrayList<Integer> IDs,
|
||||
@NonNull ArrayMap<Integer, UserInfo> usersInfo){
|
||||
ArrayList<Integer> 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
|
||||
*
|
||||
|
@ -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<Integer> {
|
||||
|
||||
/**
|
||||
* Informations about the members of the conversation
|
||||
*/
|
||||
private ArrayMap<Integer, UserInfo> 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<Integer> IDs,
|
||||
@NonNull ArrayMap<Integer, UserInfo> 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;
|
||||
}
|
||||
}
|
@ -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<Integer> membersID = null;
|
||||
|
||||
/**
|
||||
* Members informations
|
||||
*/
|
||||
private ArrayMap<Integer, UserInfo> 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<Integer> missingIDs = GetUsersHelper.get_missing_ids(membersID, membersInfo);
|
||||
|
||||
//Continue only if required
|
||||
if(missingIDs.size() == 0)
|
||||
return; //Do nothing
|
||||
|
||||
//Search informations about them
|
||||
new AsyncTask<Void, Void, ArrayMap<Integer, UserInfo>>(){
|
||||
|
||||
@Override
|
||||
protected ArrayMap<Integer, UserInfo> doInBackground(Void... params) {
|
||||
return usersHelper.getMultiple(missingIDs);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(ArrayMap<Integer, UserInfo> 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<Integer, UserInfo> 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);
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,13 @@
|
||||
android:orientation="vertical" android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<!-- Progres bar -->
|
||||
<ProgressBar
|
||||
android:id="@+id/progress_bar"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"/>
|
||||
|
||||
<!-- Conversation name -->
|
||||
<android.support.design.widget.TextInputLayout
|
||||
android:layout_width="match_parent"
|
||||
@ -22,7 +29,7 @@
|
||||
<!-- Conversation members -->
|
||||
<ListView
|
||||
android:id="@+id/fragment_update_conversation_members"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"/>
|
||||
|
||||
|
@ -82,4 +82,6 @@
|
||||
<string name="activity_searchuser_title">Search user</string>
|
||||
<string name="activity_searchuser_text_placeholder">User name</string>
|
||||
<string name="err_search_user">Could not search user !</string>
|
||||
<string name="err_add_member_double">This user is already a member of the conversation!</string>
|
||||
<string name="err_get_users_info">Could not get information about users !</string>
|
||||
</resources>
|
||||
|
Loading…
Reference in New Issue
Block a user