fix: handle mobile hardware and gesture back button in category details navigation (#40)
This commit is contained in:
parent
02682595c8
commit
3b24da2cc4
|
|
@ -1,7 +1,7 @@
|
||||||
import React, { type ReactNode } from "react";
|
import React, { type ReactNode, useCallback } from "react";
|
||||||
import { Platform, Pressable, StyleSheet, Text, View } from "react-native";
|
import { BackHandler, Platform, Pressable, StyleSheet, Text, View } from "react-native";
|
||||||
import { Ionicons } from "@expo/vector-icons";
|
import { Ionicons } from "@expo/vector-icons";
|
||||||
import { router } from "expo-router";
|
import { router, useFocusEffect } from "expo-router";
|
||||||
import { moderateScale } from "react-native-size-matters";
|
import { moderateScale } from "react-native-size-matters";
|
||||||
|
|
||||||
import { Colors } from "@/theme";
|
import { Colors } from "@/theme";
|
||||||
|
|
@ -25,6 +25,24 @@ export function ContentPageHeader({
|
||||||
titleAccessibilityLabel,
|
titleAccessibilityLabel,
|
||||||
onBack,
|
onBack,
|
||||||
}: ContentPageHeaderProps) {
|
}: ContentPageHeaderProps) {
|
||||||
|
useFocusEffect(
|
||||||
|
useCallback(() => {
|
||||||
|
if (!onBack) return;
|
||||||
|
|
||||||
|
const subscription = BackHandler.addEventListener(
|
||||||
|
"hardwareBackPress",
|
||||||
|
() => {
|
||||||
|
onBack();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
subscription.remove();
|
||||||
|
};
|
||||||
|
}, [onBack])
|
||||||
|
);
|
||||||
|
|
||||||
const handleBack = () => {
|
const handleBack = () => {
|
||||||
if (onBack) {
|
if (onBack) {
|
||||||
onBack();
|
onBack();
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import React, { useMemo } from "react";
|
import React, { useMemo, useState } from "react";
|
||||||
import { ContentMediaProvider } from "../media/ContentMediaContext";
|
import { ContentMediaProvider } from "../media/ContentMediaContext";
|
||||||
import { ScreenError, ScreenLoading } from "../components/ContentScreenState";
|
import { ScreenError, ScreenLoading } from "../components/ContentScreenState";
|
||||||
import { useContentDetailScreen } from "../hooks/use-content-detail-screen";
|
import { useContentDetailScreen } from "../hooks/use-content-detail-screen";
|
||||||
|
|
@ -7,6 +7,7 @@ import { resolveDetailRenderer } from "../renderers/registry";
|
||||||
import { RendererFallbackBoundary } from "../renderers/shared/RendererFallbackBoundary";
|
import { RendererFallbackBoundary } from "../renderers/shared/RendererFallbackBoundary";
|
||||||
import { DetailPageShell } from "../renderers/shared/DetailPageShell";
|
import { DetailPageShell } from "../renderers/shared/DetailPageShell";
|
||||||
import { applyPresentationContractToData } from "../utils/attribute-presentation";
|
import { applyPresentationContractToData } from "../utils/attribute-presentation";
|
||||||
|
import { BackActionContext } from "../context/BackActionContext";
|
||||||
|
|
||||||
export interface ContentDetailScreenProps {
|
export interface ContentDetailScreenProps {
|
||||||
moduleCode: string;
|
moduleCode: string;
|
||||||
|
|
@ -56,6 +57,7 @@ function ContentDetailScreenResolved({
|
||||||
templateCode,
|
templateCode,
|
||||||
contentId,
|
contentId,
|
||||||
}: ContentDetailScreenProps) {
|
}: ContentDetailScreenProps) {
|
||||||
|
const [customBack, setCustomBack] = useState<(() => void) | null>(null);
|
||||||
const screen = useContentDetailScreen({ moduleCode, templateCode, contentId });
|
const screen = useContentDetailScreen({ moduleCode, templateCode, contentId });
|
||||||
|
|
||||||
const rendererCode = screen.contractQuery.data?.presentation.renderers.detail;
|
const rendererCode = screen.contractQuery.data?.presentation.renderers.detail;
|
||||||
|
|
@ -104,7 +106,12 @@ function ContentDetailScreenResolved({
|
||||||
.join(" · ");
|
.join(" · ");
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DetailPageShell title={displayTitle} subtitle={alternateNames || undefined}>
|
<DetailPageShell
|
||||||
|
title={displayTitle}
|
||||||
|
subtitle={alternateNames || undefined}
|
||||||
|
onBack={customBack ?? undefined}
|
||||||
|
>
|
||||||
|
<BackActionContext.Provider value={{ setCustomBackAction: setCustomBack }}>
|
||||||
<ContentMediaProvider media={response.media}>
|
<ContentMediaProvider media={response.media}>
|
||||||
<RendererFallbackBoundary
|
<RendererFallbackBoundary
|
||||||
rendererKind="detail"
|
rendererKind="detail"
|
||||||
|
|
@ -142,6 +149,7 @@ function ContentDetailScreenResolved({
|
||||||
/>
|
/>
|
||||||
</RendererFallbackBoundary>
|
</RendererFallbackBoundary>
|
||||||
</ContentMediaProvider>
|
</ContentMediaProvider>
|
||||||
|
</BackActionContext.Provider>
|
||||||
</DetailPageShell>
|
</DetailPageShell>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue