Skip to content

Commit

Permalink
chore: previous code
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Bühler committed Mar 12, 2023
1 parent 0b7b4ff commit ee31077
Show file tree
Hide file tree
Showing 10 changed files with 398 additions and 54 deletions.
93 changes: 67 additions & 26 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,34 +1,75 @@
# See https://www.dartlang.org/guides/libraries/private-files
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Files and directories created by pub
# Flutter/Dart/Pub related
**/doc/api/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
build/

pubspec.lock

# Generated Dart Files
*.g.dart
# Android related
**/android/**/gradle-wrapper.jar
**/android/.gradle
**/android/captures/
**/android/gradlew
**/android/gradlew.bat
**/android/local.properties
**/android/**/GeneratedPluginRegistrant.java

# Directory created by dartdoc
# If you don't generate documentation locally you can remove this line.
doc/api/
# iOS/XCode related
**/ios/**/*.mode1v3
**/ios/**/*.mode2v3
**/ios/**/*.moved-aside
**/ios/**/*.pbxuser
**/ios/**/*.perspectivev3
**/ios/**/*sync/
**/ios/**/.sconsign.dblite
**/ios/**/.tags*
**/ios/**/.vagrant/
**/ios/**/DerivedData/
**/ios/**/Icon?
**/ios/**/Pods/
**/ios/**/.symlinks/
**/ios/**/profile
**/ios/**/xcuserdata
**/ios/.generated/
**/ios/Flutter/App.framework
**/ios/Flutter/Flutter.framework
**/ios/Flutter/Flutter.podspec
**/ios/Flutter/Generated.xcconfig
**/ios/Flutter/app.flx
**/ios/Flutter/app.zip
**/ios/Flutter/flutter_assets/
**/ios/Flutter/flutter_export_environment.sh
**/ios/ServiceDefinitions.json
**/ios/Runner/GeneratedPluginRegistrant.*

# Avoid committing generated Javascript files:
*.dart.js
*.info.json # Produced by the --dump-info flag.
*.js # When generated by dart2js. Don't specify *.js if your
# project includes source files written in JavaScript.
*.js_
*.js.deps
*.js.map

# OS Files
.DS_Store
Thumbs.db

# IDEs
.idea/
*.iml
.vscode/
.vs/
# Exceptions to above rules.
!**/ios/**/default.mode1v3
!**/ios/**/default.mode2v3
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
10 changes: 10 additions & 0 deletions .metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: 1d9032c7e1d867f071f2277eb1673e8f9b0274e3
channel: stable

project_type: package
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## 0.1.4

- Updates firebase_crashlytics: ^3.0.3
- Updates firebase_core: ^2.1.1

## 0.1.3

- Updates firebase_crashlytics: ^2.6.1
- Updates firebase_core: ^1.14.0

## 0.1.2

- Removed exceptions from unsupported platforms

## 0.1.1

- Added Log Warning param to CrashlyticsOutput

## 0.1.0

- Initial implementation of Crashlytics Service
74 changes: 46 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,49 @@
# New Package for Stacked
# Stacked Crashlytics Service

Created from the `package-template`.
A service accompanied with a LogOutput that tracks all warnings as non-fatal crash reports and all errors as fatal crash reports. This makes use of the Firebase Crashlytics service.

After creating the repository, proceed with the following instructions:
## How To use

- Update the repository settings to adhere to the conventions:
- General:
- No Wikis
- No Issues
- No Sponsorships
- Preserve this repository
- No Discussions
- No Projects
- Don't allow merge commits
- Allow squash merging with default commit message set to "Default to pull request title and commit details"
- Don't allow rebase merging
- Always suggest updating pull requests
- Allow auto-merge
- Automatically delete head branches
- Branch protection rule (`main`):
- Require a pull request before merging
- Dismiss stale pull request approvals when new commits are pushed
- Allow specified actors to bypass required pull requests -> `Dane Mackier` (or whoever is the current owner of the personal access token in the organization secrets `REPO_DEPLOYMENT_TOKEN`)
- Require status checks to pass before merging
- Require branches to be up to date before merging
- Add status check `Linting and Testing` (to select this, the workflow must have been run at least once. This can be done manually since the workflow has "workflow_dispatch" as a trigger)
- Require conversation resolution before merging
- Require linear history
- Create the flutter package with `flutter create -t package --project-name NAME .`
- Update the content in the `README` file.
* In the pubspec.yaml file under dependencies add
```dart
stacked_crashlytics: ^0.1.1
```
* Register the service on to the locator
* If you want to register the service
```dart
@StackedApp(
dependencies: [
...
LazySingleton(classType: CrashlyticsService),
...
),
],
)
```
* If you are presolving into an async instance, you can use
```dart
@StackedApp(
dependencies: [
...
Presolve(
classType: CrashlyticsService,
presolveUsing: CrashlyticsService.getInstance,
),
],
)
```
Note: Don't forget to run
``` flutter pub run build_runner build --delete-conflicting-outputs ```
* Add the log Output inside your logger under outputs inside multiple logger output like so
```dart
Logger(
...
output: MultipleLoggerOutput([
...
CrashlyticsOutput(),
]),
);
```
30 changes: 30 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
# invoked from the command line by running `flutter analyze`.

# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml

linter:
# The lint rules applied to this project can be customized in the
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
# included above or to enable additional rules. A list of all available lints
# and their documentation is published at
# https://dart-lang.github.io/linter/lints/index.html.
#
# Instead of disabling a lint rule for the entire project in the
# section below, it can also be suppressed for a single line of code
# or a specific dart file by using the `// ignore: name_of_lint` and
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
# producing the lint.
# TODO: review these settings.
rules:
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options

117 changes: 117 additions & 0 deletions lib/src/crashlytics_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/foundation.dart';
import 'package:logger/logger.dart';

class CrashlyticsService {
static CrashlyticsService? _instance;

static Future<CrashlyticsService> getInstance() async {
_instance ??= CrashlyticsService._(FirebaseCrashlytics.instance);

return _instance!;
}

final FirebaseCrashlytics _crashlyticsService;
CrashlyticsService._(
this._crashlyticsService,
);

void recordFlutterErrorToCrashlytics(FlutterErrorDetails details) {
try {
_crashlyticsService.recordFlutterError(details);
} catch (e) {
_catchOrThrow(e);
}
}

Future setUserIdToCrashlytics({String? id}) async {
try {
if (id != null) await _crashlyticsService.setUserIdentifier(id);
} catch (e) {
_catchOrThrow(e);
}
}

Future logToCrashlytics(
Level level,
List<String> lines,
StackTrace stacktrace, {
required bool logwarnings,
}) async {
try {
if (level == Level.error || level == Level.wtf) {
await _crashlyticsService.recordError(
lines.join('\n'),
stacktrace,
printDetails: true,
fatal: true,
);
}
if (level == Level.warning && logwarnings) {
await _crashlyticsService.recordError(
lines.join('\n'),
stacktrace,
printDetails: true,
);
}
if (level == Level.info ||
level == Level.verbose ||
level == Level.debug) {
await _crashlyticsService.log(lines.join('\n'));
}
} catch (exception) {
_catchOrThrow(exception);
}
}

Future setCustomKeysToTrack(String key, dynamic value) async {
try {
await _crashlyticsService.setCustomKey(key, value);
} catch (e) {
_catchOrThrow(e);
}
}

// Be very careful when you excute this code it will crash the app
// So, be sure to remove it after usage
void crashApp() {
try {
_crashlyticsService.crash();
} catch (e) {
_catchOrThrow(e);
}
}

void _catchOrThrow(dynamic exception) {
final exceptionString = exception.toString();
final isPluginConstantsException = exceptionString
.contains("pluginConstants['isCrashlyticsCollectionEnabled']");

if (!isPluginConstantsException) {
throw exception;
}
}
}

class CrashlyticsOutput extends LogOutput {
final bool logWarnings;
CrashlyticsOutput({this.logWarnings = false});

@override
void output(OutputEvent event) {
try {
CrashlyticsService.getInstance().then((instance) {
return instance.logToCrashlytics(
event.level,
event.lines,
StackTrace.current,
logwarnings: logWarnings,
);
});
} catch (e) {
if (kDebugMode) {
print('CRASHLYTICS FAILED: $e');
}
}
}
}
3 changes: 3 additions & 0 deletions lib/stacked_crashlytics.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
library stacked_crashlytics;

export 'src/crashlytics_service.dart';
Loading

0 comments on commit ee31077

Please sign in to comment.