Skip to content

Commit

Permalink
fix(web-storage): migrate away from deprecated ReactModuleInfo cons…
Browse files Browse the repository at this point in the history
…tructor (#3434)
  • Loading branch information
tido64 authored Dec 2, 2024
1 parent 3b43370 commit c88c843
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 57 deletions.
7 changes: 7 additions & 0 deletions .changeset/spicy-icons-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@react-native-webapis/battery-status": patch
"@react-native-webapis/web-storage": patch
"@rnx-kit/react-native-test-app-msal": patch
---

Migrate away from deprecated `ReactModuleInfo` constructor
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReactModuleWithSpec

class BatteryStatusModule(context: ReactApplicationContext?) :
ReactContextBaseJavaModule(context), ReactModuleWithSpec {
ReactContextBaseJavaModule(context),
ReactModuleWithSpec {

companion object {
const val NAME = "RNWBatteryStatus"
Expand All @@ -34,14 +35,11 @@ class BatteryStatusModule(context: ReactApplicationContext?) :
}
}

fun BatteryManager.getBatteryLevel(): Double {
return getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY) / 100.0
}
fun BatteryManager.getBatteryLevel(): Double =
getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY) / 100.0

fun BatteryManager.getChargingTime(): Int {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
(computeChargeTimeRemaining() / 1000).toInt()
} else {
-1
}
fun BatteryManager.getChargingTime(): Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
(computeChargeTimeRemaining() / 1000).toInt()
} else {
-1
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ class BatteryStatusPackage : BaseReactPackage() {
else -> throw IllegalArgumentException("No module named '$name'")
}

override fun getReactModuleInfoProvider(): ReactModuleInfoProvider = ReactModuleInfoProvider {
override fun getReactModuleInfoProvider() = ReactModuleInfoProvider {
val info = ReactModuleInfo(
BatteryStatusModule.NAME,
BatteryStatusModule::class.java.name,
false,
false,
false,
false,
false
)
mapOf(info.name() to info).toMutableMap()
mapOf(BatteryStatusModule.NAME to info).toMutableMap()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ class WebStoragePackage : BaseReactPackage() {
else -> throw IllegalArgumentException("No module named '$name'")
}

override fun getReactModuleInfoProvider(): ReactModuleInfoProvider = ReactModuleInfoProvider {
override fun getReactModuleInfoProvider() = ReactModuleInfoProvider {
val info = ReactModuleInfo(
WebStorageModule.NAME,
WebStorageModule::class.java.name,
false,
false,
false,
false,
WebStorageModule.IS_TURBO_MODULE
)
mapOf(info.name() to info).toMutableMap()
mapOf(WebStorageModule.NAME to info).toMutableMap()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,15 @@ class WebStorageModule(context: ReactApplicationContext) : NativeWebStorageSpec(
MODE_PRIVATE
)

override fun length(): Double {
return sharedPreferences.all.size.toDouble()
}
override fun length(): Double = sharedPreferences.all.size.toDouble()

override fun key(index: Double): String? {
// The order of the elements in `SharedPreferences` is not defined.
// https://developer.android.com/reference/android/content/SharedPreferences#getAll()
return null
}

override fun getItem(key: String): String? {
return sharedPreferences.getString(key, null)
}
override fun getItem(key: String): String? = sharedPreferences.getString(key, null)

override fun setItem(key: String, value: String): Boolean {
sharedPreferences.edit { putString(key, value) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ class WebStorageModule(context: ReactApplicationContext) : ReactContextBaseJavaM
override fun getName(): String = NAME

@ReactMethod(isBlockingSynchronousMethod = true)
fun length(): Int {
return sharedPreferences.all.size
}
fun length(): Int = sharedPreferences.all.size

@ReactMethod(isBlockingSynchronousMethod = true)
fun key(@Suppress("UNUSED_PARAMETER") index: Int): String? {
Expand All @@ -35,9 +33,7 @@ class WebStorageModule(context: ReactApplicationContext) : ReactContextBaseJavaM
}

@ReactMethod(isBlockingSynchronousMethod = true)
fun getItem(key: String): String? {
return sharedPreferences.getString(key, null)
}
fun getItem(key: String): String? = sharedPreferences.getString(key, null)

@ReactMethod(isBlockingSynchronousMethod = true)
fun setItem(key: String, value: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MsalPackage : BaseReactPackage() {
}
}

override fun getReactModuleInfoProvider(): ReactModuleInfoProvider = ReactModuleInfoProvider {
override fun getReactModuleInfoProvider() = ReactModuleInfoProvider {
val info = ReactNativeAuthModuleProvider.info()
if (info == null) {
mapOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ enum class AccountType(val type: String) {
ORGANIZATIONAL("Organizational");

companion object {
fun from(string: String): AccountType {
return when (string) {
"MicrosoftAccount" -> MICROSOFT_ACCOUNT
"Organizational" -> ORGANIZATIONAL
else -> INVALID
}
fun from(string: String): AccountType = when (string) {
"MicrosoftAccount" -> MICROSOFT_ACCOUNT
"Organizational" -> ORGANIZATIONAL
else -> INVALID
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ data class AuthError(
)
}

fun toWritableMap(): WritableMap {
return Arguments.createMap().also { map ->
map.putString("type", type.type)
map.putString("correlationId", correlationId)
message?.let {
map.putString("message", it)
}
fun toWritableMap(): WritableMap = Arguments.createMap().also { map ->
map.putString("type", type.type)
map.putString("correlationId", correlationId)
message?.let {
map.putString("message", it)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ data class AuthResult(
val expirationTime: Int,
val redirectUri: String? = null
) {
fun toWritableMap(): WritableMap {
return Arguments.createMap().also { map ->
map.putString("accessToken", accessToken)
map.putInt("expirationTime", expirationTime)
redirectUri?.let {
map.putString("redirectUri", redirectUri)
}
fun toWritableMap(): WritableMap = Arguments.createMap().also { map ->
map.putString("accessToken", accessToken)
map.putInt("expirationTime", expirationTime)
redirectUri?.let {
map.putString("redirectUri", redirectUri)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ typealias OnTokenAcquired = (result: AuthResult?, error: AuthError?) -> Unit
* ```
*/
abstract class ReactNativeAuthModule(context: ReactApplicationContext?) :
ReactContextBaseJavaModule(context), ReactModuleWithSpec {
ReactContextBaseJavaModule(context),
ReactModuleWithSpec {

companion object {
const val NAME = "RNXAuth"
Expand All @@ -43,7 +44,6 @@ abstract class ReactNativeAuthModule(context: ReactApplicationContext?) :
false,
false,
false,
false,
false
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ import com.facebook.react.uimanager.ViewManager
* Dummy `ReactPackage` implementation used solely for auto-linking purposes.
*/
class ReactNativeAuthPackage : ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
return emptyList()
}
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> =
emptyList()

override fun createViewManagers(
reactContext: ReactApplicationContext
): List<ViewManager<*, *>> {
return emptyList()
}
): List<ViewManager<*, *>> = emptyList()
}

0 comments on commit c88c843

Please sign in to comment.