dleurs / flutter_starter

A flutter starter
2 stars 0 forks source link

beamer navigation #29

Open dleurs opened 1 year ago

dleurs commented 1 year ago

Prerelease: 2.0.0-dev.0

Will need to check modifications

dleurs commented 1 year ago

Doing this but with cubit https://github.com/slovnicki/beamer/tree/master/examples/bottom_navigation_riverpod

dleurs commented 1 year ago

seems I will have to use riverpod

void main() async {
  debugLog("main() | Main function started");

  WidgetsFlutterBinding.ensureInitialized();
  debugLog("main() | WidgetsFlutterBinding.ensureInitialized executed");

  Beamer.setPathUrlStrategy();
  debugLog("main() | Beamer.setPathUrlStrategy executed");

  final sharedPreferences = await SharedPreferences.getInstance();
  debugLog("main() | sharedPreferences instance obtained");

  final container = ProviderContainer(
    overrides: [
      sharedPreferencesProvider.overrideWithValue(sharedPreferences),
    ],
  );

  final booksInitialPath =
      container.read(navigationStateControllerProvider).booksLocation;
  final articlesInitialPath =
      container.read(navigationStateControllerProvider).articlesLocation;
  final lastInitialPath =
      container.read(navigationStateControllerProvider).lastLocation;

  final routerDelegate = BeamerDelegate(
    initialPath: lastInitialPath,
    locationBuilder: RoutesLocationBuilder(
      routes: {
        '/home/*': (context, state, data) =>
            AppScreen(booksInitialPath, articlesInitialPath, lastInitialPath),
        '/login': (context, state, data) => BeamPage(
          key: ValueKey('login'),
          title: 'Login',
          child: LoginScreen(),
        ),
      },
    ),
    buildListener: (context, delegate) {
      final location = Beamer.of(context).configuration.location;
      debugLog("routerDelegate | buildListener() | "
          "location: $location");
    },
    routeListener: (routeInformation, delegate) {
      final location = routeInformation.location;

      Future(() {
        if (location != null) {
          debugLog("routerDelegate | routeListener() | "
              "about to save location: $location");

          if (location.startsWith('/home/books') ||
              location.startsWith('/home/articles')) {
            container
                .read(navigationStateControllerProvider.notifier)
                .setLastLocation(location);
            debugLog("routerDelegate | routeListener() | "
                "just saved last location: $location");
          }

          if (location.startsWith('/home/books')) {
            container
                .read(navigationStateControllerProvider.notifier)
                .setBooksLocation(location);
            debugLog("routerDelegate | routeListener() | "
                "just saved books location: $location");
          } else if (location.startsWith('/home/articles')) {
            container
                .read(navigationStateControllerProvider.notifier)
                .setArticlesLocation(location);
            debugLog("routerDelegate | routeListener() | "
                "just saved articles location: $location");
          }
        }
      });
    },
    guards: [
      BeamGuard(
        pathPatterns: ['/login'],
        guardNonMatching: true,
        check: (context, state) {
          debugLog("routerDelegate | "
              "BeamGuard | check() | is about to retrieve signedIn state");
          final signedIn = container.read(authStateControllerProvider);
          debugLog("routerDelegate | "
              "BeamGuard | check() | obtained signedIn state: $signedIn");
          return signedIn;
        },
        beamToNamed: (origin, target, deepLink) => '/login',
      ),
    ],
  );
dleurs commented 1 year ago

https://www.technicalfeeder.com/2023/04/flutter-how-to-implement-bloc-like-pattern-with-riverpod/?utm_content=cmp-true