Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.woocommerce.android.ui.filters

import com.woocommerce.android.tools.SelectedSite
import dagger.Reusable
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import org.wordpress.android.fluxc.model.LocalOrRemoteId.LocalId
import org.wordpress.android.fluxc.persistence.dao.FilterHistoryDao
import org.wordpress.android.fluxc.persistence.entity.FilterHistoryEntity
import org.wordpress.android.fluxc.utils.CurrentTimeProvider
import javax.inject.Inject

/**
* Per-site store for "filter history" — the previously applied filter selections on the order and
* product lists, gated by [com.woocommerce.android.util.FeatureFlag.FILTER_HISTORY].
*
* History is unlimited, deduped and newest-first (matching iOS): [save] upserts by [SavedFilter.payload],
* so re-saving an identical selection bumps it to the top instead of creating a duplicate.
*
* Callers own the [SavedFilter.payload] contract: it must be a canonical, normalized serialization of
* the active (non-empty) selection so that logically-identical filters dedup reliably across app
* versions. The per-surface encoders/decoders live with the order and product filter screens.
*/
@Reusable
class FilterHistoryRepository @Inject constructor(
private val filterHistoryDao: FilterHistoryDao,
private val selectedSite: SelectedSite,
private val currentTimeProvider: CurrentTimeProvider
) {
fun observeHistory(type: FilterHistoryType): Flow<List<SavedFilter>> =
filterHistoryDao.observeForSite(currentSiteId(), type.name)
.map { entities -> entities.map { it.toSavedFilter() } }

suspend fun save(
type: FilterHistoryType,
payload: String,
readableString: String
) {
filterHistoryDao.insertOrReplace(
FilterHistoryEntity(
localSiteId = currentSiteId(),
filterType = type.name,
payload = payload,
readableString = readableString,
dateModified = currentTimeProvider.currentDate().time
)
)
}

suspend fun remove(filter: SavedFilter) {
filterHistoryDao.delete(filter.id)
}

suspend fun clear(type: FilterHistoryType) {
filterHistoryDao.clear(currentSiteId(), type.name)
}

private fun currentSiteId() = LocalId(selectedSite.get().id)

private fun FilterHistoryEntity.toSavedFilter() = SavedFilter(
id = id,
readableString = readableString,
payload = payload
)
}

/**
* Which list a filter history entry belongs to. Persisted as the entity's `filterType` column.
*/
enum class FilterHistoryType {
ORDERS,
PRODUCTS
}

/**
* A single persisted filter history entry.
*
* @param id database id, used to [FilterHistoryRepository.remove] a specific entry.
* @param readableString human-readable summary of the filter, shown in the history list.
* @param payload canonical serialization of the selection, decoded by the per-surface consumer.
*/
data class SavedFilter(
val id: Long,
val readableString: String,
val payload: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ enum class FeatureFlag(
AI_SUPPORT_CHAT("ai_support_chat"),
SMARTER_NOTIFICATIONS("smarter_notifications", localValue = PackageUtils.isDebugBuild()),
QR_LOGIN("woo_qr_code_login"),
FILTER_HISTORY("woo_filter_history", localValue = PackageUtils.isDebugBuild()),
}
Loading
Loading