ScrollPage.js 2.2 KB

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