Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Android): Add focused states on page transitions #1894

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package com.swmansion.rnscreens

import android.annotation.SuppressLint
import android.app.Activity
import android.app.UiModeManager
import android.content.Context
import android.content.res.Configuration
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewParent
import android.widget.FrameLayout
import androidx.core.content.getSystemService
import androidx.fragment.app.Fragment
import com.facebook.react.bridge.ReactContext
import com.facebook.react.bridge.UiThreadUtil
Expand Down Expand Up @@ -155,6 +158,10 @@ open class ScreenFragment : Fragment, ScreenFragmentWrapper {
override val childScreenContainers: List<ScreenContainer>
get() = mChildScreenContainers

fun isTelevision(): Boolean {
return context?.getSystemService<UiModeManager>()?.currentModeType == Configuration.UI_MODE_TYPE_TELEVISION
}

tboba marked this conversation as resolved.
Show resolved Hide resolved
override fun canDispatchLifecycleEvent(event: ScreenLifecycleEvent): Boolean = when (event) {
ScreenLifecycleEvent.WillAppear -> canDispatchWillAppear
ScreenLifecycleEvent.Appear -> canDispatchAppear
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class ScreenStackFragment : ScreenFragment, ScreenStackFragmentWrapper {
private var mShadowHidden = false
private var mIsTranslucent = false

private var mLastFocusedChild: View? = null

var searchView: CustomSearchView? = null
var onSearchViewCreate: ((searchView: CustomSearchView) -> Unit)? = null

Expand Down Expand Up @@ -89,6 +91,11 @@ class ScreenStackFragment : ScreenFragment, ScreenStackFragmentWrapper {
}
}

override fun onStart() {
mLastFocusedChild?.requestFocus()
super.onStart()
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand Down Expand Up @@ -123,6 +130,11 @@ class ScreenStackFragment : ScreenFragment, ScreenStackFragmentWrapper {
return view
}

override fun onStop() {
if (isTelevision()) mLastFocusedChild = findLastFocusedChild()
super.onStop()
}

override fun onPrepareOptionsMenu(menu: Menu) {
updateToolbarMenu(menu)
return super.onPrepareOptionsMenu(menu)
Expand Down Expand Up @@ -163,6 +175,16 @@ class ScreenStackFragment : ScreenFragment, ScreenStackFragmentWrapper {
}
}

private fun findLastFocusedChild(): View? {
var view: View? = screen
while (view != null) {
if (view.isFocused) return view
view = if (view is ViewGroup) view.focusedChild else null
}

return null
}

override fun canNavigateBack(): Boolean {
val container: ScreenContainer? = screen.container
check(container is ScreenStack) { "ScreenStackFragment added into a non-stack container" }
Expand Down