Skip to content

Commit

Permalink
Move a function to ViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarvis Lin committed Feb 6, 2024
1 parent 6964d59 commit 3416208
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 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 @@ -87,6 +88,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 +111,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 @@ -332,15 +336,8 @@ 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 markNoteAsRead(@NonNull Note... note) {
mViewModel.markNoteAsRead(note, this);
}

private void setActionBarTitleForNote(Note note) {
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,18 @@ class NotificationsListViewModel @Inject constructor(
fun resetNotificationsPermissionWarningDismissState() {
appPrefsWrapper.notificationPermissionsWarningDismissed = false
}

fun markNoteAsRead(vararg notes: Note, context: Context) {
notes.filter { it.isUnread }
.map {
gcmMessageHandler.removeNotificationWithNoteIdFromSystemBar(context, it.id)
NotificationsActions.markNoteAsRead(it)
it.setRead()
it
}
.let {
NotificationsTable.saveNotes(it, false)
EventBus.getDefault().post(NotificationsChanged())
}
}
}

0 comments on commit 3416208

Please sign in to comment.