-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RNKC-045] - status bar management new API (#30)
* [RNKC-045] - fixed a bug with frozen animated values when user manages status bar * [RNKC-045] - ktlint fixes * [RNKC-045] - attempt to fix CI * [RNKC-045] - added example app for StatusBar * [RNKC-045] - resolve last TODOs
- Loading branch information
1 parent
7c8cb84
commit 684a110
Showing
16 changed files
with
276 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
android/src/main/java/com/reactnativekeyboardcontroller/StatusBarManagerCompatModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.reactnativekeyboardcontroller | ||
|
||
import android.animation.ArgbEvaluator | ||
import android.animation.ValueAnimator | ||
import android.os.Build | ||
import androidx.annotation.RequiresApi | ||
import androidx.core.view.WindowInsetsCompat | ||
import androidx.core.view.WindowInsetsControllerCompat | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule | ||
import com.facebook.react.bridge.ReactMethod | ||
import com.facebook.react.bridge.UiThreadUtil | ||
|
||
class StatusBarManagerCompatModule(private val mReactContext: ReactApplicationContext) : ReactContextBaseJavaModule(mReactContext) { | ||
private var controller: WindowInsetsControllerCompat? = null | ||
|
||
override fun getName(): String = "StatusBarManagerCompat" | ||
|
||
@ReactMethod | ||
private fun setHidden(hidden: Boolean) { | ||
UiThreadUtil.runOnUiThread { | ||
if (hidden) { | ||
getController()?.hide(WindowInsetsCompat.Type.statusBars()) | ||
} else { | ||
getController()?.show(WindowInsetsCompat.Type.statusBars()) | ||
} | ||
} | ||
} | ||
|
||
@RequiresApi(Build.VERSION_CODES.LOLLIPOP) | ||
@ReactMethod | ||
private fun setColor(color: Int, animated: Boolean) { | ||
UiThreadUtil.runOnUiThread { | ||
val window = mReactContext.currentActivity!!.window | ||
|
||
if (animated) { | ||
val curColor: Int = window.statusBarColor | ||
val colorAnimation = ValueAnimator.ofObject(ArgbEvaluator(), curColor, color) | ||
colorAnimation.addUpdateListener { animator -> | ||
window.statusBarColor = animator.animatedValue as Int | ||
} | ||
colorAnimation.setDuration(300).startDelay = 0 | ||
colorAnimation.start() | ||
} else { | ||
window.statusBarColor = color | ||
} | ||
} | ||
} | ||
|
||
@ReactMethod | ||
private fun setTranslucent(translucent: Boolean) { | ||
// the status bar is translucent by default (once you wrapped App in Provider, | ||
// and EdgeToEdgeReactViewGroup has been mounted and called | ||
// `setDecorFitsSystemWindows(window, false)`. By default this library applies default padding | ||
// which equal to StatusBar height, so it will have a default RN app behavior. Though once you | ||
// need to set StatusBar as translucent, you will need to use `statusBarTranslucent` prop on | ||
// `KeyboardProvider` (it will preventing of applying additional padding, and status bar will be | ||
// translucent. Though it's important to note, that this value is not reactive (i. e. if you change | ||
// `statusBarTranslucent` in runtime it will not have any effect. Just theoretically I could make | ||
// it reactive, but I know, that most of apps or don't use StatusBar translucency at all or they are | ||
// specifying it for entire app, so I don't see a lot of sense to make it reactive as of now. If your | ||
// app requires to dynamically manage it - just shoot an issue and I will try to add a support fot that. | ||
} | ||
|
||
@ReactMethod | ||
private fun setStyle(style: String) { | ||
UiThreadUtil.runOnUiThread { | ||
getController()?.isAppearanceLightStatusBars = style == "dark-content" | ||
} | ||
} | ||
|
||
private fun getController(): WindowInsetsControllerCompat? { | ||
if (this.controller == null) { | ||
val window = mReactContext.currentActivity!!.window | ||
|
||
this.controller = WindowInsetsControllerCompat(window, window.decorView) | ||
} | ||
|
||
return this.controller | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React from 'react'; | ||
import { Animated, TextInput, View } from 'react-native'; | ||
import { | ||
useKeyboardAnimation, | ||
useKeyboardAnimationReplica, | ||
} from 'react-native-keyboard-controller'; | ||
import styles from './styles'; | ||
|
||
export default function KeyboardAnimation() { | ||
const { height, progress } = useKeyboardAnimation(); | ||
const { height: heightReplica } = useKeyboardAnimationReplica(); | ||
|
||
return ( | ||
<View style={styles.container}> | ||
<View style={styles.row}> | ||
<Animated.View | ||
style={{ | ||
width: 50, | ||
height: 50, | ||
backgroundColor: 'red', | ||
borderRadius: 25, | ||
transform: [{ translateY: height }], | ||
}} | ||
/> | ||
<Animated.View | ||
style={{ | ||
width: 50, | ||
height: 50, | ||
backgroundColor: 'green', | ||
borderRadius: 25, | ||
transform: [ | ||
{ | ||
translateX: progress.interpolate({ | ||
inputRange: [0, 1], | ||
outputRange: [0, 100], | ||
}), | ||
}, | ||
], | ||
}} | ||
/> | ||
<Animated.View | ||
style={{ | ||
width: 50, | ||
height: 50, | ||
backgroundColor: 'blue', | ||
borderRadius: 25, | ||
transform: [{ translateY: heightReplica }], | ||
}} | ||
/> | ||
</View> | ||
<TextInput | ||
style={{ | ||
width: 200, | ||
marginTop: 50, | ||
height: 50, | ||
backgroundColor: 'yellow', | ||
}} | ||
/> | ||
</View> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { StyleSheet } from 'react-native'; | ||
|
||
export default StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
}, | ||
box: { | ||
width: 60, | ||
height: 60, | ||
marginVertical: 20, | ||
}, | ||
row: { | ||
flexDirection: 'row', | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,7 @@ | ||
import React from 'react'; | ||
import { Animated, TextInput, View } from 'react-native'; | ||
import { | ||
useKeyboardAnimation, | ||
useKeyboardAnimationReplica, | ||
} from 'react-native-keyboard-controller'; | ||
import styles from './styles'; | ||
|
||
export default function KeyboardAnimation() { | ||
const { height, progress } = useKeyboardAnimation(); | ||
const { height: heightReplica } = useKeyboardAnimationReplica(); | ||
import KeyboardAnimationTemplate from '../../../components/KeyboardAnimation'; | ||
|
||
return ( | ||
<View style={styles.container}> | ||
<View style={styles.row}> | ||
<Animated.View | ||
style={{ | ||
width: 50, | ||
height: 50, | ||
backgroundColor: 'red', | ||
borderRadius: 25, | ||
transform: [{ translateY: height }], | ||
}} | ||
/> | ||
<Animated.View | ||
style={{ | ||
width: 50, | ||
height: 50, | ||
backgroundColor: 'green', | ||
borderRadius: 25, | ||
transform: [ | ||
{ | ||
translateX: progress.interpolate({ | ||
inputRange: [0, 1], | ||
outputRange: [0, 100], | ||
}), | ||
}, | ||
], | ||
}} | ||
/> | ||
<Animated.View | ||
style={{ | ||
width: 50, | ||
height: 50, | ||
backgroundColor: 'blue', | ||
borderRadius: 25, | ||
transform: [{ translateY: heightReplica }], | ||
}} | ||
/> | ||
</View> | ||
<TextInput | ||
style={{ | ||
width: 200, | ||
marginTop: 50, | ||
height: 50, | ||
backgroundColor: 'yellow', | ||
}} | ||
/> | ||
</View> | ||
); | ||
export default function KeyboardAnimation() { | ||
return <KeyboardAnimationTemplate />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React, { useState } from 'react'; | ||
import { View, StatusBar, StatusBarStyle, Button } from 'react-native'; | ||
|
||
import KeyboardAnimationTemplate from '../../../components/KeyboardAnimation'; | ||
import { randomColor } from '../../../utils'; | ||
|
||
export default function StatusBarManipulation() { | ||
const [color, setColor] = useState('#00FF0000'); | ||
const [barStyle, setBarStyle] = useState<StatusBarStyle>('light-content'); | ||
const [hidden, setHidden] = useState(false); | ||
const [animated, setAnimated] = useState(true); | ||
|
||
return ( | ||
<View style={{ flex: 1, backgroundColor: 'pink' }}> | ||
<StatusBar | ||
backgroundColor={color} | ||
barStyle={barStyle} | ||
hidden={hidden} | ||
animated={animated} | ||
translucent | ||
/> | ||
<KeyboardAnimationTemplate /> | ||
<Button | ||
title={`Set ${hidden ? 'shown' : 'hidden'}`} | ||
onPress={() => setHidden(!hidden)} | ||
/> | ||
<Button | ||
title="Update color" | ||
onPress={() => setColor(`${randomColor()}`)} | ||
/> | ||
<Button | ||
title={`Set ${!animated ? '' : 'not'} animated`} | ||
onPress={() => setAnimated(!animated)} | ||
/> | ||
<Button | ||
title={`Change ${barStyle}`} | ||
onPress={() => | ||
setBarStyle( | ||
barStyle === 'light-content' ? 'dark-content' : 'light-content' | ||
) | ||
} | ||
/> | ||
</View> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function randomColor() { | ||
return '#' + Math.random().toString(16).slice(-6); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import './monkey-patch'; | ||
|
||
export * from './native'; | ||
export * from './animated'; | ||
export * from './replicas'; |
Oops, something went wrong.