Added latest posts fragment

This commit is contained in:
Pierre 2018-05-10 10:08:26 +02:00
parent 0d075032d8
commit 306977a101
6 changed files with 306 additions and 7 deletions

View File

@ -100,13 +100,33 @@ public class PostsHelper {
//Get the list of posts and process it
JSONArray posts = response.getJSONArray();
PostsList list = new PostsList();
return parse_json_posts_list(posts);
for(int i = 0; i < posts.length(); i++){
list.add(parse_json_post(posts.getJSONObject(i)));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
return list;
/**
* Get the list of latest posts of a user
*
* @return The list of posts / null in case of failure
*/
@Nullable
public PostsList get_latest() {
//Perform a request on the API
APIRequest params = new APIRequest(mContext, "posts/get_latest");
//Perform the request
try {
//Make the request on the API
APIResponse response = new APIRequestHelper().exec(params);
//Get the list of posts and process it
JSONArray posts = response.getJSONArray();
return parse_json_posts_list(posts);
} catch (Exception e) {
e.printStackTrace();
@ -255,11 +275,29 @@ public class PostsHelper {
}
}
/**
* Turn a JSONArray that contains information about posts into PostList object
*
* @param array The list of posts to process
* @return The list of posts / null in case of failure
* @throws JSONException in case of failure
*/
private PostsList parse_json_posts_list(JSONArray array) throws JSONException {
PostsList list = new PostsList();
for(int i = 0; i < array.length(); i++){
list.add(parse_json_post(array.getJSONObject(i)));
}
return list;
}
/**
* Parse a JSON post information into POST object
*
* @param json Source JSON post information
* @return The created post element
* @throws JSONException in case of failure
*/
private Post parse_json_post(JSONObject json) throws JSONException {

View File

@ -28,6 +28,7 @@ import org.communiquons.android.comunic.client.data.helpers.ConversationsListHel
import org.communiquons.android.comunic.client.data.runnables.FriendRefreshLoopRunnable;
import org.communiquons.android.comunic.client.data.services.NotificationsService;
import org.communiquons.android.comunic.client.data.utils.PreferencesUtils;
import org.communiquons.android.comunic.client.ui.fragments.LatestPostsFragment;
import org.communiquons.android.comunic.client.ui.fragments.SinglePostFragment;
import org.communiquons.android.comunic.client.ui.fragments.UserAccessDeniedFragment;
import org.communiquons.android.comunic.client.ui.listeners.onOpenUsersPageListener;
@ -183,6 +184,12 @@ public class MainActivity extends AppCompatActivity implements openConversationL
//Get action id
int id = item.getItemId();
//To display the personal page of the user
if(id == R.id.action_open_user_page){
openUserPage(AccountUtils.getID(MainActivity.this));
return true;
}
//To display the list of friends
if(id == R.id.action_friends_list){
openFriendsFragment();
@ -236,11 +243,12 @@ public class MainActivity extends AppCompatActivity implements openConversationL
//If the user chose to show information about him
case R.id.main_bottom_navigation_me_view:
//Old version
//Old versions
//openUserInfosFragment();
//openUserPage(AccountUtils.getID(MainActivity.this));
//New version
openUserPage(AccountUtils.getID(MainActivity.this));
openLatestPostsFragment();
return true;
//If the user wants to switch to the conversation fragment
@ -503,6 +511,21 @@ public class MainActivity extends AppCompatActivity implements openConversationL
transaction.commit();
}
/**
* Open latest posts fragment
*/
public void openLatestPostsFragment(){
//Create the fragment
LatestPostsFragment latestPostsFragment = new LatestPostsFragment();
//Perform the transaction
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.main_fragment, latestPostsFragment);
transaction.addToBackStack(null);
transaction.commit();
}
/**
* Clear the cache database of the application
*/

View File

@ -0,0 +1,200 @@
package org.communiquons.android.comunic.client.ui.fragments;
import android.app.Fragment;
import android.app.FragmentTransaction;
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.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import org.communiquons.android.comunic.client.R;
import org.communiquons.android.comunic.client.data.arrays.PostsList;
import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper;
import org.communiquons.android.comunic.client.data.helpers.PostsHelper;
import org.communiquons.android.comunic.client.data.models.UserInfo;
import org.communiquons.android.comunic.client.ui.activities.MainActivity;
/**
* Latest posts fragment
*
* @author Pierre HUBERT
* Created by pierre on 5/10/18.
*/
public class LatestPostsFragment extends Fragment {
/**
* Debug tag
*/
private static final String TAG = "LatestPostsFragment";
/**
* Posts helper
*/
PostsHelper mPostsHelper;
/**
* User information helper
*/
GetUsersHelper mUserHelper;
/**
* The list of posts
*/
PostsList mPostsList;
/**
* Information about the related users
*/
ArrayMap<Integer, UserInfo> mUserInfo;
/**
* Loading progress bar
*/
ProgressBar mProgressBar;
/**
* No posts notice
*/
TextView mNoPostNotice;
@Override
public void onStart() {
super.onStart();
//Create posts helper
mPostsHelper = new PostsHelper(getActivity());
//Create user helper
mUserHelper = new GetUsersHelper(getActivity());
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_latest_posts, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//Get some of the views
mProgressBar = view.findViewById(R.id.loading_progress);
mNoPostNotice = view.findViewById(R.id.no_post_notice);
}
@Override
public void onResume() {
super.onResume();
//Update dock and activity title
getActivity().setTitle(R.string.fragment_latestposts_title);
//Update the bottom navigation menu
((MainActivity) getActivity())
.setSelectedNavigationItem(R.id.main_bottom_navigation_me_view);
//Refresh the list of posts of the user
refresh_posts_list();
}
/**
* Refresh the list of posts of the user
*/
private void refresh_posts_list(){
//Show progress bar / hide no posts notice
toggleLoadingBarVisibility(true);
toggleNoPostNoticeVisibility(false);
//Get the list of latest posts
new AsyncTask<Void, Void, PostsList>(){
@Override
protected PostsList doInBackground(Void... params) {
PostsList postsList = mPostsHelper.get_latest();
//Get user information, if possible
if(postsList != null)
mUserInfo = mUserHelper.getMultiple(postsList.getUsersId());
return postsList;
}
@Override
protected void onPostExecute(PostsList posts) {
//Check if the activity still exists or not
if(getActivity() == null)
return;
on_got_new_posts_list(posts);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
/**
* This function is called when we have got a new post list
*
* @param list The new list of posts
*/
private void on_got_new_posts_list(@Nullable PostsList list){
//Hide loading bar
toggleLoadingBarVisibility(false);
//Check for errors
if(list == null){
Toast.makeText(getActivity(), R.string.err_get_latest_posts, Toast.LENGTH_SHORT).show();
return;
}
if(mUserInfo == null){
Toast.makeText(getActivity(), R.string.err_get_users_info, Toast.LENGTH_SHORT).show();
return;
}
//Save the list of posts
mPostsList = list;
//Check if the posts list is empty
if(mPostsList.size() == 0)
toggleNoPostNoticeVisibility(true);
//Append the new posts list
//Apply the post fragment
PostsListFragment postsListFragment = new PostsListFragment();
postsListFragment.setPostsList(mPostsList);
postsListFragment.setUsersInfos(mUserInfo);
//Create and commit a transaction
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.posts_list_target, postsListFragment);
transaction.commit();
}
/**
* Toggle progress bar visibility
*
* @param visible Specify whether the progress bar should be visible or not
*/
private void toggleLoadingBarVisibility(boolean visible){
mProgressBar.setVisibility(visible ? View.VISIBLE : View.GONE);
}
/**
* Toggle no post notice visibility
*
* @param visible The visibility of the no post notice
*/
private void toggleNoPostNoticeVisibility(boolean visible){
mNoPostNotice.setVisibility(visible ? View.VISIBLE : View.GONE);
}
}

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Loading progress bar -->
<ProgressBar
android:id="@+id/loading_progress"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<!-- No posts notice -->
<TextView
android:id="@+id/no_post_notice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/notice_no_latest_posts"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
<!-- Posts list -->
<FrameLayout
android:id="@+id/posts_list_target"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

View File

@ -2,6 +2,11 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Open user page -->
<item
android:id="@+id/action_open_user_page"
android:title="@string/main_menu_personal_page"/>
<!-- Display friends list -->
<item
android:id="@+id/action_friends_list"

View File

@ -185,4 +185,8 @@
<string name="success_clear_local_db">The local database has been successfully cleared! You may need to restart the application to make the changes being fully applied.</string>
<string name="err_clear_local_db">An error occurred while trying to clear local database!</string>
<string name="image_description_pick_image">Pick an image</string>
<string name="main_menu_personal_page">My page</string>
<string name="fragment_latestposts_title">Latest posts</string>
<string name="err_get_latest_posts">An error occurred while trying to retrieve the list of latest posts!</string>
<string name="notice_no_latest_posts">You do not have any latest posts to display here.</string>
</resources>