diff --git a/app/src/main/java/fr/free/nrw/commons/notification/NotificationController.java b/app/src/main/java/fr/free/nrw/commons/notification/NotificationController.java index b22bafbb5..d8319005d 100644 --- a/app/src/main/java/fr/free/nrw/commons/notification/NotificationController.java +++ b/app/src/main/java/fr/free/nrw/commons/notification/NotificationController.java @@ -2,6 +2,7 @@ package fr.free.nrw.commons.notification; import java.io.IOException; import java.util.ArrayList; +import java.util.Date; import java.util.List; import javax.inject.Inject; 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 new file mode 100644 index 000000000..2644c8f62 --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/notification/UnreadNotificationsCheckAsync.java @@ -0,0 +1,86 @@ +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.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> { + + WeakReference context; + NotificationController notificationController; + + + public UnreadNotificationsCheckAsync(ContributionsActivity context, NotificationController notificationController) { + this.context = new WeakReference<>(context); + this.notificationController = notificationController; + } + + @Override + protected List doInBackground(Void... voids) { + List unreadNotifications = 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? + Log.d("deneme","You read all notifications"); + Timber.d("You read all notifications of your at"+lastReadNotificationDateStored +"++curr date is:"+currentDate); + } + return unreadNotifications; + } + + @Override + protected void onPostExecute(List unreadNotification) { + super.onPostExecute(unreadNotification); + + // 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) { + //Log.d("deneme3","context.get().contributionsFragment"+((NewContributionsFragment)context.get().contributionsFragment).toString()); + ((ContributionsFragment)context.get().contributionsActivityPagerAdapter.contributionsFragment).updateNotificationsNotification(unreadNotification); + } + } + } + + private List findUnreadNotifications(List allNotifications) { + // TODO: only return notifications after last read date + return allNotifications; + } +}