Incomplete rendering of the notifications.

This commit is contained in:
Pierre 2018-04-10 18:26:50 +02:00
parent 8088ab39c3
commit 648f6ca9af
6 changed files with 324 additions and 5 deletions

View File

@ -40,6 +40,15 @@ public class GetUsersHelper {
*/ */
private UsersInfosDbHelper udbHelper = null; private UsersInfosDbHelper udbHelper = null;
/**
* Public constructor of the class
*
* @param context The context of the application
*/
public GetUsersHelper(@NonNull Context context){
this(context, DatabaseHelper.getInstance(context));
}
/** /**
* Public constructor of the class * Public constructor of the class
* *
@ -137,11 +146,11 @@ public class GetUsersHelper {
} }
/** /**
* Get the list of missing users ID in a set of users informations * Get the list of missing users ID in a set of users information
* *
* @param IDs The reference IDs list * @param IDs The reference IDs list
* @param usersInfo Informations about the users * @param usersInfo Information about the users
* @return * @return The list of missing IDs
*/ */
public static ArrayList<Integer> get_missing_ids(@NonNull ArrayList<Integer> IDs, public static ArrayList<Integer> get_missing_ids(@NonNull ArrayList<Integer> IDs,
@NonNull ArrayMap<Integer, UserInfo> usersInfo){ @NonNull ArrayMap<Integer, UserInfo> usersInfo){
@ -299,12 +308,12 @@ public class GetUsersHelper {
//Process each user ID //Process each user ID
for(int userID : IDs) { for(int userID : IDs) {
UserInfo userInfos = null; UserInfo userInfos;
//Extract user object //Extract user object
JSONObject userObject = userObjectContainer.getJSONObject(""+userID); JSONObject userObject = userObjectContainer.getJSONObject(""+userID);
//Continue only if we could extract required informations //Continue only if we could extract required information
if (userObject != null) { if (userObject != null) {
//Parse user information //Parse user information
userInfos = parse_user_json(userObject); userInfos = parse_user_json(userObject);

View File

@ -1,5 +1,10 @@
package org.communiquons.android.comunic.client.data.notifications; package org.communiquons.android.comunic.client.data.notifications;
import android.support.annotation.Nullable;
import android.util.ArrayMap;
import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
@ -10,4 +15,67 @@ import java.util.ArrayList;
*/ */
public class NotifsList extends ArrayList<Notif> { public class NotifsList extends ArrayList<Notif> {
/**
* Information about the users of the notifications
*/
private ArrayMap<Integer, UserInfo> mUsersInfo;
/**
* Get and return the IDs of the users related to the notifications
*
* @return The list of users
*/
public ArrayList<Integer> getUsersID(){
ArrayList<Integer> IDs = new ArrayList<>();
//Process each notification
for(Notif notif : this){
if(!IDs.contains(notif.getFrom_user_id()))
IDs.add(notif.getFrom_user_id());
if(!IDs.contains(notif.getDest_user_id()))
IDs.add(notif.getDest_user_id());
if(notif.getOn_elem_type() == NotifElemType.FRIEND_REQUEST ||
notif.getOn_elem_type() == NotifElemType.USER_PAGE){
if(!IDs.contains(notif.getOn_elem_id()))
IDs.add(notif.getOn_elem_id());
}
if(notif.getFrom_container_type() == NotifElemType.FRIEND_REQUEST ||
notif.getFrom_container_type() == NotifElemType.USER_PAGE){
if(!IDs.contains(notif.getFrom_container_id()))
IDs.add(notif.getFrom_container_id());
}
}
return IDs;
}
/**
* Set information about the users related to the notifications
*
* @param mUsersInfo Information about the users
*/
public void setUsersInfo(ArrayMap<Integer, UserInfo> mUsersInfo) {
this.mUsersInfo = mUsersInfo;
}
/**
* Get the information about the users related to the notifications
*
* @return Information about the users related to the notifications
*/
@Nullable
public ArrayMap<Integer, UserInfo> getUsersInfo() {
return mUsersInfo;
}
} }

View File

@ -0,0 +1,82 @@
package org.communiquons.android.comunic.client.ui.adapters;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import org.communiquons.android.comunic.client.R;
import org.communiquons.android.comunic.client.data.ImageLoad.ImageLoadManager;
import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo;
import org.communiquons.android.comunic.client.data.notifications.Notif;
import org.communiquons.android.comunic.client.data.notifications.NotifsList;
import org.communiquons.android.comunic.client.data.utils.Utilities;
/**
* Notifications list adapter
*
* @author Pierre HUBERT
* Created by pierre on 4/10/18.
*/
public class NotificationsAdapter extends ArrayAdapter<Notif>{
/**
* Utilities
*/
private Utilities mUtils;
/**
* Information about the users of the notifications
*/
private ArrayMap<Integer, UserInfo> mUsersInfo;
/**
* Public adapter constructor
*
* @param context The context of the application
* @param list The list of notifications
*/
public NotificationsAdapter(Context context, NotifsList list){
super(context, 0, list);
//Save user information
mUsersInfo = list.getUsersInfo();
mUtils = new Utilities(context);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
//Inflate the view, if required
if(convertView == null){
convertView = LayoutInflater.from(getContext())
.inflate(R.layout.notification_item, parent, false);
}
//Get the notification
Notif notif = getItem(position);
assert notif != null;
//Update the user account image
ImageView image = convertView.findViewById(R.id.user_account_image);
ImageLoadManager.remove(image);
ImageLoadManager.load(getContext(),
mUsersInfo.get(notif.getFrom_user_id()).getAcountImageURL(), image);
//Update the date of the notification
TextView date = convertView.findViewById(R.id.notification_date);
date.setText(mUtils.timeToString(Utilities.time() - notif.getTime_create()));
return convertView;
}
}

View File

@ -10,11 +10,17 @@ import android.support.annotation.Nullable;
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.ListView;
import android.widget.ProgressBar;
import android.widget.Toast; import android.widget.Toast;
import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.R;
import org.communiquons.android.comunic.client.data.DatabaseHelper;
import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersHelper;
import org.communiquons.android.comunic.client.data.notifications.NotificationsHelper; import org.communiquons.android.comunic.client.data.notifications.NotificationsHelper;
import org.communiquons.android.comunic.client.data.notifications.NotifsList;
import org.communiquons.android.comunic.client.ui.activities.MainActivity; import org.communiquons.android.comunic.client.ui.activities.MainActivity;
import org.communiquons.android.comunic.client.ui.adapters.NotificationsAdapter;
/** /**
* Notifications fragment * Notifications fragment
@ -30,17 +36,46 @@ public class NotificationsFragment extends Fragment {
*/ */
private NotificationsHelper mNotificationsHelper; private NotificationsHelper mNotificationsHelper;
/**
* Get users helper
*/
private GetUsersHelper mUsersInfoHelper;
/**
* Notifications list
*/
private NotifsList mNotificationsList;
/** /**
* Delete all the notifications button * Delete all the notifications button
*/ */
private View mDeleteNotificationsBtn; private View mDeleteNotificationsBtn;
/**
* Notifications list view
*/
private ListView mNotificationsListView;
/**
* Notifications adapter
*/
private NotificationsAdapter mNotificationsAdapter;
/**
* Loading progress bar
*/
private ProgressBar mLoadingProgress;
@Override @Override
public void onAttach(Context context) { public void onAttach(Context context) {
super.onAttach(context); super.onAttach(context);
//Create notifications helper //Create notifications helper
mNotificationsHelper = new NotificationsHelper(context); mNotificationsHelper = new NotificationsHelper(context);
//Create get users helper
mUsersInfoHelper = new GetUsersHelper(context);
} }
@Nullable @Nullable
@ -53,6 +88,9 @@ public class NotificationsFragment extends Fragment {
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
//Get loading progress view
mLoadingProgress = view.findViewById(R.id.loading_progress);
//Delete all the notifications action //Delete all the notifications action
mDeleteNotificationsBtn = view.findViewById(R.id.delete_all_notif_btn); mDeleteNotificationsBtn = view.findViewById(R.id.delete_all_notif_btn);
mDeleteNotificationsBtn.setOnClickListener(new View.OnClickListener() { mDeleteNotificationsBtn.setOnClickListener(new View.OnClickListener() {
@ -61,6 +99,9 @@ public class NotificationsFragment extends Fragment {
confirmNotificationsDeletion(); confirmNotificationsDeletion();
} }
}); });
//Get the notifications list view
mNotificationsListView = view.findViewById(R.id.notificcation_list);
} }
@Override @Override
@ -73,6 +114,17 @@ public class NotificationsFragment extends Fragment {
//Update the bottom navigation menu //Update the bottom navigation menu
((MainActivity) getActivity()) ((MainActivity) getActivity())
.setSelectedNavigationItem(R.id.main_bottom_navigation_notif); .setSelectedNavigationItem(R.id.main_bottom_navigation_notif);
//Check if it is required to fetch the list of notifications
if(mNotificationsList == null){
//Get the list of notifications
getListNotifications();
}
else
//Display the list of notifications
displayNotificationsList();
} }
/** /**
@ -132,4 +184,66 @@ public class NotificationsFragment extends Fragment {
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
/**
* Get and save the list of notifications from the server
*/
private void getListNotifications(){
//Perform the task on a separate thread
new AsyncTask<Void, Void, NotifsList>(){
@Override
protected NotifsList doInBackground(Void... params) {
NotifsList list = mNotificationsHelper.getListUnread();
//If we got the list of notifications, fetch users information
if(list != null)
list.setUsersInfo(mUsersInfoHelper.getMultiple(list.getUsersID()));
return list;
}
@Override
protected void onPostExecute(@Nullable NotifsList notifs) {
//Check if the activity has been destroyed
if(getActivity() == null)
return;
//Check if we could not get the list of notifications
if(notifs == null){
Toast.makeText(getActivity(), R.string.err_get_list_notifs,
Toast.LENGTH_SHORT).show();
return;
}
//Check if we could not get information about the users of the notifications
if(notifs.getUsersInfo() == null){
Toast.makeText(getActivity(), R.string.err_get_users_info,
Toast.LENGTH_SHORT).show();
return;
}
//Save the list of notifications and display it
mNotificationsList = notifs;
displayNotificationsList();
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
/**
* Display the list of notifications
*/
private void displayNotificationsList(){
//Hide loading progress bar
mLoadingProgress.setVisibility(View.GONE);
//Create notification adapter
mNotificationsAdapter = new NotificationsAdapter(getActivity(), mNotificationsList);
mNotificationsListView.setAdapter(mNotificationsAdapter);
}
} }

View File

@ -0,0 +1,45 @@
<?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="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp">
<!-- User image -->
<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"
android:layout_marginEnd="10dp"/>
<!-- Message -->
<TextView
android:id="@+id/notification_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:text="Notification message content from a user. This message might be long"
android:layout_gravity="center"/>
<!-- Date of the message -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@android:drawable/ic_menu_my_calendar"/>
<TextView
android:id="@+id/notification_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="1min" />
</LinearLayout>
</LinearLayout>

View File

@ -156,4 +156,5 @@
<string name="dialog_deleteallnotifs_confirm">Yes</string> <string name="dialog_deleteallnotifs_confirm">Yes</string>
<string name="err_delete_all_notifs">An error occurred while trying to delete the entire list of notifications!</string> <string name="err_delete_all_notifs">An error occurred while trying to delete the entire list of notifications!</string>
<string name="success_delete_all_notifs">All the notifications have been deleted!</string> <string name="success_delete_all_notifs">All the notifications have been deleted!</string>
<string name="err_get_list_notifs">An error occurred while trying to get the list of notifications !</string>
</resources> </resources>