From 306977a101184678595185fb5eb25604bb5cb0c5 Mon Sep 17 00:00:00 2001 From: Pierre Date: Thu, 10 May 2018 10:08:26 +0200 Subject: [PATCH] Added latest posts fragment --- .../client/data/helpers/PostsHelper.java | 48 ++++- .../client/ui/activities/MainActivity.java | 27 ++- .../ui/fragments/LatestPostsFragment.java | 200 ++++++++++++++++++ .../main/res/layout/fragment_latest_posts.xml | 29 +++ app/src/main/res/menu/main_menu.xml | 5 + app/src/main/res/values/strings.xml | 4 + 6 files changed, 306 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/LatestPostsFragment.java create mode 100644 app/src/main/res/layout/fragment_latest_posts.xml diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/PostsHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/PostsHelper.java index 012afb5..033aa5b 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/PostsHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/PostsHelper.java @@ -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 { diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java index c50daf2..b06f767 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java @@ -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 */ diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/LatestPostsFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/LatestPostsFragment.java new file mode 100644 index 0000000..d9b751e --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/LatestPostsFragment.java @@ -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 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(){ + @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); + } +} diff --git a/app/src/main/res/layout/fragment_latest_posts.xml b/app/src/main/res/layout/fragment_latest_posts.xml new file mode 100644 index 0000000..9d8d709 --- /dev/null +++ b/app/src/main/res/layout/fragment_latest_posts.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/main_menu.xml b/app/src/main/res/menu/main_menu.xml index 30d362a..6e227ae 100644 --- a/app/src/main/res/menu/main_menu.xml +++ b/app/src/main/res/menu/main_menu.xml @@ -2,6 +2,11 @@ + + + The local database has been successfully cleared! You may need to restart the application to make the changes being fully applied. An error occurred while trying to clear local database! Pick an image + My page + Latest posts + An error occurred while trying to retrieve the list of latest posts! + You do not have any latest posts to display here.