Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move navigation to machine #1

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/contexts/useApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { createActorContext, useSelector } from "@xstate/react";
import {
AuthenticatingMachineActor,
authenticatingMachine,
} from "../machines/authenticating";
} from "../machines/authenticating.navigator";
import {
AuthenticatedMachineActor,
authenticatedMachine,
} from "../machines/authenticated";
} from "../machines/authenticated.navigator";

export const appMachine = setup({
types: {
Expand Down
27 changes: 0 additions & 27 deletions src/hooks/useNavigator.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ActorRefFrom, setup, assign } from "xstate";
import { ActorRefFrom, setup, assign, fromCallback } from "xstate";
import { AuthenticatedParamList } from "../types/navigation";
import { HomeMachineActor, homeMachine } from "./home";
import { ListMachineActor, listMachine } from "./list";
import { navigationSubscriber } from "./shared/actors";

export type AuthenticatedMachineActor = ActorRefFrom<
typeof authenticatedMachine
Expand All @@ -27,7 +28,11 @@ export const authenticatedMachine = setup({
},
}),
},
actors: { homeMachine, listMachine },
actors: {
homeMachine,
listMachine,
navigationSubscriber,
},
guards: {
isHomeScreen(_, params: { screen: keyof AuthenticatedParamList }) {
return params.screen === "Home";
Expand All @@ -40,6 +45,7 @@ export const authenticatedMachine = setup({
context: { refHome: undefined, refList: undefined },
id: "application",
initial: "homeScreen",
invoke: { src: "navigationSubscriber" },
on: {
NAVIGATE: [
{
Expand Down
17 changes: 17 additions & 0 deletions src/machines/shared/actors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { fromCallback } from "xstate";
import {
getCurrentRouteName,
navigationRef,
} from "../../navigation/NavigationRef";

export const navigationSubscriber = fromCallback(({ sendBack }) => {
const unsubscribe = navigationRef.addListener("state", (_event) => {
const screenRoute = getCurrentRouteName();

if (screenRoute) {
sendBack({ type: "NAVIGATE", screen: screenRoute });
}
});

return unsubscribe;
});
7 changes: 1 addition & 6 deletions src/navigation/AuthenticatedNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import {
AuthenticatedParamList,
RootStackScreenProps,
} from "../types/navigation";
import { AuthenticatedMachineActor } from "../machines/authenticated";
import { AuthenticatedMachineActor } from "../machines/authenticated.navigator";
import HomeScreen from "../screens/Home";
import ListScreen from "../screens/List";
import { useNavigator } from "../hooks/useNavigator";

const Stack = createNativeStackNavigator<AuthenticatedParamList>();

Expand All @@ -22,10 +21,6 @@ export function AuthenticatedNavigator({ actorRef }: Props) {
return snapshot;
});

useNavigator<AuthenticatedParamList>((route) => {
actorRef.send({ type: "NAVIGATE", screen: route });
});

return (
<Stack.Navigator initialRouteName="Home">
<Stack.Screen name="Home">
Expand Down
7 changes: 1 addition & 6 deletions src/navigation/AuthenticatingNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import {
AuthenticatingParamList,
RootStackScreenProps,
} from "../types/navigation";
import { AuthenticatingMachineActor } from "../machines/authenticating";
import { AuthenticatingMachineActor } from "../machines/authenticating.navigator";
import SignInScreen from "../screens/SignIn";
import { useNavigator } from "../hooks/useNavigator";

const Stack = createNativeStackNavigator<AuthenticatingParamList>();

Expand All @@ -21,10 +20,6 @@ export function AuthenticatingNavigator({ navigation, actorRef }: Props) {
return snapshot;
});

useNavigator<AuthenticatingParamList>((route) => {
actorRef.send({ type: "NAVIGATE", screen: route });
});

return (
<Stack.Navigator initialRouteName="SignIn">
<Stack.Screen name="SignIn">
Expand Down