Skip to content

Commit

Permalink
[android][splash-screen] Migrate to new modules API (expo#22827)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanjhughes authored Jun 19, 2023
1 parent cd1a5cd commit 2d8d8b6
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 81 deletions.
1 change: 1 addition & 0 deletions packages/expo-splash-screen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### 🎉 New features

- Added support for React Native 0.72. ([#22588](https://github.com/expo/expo/pull/22588) by [@kudo](https://github.com/kudo))
- Migrated Android codebase to use Expo modules API. ([#22827](https://github.com/expo/expo/pull/22827) by [@alanjhughes](https://github.com/alanjhughes))

### 🐛 Bug fixes

Expand Down
Original file line number Diff line number Diff line change
@@ -1,61 +1,42 @@
package expo.modules.splashscreen

import android.content.Context

import expo.modules.core.ExportedModule
import expo.modules.core.ModuleRegistry
import expo.modules.core.Promise
import expo.modules.core.errors.CurrentActivityNotFoundException
import expo.modules.core.interfaces.ActivityProvider
import expo.modules.core.interfaces.ExpoMethod
import expo.modules.kotlin.Promise
import expo.modules.kotlin.exception.Exceptions
import expo.modules.kotlin.modules.Module
import expo.modules.kotlin.modules.ModuleDefinition
import expo.modules.splashscreen.exceptions.HideAsyncException
import expo.modules.splashscreen.exceptions.PreventAutoHideException

// Below import must be kept unversioned even in versioned code to provide a redirection from
// versioned code realm to unversioned code realm.
// Without this import any `SplashScreen.anyMethodName(...)` invocation on JS side ends up
// in versioned SplashScreen kotlin object that stores no information about the ExperienceActivity.
import expo.modules.splashscreen.singletons.SplashScreen

class SplashScreenModule(context: Context) : ExportedModule(context) {
companion object {
private const val NAME = "ExpoSplashScreen"
private const val ERROR_TAG = "ERR_SPLASH_SCREEN"
}

private lateinit var activityProvider: ActivityProvider

override fun getName(): String {
return NAME
}
class SplashScreenModule : Module() {
override fun definition() = ModuleDefinition {
Name("ExpoSplashScreen")

override fun onCreate(moduleRegistry: ModuleRegistry) {
activityProvider = moduleRegistry.getModule(ActivityProvider::class.java)
}
AsyncFunction("preventAutoHideAsync") { promise: Promise ->
val currentActivity =
appContext.currentActivity ?: throw Exceptions.MissingActivity()

@ExpoMethod
fun preventAutoHideAsync(promise: Promise) {
val activity = activityProvider.currentActivity
if (activity == null) {
promise.reject(CurrentActivityNotFoundException())
return
SplashScreen.preventAutoHide(
currentActivity,
{ hasEffect -> promise.resolve(hasEffect) },
{ m -> promise.reject(PreventAutoHideException(m)) }
)
}
SplashScreen.preventAutoHide(
activity,
{ hasEffect -> promise.resolve(hasEffect) },
{ m -> promise.reject(ERROR_TAG, m) }
)
}

@ExpoMethod
fun hideAsync(promise: Promise) {
val activity = activityProvider.currentActivity
if (activity == null) {
promise.reject(CurrentActivityNotFoundException())
return
AsyncFunction("hideAsync") { promise: Promise ->
val currentActivity =
appContext.currentActivity ?: throw Exceptions.MissingActivity()

SplashScreen.hide(
currentActivity,
{ hasEffect -> promise.resolve(hasEffect) },
{ m -> promise.reject(HideAsyncException(m)) }
)
}
SplashScreen.hide(
activity,
{ hasEffect -> promise.resolve(hasEffect) },
{ m -> promise.reject(ERROR_TAG, m) }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ package expo.modules.splashscreen

import android.content.Context
import expo.modules.splashscreen.singletons.SplashScreen
import expo.modules.core.BasePackage
import expo.modules.core.ExportedModule
import expo.modules.core.interfaces.Package
import expo.modules.core.interfaces.ReactActivityLifecycleListener
import expo.modules.core.interfaces.SingletonModule

class SplashScreenPackage : BasePackage() {
override fun createExportedModules(context: Context): List<ExportedModule> {
return listOf(SplashScreenModule(context))
}

class SplashScreenPackage : Package {
override fun createSingletonModules(context: Context?): List<SingletonModule> {
return listOf(SplashScreen)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package expo.modules.splashscreen

import android.app.Activity
import android.os.Handler
import android.os.Looper
import android.view.View
import android.view.ViewGroup
import expo.modules.splashscreen.exceptions.NoContentViewException
Expand All @@ -17,7 +18,7 @@ open class SplashScreenViewController(
private val weakActivity = WeakReference(activity)
private val contentView: ViewGroup = activity.findViewById(android.R.id.content)
?: throw NoContentViewException()
private val handler = Handler()
private val handler = Handler(Looper.getMainLooper())

private var autoHideEnabled = true
private var splashScreenShown = false
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package expo.modules.splashscreen.exceptions

import expo.modules.kotlin.exception.CodedException

class NoContentViewException :
CodedException("ContentView is not yet available. Call 'SplashScreen.show(...)' once 'setContentView()' is called.")

class PreventAutoHideException(message: String) :
CodedException(message)

class HideAsyncException(message: String) :
CodedException(message)
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package expo.modules.splashscreen.singletons

import android.annotation.SuppressLint
import android.app.Activity
import android.os.Build
import androidx.core.view.ViewCompat

object SplashScreenStatusBar {
fun configureTranslucent(activity: Activity, translucent: Boolean?) {
@SuppressLint("ObsoleteSdkInt")
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return
}

translucent?.let {
activity.runOnUiThread {
// If the status bar is translucent hook into the window insets calculations
Expand Down
2 changes: 1 addition & 1 deletion packages/expo-splash-screen/build/ExpoSplashScreen.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/expo-splash-screen/build/ExpoSplashScreen.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/expo-splash-screen/build/ExpoSplashScreen.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/expo-splash-screen/expo-module.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "expo-splash-screen",
"platforms": ["ios", "android"],
"android": {
"modules": ["expo.modules.splashscreen.SplashScreenModule"]
}
}
5 changes: 2 additions & 3 deletions packages/expo-splash-screen/src/ExpoSplashScreen.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
import { NativeModulesProxy } from 'expo-modules-core';

export default NativeModulesProxy.ExpoSplashScreen || {};
import { requireNativeModule } from 'expo-modules-core';
export default requireNativeModule('ExpoSplashScreen') || {};
4 changes: 0 additions & 4 deletions packages/expo-splash-screen/unimodule.json

This file was deleted.

0 comments on commit 2d8d8b6

Please sign in to comment.