Skip to content

Commit

Permalink
Use device info plus package for support all platform (#27)
Browse files Browse the repository at this point in the history
* Added device info plus package

* Used new package for get device id

* Used userAgent hash as web id

* Added userID as optional parameter
  • Loading branch information
M97Chahboun authored Oct 1, 2023
1 parent a4fae44 commit b37bcae
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
8 changes: 5 additions & 3 deletions lib/src/gh_snitch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GhSnitch {
static GhSnitchInstance get _instance {
return _ghSnitchDelegate ??= GhSnitchInstance.instance;
}

/// Listens to exceptions thrown in the app and reports them as issues on GitHub.
///
/// This method sets up a Flutter error handler to catch uncaught exceptions and report them as issues on GitHub.
Expand Down Expand Up @@ -69,7 +69,8 @@ class GhSnitch {
String? screenShotBranch,
List<String>? labels,
List<String>? assignees,
int? milestone}) {
int? milestone,
String? userId}) {
_handleNotInitialized();
return _instance.report(
title: title,
Expand All @@ -78,7 +79,8 @@ class GhSnitch {
screenShotsBranch: screenShotBranch,
labels: labels,
assignees: assignees,
milestone: milestone);
milestone: milestone,
userId: userId);
}

/// Checks if the `GhSnitch` instance has been initialized or not.
Expand Down
40 changes: 31 additions & 9 deletions lib/src/utils/github_snitch_instance.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:convert';
import 'dart:developer';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter_udid/flutter_udid.dart';
import 'package:device_info_plus/device_info_plus.dart';

import 'package:flutter/foundation.dart';
import 'package:github_snitch/src/utils/extensions.dart';
Expand Down Expand Up @@ -35,6 +35,7 @@ class GhSnitchInstance {
List<String>? labels,
List<String>? assignees,
int? milestone,
String? userId,
bool fromCatch = false}) async {
ConnectivityResult connectivity = await Connectivity().checkConnectivity();
if (!(connectivity == ConnectivityResult.none)) {
Expand All @@ -50,12 +51,12 @@ class GhSnitchInstance {
screenShotsBranch: screenShotsBranch!);
url = "\n## ScreenShot \n![]($url)";
}
String deviceId = await FlutterUdid.udid;
String? id = userId ?? await deviceId;
Map<String, dynamic> issueBody = {
ownerBody: owner,
repoBody: repo,
bodyTitle: title,
bodyBody: '$body$url\n$deviceId',
bodyBody: '$body$url\n$id',
};
if (assignees != null) {
issueBody["assignees"] = assignees;
Expand Down Expand Up @@ -242,11 +243,11 @@ class GhSnitchInstance {
}

/// Retrieves all the comments made on the issues reported by the current user.
Future<Issue> getReportsComments() async {
Future<Issue> getReportsComments({String? userId}) async {
final Issue issues = Issue();

String deviceId = await FlutterUdid.udid;
List userIssues = await getIssuesByUserID(deviceId);
String? id = userId ?? await deviceId;
List userIssues = await getIssuesByUserID(id ?? '');
for (Issue issue in userIssues) {
String listCommentsEp = "$owner/$repo/issues/${issue.id}/comments";
GhResponse response = await ghRequest.request("GET", listCommentsEp);
Expand All @@ -257,13 +258,14 @@ class GhSnitchInstance {
}

/// Submits a comment to the specified issue.
Future<bool> submitComment(String reportId, String comment) async {
Future<bool> submitComment(String reportId, String comment,
{String? userId}) async {
bool commented = false;
String submitCommentEp = "$owner/$repo/issues/$reportId/comments";
String deviceId = await FlutterUdid.udid;
String? id = userId ?? await deviceId;
Map commentBody = {
commentsBodyField:
"$comment\n${deviceIdTemplate.replaceFirst(idMark, deviceId)}"
"$comment\n${deviceIdTemplate.replaceFirst(idMark, id ?? '')}"
};
String commentBodyToString = json.encode(commentBody);
GhResponse response = await ghRequest.request("POST", submitCommentEp,
Expand Down Expand Up @@ -306,4 +308,24 @@ class GhSnitchInstance {
}
return null;
}

Future<String?> get deviceId async {
final deviceInfoPlugin = DeviceInfoPlugin();
String? id;
if (kIsWeb) {
id =
(await deviceInfoPlugin.webBrowserInfo).userAgent.hashCode.toString();
} else {
id = switch (defaultTargetPlatform) {
TargetPlatform.android => (await deviceInfoPlugin.androidInfo).id,
TargetPlatform.iOS =>
(await deviceInfoPlugin.iosInfo).identifierForVendor,
TargetPlatform.linux => (await deviceInfoPlugin.linuxInfo).id,
TargetPlatform.windows => (await deviceInfoPlugin.windowsInfo).deviceId,
TargetPlatform.macOS => (await deviceInfoPlugin.macOsInfo).systemGUID,
TargetPlatform.fuchsia => null
};
}
return id;
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ environment:
flutter: ">=1.17.0"

dependencies:
flutter_udid: ^2.0.1
flutter:
sdk: flutter
http: ^0.13.5
shared_preferences: ^2.0.15
universal_io: ^2.0.4
string_similarity: ^2.0.0
connectivity_plus: ^4.0.1
device_info_plus: ^9.0.2

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit b37bcae

Please sign in to comment.