mirror of
				https://github.com/pierre42100/ComunicAndroid
				synced 2025-10-31 01:24:43 +00:00 
			
		
		
		
	Added friends tab
This commit is contained in:
		
							
								
								
									
										2
									
								
								.idea/misc.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										2
									
								
								.idea/misc.xml
									
									
									
										generated
									
									
									
								
							| @@ -25,7 +25,7 @@ | ||||
|       </value> | ||||
|     </option> | ||||
|   </component> | ||||
|   <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK"> | ||||
|   <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK"> | ||||
|     <output url="file://$PROJECT_DIR$/build/classes" /> | ||||
|   </component> | ||||
|   <component name="ProjectType"> | ||||
|   | ||||
| @@ -343,4 +343,34 @@ public class FriendsListHelper { | ||||
|             return false; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get the list of friends of a user | ||||
|      * | ||||
|      * @param userID the ID of the target user | ||||
|      * @return The list of the IDs of the friends of the user / null in case of failure | ||||
|      */ | ||||
|     @Nullable | ||||
|     public ArrayList<Integer> getUserFriends(int userID){ | ||||
|         APIRequest request = new APIRequest(mContext, "friends/get_user_list"); | ||||
|         request.addInt("userID", userID); | ||||
|  | ||||
|         try { | ||||
|             APIResponse response = new APIRequestHelper().exec(request); | ||||
|  | ||||
|             if(response.getResponse_code() != 200) return null; | ||||
|  | ||||
|             JSONArray array = response.getJSONArray(); | ||||
|             ArrayList<Integer> list = new ArrayList<>(); | ||||
|  | ||||
|             for (int i = 0; i < array.length(); i++) | ||||
|                 list.add(array.getInt(i)); | ||||
|  | ||||
|             return list; | ||||
|  | ||||
|         } catch (Exception e) { | ||||
|             e.printStackTrace(); | ||||
|             return null; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -403,6 +403,8 @@ public class GetUsersHelper { | ||||
|             //Check if user can post text or this page or not | ||||
|             advancedUserInfo.setCanPostText(userObject.getBoolean("can_post_texts")); | ||||
|  | ||||
|             advancedUserInfo.setFriendListPublic(userObject.getBoolean("friend_list_public")); | ||||
|  | ||||
|         } catch (JSONException e){ | ||||
|             e.printStackTrace(); | ||||
|             return null; | ||||
|   | ||||
| @@ -13,6 +13,7 @@ public class AdvancedUserInfo extends UserInfo { | ||||
|     private int account_creation_time; | ||||
|     private boolean accessForbidden = false; | ||||
|     private boolean canPostText; | ||||
|     private boolean friendListPublic; | ||||
|  | ||||
|     /** | ||||
|      * Get the account creation time | ||||
| @@ -67,4 +68,13 @@ public class AdvancedUserInfo extends UserInfo { | ||||
|     public boolean isCanPostText() { | ||||
|         return canPostText; | ||||
|     } | ||||
|  | ||||
|     //Set and get friend is public status | ||||
|     public boolean isFriendListPublic() { | ||||
|         return friendListPublic; | ||||
|     } | ||||
|  | ||||
|     public void setFriendListPublic(boolean friendListPublic) { | ||||
|         this.friendListPublic = friendListPublic; | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -0,0 +1,33 @@ | ||||
| package org.communiquons.android.comunic.client.ui.asynctasks; | ||||
|  | ||||
| import android.content.Context; | ||||
| import android.util.ArrayMap; | ||||
|  | ||||
| import org.communiquons.android.comunic.client.data.asynctasks.SafeAsyncTask; | ||||
| import org.communiquons.android.comunic.client.data.helpers.FriendsListHelper; | ||||
| import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper; | ||||
| import org.communiquons.android.comunic.client.data.models.UserInfo; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
|  | ||||
| /** | ||||
|  * Get the list of friends of a specific user with their information | ||||
|  * | ||||
|  * @author Pierre HUBERT | ||||
|  */ | ||||
| public class GetUserFriendsListTask extends SafeAsyncTask<Integer, Void, ArrayMap<Integer, UserInfo>> { | ||||
|  | ||||
|     public GetUserFriendsListTask(Context context) { | ||||
|         super(context); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     protected ArrayMap<Integer, UserInfo> doInBackground(Integer... integers) { | ||||
|         ArrayList<Integer> list = new FriendsListHelper(getContext()).getUserFriends(integers[0]); | ||||
|  | ||||
|         if(list == null) | ||||
|             return null; | ||||
|  | ||||
|         return new GetUsersHelper(getContext()).getMultiple(list); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,174 @@ | ||||
| package org.communiquons.android.comunic.client.ui.fragments; | ||||
|  | ||||
| import android.os.AsyncTask; | ||||
| import android.os.Bundle; | ||||
| import android.support.annotation.NonNull; | ||||
| import android.support.annotation.Nullable; | ||||
| import android.support.v4.app.Fragment; | ||||
| import android.util.ArrayMap; | ||||
| import android.view.LayoutInflater; | ||||
| import android.view.View; | ||||
| import android.view.ViewGroup; | ||||
| import android.widget.AdapterView; | ||||
| import android.widget.ListView; | ||||
| import android.widget.ProgressBar; | ||||
| import android.widget.TextView; | ||||
| import android.widget.Toast; | ||||
|  | ||||
| import org.communiquons.android.comunic.client.R; | ||||
| import org.communiquons.android.comunic.client.data.asynctasks.SafeAsyncTask; | ||||
| import org.communiquons.android.comunic.client.data.models.UserInfo; | ||||
| import org.communiquons.android.comunic.client.ui.adapters.UsersBasicAdapter; | ||||
| import org.communiquons.android.comunic.client.ui.asynctasks.GetUserFriendsListTask; | ||||
| import org.communiquons.android.comunic.client.ui.listeners.onOpenUsersPageListener; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.Objects; | ||||
|  | ||||
| /** | ||||
|  * Friend list fragment for a specific user (not the current user signed in) | ||||
|  * | ||||
|  * @author Pierre HUBERT | ||||
|  */ | ||||
| public class UserFriendListFragment extends Fragment implements AdapterView.OnItemClickListener { | ||||
|  | ||||
|     private static final String TAG = UserFriendListFragment.class.getCanonicalName(); | ||||
|  | ||||
|     /** | ||||
|      * Argument to pass to the fragment to transfer user ID | ||||
|      */ | ||||
|     public static final String ARGUMENT_USER_ID = "user_id"; | ||||
|  | ||||
|     private int mUserID; | ||||
|  | ||||
|     /** | ||||
|      * Views | ||||
|      */ | ||||
|     private ProgressBar mProgress; | ||||
|     private TextView mNoFriendsNotice; | ||||
|     private ListView mList; | ||||
|  | ||||
|     /** | ||||
|      * Users list | ||||
|      */ | ||||
|     private ArrayList<UserInfo> mUsersList; | ||||
|  | ||||
|     /** | ||||
|      * Get user task | ||||
|      */ | ||||
|     private GetUserFriendsListTask mLoadTask; | ||||
|  | ||||
|     @Override | ||||
|     public void onCreate(@Nullable Bundle savedInstanceState) { | ||||
|         super.onCreate(savedInstanceState); | ||||
|  | ||||
|         assert getArguments() != null; | ||||
|         mUserID = getArguments().getInt(ARGUMENT_USER_ID); | ||||
|     } | ||||
|  | ||||
|     @Nullable | ||||
|     @Override | ||||
|     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | ||||
|         return inflater.inflate(R.layout.fragment_user_friends_list, container, false); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { | ||||
|         super.onViewCreated(view, savedInstanceState); | ||||
|  | ||||
|         mProgress = view.findViewById(R.id.progressBar); | ||||
|         mNoFriendsNotice = view.findViewById(R.id.nofriendsNotice); | ||||
|         mList = view.findViewById(R.id.list); | ||||
|  | ||||
|         mList.setOnItemClickListener(this); | ||||
|  | ||||
|         setProgressBarVisibility(true); | ||||
|         setNoFriendsNoticeVisibility(false); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onResume() { | ||||
|         super.onResume(); | ||||
|  | ||||
|         if (mUsersList != null) | ||||
|             showFriendsList(); | ||||
|  | ||||
|         load_list(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onDestroy() { | ||||
|         super.onDestroy(); | ||||
|  | ||||
|         if (mLoadTask != null) | ||||
|             mLoadTask.setOnPostExecuteListener(null); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Load friends list | ||||
|      */ | ||||
|     private void load_list() { | ||||
|  | ||||
|         if (mLoadTask != null) | ||||
|             mLoadTask.setOnPostExecuteListener(null); | ||||
|  | ||||
|         mLoadTask = new GetUserFriendsListTask(getActivity()); | ||||
|         mLoadTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<ArrayMap<Integer, UserInfo>>() { | ||||
|             @Override | ||||
|             public void OnPostExecute(ArrayMap<Integer, UserInfo> users) { | ||||
|                 if (getActivity() != null) | ||||
|                     applyFriendsList(users); | ||||
|             } | ||||
|         }); | ||||
|  | ||||
|         mLoadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mUserID); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Apply friend list | ||||
|      * | ||||
|      * @param list The list to apply | ||||
|      */ | ||||
|     private void applyFriendsList(@Nullable ArrayMap<Integer, UserInfo> list) { | ||||
|  | ||||
|         setProgressBarVisibility(false); | ||||
|  | ||||
|         if (list == null) { | ||||
|             Toast.makeText(getContext(), R.string.err_get_user_friends, Toast.LENGTH_SHORT).show(); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         this.mUsersList = new ArrayList<>(list.values()); | ||||
|  | ||||
|         showFriendsList(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Show friends list | ||||
|      */ | ||||
|     private void showFriendsList() { | ||||
|  | ||||
|         setNoFriendsNoticeVisibility(mUsersList.size() == 0); | ||||
|  | ||||
|         UsersBasicAdapter usersBasicAdapter = new UsersBasicAdapter( | ||||
|                 Objects.requireNonNull(getActivity()), mUsersList); | ||||
|  | ||||
|         mList.setAdapter(usersBasicAdapter); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     private void setProgressBarVisibility(boolean visible) { | ||||
|         mProgress.setVisibility(visible ? View.VISIBLE : View.GONE); | ||||
|     } | ||||
|  | ||||
|     private void setNoFriendsNoticeVisibility(boolean visible){ | ||||
|         mNoFriendsNotice.setVisibility(visible ? View.VISIBLE : View.GONE); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | ||||
|         if (mUsersList.size() > position) | ||||
|             ((onOpenUsersPageListener) Objects.requireNonNull(getActivity())).openUserPage( | ||||
|                     mUsersList.get(position).getId()); | ||||
|     } | ||||
| } | ||||
| @@ -1,12 +1,12 @@ | ||||
| package org.communiquons.android.comunic.client.ui.fragments; | ||||
|  | ||||
| import android.app.AlertDialog; | ||||
| import android.support.annotation.NonNull; | ||||
| import android.support.v4.app.Fragment; | ||||
| import android.os.AsyncTask; | ||||
| import android.os.Bundle; | ||||
| import android.support.annotation.NonNull; | ||||
| import android.support.annotation.Nullable; | ||||
| import android.support.design.widget.TabLayout; | ||||
| import android.support.v4.app.Fragment; | ||||
| import android.support.v4.view.ViewPager; | ||||
| import android.view.LayoutInflater; | ||||
| import android.view.View; | ||||
| @@ -17,6 +17,7 @@ import org.communiquons.android.comunic.client.R; | ||||
| import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper; | ||||
| import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper; | ||||
| import org.communiquons.android.comunic.client.data.models.AdvancedUserInfo; | ||||
| import org.communiquons.android.comunic.client.data.utils.AccountUtils; | ||||
| import org.communiquons.android.comunic.client.ui.activities.MainActivity; | ||||
| import org.communiquons.android.comunic.client.ui.adapters.FragmentPagerBaseAdapter; | ||||
| import org.communiquons.android.comunic.client.ui.listeners.onOpenUsersPageListener; | ||||
| @@ -196,6 +197,18 @@ public class UserPageFragment extends Fragment { | ||||
|         adapter.addFragment(postsFragment, UiUtils.getString(getActivity(), | ||||
|                 R.string.tab_posts)); | ||||
|  | ||||
|         //Friends fragment (if required) | ||||
|         if(mUserID != AccountUtils.getID(getActivity()) && userInfo.isFriendListPublic()){ | ||||
|  | ||||
|             Bundle friendsArgs = new Bundle(); | ||||
|             friendsArgs.putInt(UserFriendListFragment.ARGUMENT_USER_ID, mUserID); | ||||
|  | ||||
|             UserFriendListFragment friendsFragment = new UserFriendListFragment(); | ||||
|             friendsFragment.setArguments(friendsArgs); | ||||
|             adapter.addFragment(friendsFragment, UiUtils.getString(getActivity(), | ||||
|                     R.string.tab_friends)); | ||||
|         } | ||||
|  | ||||
|  | ||||
|         mPager.setAdapter(adapter); | ||||
|         mTabLayout.setupWithViewPager(mPager); | ||||
|   | ||||
							
								
								
									
										51
									
								
								app/src/main/res/layout/fragment_user_friends_list.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								app/src/main/res/layout/fragment_user_friends_list.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,51 @@ | ||||
| <?xml version="1.0" encoding="utf-8"?> | ||||
| <android.support.constraint.ConstraintLayout | ||||
|     xmlns:android="http://schemas.android.com/apk/res/android" | ||||
|     xmlns:app="http://schemas.android.com/apk/res-auto" | ||||
|     xmlns:tools="http://schemas.android.com/tools" | ||||
|     android:layout_width="match_parent" | ||||
|     android:layout_height="match_parent"> | ||||
|  | ||||
|     <ProgressBar | ||||
|         android:id="@+id/progressBar" | ||||
|         style="?android:attr/progressBarStyle" | ||||
|         android:layout_width="wrap_content" | ||||
|         android:layout_height="wrap_content" | ||||
|         android:layout_marginBottom="8dp" | ||||
|         android:layout_marginEnd="8dp" | ||||
|         android:layout_marginStart="8dp" | ||||
|         android:layout_marginTop="8dp" | ||||
|         app:layout_constraintBottom_toBottomOf="parent" | ||||
|         app:layout_constraintEnd_toEndOf="parent" | ||||
|         app:layout_constraintStart_toStartOf="parent" | ||||
|         app:layout_constraintTop_toTopOf="parent" | ||||
|         app:layout_constraintVertical_bias="0.499" /> | ||||
|  | ||||
|     <ListView | ||||
|         android:id="@+id/list" | ||||
|         android:layout_width="match_parent" | ||||
|         android:layout_height="match_parent" | ||||
|         android:layout_marginBottom="8dp" | ||||
|         android:layout_marginEnd="8dp" | ||||
|         android:layout_marginStart="8dp" | ||||
|         android:layout_marginTop="8dp" | ||||
|         app:layout_constraintBottom_toBottomOf="parent" | ||||
|         app:layout_constraintEnd_toEndOf="parent" | ||||
|         app:layout_constraintStart_toStartOf="parent" | ||||
|         app:layout_constraintTop_toTopOf="parent" | ||||
|         tools:listitem="@layout/user_basic_adapter_item" /> | ||||
|  | ||||
|     <TextView | ||||
|         android:id="@+id/nofriendsNotice" | ||||
|         android:layout_width="wrap_content" | ||||
|         android:layout_height="wrap_content" | ||||
|         android:layout_marginBottom="8dp" | ||||
|         android:layout_marginEnd="8dp" | ||||
|         android:layout_marginStart="8dp" | ||||
|         android:layout_marginTop="8dp" | ||||
|         android:text="@string/notice_user_has_no_friend" | ||||
|         app:layout_constraintBottom_toBottomOf="parent" | ||||
|         app:layout_constraintEnd_toEndOf="parent" | ||||
|         app:layout_constraintStart_toStartOf="parent" | ||||
|         app:layout_constraintTop_toTopOf="parent" /> | ||||
| </android.support.constraint.ConstraintLayout> | ||||
| @@ -249,4 +249,5 @@ | ||||
|     <string name="dialog_open_source_licenses_title">Licences Open Source</string> | ||||
|     <string name="btn_open_source_licences">Licences Open Source</string> | ||||
|     <string name="date_days_short">j</string> | ||||
|     <string name="tab_friends">Amis</string> | ||||
| </resources> | ||||
| @@ -253,4 +253,7 @@ | ||||
|     <string name="dialog_delete_conversation_message_confirm">Delete</string> | ||||
|     <string name="err_delete_conversation_message">Could not delete conversation message!</string> | ||||
|     <string name="date_days_short">d</string> | ||||
|     <string name="tab_friends">Friends</string> | ||||
|     <string name="err_get_user_friends">Could not get the friends of the user!</string> | ||||
|     <string name="notice_user_has_no_friend">This user has not any friend yet.</string> | ||||
| </resources> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Pierre HUBERT
					Pierre HUBERT