Created user page fragment

This commit is contained in:
Pierre 2018-01-13 17:47:59 +01:00
parent e837449f5c
commit df91120b66
6 changed files with 353 additions and 5 deletions

View File

@ -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
*/

View File

@ -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;
}
}

View File

@ -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

View File

@ -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<Integer, Void, AdvancedUserInfo>(){
@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);
}
}

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
style="@style/UserPageHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- User name and account image -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/user_account_image"
android:layout_width="@dimen/account_image_default_width"
android:layout_height="@dimen/account_image_default_height"
android:src="@drawable/default_account_image"
android:contentDescription="@string/user_image_description"/>
<TextView
android:id="@+id/user_account_name"
style="@style/UserPageName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="User name"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>

View File

@ -8,4 +8,22 @@
<item name="colorAccent">@color/colorAccent</item>
</style>
<!-- User page fragment -->
<!-- User page header -->
<style name="UserPageHeader">
<item name="android:background">@color/colorPrimary</item>
<item name="android:paddingTop">1dp</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:paddingStart">5dp</item>
<item name="android:paddingEnd">5dp</item>
</style>
<!-- User name -->
<style name="UserPageName">
<item name="android:layout_gravity">center</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textSize">20sp</item>
<item name="android:paddingStart">5dp</item>
<item name="android:paddingEnd">5dp</item>
</style>
</resources>