mattermost-mobile/app/hooks/device.ts
Mattermost Build f830338640
Automated cherry pick of #8646 (#8662)
* MM-62896 Keyboard blocks input (#8646)

* WIP

* fix keyboard blocking text input for interactive dialog

* do not use extra keyboard and windowSoftInputMode:adjustNothing on Android 10 and under

---------

Co-authored-by: Devin Binnie <devin.binnie@mattermost.com>
(cherry picked from commit 1c8770c74d)

* Bump the github-actions-updates group across 5 directories with 6 updates (#8613)

Bumps the github-actions-updates group with 1 update in the /.github/actions/prepare-android-build directory: [actions/checkout](https://github.com/actions/checkout).
Bumps the github-actions-updates group with 1 update in the /.github/actions/prepare-ios-build directory: [actions/cache](https://github.com/actions/cache).
Bumps the github-actions-updates group with 2 updates in the /.github/actions/prepare-mobile-build directory: [actions/cache](https://github.com/actions/cache) and [ruby/setup-ruby](https://github.com/ruby/setup-ruby).
Bumps the github-actions-updates group with 2 updates in the /.github/actions/prepare-node-deps directory: [actions/cache](https://github.com/actions/cache) and [actions/setup-node](https://github.com/actions/setup-node).
Bumps the github-actions-updates group with 4 updates in the /.github/workflows directory: [actions/checkout](https://github.com/actions/checkout), [ruby/setup-ruby](https://github.com/ruby/setup-ruby), [actions/upload-artifact](https://github.com/actions/upload-artifact) and [github/codeql-action](https://github.com/github/codeql-action).

Updates `actions/checkout` from 3.3.0 to 4.2.2
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](ac59398561...11bd71901b)

Updates `actions/cache` from 4.0.2 to 4.2.1
- [Release notes](https://github.com/actions/cache/releases)
- [Changelog](https://github.com/actions/cache/blob/main/RELEASES.md)
- [Commits](0c45773b62...0c907a75c2)

Updates `actions/cache` from 4.0.2 to 4.2.1
- [Release notes](https://github.com/actions/cache/releases)
- [Changelog](https://github.com/actions/cache/blob/main/RELEASES.md)
- [Commits](0c45773b62...0c907a75c2)

Updates `ruby/setup-ruby` from 1.180.0 to 1.221.0
- [Release notes](https://github.com/ruby/setup-ruby/releases)
- [Changelog](https://github.com/ruby/setup-ruby/blob/master/release.rb)
- [Commits](ff740bc00a...32110d4e31)

Updates `actions/cache` from 4.0.2 to 4.2.1
- [Release notes](https://github.com/actions/cache/releases)
- [Changelog](https://github.com/actions/cache/blob/main/RELEASES.md)
- [Commits](0c45773b62...0c907a75c2)

Updates `actions/setup-node` from 4.0.2 to 4.2.0
- [Release notes](https://github.com/actions/setup-node/releases)
- [Commits](60edb5dd54...1d0ff469b7)

Updates `actions/checkout` from 3 to 4
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](ac59398561...11bd71901b)

Updates `ruby/setup-ruby` from 1.180.0 to 1.221.0
- [Release notes](https://github.com/ruby/setup-ruby/releases)
- [Changelog](https://github.com/ruby/setup-ruby/blob/master/release.rb)
- [Commits](ff740bc00a...32110d4e31)

Updates `actions/upload-artifact` from 4.3.1 to 4.6.0
- [Release notes](https://github.com/actions/upload-artifact/releases)
- [Commits](https://github.com/actions/upload-artifact/compare/v4.3.1...65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08)

Updates `github/codeql-action` from 2 to 3
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](https://github.com/github/codeql-action/compare/v2...v3)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: github-actions-updates
- dependency-name: actions/cache
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: github-actions-updates
- dependency-name: actions/cache
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: github-actions-updates
- dependency-name: ruby/setup-ruby
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: github-actions-updates
- dependency-name: actions/cache
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: github-actions-updates
- dependency-name: actions/setup-node
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: github-actions-updates
- dependency-name: actions/checkout
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: github-actions-updates
- dependency-name: ruby/setup-ruby
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: github-actions-updates
- dependency-name: actions/upload-artifact
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: github-actions-updates
- dependency-name: github/codeql-action
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: github-actions-updates
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-12 10:41:02 +08:00

145 lines
4.6 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import RNUtils, {type WindowDimensions} from '@mattermost/rnutils';
import React, {type RefObject, useEffect, useRef, useState, useContext} from 'react';
import {AppState, Keyboard, NativeEventEmitter, Platform, View} from 'react-native';
import {useSafeAreaInsets} from 'react-native-safe-area-context';
import {DeviceContext} from '@context/device';
import type {KeyboardAwareScrollView} from 'react-native-keyboard-aware-scroll-view';
let utilsEmitter = new NativeEventEmitter(RNUtils);
export function testSetUtilsEmitter(emitter: NativeEventEmitter) {
utilsEmitter = emitter;
}
export function useSplitView() {
const {isSplit} = React.useContext(DeviceContext);
return isSplit;
}
export function useAppState() {
const [appState, setAppState] = useState(AppState.currentState);
useEffect(() => {
const listener = AppState.addEventListener('change', (nextState) => {
setAppState(nextState);
});
return () => listener.remove();
}, [appState]);
return appState;
}
export function useWindowDimensions() {
const [dimensions, setDimensions] = useState(RNUtils.getWindowDimensions());
useEffect(() => {
const listener = utilsEmitter.addListener('DimensionsChanged', (window: WindowDimensions) => {
setDimensions(window);
});
return () => listener.remove();
}, []);
return dimensions;
}
export function useIsTablet() {
const {isSplit, isTablet} = useContext(DeviceContext);
return isTablet && !isSplit;
}
export function useKeyboardHeightWithDuration() {
const [keyboardHeight, setKeyboardHeight] = useState({height: 0, duration: 0});
const updateTimeout = useRef<NodeJS.Timeout | null>(null);
const insets = useSafeAreaInsets();
useEffect(() => {
const show = Keyboard.addListener(Platform.select({ios: 'keyboardWillShow', default: 'keyboardDidShow'}), async (event) => {
// Do not use set the height on Android versions below 11
if (Platform.OS === 'android' && Platform.Version < 30) {
return;
}
setKeyboardHeight({height: event.endCoordinates.height, duration: event.duration});
});
const hide = Keyboard.addListener(Platform.select({ios: 'keyboardWillHide', default: 'keyboardDidHide'}), (event) => {
// Do not use set the height on Android versions below 11
if (Platform.OS === 'android' && Platform.Version < 30) {
return;
}
if (updateTimeout.current != null) {
clearTimeout(updateTimeout.current);
updateTimeout.current = null;
}
setKeyboardHeight({height: 0, duration: event.duration});
});
return () => {
show.remove();
hide.remove();
};
}, [insets.bottom]);
return keyboardHeight;
}
export function useKeyboardHeight() {
const {height} = useKeyboardHeightWithDuration();
return height;
}
export function useViewPosition(viewRef: RefObject<View>, deps: React.DependencyList = []) {
const [modalPosition, setModalPosition] = useState(0);
const isTablet = useIsTablet();
const height = useKeyboardHeight();
useEffect(() => {
if (Platform.OS === 'ios' && isTablet) {
viewRef.current?.measureInWindow((_, y) => {
if (y !== modalPosition) {
setModalPosition(y);
}
});
}
}, [...deps, isTablet, height, viewRef, modalPosition]);
return modalPosition;
}
export function useKeyboardOverlap(viewRef: RefObject<View>, containerHeight: number) {
const keyboardHeight = useKeyboardHeight();
const isTablet = useIsTablet();
const viewPosition = useViewPosition(viewRef, [containerHeight]);
const dimensions = useWindowDimensions();
const insets = useSafeAreaInsets();
const bottomSpace = (dimensions.height - containerHeight - viewPosition);
const tabletOverlap = Math.max(0, keyboardHeight - bottomSpace);
const phoneOverlap = keyboardHeight || insets.bottom;
const overlap = Platform.select({
ios: isTablet ? tabletOverlap : phoneOverlap,
default: 0,
});
return overlap;
}
export function useAvoidKeyboard(ref: RefObject<KeyboardAwareScrollView>, dimisher = 3) {
const height = useKeyboardHeight();
useEffect(() => {
let offsetY = height / dimisher;
if (offsetY < 80) {
offsetY = 0;
}
ref.current?.scrollToPosition(0, offsetY);
}, [height, dimisher, ref]);
}