Ready to display posts list

This commit is contained in:
Pierre 2018-01-21 18:41:19 +01:00
parent 93c755ee9e
commit d27a10a007
4 changed files with 122 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package org.communiquons.android.comunic.client.data.posts;
import java.util.ArrayList;
/**
* Posts list
*
* @author PIerre HUBERT
* Created by pierre on 1/21/18.
*/
public class PostsList extends ArrayList<Post> {
/**
* Get the IDs of the users who created the posts
*
* @return The list of users of the post
*/
public ArrayList<Integer> getUsersId(){
ArrayList<Integer> ids = new ArrayList<>();
for(Post post : this){
int userID = post.getUserID();
//Add User ID if required
if(ids.contains(userID))
ids.add(userID);
}
return ids;
}
}

View File

@ -5,10 +5,12 @@ import android.app.Fragment;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.ArrayMap;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -19,6 +21,9 @@ import org.communiquons.android.comunic.client.data.DatabaseHelper;
import org.communiquons.android.comunic.client.data.ImageLoad.ImageLoadManager; import org.communiquons.android.comunic.client.data.ImageLoad.ImageLoadManager;
import org.communiquons.android.comunic.client.data.UsersInfo.AdvancedUserInfo; import org.communiquons.android.comunic.client.data.UsersInfo.AdvancedUserInfo;
import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersHelper; 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.posts.PostsHelper;
import org.communiquons.android.comunic.client.data.posts.PostsList;
import org.communiquons.android.comunic.client.data.utils.UiUtils; import org.communiquons.android.comunic.client.data.utils.UiUtils;
/** /**
@ -47,11 +52,26 @@ public class UserPageFragment extends Fragment {
*/ */
private AdvancedUserInfo userInfo; private AdvancedUserInfo userInfo;
/**
* User posts
*/
private PostsList postsList;
/**
* User informations
*/
private ArrayMap<Integer, UserInfo> usersInfos;
/** /**
* Get user helper * Get user helper
*/ */
private GetUsersHelper getUsersHelper; private GetUsersHelper getUsersHelper;
/**
* Posts helper
*/
private PostsHelper postsHelper;
/** /**
* Loading alert dialog * Loading alert dialog
*/ */
@ -67,6 +87,11 @@ public class UserPageFragment extends Fragment {
*/ */
private ImageView user_image; private ImageView user_image;
/**
* Posts list view
*/
private ListView postsListView;
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -79,6 +104,9 @@ public class UserPageFragment extends Fragment {
//Create getUserHelper instance //Create getUserHelper instance
getUsersHelper = new GetUsersHelper(getActivity(), dbHelper); getUsersHelper = new GetUsersHelper(getActivity(), dbHelper);
//Create posts helper instance
postsHelper = new PostsHelper(getActivity());
} }
@Nullable @Nullable
@ -170,5 +198,56 @@ public class UserPageFragment extends Fragment {
ImageLoadManager.remove(user_image); ImageLoadManager.remove(user_image);
ImageLoadManager.load(getActivity(), userInfo.getAcountImageURL(), user_image); ImageLoadManager.load(getActivity(), userInfo.getAcountImageURL(), user_image);
//Load the list of posts of the user
load_posts();
}
/**
* Load the posts of the user
*/
private void load_posts(){
new AsyncTask<Void, Void, PostsList>(){
@Override
protected PostsList doInBackground(Void... params) {
PostsList list = postsHelper.get_user(userID);
//Get the information about the users who created the posts
if(list != null)
usersInfos = getUsersHelper.getMultiple(list.getUsersId());
return list;
}
@Override
protected void onPostExecute(PostsList posts) {
if(getActivity() != null)
display_posts(posts);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
/**
* Display the posts of the user
*
* @param list the list of posts / null in case of failure
*/
private void display_posts(@Nullable PostsList list){
//Check for errors
if(list == null){
Toast.makeText(getActivity(), R.string.err_get_user_posts, Toast.LENGTH_SHORT).show();
return;
}
//Check we didn't get user informations
if(usersInfos == null){
Toast.makeText(getActivity(), R.string.err_get_users_info, Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(getActivity(), "Got posts !", Toast.LENGTH_SHORT).show();
} }
} }

View File

@ -33,4 +33,11 @@
</FrameLayout> </FrameLayout>
<!-- User posts -->
<ListView
android:id="@+id/user_posts"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout> </LinearLayout>

View File

@ -96,4 +96,5 @@
<string name="err_get_private_conversation">Could not get a private conversation !</string> <string name="err_get_private_conversation">Could not get a private conversation !</string>
<string name="dialog_loading_msg">Loading…</string> <string name="dialog_loading_msg">Loading…</string>
<string name="navigation_bottom_user_item">User</string> <string name="navigation_bottom_user_item">User</string>
<string name="err_get_user_posts">Couldn\'t get user posts !</string>
</resources> </resources>