diff --git a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsActivity.java b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsActivity.java index bd9fff78c..36d5c8409 100644 --- a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsActivity.java +++ b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsActivity.java @@ -223,12 +223,12 @@ public class ContributionsActivity extends AuthenticatedActivity implements Frag pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT); } - public void updateNotificationIcon(int unreadNotificationListSize) { - if (unreadNotificationListSize==0) { - isThereUnreadNotifications = false; + public void updateNotificationIcon(boolean isThereUnreadNotifications) { + if (!isThereUnreadNotifications) { + this.isThereUnreadNotifications = false; menu.findItem(R.id.notifications).setIcon(ContextCompat.getDrawable(this, R.drawable.ic_notifications_white_24dp)); } else { - isThereUnreadNotifications = true; + this.isThereUnreadNotifications = true; menu.findItem(R.id.notifications).setIcon(ContextCompat.getDrawable(this, R.drawable.ic_notifications_white_with_marker)); } } diff --git a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsFragment.java b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsFragment.java index 892238772..8766c6613 100644 --- a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsFragment.java @@ -388,11 +388,11 @@ public class ContributionsFragment /** * Updates notification indicator on toolbar to indicate there are unread notifications - * @param unreadNotifications + * @param isThereUnreadNotifications true if user checked notifications before last notification date */ - public void updateNotificationsNotification(List unreadNotifications) { + public void updateNotificationsNotification(boolean isThereUnreadNotifications) { Log.d("deneme","notification updates are called"); - ((ContributionsActivity)getActivity()).updateNotificationIcon(unreadNotifications.size()); + ((ContributionsActivity)getActivity()).updateNotificationIcon(isThereUnreadNotifications); } /** diff --git a/app/src/main/java/fr/free/nrw/commons/notification/NotificationActivity.java b/app/src/main/java/fr/free/nrw/commons/notification/NotificationActivity.java index 2358e4d13..480bbaa97 100644 --- a/app/src/main/java/fr/free/nrw/commons/notification/NotificationActivity.java +++ b/app/src/main/java/fr/free/nrw/commons/notification/NotificationActivity.java @@ -10,6 +10,7 @@ import android.support.design.widget.Snackbar; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.util.Log; import android.view.View; import android.widget.ProgressBar; import android.widget.RelativeLayout; @@ -18,6 +19,7 @@ import com.pedrogomez.renderers.RVRendererAdapter; import java.util.Calendar; import java.util.Collections; +import java.util.Date; import java.util.List; import javax.inject.Inject; @@ -89,7 +91,8 @@ public class NotificationActivity extends NavigationBaseActivity { Timber.d("Add notifications"); // Store when add notification is called last - long currentDate = Calendar.getInstance().getTime().getTime(); + long currentDate = new Date(System.currentTimeMillis()).getTime(); + Log.d("denemeee", "current date"+currentDate); getSharedPreferences("prefs", MODE_PRIVATE).edit().putLong("last_read_notification_date", currentDate).apply(); Timber.d("Set last notification read date to current date:"+ currentDate); diff --git a/app/src/main/java/fr/free/nrw/commons/notification/UnreadNotificationsCheckAsync.java b/app/src/main/java/fr/free/nrw/commons/notification/UnreadNotificationsCheckAsync.java index 45a6d69c5..b26b8686c 100644 --- a/app/src/main/java/fr/free/nrw/commons/notification/UnreadNotificationsCheckAsync.java +++ b/app/src/main/java/fr/free/nrw/commons/notification/UnreadNotificationsCheckAsync.java @@ -1,25 +1,24 @@ package fr.free.nrw.commons.notification; -import android.content.Context; -import android.content.SharedPreferences; import android.os.AsyncTask; import android.util.Log; import java.io.IOException; import java.lang.ref.WeakReference; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.List; import fr.free.nrw.commons.contributions.ContributionsActivity; -import fr.free.nrw.commons.contributions.ContributionsFragment; import timber.log.Timber; /** * This asynctask will check unread notifications after a date (date user check notifications last) */ -public class UnreadNotificationsCheckAsync extends AsyncTask> { +public class UnreadNotificationsCheckAsync extends AsyncTask { WeakReference context; NotificationController notificationController; @@ -31,54 +30,47 @@ public class UnreadNotificationsCheckAsync extends AsyncTask doInBackground(Void... voids) { - List unreadNotifications = null; + protected Notification doInBackground(Void... voids) { + Notification lastNotification = null; - Date currentDate = Calendar.getInstance().getTime(); - Date lastReadNotificationDateStored = - new Date(context.get() - .getSharedPreferences("prefs",0) - .getLong("last_read_notification_date", 0)); - - if (currentDate.after(lastReadNotificationDateStored)) { - Timber.d("You may have unread notifications since"+lastReadNotificationDateStored); - Log.d("deneme","You may have unread notifications since"+lastReadNotificationDateStored +"++curr date is:"+currentDate); - - //TODO: fetch latest notification of user and save latest notification date to shared preferences - //TODO: pass latest notification update date here. So that you will get notifications after that date - - try { - unreadNotifications = findUnreadNotifications(notificationController.getNotifications()); - Log.d("deneme","notifications is:"+unreadNotifications); - for (Notification notification : unreadNotifications) { - Log.d("deneme", notification.notificationText); - } - } catch (IOException e) { - e.printStackTrace(); - } - - } else { - // This case is not possible ever? - Timber.d("You read all notifications of your at"+lastReadNotificationDateStored +"++curr date is:"+currentDate); + try { + lastNotification = findLastNotification(notificationController.getNotifications()); + } catch (IOException e) { + e.printStackTrace(); } - return unreadNotifications; + + return lastNotification; } @Override - protected void onPostExecute(List unreadNotification) { - super.onPostExecute(unreadNotification); + protected void onPostExecute(Notification lastNotification) { + super.onPostExecute(lastNotification); + + Date lastNotificationCheckDate = new Date(context.get() + .getSharedPreferences("prefs",0) + .getLong("last_read_notification_date", 0)); + Timber.d("You may have unread notifications since"+lastNotificationCheckDate); + + boolean isThereUnreadNotifications; + + Date lastReadNotificationDate = new java.util.Date(Long.parseLong(lastNotification.dateWithYear)*1000); + + if (lastNotificationCheckDate.before(lastReadNotificationDate)) { + isThereUnreadNotifications = true; + } else { + isThereUnreadNotifications = false; + } // Check if activity is still running if (context.get().getWindow().getDecorView().isShown() && !context.get().isFinishing()) { // Check if fragment is not null and visible if (context.get().isContributionsFragmentVisible && context.get().contributionsActivityPagerAdapter.contributionsFragment != null) { - (context.get().contributionsActivityPagerAdapter.contributionsFragment).updateNotificationsNotification(unreadNotification); + (context.get().contributionsActivityPagerAdapter.contributionsFragment).updateNotificationsNotification(isThereUnreadNotifications); } } } - private List findUnreadNotifications(List allNotifications) { - // TODO: only return notifications after last read date - return allNotifications; + private Notification findLastNotification(List allNotifications) { + return allNotifications.get(allNotifications.size()-1); } }