ScrollPage.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import React from "react";
  2. import { RefreshControl } from "react-native";
  3. import { ScrollView } from "react-native-gesture-handler";
  4. import { useFocusEffect } from "@react-navigation/native";
  5. import { useTheme } from "@ui-kitten/components";
  6. import { useModel } from "flooks";
  7. import NavHeaderBar from "./NavHeaderBar";
  8. import { initState, refreashReducer } from "../Redux/RefreashRedux";
  9. export default function scrollPage(props) {
  10. const {
  11. style,
  12. enabledFresh,
  13. refreshEvent,
  14. statusType,
  15. navHeaderBarTitle,
  16. children,
  17. } = props;
  18. const [state, dispatch] = React.useReducer(refreashReducer, initState);
  19. const { refreshing } = state;
  20. const theme = useTheme();
  21. const { changeBackground } = useModel("barModel");
  22. function onRefresh() {
  23. dispatch({
  24. type: "startRefresh",
  25. });
  26. if (refreshEvent) {
  27. refreshEvent()
  28. .then(() => {
  29. dispatch({
  30. type: "refreshFinish",
  31. });
  32. })
  33. .catch(() => {
  34. dispatch({
  35. type: "refreshError",
  36. });
  37. });
  38. } else {
  39. setTimeout(() => {
  40. dispatch({
  41. type: "refreshFinish",
  42. });
  43. }, 1000);
  44. }
  45. }
  46. useFocusEffect(
  47. React.useCallback(() => {
  48. if (enabledFresh && refreshEvent) {
  49. onRefresh();
  50. }
  51. if (statusType === "primary") {
  52. changeBackground(theme["color-primary-500"]);
  53. }
  54. }, [])
  55. );
  56. return (
  57. <>
  58. {navHeaderBarTitle != null && <NavHeaderBar title={navHeaderBarTitle} />}
  59. <ScrollView
  60. refreshControl={
  61. <RefreshControl
  62. refreshing={refreshing}
  63. onRefresh={onRefresh}
  64. enabled={enabledFresh || false}
  65. />
  66. }
  67. style={style}
  68. contentContainerStyle={{ flexGrow: 1 }}
  69. >
  70. {children}
  71. </ScrollView>
  72. </>
  73. );
  74. }