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 9921f76..a320a2b 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 @@ -40,6 +40,15 @@ public class GetUsersHelper { */ 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 * @@ -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 usersInfo Informations about the users - * @return + * @param usersInfo Information about the users + * @return The list of missing IDs */ public static ArrayList get_missing_ids(@NonNull ArrayList IDs, @NonNull ArrayMap usersInfo){ @@ -299,12 +308,12 @@ public class GetUsersHelper { //Process each user ID for(int userID : IDs) { - UserInfo userInfos = null; + UserInfo userInfos; //Extract user object JSONObject userObject = userObjectContainer.getJSONObject(""+userID); - //Continue only if we could extract required informations + //Continue only if we could extract required information if (userObject != null) { //Parse user information userInfos = parse_user_json(userObject); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/notifications/NotifsList.java b/app/src/main/java/org/communiquons/android/comunic/client/data/notifications/NotifsList.java index e5372d0..3c76839 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/notifications/NotifsList.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/notifications/NotifsList.java @@ -1,5 +1,10 @@ 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; /** @@ -10,4 +15,67 @@ import java.util.ArrayList; */ public class NotifsList extends ArrayList { + + /** + * Information about the users of the notifications + */ + private ArrayMap mUsersInfo; + + /** + * Get and return the IDs of the users related to the notifications + * + * @return The list of users + */ + public ArrayList getUsersID(){ + + ArrayList 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 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 getUsersInfo() { + return mUsersInfo; + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/NotificationsAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/NotificationsAdapter.java new file mode 100644 index 0000000..56bd16b --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/NotificationsAdapter.java @@ -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{ + + /** + * Utilities + */ + private Utilities mUtils; + + /** + * Information about the users of the notifications + */ + private ArrayMap 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; + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/NotificationsFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/NotificationsFragment.java index eb87bb0..59a4a93 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/NotificationsFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/NotificationsFragment.java @@ -10,11 +10,17 @@ import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ListView; +import android.widget.ProgressBar; 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.UsersInfo.GetUsersHelper; 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.adapters.NotificationsAdapter; /** * Notifications fragment @@ -30,17 +36,46 @@ public class NotificationsFragment extends Fragment { */ private NotificationsHelper mNotificationsHelper; + /** + * Get users helper + */ + private GetUsersHelper mUsersInfoHelper; + + /** + * Notifications list + */ + private NotifsList mNotificationsList; + /** * Delete all the notifications button */ private View mDeleteNotificationsBtn; + /** + * Notifications list view + */ + private ListView mNotificationsListView; + + /** + * Notifications adapter + */ + private NotificationsAdapter mNotificationsAdapter; + + /** + * Loading progress bar + */ + private ProgressBar mLoadingProgress; + + @Override public void onAttach(Context context) { super.onAttach(context); //Create notifications helper mNotificationsHelper = new NotificationsHelper(context); + + //Create get users helper + mUsersInfoHelper = new GetUsersHelper(context); } @Nullable @@ -53,6 +88,9 @@ public class NotificationsFragment extends Fragment { public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); + //Get loading progress view + mLoadingProgress = view.findViewById(R.id.loading_progress); + //Delete all the notifications action mDeleteNotificationsBtn = view.findViewById(R.id.delete_all_notif_btn); mDeleteNotificationsBtn.setOnClickListener(new View.OnClickListener() { @@ -61,6 +99,9 @@ public class NotificationsFragment extends Fragment { confirmNotificationsDeletion(); } }); + + //Get the notifications list view + mNotificationsListView = view.findViewById(R.id.notificcation_list); } @Override @@ -73,6 +114,17 @@ public class NotificationsFragment extends Fragment { //Update the bottom navigation menu ((MainActivity) getActivity()) .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); } + + /** + * Get and save the list of notifications from the server + */ + private void getListNotifications(){ + + //Perform the task on a separate thread + new AsyncTask(){ + + @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); + } } diff --git a/app/src/main/res/layout/notification_item.xml b/app/src/main/res/layout/notification_item.xml new file mode 100644 index 0000000..565f5b6 --- /dev/null +++ b/app/src/main/res/layout/notification_item.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0393535..1cffa77 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -156,4 +156,5 @@ Yes An error occurred while trying to delete the entire list of notifications! All the notifications have been deleted! + An error occurred while trying to get the list of notifications !