Skip to content

Commit

Permalink
Rename WEAK_INSTANCE -> weakInstance and run linter again
Browse files Browse the repository at this point in the history
  • Loading branch information
kkafar committed Jun 19, 2024
1 parent da33b62 commit 4c0a170
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 45 deletions.
8 changes: 4 additions & 4 deletions android/src/main/java/com/swmansion/rnscreens/Screen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Screen(

val headerHeight = calculateHeaderHeight()
val totalHeight =
headerHeight.first + headerHeight.second // action bar height + status bar height
headerHeight.first + headerHeight.second // action bar height + status bar height
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
updateScreenSizeFabric(width, height, totalHeight)
} else {
Expand Down Expand Up @@ -192,7 +192,7 @@ class Screen(
ScreenWindowTraits.setStyle(
this,
it.tryGetActivity(),
it.tryGetContext()
it.tryGetContext(),
)
}
}
Expand Down Expand Up @@ -231,7 +231,7 @@ class Screen(
ScreenWindowTraits.setColor(
this,
it.tryGetActivity(),
it.tryGetContext()
it.tryGetContext(),
)
}
}
Expand All @@ -245,7 +245,7 @@ class Screen(
fragmentWrapper?.let {
ScreenWindowTraits.setNavigationBarColor(
this,
it.tryGetActivity()
it.tryGetActivity(),
)
}
}
Expand Down
3 changes: 2 additions & 1 deletion android/src/main/java/com/swmansion/rnscreens/ScreenStack.kt
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ class ScreenStack(
goingForward = shouldUseOpenAnimation

if (shouldUseOpenAnimation &&
newTop != null && needsDrawReordering(newTop) &&
newTop != null &&
needsDrawReordering(newTop) &&
visibleBottom == null
) {
// When using an open animation in which two screens overlap (eg. fade_from_bottom or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import java.lang.ref.WeakReference
* See https://github.com/software-mansion/react-native-screens/pull/2169
* for more detailed description of the issue this code solves.
*/
internal class ScreenDummyLayoutHelper(reactContext: ReactApplicationContext) {
internal class ScreenDummyLayoutHelper(
reactContext: ReactApplicationContext,
) {
// The state required to compute header dimensions. We want this on instance rather than on class
// for context access & being tied to instance lifetime.
private lateinit var coordinatorLayout: CoordinatorLayout
Expand All @@ -33,7 +35,8 @@ internal class ScreenDummyLayoutHelper(reactContext: ReactApplicationContext) {
// We do not want to be responsible for the context lifecycle. If it's null, we're fine.
// This same context is being passed down to our view components so it is destroyed
// only if our views also are.
private var reactContextRef: WeakReference<ReactApplicationContext> = WeakReference(reactContext)
private var reactContextRef: WeakReference<ReactApplicationContext> =
WeakReference(reactContext)

init {

Expand All @@ -46,7 +49,7 @@ internal class ScreenDummyLayoutHelper(reactContext: ReactApplicationContext) {
Log.w(TAG, "Failed to load $LIBRARY_NAME")
}

WEAK_INSTANCE = WeakReference(this)
weakInstance = WeakReference(this)
ensureDummyLayoutWithHeader(reactContext)
}

Expand All @@ -66,33 +69,40 @@ internal class ScreenDummyLayoutHelper(reactContext: ReactApplicationContext) {

coordinatorLayout = CoordinatorLayout(contextWithTheme)

appBarLayout = AppBarLayout(contextWithTheme).apply {
layoutParams = CoordinatorLayout.LayoutParams(
CoordinatorLayout.LayoutParams.MATCH_PARENT,
CoordinatorLayout.LayoutParams.WRAP_CONTENT,
)
}

toolbar = Toolbar(contextWithTheme).apply {
title = DEFAULT_HEADER_TITLE
layoutParams = AppBarLayout.LayoutParams(
AppBarLayout.LayoutParams.MATCH_PARENT,
AppBarLayout.LayoutParams.WRAP_CONTENT
).apply { scrollFlags = 0 }
}
appBarLayout =
AppBarLayout(contextWithTheme).apply {
layoutParams =
CoordinatorLayout.LayoutParams(
CoordinatorLayout.LayoutParams.MATCH_PARENT,
CoordinatorLayout.LayoutParams.WRAP_CONTENT,
)
}

toolbar =
Toolbar(contextWithTheme).apply {
title = DEFAULT_HEADER_TITLE
layoutParams =
AppBarLayout
.LayoutParams(
AppBarLayout.LayoutParams.MATCH_PARENT,
AppBarLayout.LayoutParams.WRAP_CONTENT,
).apply { scrollFlags = 0 }
}

// We know the title text view will be there, cause we've just set title.
defaultFontSize = ScreenStackHeaderConfig.findTitleTextViewInToolbar(toolbar)!!.textSize
defaultContentInsetStartWithNavigation = toolbar.contentInsetStartWithNavigation

appBarLayout.addView(toolbar)

dummyContentView = View(contextWithTheme).apply {
layoutParams = CoordinatorLayout.LayoutParams(
CoordinatorLayout.LayoutParams.MATCH_PARENT,
CoordinatorLayout.LayoutParams.MATCH_PARENT
)
}
dummyContentView =
View(contextWithTheme).apply {
layoutParams =
CoordinatorLayout.LayoutParams(
CoordinatorLayout.LayoutParams.MATCH_PARENT,
CoordinatorLayout.LayoutParams.MATCH_PARENT,
)
}

coordinatorLayout.apply {
addView(appBarLayout)
Expand All @@ -107,9 +117,15 @@ internal class ScreenDummyLayoutHelper(reactContext: ReactApplicationContext) {
* @param fontSize font size value as passed from JS
* @return header height in dp as consumed by Yoga
*/
private fun computeDummyLayout(fontSize: Int, isTitleEmpty: Boolean): Float {
private fun computeDummyLayout(
fontSize: Int,
isTitleEmpty: Boolean,
): Float {
if (!::coordinatorLayout.isInitialized) {
Log.e(TAG, "[RNScreens] Attempt to access dummy view hierarchy before it is initialized")
Log.e(
TAG,
"[RNScreens] Attempt to access dummy view hierarchy before it is initialized",
)
return 0.0f
}

Expand All @@ -124,8 +140,10 @@ internal class ScreenDummyLayoutHelper(reactContext: ReactApplicationContext) {
val decorViewWidth = topLevelDecorView.width
val decorViewHeight = topLevelDecorView.height

val widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(decorViewWidth, View.MeasureSpec.EXACTLY)
val heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(decorViewHeight, View.MeasureSpec.EXACTLY)
val widthMeasureSpec =
View.MeasureSpec.makeMeasureSpec(decorViewWidth, View.MeasureSpec.EXACTLY)
val heightMeasureSpec =
View.MeasureSpec.makeMeasureSpec(decorViewHeight, View.MeasureSpec.EXACTLY)

if (isTitleEmpty) {
toolbar.title = ""
Expand All @@ -136,7 +154,8 @@ internal class ScreenDummyLayoutHelper(reactContext: ReactApplicationContext) {
}

val textView = ScreenStackHeaderConfig.findTitleTextViewInToolbar(toolbar)
textView?.textSize = if (fontSize != FONT_SIZE_UNSET) fontSize.toFloat() else defaultFontSize
textView?.textSize =
if (fontSize != FONT_SIZE_UNSET) fontSize.toFloat() else defaultFontSize

coordinatorLayout.measure(widthMeasureSpec, heightMeasureSpec)

Expand All @@ -149,13 +168,15 @@ internal class ScreenDummyLayoutHelper(reactContext: ReactApplicationContext) {
return headerHeight
}

private fun requireReactContext(): ReactApplicationContext {
return requireNotNull(reactContextRef.get()) { "[RNScreens] Attempt to require missing react context" }
}
private fun requireReactContext(): ReactApplicationContext =
requireNotNull(reactContextRef.get()) {
"[RNScreens] Attempt to require missing react context"
}

private fun requireActivity(): Activity {
return requireNotNull(requireReactContext().currentActivity) { "[RNScreens] Attempt to use context detached from activity" }
}
private fun requireActivity(): Activity =
requireNotNull(requireReactContext().currentActivity) {
"[RNScreens] Attempt to use context detached from activity"
}

companion object {
const val TAG = "ScreenDummyLayoutHelper"
Expand All @@ -169,18 +190,22 @@ internal class ScreenDummyLayoutHelper(reactContext: ReactApplicationContext) {
// We access this field from C++ layer, through `getInstance` method.
// We don't care what instance we get access to as long as it has initialized
// dummy view hierarchy.
private var WEAK_INSTANCE = WeakReference<ScreenDummyLayoutHelper>(null)
private var weakInstance = WeakReference<ScreenDummyLayoutHelper>(null)

@JvmStatic
fun getInstance(): ScreenDummyLayoutHelper? {
return WEAK_INSTANCE.get()
}
fun getInstance(): ScreenDummyLayoutHelper? = weakInstance.get()
}
}

private data class CacheKey(val fontSize: Int, val isTitleEmpty: Boolean)
private data class CacheKey(
val fontSize: Int,
val isTitleEmpty: Boolean,
)

private class CacheEntry(val cacheKey: CacheKey, val headerHeight: Float) {
private class CacheEntry(
val cacheKey: CacheKey,
val headerHeight: Float,
) {
fun hasKey(key: CacheKey) = cacheKey.fontSize != Int.MIN_VALUE && cacheKey == key

companion object {
Expand Down

0 comments on commit 4c0a170

Please sign in to comment.