Skip to content

Commit

Permalink
Move navigation to machine
Browse files Browse the repository at this point in the history
  • Loading branch information
g-todorov committed Jun 20, 2024
1 parent 9cd7bfa commit 235634c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 deletions.
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
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
File renamed without changes.
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

0 comments on commit 235634c

Please sign in to comment.