Skip to content

Commit

Permalink
Merge pull request #2294 from wordpress-mobile/notifications-flow-obs…
Browse files Browse the repository at this point in the history
…erve-db

POC for exposing a Flow to observe the NotificationModel table
  • Loading branch information
JorgeMucientes authored Feb 16, 2022
2 parents b9a7145 + 6ffe637 commit dd9c87b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import com.yarolegovich.wellsql.core.Identifiable
import com.yarolegovich.wellsql.core.annotation.Column
import com.yarolegovich.wellsql.core.annotation.PrimaryKey
import com.yarolegovich.wellsql.core.annotation.Table
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.onStart
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.model.notification.NoteIdSet
import org.wordpress.android.fluxc.model.notification.NotificationModel
Expand All @@ -21,6 +27,8 @@ import javax.inject.Singleton

@Singleton
class NotificationSqlUtils @Inject constructor(private val formattableContentMapper: FormattableContentMapper) {
private val dataUpdatesTrigger = MutableSharedFlow<Unit>(extraBufferCapacity = 1)

fun insertOrUpdateNotification(notification: NotificationModel): Int {
val notificationResult = WellSql.select(NotificationModelBuilder::class.java)
.where().beginGroup()
Expand All @@ -36,14 +44,15 @@ class NotificationSqlUtils @Inject constructor(private val formattableContentMap
return if (notificationResult.isEmpty()) {
// insert
WellSql.insert(notification.toBuilder()).asSingleTransaction(true).execute()
dataUpdatesTrigger.tryEmit(Unit)
1
} else {
// update
val oldId = notificationResult[0].id
WellSql.update(NotificationModelBuilder::class.java).whereId(oldId).put(
notification.toBuilder(),
UpdateAllExceptId<NotificationModelBuilder>(NotificationModelBuilder::class.java)
).execute()
).execute().also(::triggerUpdateIfNeeded)
}
}

Expand Down Expand Up @@ -120,6 +129,20 @@ class NotificationSqlUtils @Inject constructor(private val formattableContentMap
.map { it.build(formattableContentMapper) }
}

fun observeNotificationsForSite(
site: SiteModel,
@SelectQuery.Order order: Int = ORDER_DESCENDING,
filterByType: List<String>? = null,
filterBySubtype: List<String>? = null
): Flow<List<NotificationModel>> {
return dataUpdatesTrigger
.onStart { emit(Unit) }
.mapLatest {
getNotificationsForSite(site, order, filterByType, filterBySubtype)
}
.flowOn(Dispatchers.IO)
}

fun hasUnreadNotificationsForSite(
site: SiteModel,
filterByType: List<String>? = null,
Expand Down Expand Up @@ -175,13 +198,21 @@ class NotificationSqlUtils @Inject constructor(private val formattableContentMap
.firstOrNull()?.build(formattableContentMapper)
}

fun deleteAllNotifications() = WellSql.delete(NotificationModelBuilder::class.java).execute()
fun deleteAllNotifications() = WellSql.delete(NotificationModelBuilder::class.java)
.execute()
.also(::triggerUpdateIfNeeded)

fun deleteNotificationByRemoteId(remoteNoteId: Long): Int {
return WellSql.delete(NotificationModelBuilder::class.java)
.where().beginGroup()
.equals(NotificationModelTable.REMOTE_NOTE_ID, remoteNoteId)
.endGroup().endWhere().execute()
.endGroup().endWhere()
.execute()
.also(::triggerUpdateIfNeeded)
}

private fun triggerUpdateIfNeeded(affectedRows: Int) {
if (affectedRows != 0) dataUpdatesTrigger.tryEmit(Unit)
}

private fun NotificationModel.toBuilder(): NotificationModelBuilder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.wordpress.android.fluxc.store
import android.annotation.SuppressLint
import android.content.Context
import com.yarolegovich.wellsql.SelectQuery.ORDER_DESCENDING
import kotlinx.coroutines.flow.Flow
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.wordpress.android.fluxc.Dispatcher
Expand Down Expand Up @@ -230,6 +231,13 @@ class NotificationStore @Inject constructor(
): List<NotificationModel> =
notificationSqlUtils.getNotificationsForSite(site, ORDER_DESCENDING, filterByType, filterBySubtype)

fun observeNotificationsForSite(
site: SiteModel,
filterByType: List<String>? = null,
filterBySubtype: List<String>? = null
): Flow<List<NotificationModel>> =
notificationSqlUtils.observeNotificationsForSite(site, ORDER_DESCENDING, filterByType, filterBySubtype)

/**
* Returns true if the given site has unread notifications
*
Expand Down

0 comments on commit dd9c87b

Please sign in to comment.