Skip to content

Commit

Permalink
Convert DefaultDevLoadingView.java->.kt (#45729)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #45729

# Changelog:
[Internal] -

As in the title.

Reviewed By: tdn120

Differential Revision: D60286594

fbshipit-source-id: 91582e1ba01415544cd75cdd46f9cadcacd6d89d
  • Loading branch information
rshest authored and facebook-github-bot committed Jul 28, 2024
1 parent 329ab64 commit 7dec080
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 158 deletions.
8 changes: 6 additions & 2 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -2049,14 +2049,18 @@ public class com/facebook/react/devsupport/BundleDownloader$BundleInfo {
public fun toJSONString ()Ljava/lang/String;
}

public class com/facebook/react/devsupport/DefaultDevLoadingViewImplementation : com/facebook/react/devsupport/interfaces/DevLoadingViewManager {
public final class com/facebook/react/devsupport/DefaultDevLoadingViewImplementation : com/facebook/react/devsupport/interfaces/DevLoadingViewManager {
public static final field Companion Lcom/facebook/react/devsupport/DefaultDevLoadingViewImplementation$Companion;
public fun <init> (Lcom/facebook/react/devsupport/ReactInstanceDevHelper;)V
public fun hide ()V
public static fun setDevLoadingEnabled (Z)V
public fun showMessage (Ljava/lang/String;)V
public fun updateProgress (Ljava/lang/String;Ljava/lang/Integer;Ljava/lang/Integer;)V
}

public final class com/facebook/react/devsupport/DefaultDevLoadingViewImplementation$Companion {
public final fun setDevLoadingEnabled (Z)V
}

public final class com/facebook/react/devsupport/DefaultDevSupportManagerFactory : com/facebook/react/devsupport/DevSupportManagerFactory {
public fun <init> ()V
public final fun create (Landroid/content/Context;Lcom/facebook/react/devsupport/ReactInstanceDevHelper;Ljava/lang/String;ZI)Lcom/facebook/react/devsupport/interfaces/DevSupportManager;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.devsupport

import android.content.Context
import android.graphics.Rect
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.PopupWindow
import android.widget.TextView
import com.facebook.common.logging.FLog
import com.facebook.react.R
import com.facebook.react.bridge.UiThreadUtil
import com.facebook.react.common.ReactConstants
import com.facebook.react.devsupport.interfaces.DevLoadingViewManager
import java.util.Locale

/**
* Default implementation of Dev Loading View Manager to display loading messages on top of the
* screen. All methods are thread safe.
*/
public class DefaultDevLoadingViewImplementation(
private val reactInstanceDevHelper: ReactInstanceDevHelper
) : DevLoadingViewManager {
private var devLoadingView: TextView? = null
private var devLoadingPopup: PopupWindow? = null

override fun showMessage(message: String) {
if (!isEnabled) {
return
}
UiThreadUtil.runOnUiThread { showInternal(message) }
}

override fun updateProgress(status: String?, done: Int?, total: Int?) {
if (!isEnabled) {
return
}
UiThreadUtil.runOnUiThread {
val percentage =
if (done != null && total != null && total > 0)
String.format(Locale.getDefault(), " %.1f%%", done.toFloat() / total * 100)
else ""
devLoadingView?.text =
"${status ?: "Loading"}${percentage}\u2026" // `...` character at the end
}
}

public override fun hide() {
if (isEnabled) {
UiThreadUtil.runOnUiThread { hideInternal() }
}
}

private fun showInternal(message: String) {
if (devLoadingPopup?.isShowing == true) {
// already showing
return
}
val currentActivity = reactInstanceDevHelper.currentActivity
if (currentActivity == null) {
FLog.e(
ReactConstants.TAG,
"Unable to display loading message because react " + "activity isn't available")
return
}

// PopupWindow#showAtLocation uses absolute screen position. In order for
// loading view to be placed below status bar (if the status bar is present) we need to pass
// an appropriate Y offset.
try {
val rectangle = Rect()
currentActivity.window.decorView.getWindowVisibleDisplayFrame(rectangle)
val topOffset = rectangle.top
val inflater =
currentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view = inflater.inflate(R.layout.dev_loading_view, null) as TextView
view.text = message
val popup =
PopupWindow(
view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
popup.isTouchable = false
popup.showAtLocation(currentActivity.window.decorView, Gravity.NO_GRAVITY, 0, topOffset)
devLoadingView = view
devLoadingPopup = popup
// TODO T164786028: Find out the root cause of the BadTokenException exception here
} catch (e: WindowManager.BadTokenException) {
FLog.e(
ReactConstants.TAG,
"Unable to display loading message because react activity isn't active, message: $message")
}
}

private fun hideInternal() {
val popup = devLoadingPopup ?: return
if (popup.isShowing == true) {
popup.dismiss()
devLoadingPopup = null
devLoadingView = null
}
}

private val context: Context?
get() = reactInstanceDevHelper.currentActivity

public companion object {
private var isEnabled = true

public fun setDevLoadingEnabled(enabled: Boolean) {
isEnabled = enabled
}
}
}

0 comments on commit 7dec080

Please sign in to comment.