Skip to content

Commit 0f7f493

Browse files
author
a.iudin
committed
UserStorageRefactoring: Passed SharedPreferencesStorage to ServiceLocator
The SharedPreferencesStorage access mechanism has been redesigned. Instead of creating local instances in different classes, a single instance provided through ServiceLocator is now used. This change centralizes dependency management, simplifies the structure of the code, and improves its testability by eliminating the direct creation of dependencies within classes.
1 parent 759eea9 commit 0f7f493

File tree

7 files changed

+57
-54
lines changed

7 files changed

+57
-54
lines changed

sdk/src/main/java/com/apphud/sdk/ApphudInternal+Attribution.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.apphud.sdk.domain.AdjustInfo
1414
import com.apphud.sdk.domain.ApphudUser
1515
import com.apphud.sdk.domain.AppsflyerInfo
1616
import com.apphud.sdk.domain.FacebookInfo
17+
import com.apphud.sdk.internal.ServiceLocator
1718
import com.apphud.sdk.internal.data.dto.AttributionRequestDto
1819
import com.apphud.sdk.internal.util.runCatchingCancellable
1920
import com.apphud.sdk.managers.RequestManager
@@ -28,7 +29,7 @@ internal fun ApphudInternal.setAttribution(
2829
) {
2930
when (provider) {
3031
APPSFLYER -> {
31-
val temporary = storage.appsflyer
32+
val temporary = ServiceLocator.instance.sharedPreferencesStorage.appsflyer
3233
when {
3334
temporary == null -> Unit
3435
(temporary.id == identifier) && (temporary.data == apphudAttributionData.rawData) -> {
@@ -38,7 +39,7 @@ internal fun ApphudInternal.setAttribution(
3839
}
3940
}
4041
FACEBOOK -> {
41-
val temporary = storage.facebook
42+
val temporary = ServiceLocator.instance.sharedPreferencesStorage.facebook
4243
when {
4344
temporary == null -> Unit
4445
temporary.data == apphudAttributionData.rawData -> {
@@ -48,13 +49,13 @@ internal fun ApphudInternal.setAttribution(
4849
}
4950
}
5051
FIREBASE -> {
51-
if (storage.firebase == identifier) {
52+
if (ServiceLocator.instance.sharedPreferencesStorage.firebase == identifier) {
5253
ApphudLog.logI("Already submitted the same Firebase attribution, skipping")
5354
return
5455
}
5556
}
5657
ADJUST -> {
57-
val temporary = storage.adjust
58+
val temporary = ServiceLocator.instance.sharedPreferencesStorage.adjust
5859
when {
5960
temporary == null -> Unit
6061
(temporary.adid == identifier) && (temporary.adjustData == apphudAttributionData.rawData) -> {
@@ -115,22 +116,23 @@ internal fun ApphudInternal.setAttribution(
115116
withContext(Dispatchers.Main) {
116117
when (provider) {
117118
APPSFLYER -> {
118-
storage.appsflyer = AppsflyerInfo(
119+
ServiceLocator.instance.sharedPreferencesStorage.appsflyer = AppsflyerInfo(
119120
id = identifier,
120121
data = apphudAttributionData.rawData,
121122
)
122123
}
123124

124125
FACEBOOK -> {
125-
storage.facebook = FacebookInfo(apphudAttributionData.rawData)
126+
ServiceLocator.instance.sharedPreferencesStorage.facebook =
127+
FacebookInfo(apphudAttributionData.rawData)
126128
}
127129

128130
FIREBASE -> {
129-
storage.firebase = identifier
131+
ServiceLocator.instance.sharedPreferencesStorage.firebase = identifier
130132
}
131133

132134
ADJUST -> {
133-
storage.adjust = AdjustInfo(
135+
ServiceLocator.instance.sharedPreferencesStorage.adjust = AdjustInfo(
134136
adid = identifier,
135137
adjustData = apphudAttributionData.rawData,
136138
)

sdk/src/main/java/com/apphud/sdk/ApphudInternal+Fallback.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.Context
44
import com.android.billingclient.api.BillingClient.BillingResponseCode
55
import com.apphud.sdk.domain.ApphudUser
66
import com.apphud.sdk.domain.FallbackJsonObject
7+
import com.apphud.sdk.internal.ServiceLocator
78
import com.apphud.sdk.mappers.PaywallsMapperLegacy
89
import com.apphud.sdk.parser.GsonParser
910
import com.apphud.sdk.parser.Parser
@@ -112,7 +113,7 @@ internal fun ApphudInternal.disableFallback() {
112113
ApphudLog.log("Fallback: reload products")
113114
loadProducts()
114115
}
115-
if (storage.isNeedSync) {
116+
if (ServiceLocator.instance.sharedPreferencesStorage.isNeedSync) {
116117
syncPurchases()
117118
}
118119
}

sdk/src/main/java/com/apphud/sdk/ApphudInternal+Purchases.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.apphud.sdk.domain.ApphudNonRenewingPurchase
88
import com.apphud.sdk.domain.ApphudProduct
99
import com.apphud.sdk.domain.ApphudSubscription
1010
import com.apphud.sdk.domain.ApphudUser
11+
import com.apphud.sdk.internal.ServiceLocator
1112
import com.apphud.sdk.internal.callback_status.PurchaseCallbackStatus
1213
import com.apphud.sdk.internal.callback_status.PurchaseUpdatedCallbackStatus
1314
import com.apphud.sdk.internal.domain.model.PurchaseContext
@@ -208,7 +209,7 @@ private fun ApphudInternal.purchaseInternal(
208209
)
209210
ApphudLog.log("Purchase Pending")
210211
callback?.invoke(ApphudPurchaseResult(null, null, purchase, error))
211-
storage.isNeedSync = true
212+
ServiceLocator.instance.sharedPreferencesStorage.isNeedSync = true
212213
}
213214
Purchase.PurchaseState.PURCHASED -> {
214215
val product = apphudProduct.productDetails
@@ -292,7 +293,7 @@ internal fun ApphudInternal.lookupFreshPurchase(extraMessage: String = "resend_f
292293
mFreshPurchase
293294
}
294295
}
295-
if ((purchaseCallbacks.isNotEmpty() && purchasingProduct != null) || storage.isNeedSync) {
296+
if ((purchaseCallbacks.isNotEmpty() && purchasingProduct != null) || ServiceLocator.instance.sharedPreferencesStorage.isNeedSync) {
296297

297298
launch(Dispatchers.Main) { apphudListener?.apphudDidReceivePurchase(purchase) }
298299

@@ -319,7 +320,7 @@ internal fun ApphudInternal.lookupFreshPurchase(extraMessage: String = "resend_f
319320
val newPurchases =
320321
customer.purchases.firstOrNull { it.productId == purchase.products.first() }
321322

322-
storage.isNeedSync = false
323+
ServiceLocator.instance.sharedPreferencesStorage.isNeedSync = false
323324
handleCheckSubmissionResult(customer, purchase, newSubscriptions, newPurchases, false)
324325
}
325326
}
@@ -402,7 +403,7 @@ internal fun ApphudInternal.handleObservedPurchase(
402403

403404
private fun ApphudInternal.processPurchaseError(status: PurchaseUpdatedCallbackStatus.Error) {
404405
if (status.result.responseCode == BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED) {
405-
storage.isNeedSync = true
406+
ServiceLocator.instance.sharedPreferencesStorage.isNeedSync = true
406407
coroutineScope.launch(errorHandler) {
407408
ApphudLog.log("ProcessPurchaseError: syncPurchases()")
408409
fetchNativePurchases(forceRefresh = true)
@@ -428,7 +429,7 @@ private suspend fun ApphudInternal.sendCheckToApphud(
428429
) {
429430
val localCurrentUser = currentUser
430431
when {
431-
localCurrentUser == null -> storage.isNeedSync = true
432+
localCurrentUser == null -> ServiceLocator.instance.sharedPreferencesStorage.isNeedSync = true
432433
fallbackMode -> {
433434
runCatchingCancellable {
434435
RequestManager.purchased(
@@ -484,7 +485,7 @@ private suspend fun ApphudInternal.sendCheckToApphud(
484485
val newPurchases =
485486
customer.purchases.firstOrNull { it.productId == purchase.products.first() }
486487

487-
storage.isNeedSync = false
488+
ServiceLocator.instance.sharedPreferencesStorage.isNeedSync = false
488489
handleCheckSubmissionResult(customer, purchase, newSubscriptions, newPurchases, false)
489490
}
490491
}
@@ -513,7 +514,7 @@ private suspend fun ApphudInternal.sendCheckToApphud(
513514
}
514515
}
515516
}
516-
storage.isNeedSync = true
517+
ServiceLocator.instance.sharedPreferencesStorage.isNeedSync = true
517518

518519
handleCheckSubmissionResult(null, purchase, null, null, false)
519520
}
@@ -552,7 +553,7 @@ internal fun ApphudInternal.addTempPurchase(
552553
// nothing
553554
}
554555
}
555-
storage.isNeedSync = true
556+
ServiceLocator.instance.sharedPreferencesStorage.isNeedSync = true
556557
handleCheckSubmissionResult(apphudUser, purchase, newSubscription, newPurchase, true)
557558
}
558559

@@ -619,7 +620,7 @@ internal suspend fun ApphudInternal.trackPurchase(
619620
ApphudLog.logE("trackPurchase: could not found purchase for product $productId")
620621
}
621622
} else {
622-
storage.isNeedSync = true
623+
ServiceLocator.instance.sharedPreferencesStorage.isNeedSync = true
623624
withContext(Dispatchers.Main) {
624625
syncPurchases(observerMode = true)
625626
}

sdk/src/main/java/com/apphud/sdk/ApphudInternal+RestorePurchases.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.android.billingclient.api.Purchase
66
import com.android.billingclient.api.PurchaseHistoryRecord
77
import com.apphud.sdk.domain.ApphudProduct
88
import com.apphud.sdk.domain.PurchaseRecordDetails
9+
import com.apphud.sdk.internal.ServiceLocator
910
import com.apphud.sdk.internal.callback_status.PurchaseHistoryCallbackStatus
1011
import com.apphud.sdk.internal.callback_status.PurchaseRestoredCallbackStatus
1112
import com.apphud.sdk.internal.util.runCatchingCancellable
@@ -101,7 +102,7 @@ internal suspend fun ApphudInternal.syncPurchases(
101102

102103
if (purchases.isEmpty()) {
103104
ApphudLog.log(message = "SyncPurchases: Nothing to restore")
104-
storage.isNeedSync = false
105+
ServiceLocator.instance.sharedPreferencesStorage.isNeedSync = false
105106
refreshEntitlements(true)
106107
val user = currentUser
107108
return if (user != null) {
@@ -146,7 +147,7 @@ internal suspend fun ApphudInternal.syncPurchases(
146147

147148
if (prevPurchases.containsAll(restoredPurchases)) {
148149
ApphudLog.log("SyncPurchases: Don't send equal purchases from prev state")
149-
storage.isNeedSync = false
150+
ServiceLocator.instance.sharedPreferencesStorage.isNeedSync = false
150151
mainScope.launch {
151152
refreshEntitlements(true)
152153
}
@@ -210,7 +211,7 @@ internal suspend fun ApphudInternal.sendPurchasesToApphud(
210211
ApphudLog.log("SyncPurchases: customer was successfully updated $customer")
211212
}
212213

213-
storage.isNeedSync = false
214+
ServiceLocator.instance.sharedPreferencesStorage.isNeedSync = false
214215
prevPurchases.addAll(tempPurchaseRecordDetails)
215216

216217
userId = customer.userId

0 commit comments

Comments
 (0)