Skip to content

Commit

Permalink
Merge pull request #20136 from wordpress-mobile/issue/20066-add-Mark-…
Browse files Browse the repository at this point in the history
…all-as-read-functionality

Add mark all as read functionality
  • Loading branch information
Antonis Lilis authored Feb 7, 2024
2 parents fa83b6f + ac8c93e commit 1f17263
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager.widget.ViewPager;

import org.greenrobot.eventbus.EventBus;
Expand Down Expand Up @@ -66,6 +67,7 @@
import org.wordpress.android.widgets.WPViewPagerTransformer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -87,6 +89,8 @@ public class NotificationsDetailActivity extends LocaleAwareActivity implements
private static final String ARG_TITLE = "activityTitle";
private static final String DOMAIN_WPCOM = "wordpress.com";

private NotificationsListViewModel mViewModel;

@Inject AccountStore mAccountStore;
@Inject SiteStore mSiteStore;
@Inject GCMMessageHandler mGCMMessageHandler;
Expand All @@ -108,6 +112,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
((WordPress) getApplication()).component().inject(this);
AppLog.i(AppLog.T.NOTIFS, "Creating NotificationsDetailActivity");

mViewModel = new ViewModelProvider(this).get(NotificationsListViewModel.class);
mBinding = NotificationsDetailActivityBinding.inflate(getLayoutInflater());
setContentView(mBinding.getRoot());

Expand Down Expand Up @@ -210,7 +215,7 @@ private void updateUIAndNote(boolean doRefresh) {

// set title
setActionBarTitleForNote(note);
markNoteAsRead(note);
mViewModel.markNoteAsRead(this, Collections.singletonList(note));

// If `note.getTimestamp()` is not the most recent seen note, the server will discard the value.
NotificationsActions.updateSeenTimestamp(note);
Expand Down Expand Up @@ -246,7 +251,8 @@ public void onPageSelected(int position) {
Note currentNote = mAdapter.getNoteAtPosition(position);
if (currentNote != null) {
setActionBarTitleForNote(currentNote);
markNoteAsRead(currentNote);
mViewModel.markNoteAsRead(NotificationsDetailActivity.this,
Collections.singletonList(currentNote));
NotificationsActions.updateSeenTimestamp(currentNote);
// track subsequent comment note views
trackCommentNote(currentNote);
Expand Down Expand Up @@ -332,17 +338,6 @@ private void showErrorToastAndFinish() {
finish();
}

private void markNoteAsRead(Note note) {
mGCMMessageHandler.removeNotificationWithNoteIdFromSystemBar(this, note.getId());
// mark the note as read if it's unread
if (note.isUnread()) {
NotificationsActions.markNoteAsRead(note);
note.setRead();
NotificationsTable.saveNote(note);
EventBus.getDefault().post(new NotificationEvents.NotificationsChanged());
}
}

private void setActionBarTitleForNote(Note note) {
if (getSupportActionBar() != null) {
String title = note.getTitle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import org.wordpress.android.ui.main.WPMainNavigationView.PageType
import org.wordpress.android.ui.mysite.jetpackbadge.JetpackPoweredBottomSheetFragment
import org.wordpress.android.ui.notifications.NotificationEvents.NotificationsUnseenStatus
import org.wordpress.android.ui.notifications.NotificationsListFragment.Companion.TabPosition.All
import org.wordpress.android.ui.notifications.NotificationsListFragmentPage.Companion.KEY_TAB_POSITION
import org.wordpress.android.ui.notifications.adapters.NotesAdapter.FILTERS
import org.wordpress.android.ui.notifications.adapters.NotesAdapter.FILTERS.FILTER_ALL
import org.wordpress.android.ui.notifications.adapters.NotesAdapter.FILTERS.FILTER_COMMENT
Expand Down Expand Up @@ -330,7 +331,10 @@ class NotificationsListFragment : Fragment(R.layout.notifications_list_fragment)
*/
private fun markAllAsRead() {
analyticsTrackerWrapper.track(NOTIFICATIONS_MARK_ALL_READ_TAPPED)
// TODO("not yet implemented")
(childFragmentManager.fragments.firstOrNull {
// use -1 to make sure that the (null == null) will not happen
(it.arguments?.getInt(KEY_TAB_POSITION) ?: -1) == binding?.viewPager?.currentItem
} as? NotificationsListFragmentPage)?.markAllNotesAsRead()
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import android.view.animation.Animation
import android.view.animation.Animation.AnimationListener
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.OnScrollListener
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode.MAIN
Expand Down Expand Up @@ -57,6 +60,7 @@ import org.wordpress.android.util.helpers.SwipeToRefreshHelper
import org.wordpress.android.widgets.AppRatingDialog.incrementInteractions
import javax.inject.Inject

@AndroidEntryPoint
class NotificationsListFragmentPage : ViewPagerFragment(R.layout.notifications_list_fragment_page),
OnScrollToTopListener,
DataLoadedListener {
Expand All @@ -65,6 +69,7 @@ class NotificationsListFragmentPage : ViewPagerFragment(R.layout.notifications_l
private var isAnimatingOutNewNotificationsBar = false
private var shouldRefreshNotifications = false
private var tabPosition = 0
private val viewModel: NotificationsListViewModel by viewModels()

@Inject
lateinit var accountStore: AccountStore
Expand Down Expand Up @@ -405,6 +410,13 @@ class NotificationsListFragmentPage : ViewPagerFragment(R.layout.notifications_l
}
}

/**
* Mark notifications as read in CURRENT tab, use filteredNotes instead of notes
*/
fun markAllNotesAsRead() {
viewModel.markNoteAsRead(requireContext(), createOrGetNotesAdapter().filteredNotes)
}

@Subscribe(sticky = true, threadMode = MAIN)
fun onEventMainThread(event: NoteLikeOrModerationStatusChanged) {
NotificationsActions.downloadNoteAndUpdateDB(
Expand Down Expand Up @@ -465,7 +477,7 @@ class NotificationsListFragmentPage : ViewPagerFragment(R.layout.notifications_l
}

companion object {
private const val KEY_TAB_POSITION = "tabPosition"
const val KEY_TAB_POSITION = "tabPosition"
fun newInstance(position: Int): Fragment {
val fragment = NotificationsListFragmentPage()
val bundle = Bundle()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package org.wordpress.android.ui.notifications

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineDispatcher
import org.greenrobot.eventbus.EventBus
import org.wordpress.android.datasets.NotificationsTable
import org.wordpress.android.models.Note
import org.wordpress.android.modules.UI_THREAD
import org.wordpress.android.push.GCMMessageHandler
import org.wordpress.android.ui.jetpackoverlay.JetpackFeatureRemovalOverlayUtil
import org.wordpress.android.ui.jetpackoverlay.JetpackOverlayConnectedFeature.NOTIFICATIONS
import org.wordpress.android.ui.notifications.NotificationEvents.NotificationsChanged
import org.wordpress.android.ui.notifications.utils.NotificationsActions
import org.wordpress.android.ui.prefs.AppPrefsWrapper
import org.wordpress.android.util.JetpackBrandingUtils
import org.wordpress.android.viewmodel.Event
Expand All @@ -19,7 +26,8 @@ class NotificationsListViewModel @Inject constructor(
@Named(UI_THREAD) mainDispatcher: CoroutineDispatcher,
private val appPrefsWrapper: AppPrefsWrapper,
private val jetpackBrandingUtils: JetpackBrandingUtils,
private val jetpackFeatureRemovalOverlayUtil: JetpackFeatureRemovalOverlayUtil
private val jetpackFeatureRemovalOverlayUtil: JetpackFeatureRemovalOverlayUtil,
private val gcmMessageHandler: GCMMessageHandler

) : ScopedViewModel(mainDispatcher) {
private val _showJetpackPoweredBottomSheet = MutableLiveData<Event<Boolean>>()
Expand Down Expand Up @@ -55,4 +63,17 @@ class NotificationsListViewModel @Inject constructor(
fun resetNotificationsPermissionWarningDismissState() {
appPrefsWrapper.notificationPermissionsWarningDismissed = false
}

fun markNoteAsRead(context: Context, notes: List<Note>) {
notes.filter { it.isUnread }
.map {
gcmMessageHandler.removeNotificationWithNoteIdFromSystemBar(context, it.id)
NotificationsActions.markNoteAsRead(it)
it.setRead()
it
}.takeIf { it.isNotEmpty() }?.let {
NotificationsTable.saveNotes(it, false)
EventBus.getDefault().post(NotificationsChanged())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class NotesAdapter(
private val dataLoadedListener: DataLoadedListener
private val onLoadMoreListener: OnLoadMoreListener?
private val notes = ArrayList<Note>()
private val filteredNotes = ArrayList<Note>()
val filteredNotes = ArrayList<Note>()

@JvmField
@Inject
Expand Down

0 comments on commit 1f17263

Please sign in to comment.