|
| 1 | +import '/helpers/backpack.dart'; |
| 2 | +import '/widgets/navigation_hub/navigation_hub.dart'; |
| 3 | + |
| 4 | +/// A helper class for journey navigation |
| 5 | +class JourneyHelper { |
| 6 | + final String navigationHubState; |
| 7 | + |
| 8 | + /// Create a new journey helper for the given navigation hub state |
| 9 | + JourneyHelper(this.navigationHubState); |
| 10 | + |
| 11 | + /// Get the current step index (0-based) |
| 12 | + int getCurrentStep() { |
| 13 | + final currentData = |
| 14 | + Backpack.instance.read('${navigationHubState}_current_tab'); |
| 15 | + return (currentData is int) ? currentData : 0; |
| 16 | + } |
| 17 | + |
| 18 | + /// Get the total number of steps |
| 19 | + int getTotalSteps() { |
| 20 | + final totalPagesData = |
| 21 | + Backpack.instance.read('${navigationHubState}_total_pages'); |
| 22 | + return (totalPagesData is int) ? totalPagesData : 0; |
| 23 | + } |
| 24 | + |
| 25 | + /// Check if this is the first step |
| 26 | + bool isFirstStep() { |
| 27 | + return getCurrentStep() == 0; |
| 28 | + } |
| 29 | + |
| 30 | + /// Check if this is the last step |
| 31 | + bool isLastStep() { |
| 32 | + final currentStep = getCurrentStep(); |
| 33 | + final totalSteps = getTotalSteps(); |
| 34 | + return currentStep >= totalSteps - 1; |
| 35 | + } |
| 36 | + |
| 37 | + /// Get the completion percentage (0.0 to 1.0) |
| 38 | + double getCompletionPercentage() { |
| 39 | + final currentStep = getCurrentStep(); |
| 40 | + final totalSteps = getTotalSteps(); |
| 41 | + |
| 42 | + if (totalSteps == 0) return 0.0; |
| 43 | + return (currentStep + 1) / totalSteps; |
| 44 | + } |
| 45 | + |
| 46 | + /// Navigate to the next step, returns false if already at the last step |
| 47 | + Future<bool> nextStep() async { |
| 48 | + final actions = NavigationHubStateActions(navigationHubState); |
| 49 | + return await actions.nextPage(); |
| 50 | + } |
| 51 | + |
| 52 | + /// Navigate to the previous step, returns false if already at the first step |
| 53 | + Future<bool> previousStep() async { |
| 54 | + final actions = NavigationHubStateActions(navigationHubState); |
| 55 | + return await actions.previousPage(); |
| 56 | + } |
| 57 | + |
| 58 | + /// Jump to a specific step by index |
| 59 | + void goToStep(int stepIndex) { |
| 60 | + final actions = NavigationHubStateActions(navigationHubState); |
| 61 | + actions.currentTabIndex(stepIndex); |
| 62 | + } |
| 63 | + |
| 64 | + /// Reset the current step |
| 65 | + void resetCurrentStep() { |
| 66 | + final actions = NavigationHubStateActions(navigationHubState); |
| 67 | + actions.resetTabIndex(getCurrentStep()); |
| 68 | + } |
| 69 | +} |
0 commit comments