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

feat: export instabug navigator #528

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased](https://github.com/Instabug/Instabug-Flutter/compare/v13.4.0...dev)

### Added

- Add an `InstabugNavigator` class to facilitate integration with any navigation library [#528](https://github.com/Instabug/Instabug-Flutter/pull/528)).

## [13.4.0](https://github.com/Instabug/Instabug-Flutter/compare/v13.3.0...v13.4.0) (September 29, 2024)

### Added
Expand Down
1 change: 1 addition & 0 deletions lib/instabug_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export 'src/modules/replies.dart';
export 'src/modules/session_replay.dart';
export 'src/modules/surveys.dart';
// Utils
export 'src/utils/instabug_navigator.dart';
export 'src/utils/instabug_navigator_observer.dart';
export 'src/utils/screen_loading/instabug_capture_screen_loading.dart';
export 'src/utils/screen_loading/route_matcher.dart';
Expand Down
42 changes: 42 additions & 0 deletions lib/src/utils/instabug_navigator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:flutter/material.dart';
import 'package:instabug_flutter/instabug_flutter.dart';
import 'package:instabug_flutter/src/utils/instabug_logger.dart';
import 'package:instabug_flutter/src/utils/repro_steps_constants.dart';
import 'package:instabug_flutter/src/utils/screen_loading/screen_loading_manager.dart';
import 'package:instabug_flutter/src/utils/screen_name_masker.dart';

class InstabugNavigator {
final List<String> _steps = [];

void screenChanged(String? rawScreenName) {
try {
final screenName = rawScreenName == null || rawScreenName.isEmpty
? ReproStepsConstants.emptyScreenFallback
: rawScreenName;
final maskedScreenName = ScreenNameMasker.I.mask(screenName);

final route = maskedScreenName;
// Starts a the new UI trace which is exclusive to screen loading
ScreenLoadingManager.I.startUiTrace(maskedScreenName, screenName);
// If there is a step that hasn't been pushed yet
if (_steps.isNotEmpty) {
// Report the last step and remove it from the list
Instabug.reportScreenChange(_steps.last);
_steps.removeLast();
}

// Add the new step to the list
_steps.add(route);
Future<dynamic>.delayed(const Duration(milliseconds: 1000), () {
// If this route is in the array, report it and remove it from the list
if (_steps.contains(route)) {
Instabug.reportScreenChange(route);
_steps.remove(route);
}
});
} catch (e) {
InstabugLogger.I.e('Reporting screen change failed:', tag: Instabug.tag);
InstabugLogger.I.e(e.toString(), tag: Instabug.tag);
}
}
}
44 changes: 4 additions & 40 deletions lib/src/utils/instabug_navigator_observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,23 @@ import 'package:instabug_flutter/instabug_flutter.dart';
import 'package:instabug_flutter/src/models/instabug_route.dart';
import 'package:instabug_flutter/src/modules/instabug.dart';
import 'package:instabug_flutter/src/utils/instabug_logger.dart';
import 'package:instabug_flutter/src/utils/instabug_navigator.dart';
import 'package:instabug_flutter/src/utils/repro_steps_constants.dart';
import 'package:instabug_flutter/src/utils/screen_loading/screen_loading_manager.dart';
import 'package:instabug_flutter/src/utils/screen_name_masker.dart';

class InstabugNavigatorObserver extends NavigatorObserver {
final List<InstabugRoute> _steps = [];

void screenChanged(Route newRoute) {
try {
final rawScreenName = newRoute.settings.name.toString().trim();
final screenName = rawScreenName.isEmpty
? ReproStepsConstants.emptyScreenFallback
: rawScreenName;
final maskedScreenName = ScreenNameMasker.I.mask(screenName);

final route = InstabugRoute(
route: newRoute,
name: maskedScreenName,
);

// Starts a the new UI trace which is exclusive to screen loading
ScreenLoadingManager.I.startUiTrace(maskedScreenName, screenName);
// If there is a step that hasn't been pushed yet
if (_steps.isNotEmpty) {
// Report the last step and remove it from the list
Instabug.reportScreenChange(_steps.last.name);
_steps.removeLast();
}

// Add the new step to the list
_steps.add(route);
Future<dynamic>.delayed(const Duration(milliseconds: 1000), () {
// If this route is in the array, report it and remove it from the list
if (_steps.contains(route)) {
Instabug.reportScreenChange(route.name);
_steps.remove(route);
}
});
} catch (e) {
InstabugLogger.I.e('Reporting screen change failed:', tag: Instabug.tag);
InstabugLogger.I.e(e.toString(), tag: Instabug.tag);
}
}
InstabugNavigator instabugNavigator = new InstabugNavigator();

@override
void didPop(Route route, Route? previousRoute) {
if (previousRoute != null) {
screenChanged(previousRoute);
instabugNavigator.screenChanged(previousRoute.settings.name?.trim());
}
}

@override
void didPush(Route route, Route? previousRoute) {
screenChanged(route);
instabugNavigator.screenChanged(route.settings.name?.trim());
}
}