-
Notifications
You must be signed in to change notification settings - Fork 628
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store encoded request to disk when using WorkManager
- Loading branch information
1 parent
2f39506
commit ec39c34
Showing
7 changed files
with
127 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
stripe-core/src/main/java/com/stripe/android/core/networking/AnalyticsRequestV2Storage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.stripe.android.core.networking | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import androidx.annotation.RestrictTo | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import java.util.UUID | ||
import javax.inject.Inject | ||
|
||
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) | ||
interface AnalyticsRequestV2Storage { | ||
suspend fun store(request: AnalyticsRequestV2): String | ||
suspend fun retrieve(id: String): AnalyticsRequestV2? | ||
suspend fun delete(id: String) | ||
} | ||
|
||
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) | ||
class RealAnalyticsRequestV2Storage private constructor( | ||
private val sharedPrefs: SharedPreferences, | ||
private val dispatcher: CoroutineDispatcher = Dispatchers.IO, | ||
) : AnalyticsRequestV2Storage { | ||
|
||
@Inject constructor(application: Application) : this( | ||
sharedPrefs = application.getSharedPreferences( | ||
"StripeAnalyticsRequestV2Storage", | ||
Context.MODE_PRIVATE | ||
), | ||
) | ||
|
||
override suspend fun store(request: AnalyticsRequestV2): String = withContext(dispatcher) { | ||
val id = UUID.randomUUID().toString() | ||
val encodedRequest = Json.encodeToString(request) | ||
sharedPrefs | ||
.edit() | ||
.putString(id, encodedRequest) | ||
.apply() | ||
id | ||
} | ||
|
||
override suspend fun retrieve(id: String): AnalyticsRequestV2? = withContext(dispatcher) { | ||
val encodedRequest = sharedPrefs.getString(id, null) ?: return@withContext null | ||
|
||
sharedPrefs.edit().remove(id).apply() | ||
|
||
runCatching<AnalyticsRequestV2> { | ||
Json.decodeFromString(encodedRequest) | ||
}.getOrNull() | ||
} | ||
|
||
override suspend fun delete(id: String) = withContext(dispatcher) { | ||
sharedPrefs.edit().remove(id).apply() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
stripe-core/src/test/java/com/stripe/android/core/utils/FakeAnalyticsRequestV2Storage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.stripe.android.core.utils | ||
|
||
import com.stripe.android.core.networking.AnalyticsRequestV2 | ||
import com.stripe.android.core.networking.AnalyticsRequestV2Storage | ||
import java.util.UUID | ||
|
||
internal class FakeAnalyticsRequestV2Storage : AnalyticsRequestV2Storage { | ||
|
||
private val store = mutableMapOf<String, AnalyticsRequestV2>() | ||
|
||
override suspend fun store(request: AnalyticsRequestV2): String { | ||
val id = UUID.randomUUID().toString() | ||
store[id] = request | ||
return id | ||
} | ||
|
||
override suspend fun retrieve(id: String): AnalyticsRequestV2? { | ||
return store[id] | ||
} | ||
|
||
override suspend fun delete(id: String) { | ||
store.remove(id) | ||
} | ||
} |