From df91120b66a11ee872f0dc4e1af3d5b014a92889 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 13 Jan 2018 17:47:59 +0100 Subject: [PATCH] Created user page fragment --- .../android/comunic/client/MainActivity.java | 30 +++- .../data/UsersInfo/AdvancedUserInfo.java | 32 ++++ .../client/data/UsersInfo/GetUsersHelper.java | 82 ++++++++- .../client/fragments/UserPageFragment.java | 160 ++++++++++++++++++ .../main/res/layout/fragment_user_page.xml | 36 ++++ app/src/main/res/values/styles.xml | 18 ++ 6 files changed, 353 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/AdvancedUserInfo.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/fragments/UserPageFragment.java create mode 100644 app/src/main/res/layout/fragment_user_page.xml diff --git a/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java index 392f85f..40145d0 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java @@ -27,6 +27,7 @@ import org.communiquons.android.comunic.client.fragments.ConversationsListFragme import org.communiquons.android.comunic.client.fragments.FriendsListFragment; import org.communiquons.android.comunic.client.fragments.UpdateConversationFragment; import org.communiquons.android.comunic.client.fragments.UserInfosFragment; +import org.communiquons.android.comunic.client.fragments.UserPageFragment; /** @@ -180,7 +181,12 @@ public class MainActivity extends AppCompatActivity //If the user chose to show information about him case R.id.main_bottom_navigation_me_view: - openUserInfosFragment(); + + //Old version + //openUserInfosFragment(); + + //New version + openUserPage(AccountUtils.getID(MainActivity.this)); return true; //If the user wants to switch to the conversation fragment @@ -261,6 +267,28 @@ public class MainActivity extends AppCompatActivity transaction.commit(); } + /** + * Open the page of the specified user + * + * @param userID The ID of the user to open + */ + void openUserPage(int userID){ + + //Prepare arguments + Bundle args = new Bundle(); + args.putInt(UserPageFragment.ARGUMENT_USER_ID, userID); + + //Create fragment + UserPageFragment userPageFragment = new UserPageFragment(); + userPageFragment.setArguments(args); + + FragmentTransaction transaction = getFragmentManager().beginTransaction(); + transaction.replace(R.id.main_fragment, userPageFragment); + transaction.addToBackStack(null); + transaction.commit(); + + } + /** * Open the conversation list fragment */ diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/AdvancedUserInfo.java b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/AdvancedUserInfo.java new file mode 100644 index 0000000..62c2cb9 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/AdvancedUserInfo.java @@ -0,0 +1,32 @@ +package org.communiquons.android.comunic.client.data.UsersInfo; + +/** + * Advanced informations about a single user + * + * @author Pierre HUBERT + * Created by pierre on 1/13/18. + */ + +public class AdvancedUserInfo extends UserInfo { + + //Private fields + private int account_creation_time; + + /** + * Get the account creation time + * + * @return The time of creation of the account + */ + public int getAccount_creation_time() { + return account_creation_time; + } + + /** + * Set the account creation time + * + * @param account_creation_time The time when the account was created + */ + public void setAccount_creation_time(int account_creation_time) { + this.account_creation_time = account_creation_time; + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/GetUsersHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/GetUsersHelper.java index f67daaf..e6c67de 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/GetUsersHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/GetUsersHelper.java @@ -108,6 +108,34 @@ public class GetUsersHelper { } + /** + * Get advanced informations about a single user + * + * @param userID The user to get information about + * @return Informations about the user / null in case of failure + */ + @Nullable + public AdvancedUserInfo get_advanced_infos(int userID){ + + //Perform an API request + APIRequestParameters params = new APIRequestParameters(mContext, + "user/getAdvancedUserInfos"); + params.addParameter("userID", userID); + + //Perform the request + try { + APIResponse response = new APIRequest().exec(params); + + //Parse user informations + return parse_advanced_user_json(response.getJSONObject()); + + } catch (Exception e) { + e.printStackTrace(); + return null; + } + + } + /** * Get the list of missing users ID in a set of users informations * @@ -311,15 +339,61 @@ public class GetUsersHelper { * @param userObject Informations about the user in an object * @return User object in case of success, null else */ + @Nullable private UserInfo parse_user_json(JSONObject userObject){ - //Check if the JSON object passed is null or not + //Check if we got a null response if(userObject == null) - return null; //Failure + return null; - UserInfo userInfos = new UserInfo(); + //Parse basic user infos + return parse_base_user_json(new UserInfo(), userObject); + } - //Try to retrieve user informations + /** + * Parse advanced user informations into an object + * + * @param userObject Source JSON object + * @return The parse JSON object or null in case of failure + */ + @Nullable + private AdvancedUserInfo parse_advanced_user_json(JSONObject userObject){ + + //Parse basic informations about the user + AdvancedUserInfo advancedUserInfo = (AdvancedUserInfo) + parse_base_user_json(new AdvancedUserInfo(), userObject); + + //Check for errors + if(advancedUserInfo == null) + return null; + + //Parse advanced user informations + try { + + //Get account creation time + advancedUserInfo.setAccount_creation_time(userObject.getInt("account_creation_time")); + + + } catch (JSONException e){ + e.printStackTrace(); + return null; + } + + //Return result + return advancedUserInfo; + } + + /** + * Parse user basic informations into a user object + * + * @param userInfos The user informations object to fill + * @param userObject The source JSON object + * @return The filled user Infos object + */ + @Nullable + private UserInfo parse_base_user_json(UserInfo userInfos, JSONObject userObject){ + + //Try to retrieve basic user informations try { //Retrieve all user informations diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/UserPageFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/UserPageFragment.java new file mode 100644 index 0000000..82f6314 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/UserPageFragment.java @@ -0,0 +1,160 @@ +package org.communiquons.android.comunic.client.fragments; + +import android.app.AlertDialog; +import android.app.Fragment; +import android.os.AsyncTask; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; +import android.widget.Toast; + +import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.data.DatabaseHelper; +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.GetUsersHelper; +import org.communiquons.android.comunic.client.data.utils.UiUtils; + +/** + * User page fragment + * + * Display the page of a user + * + * @author Pierre HUBERT + * Created by pierre on 1/13/18. + */ + +public class UserPageFragment extends Fragment { + + /** + * The name of the argument that contains user id + */ + public static final String ARGUMENT_USER_ID = "userID"; + + /** + * The ID of the current user + */ + private int userID; + + /** + * User informations + */ + private AdvancedUserInfo userInfo; + + /** + * Get user helper + */ + private GetUsersHelper getUsersHelper; + + /** + * Loading alert dialog + */ + private AlertDialog loadingDialog; + + /** + * User account name + */ + private TextView user_name; + + /** + * User account image + */ + private ImageView user_image; + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + //Save user ID + userID = getArguments().getInt(ARGUMENT_USER_ID); + + //Get database helper + DatabaseHelper dbHelper = DatabaseHelper.getInstance(getActivity()); + + //Create getUserHelper instance + getUsersHelper = new GetUsersHelper(getActivity(), dbHelper); + } + + @Nullable + @Override + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { + return inflater.inflate(R.layout.fragment_user_page, container, false); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + //Get the user + user_image = view.findViewById(R.id.user_account_image); + user_name = view.findViewById(R.id.user_account_name); + } + + @Override + public void onResume() { + super.onResume(); + + //Check if we got informations about the user + if(userInfo == null){ + + //Show loading alert dialog + loadingDialog = UiUtils.create_loading_dialog(getActivity()); + + //Fetch user informations + new AsyncTask(){ + @Override + protected AdvancedUserInfo doInBackground(Integer... params) { + return getUsersHelper.get_advanced_infos(params[0]); + } + + @Override + protected void onPostExecute(AdvancedUserInfo advancedUserInfo) { + //Continue only if the activity hasn't ended + if(getActivity() != null) + onGotUserInfo(advancedUserInfo); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, userID); + } + else + onGotUserInfo(userInfo); + } + + @Override + public void onPause() { + super.onPause(); + + //Remove loading dialog + if(loadingDialog != null) + loadingDialog.dismiss(); + } + + /** + * This function is called on the main thread once we got user informations + * + * @param info Informations about the user + */ + public void onGotUserInfo(@Nullable AdvancedUserInfo info){ + + //Remove loading dialog + if(loadingDialog != null) + loadingDialog.dismiss(); + + //Check for errors + if(info == null){ + Toast.makeText(getActivity(), R.string.err_get_user_info, Toast.LENGTH_SHORT).show(); + return; + } + + //Save user informations + userInfo = info; + + //Update user name and account image + user_name.setText(userInfo.getDisplayFullName()); + ImageLoadManager.remove(user_image); + ImageLoadManager.load(getActivity(), userInfo.getAcountImageURL(), user_image); + } +} diff --git a/app/src/main/res/layout/fragment_user_page.xml b/app/src/main/res/layout/fragment_user_page.xml new file mode 100644 index 0000000..3c08b28 --- /dev/null +++ b/app/src/main/res/layout/fragment_user_page.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 5885930..68d852c 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -8,4 +8,22 @@ @color/colorAccent + + + + + +