-
Notifications
You must be signed in to change notification settings - Fork 133
[POS][Local Catalog] Repo for incremental sync timestamps #14511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
102bce9
b844fa6
be5980d
35b6cd1
1ce3f7a
9553a2e
0e9c15e
544baa7
a2d9caf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.woocommerce.android.ui.woopos.util.datastore | ||
|
||
import com.woocommerce.android.ui.woopos.common.util.WooPosLogWrapper | ||
import java.text.ParseException | ||
import java.text.SimpleDateFormat | ||
import java.util.Date | ||
import java.util.Locale | ||
import java.util.TimeZone | ||
import javax.inject.Inject | ||
|
||
class WooPosSyncTimestampManager @Inject constructor( | ||
private val timestampRepository: WooPosSyncTimestampRepository, | ||
private val logger: WooPosLogWrapper | ||
) { | ||
private val gmtDateFormat = SimpleDateFormat(GMT_DATE_FORMAT, Locale.US).apply { | ||
timeZone = TimeZone.getTimeZone("GMT") | ||
isLenient = false | ||
} | ||
|
||
suspend fun storeProductsLastSyncTimestamp(timestamp: Date) { | ||
val timestampGmt: String = gmtDateFormat.format(timestamp) | ||
timestampRepository.storeProductsLastSyncTimestamp(timestampGmt) | ||
|
||
} | ||
|
||
suspend fun getProductsLastSyncTimestamp(): Date? { | ||
val timestampString = timestampRepository.getProductsLastSyncTimestamp() | ||
return timestampString?.let { parseGmtTimestamp(it) } | ||
} | ||
|
||
suspend fun clearProductsLastSyncTimestamp() { | ||
timestampRepository.clearProductsLastSyncTimestamp() | ||
} | ||
|
||
suspend fun storeVariationsLastSyncTimestamp(timestamp: Date) { | ||
val timestampGmt: String = gmtDateFormat.format(timestamp) | ||
timestampRepository.storeVariationsLastSyncTimestamp(timestampGmt) | ||
} | ||
|
||
suspend fun getVariationsLastSyncTimestamp(): Date? { | ||
val timestampString = timestampRepository.getVariationsLastSyncTimestamp() | ||
return timestampString?.let { parseGmtTimestamp(it) } | ||
} | ||
|
||
suspend fun clearVariationsLastSyncTimestamp() { | ||
timestampRepository.clearVariationsLastSyncTimestamp() | ||
} | ||
|
||
suspend fun clearAllSyncTimestamps() { | ||
timestampRepository.clearAllSyncTimestamps() | ||
} | ||
|
||
fun formatTimestampForApi(timestamp: Date): String { | ||
return gmtDateFormat.format(timestamp) | ||
} | ||
|
||
fun parseTimestampFromApi(timestampString: String): Date? { | ||
return parseGmtTimestamp(timestampString) | ||
} | ||
|
||
private fun parseGmtTimestamp(timestampString: String): Date? { | ||
return try { | ||
gmtDateFormat.parse(timestampString) | ||
} catch (e: ParseException) { | ||
logger.e("Failed to parse GMT timestamp: '$timestampString'", e) | ||
null | ||
} | ||
} | ||
|
||
private companion object { | ||
const val GMT_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.woocommerce.android.ui.woopos.util.datastore | ||
|
||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
import com.woocommerce.android.tools.SelectedSite | ||
import com.woocommerce.android.ui.woopos.common.util.WooPosLogWrapper | ||
import kotlinx.coroutines.flow.first | ||
import javax.inject.Inject | ||
|
||
class WooPosSyncTimestampRepository @Inject constructor( | ||
private val selectedSite: SelectedSite, | ||
private val dataStore: DataStore<Preferences>, | ||
private val logger: WooPosLogWrapper | ||
) { | ||
|
||
suspend fun storeProductsLastSyncTimestamp(timestampGmt: String) { | ||
val key = buildSiteSpecificKey(PRODUCTS_TIMESTAMP_KEY) | ||
if (key != null) { | ||
dataStore.edit { preferences -> | ||
preferences[key] = timestampGmt | ||
} | ||
} | ||
} | ||
|
||
suspend fun getProductsLastSyncTimestamp(): String? { | ||
val key = buildSiteSpecificKey(PRODUCTS_TIMESTAMP_KEY) | ||
return if (key != null) { | ||
dataStore.data.first()[key] | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
suspend fun clearProductsLastSyncTimestamp() { | ||
val key = buildSiteSpecificKey(PRODUCTS_TIMESTAMP_KEY) | ||
if (key != null) { | ||
dataStore.edit { preferences -> | ||
preferences.remove(key) | ||
} | ||
} | ||
} | ||
|
||
suspend fun storeVariationsLastSyncTimestamp(timestampGmt: String) { | ||
val key = buildSiteSpecificKey(VARIATIONS_TIMESTAMP_KEY) | ||
if (key != null) { | ||
dataStore.edit { preferences -> | ||
preferences[key] = timestampGmt | ||
} | ||
} | ||
} | ||
|
||
suspend fun getVariationsLastSyncTimestamp(): String? { | ||
val key = buildSiteSpecificKey(VARIATIONS_TIMESTAMP_KEY) | ||
return if (key != null) { | ||
dataStore.data.first()[key] | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
suspend fun clearVariationsLastSyncTimestamp() { | ||
val key = buildSiteSpecificKey(VARIATIONS_TIMESTAMP_KEY) | ||
if (key != null) { | ||
dataStore.edit { preferences -> | ||
preferences.remove(key) | ||
} | ||
} | ||
} | ||
|
||
suspend fun clearAllSyncTimestamps() { | ||
val productsKey = buildSiteSpecificKey(PRODUCTS_TIMESTAMP_KEY) | ||
val variationsKey = buildSiteSpecificKey(VARIATIONS_TIMESTAMP_KEY) | ||
|
||
if (productsKey != null && variationsKey != null) { | ||
dataStore.edit { preferences -> | ||
preferences.remove(productsKey) | ||
preferences.remove(variationsKey) | ||
samiuelson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
samiuelson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private fun buildSiteSpecificKey(key: String): Preferences.Key<String>? { | ||
val site = selectedSite.getOrNull() | ||
return if (site != null) { | ||
stringPreferencesKey("${site.siteId}-$key") | ||
} else { | ||
logger.e("Cannot build site-specific key '$key': no site selected") | ||
null | ||
} | ||
} | ||
|
||
private companion object { | ||
const val PRODUCTS_TIMESTAMP_KEY = "pos_products_sync_timestamp" | ||
const val VARIATIONS_TIMESTAMP_KEY = "pos_variations_sync_timestamp" | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.