Skip to content

Commit d0d5671

Browse files
authored
Convert BraintreeSharedPreferences to Kotlin (#1384)
* Convert BraintreeSharedPreferences to Kotlin * Clean up single line return functions
1 parent 9813a1b commit d0d5671

File tree

7 files changed

+70
-95
lines changed

7 files changed

+70
-95
lines changed

BraintreeCore/src/main/java/com/braintreepayments/api/core/ConfigurationCache.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ internal class ConfigurationCache(
2323
return null
2424
}
2525

26-
fun saveConfiguration(configuration: Configuration, cacheKey: String?) {
26+
fun saveConfiguration(configuration: Configuration, cacheKey: String) {
2727
saveConfiguration(configuration, cacheKey, System.currentTimeMillis())
2828
}
2929

3030
fun saveConfiguration(
3131
configuration: Configuration,
32-
cacheKey: String?,
32+
cacheKey: String,
3333
currentTimeMillis: Long
3434
) {
3535
val timestampKey = "${cacheKey}_timestamp"

BraintreeCore/src/main/java/com/braintreepayments/api/core/UUIDHelper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class UUIDHelper {
1414
val formattedUUID: String
1515
get() = UUID.randomUUID().toString().replace("-", "")
1616

17-
fun getInstallationGUID(context: Context?): String {
17+
fun getInstallationGUID(context: Context): String {
1818
return getInstallationGUID(BraintreeSharedPreferences.getInstance(context))
1919
}
2020

DataCollector/src/main/java/com/braintreepayments/api/datacollector/DataCollector.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class DataCollector @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) constructor(
3535
* @suppress
3636
*/
3737
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
38-
fun getPayPalInstallationGUID(context: Context?): String {
38+
fun getPayPalInstallationGUID(context: Context): String {
3939
return uuidHelper.getInstallationGUID(context)
4040
}
4141

@@ -45,7 +45,7 @@ class DataCollector @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) constructor(
4545
@MainThread
4646
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
4747
fun getClientMetadataId(
48-
context: Context?,
48+
context: Context,
4949
configuration: Configuration?,
5050
hasUserLocationConsent: Boolean
5151
): String {

SharedUtils/src/main/java/com/braintreepayments/api/sharedutils/BraintreeSharedPreferences.java

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.braintreepayments.api.sharedutils
2+
3+
import android.content.Context
4+
import android.content.SharedPreferences
5+
import androidx.annotation.RestrictTo
6+
7+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
8+
class BraintreeSharedPreferences(
9+
private val sharedPreferences: SharedPreferences
10+
) {
11+
12+
companion object {
13+
private const val PREFERENCES_FILE_KEY = "com.braintreepayments.api.SHARED_PREFERENCES"
14+
15+
@Volatile
16+
private var INSTANCE: BraintreeSharedPreferences? = null
17+
18+
@JvmStatic
19+
fun getInstance(context: Context): BraintreeSharedPreferences {
20+
return INSTANCE ?: synchronized(this) {
21+
INSTANCE ?: BraintreeSharedPreferences(createSharedPreferencesInstance(context))
22+
.also { INSTANCE = it }
23+
}
24+
}
25+
26+
private fun createSharedPreferencesInstance(context: Context): SharedPreferences {
27+
return context.getSharedPreferences(PREFERENCES_FILE_KEY, Context.MODE_PRIVATE)
28+
}
29+
}
30+
31+
fun getString(key: String, fallback: String?): String? = sharedPreferences.getString(key, fallback)
32+
33+
fun putString(key: String, value: String) {
34+
sharedPreferences.edit().putString(key, value).apply()
35+
}
36+
37+
fun getBoolean(key: String): Boolean = sharedPreferences.getBoolean(key, false)
38+
39+
fun putBoolean(key: String, value: Boolean) {
40+
sharedPreferences.edit().putBoolean(key, value).apply()
41+
}
42+
43+
fun containsKey(key: String): Boolean = sharedPreferences.contains(key)
44+
45+
fun getLong(key: String): Long = sharedPreferences.getLong(key, 0)
46+
47+
fun putStringAndLong(
48+
stringKey: String,
49+
stringValue: String,
50+
longKey: String,
51+
longValue: Long
52+
) {
53+
sharedPreferences
54+
.edit()
55+
.putString(stringKey, stringValue)
56+
.putLong(longKey, longValue)
57+
.apply()
58+
}
59+
60+
fun clearSharedPreferences() {
61+
sharedPreferences.edit().clear().apply()
62+
}
63+
}

TestUtils/src/main/java/com/braintreepayments/api/testutils/SharedPreferencesHelper.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,4 @@ public static void overrideConfigurationCache(Context context, Authorization aut
3535
.putStringAndLong(cacheKey, configuration.toJson(), timestampKey,
3636
System.currentTimeMillis());
3737
}
38-
39-
public static void clearConfigurationCacheOverride(Context context) {
40-
BraintreeSharedPreferences.getInstance(context).clearSharedPreferences();
41-
}
4238
}

Venmo/src/main/java/com/braintreepayments/api/venmo/VenmoSharedPrefsWriter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import android.content.Context
44
import com.braintreepayments.api.sharedutils.BraintreeSharedPreferences
55

66
internal class VenmoSharedPrefsWriter {
7-
fun persistVenmoVaultOption(context: Context?, shouldVault: Boolean) {
7+
fun persistVenmoVaultOption(context: Context, shouldVault: Boolean) {
88
persistVenmoVaultOption(BraintreeSharedPreferences.getInstance(context), shouldVault)
99
}
1010

@@ -15,7 +15,7 @@ internal class VenmoSharedPrefsWriter {
1515
braintreeSharedPreferences.putBoolean(VAULT_VENMO_KEY, shouldVault)
1616
}
1717

18-
fun getVenmoVaultOption(context: Context?): Boolean {
18+
fun getVenmoVaultOption(context: Context): Boolean {
1919
return getVenmoVaultOption(BraintreeSharedPreferences.getInstance(context))
2020
}
2121

0 commit comments

Comments
 (0)