From 361ee40868e58407b10b88d104b11a663d76ed4f Mon Sep 17 00:00:00 2001
From: Vishesh Handa <me@vhanda.in>
Date: Mon, 14 Jan 2019 14:35:05 +0100
Subject: [PATCH] Add crashanalytics

---
 android/app/build.gradle                      |  9 ++++++-
 android/build.gradle                          | 10 ++++++++
 .../gradle/wrapper/gradle-wrapper.properties  |  2 +-
 lib/main.dart                                 | 24 +++++++++++++++++++
 pubspec.lock                                  |  7 ++++++
 pubspec.yaml                                  |  1 +
 6 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/android/app/build.gradle b/android/app/build.gradle
index af9da85d..de033982 100644
--- a/android/app/build.gradle
+++ b/android/app/build.gradle
@@ -53,6 +53,12 @@ dependencies {
 
     // For reading a file to string
     implementation 'commons-io:commons-io:2.5'
+
+    // Check for v11.4.2 or higher
+    implementation 'com.google.firebase:firebase-core:16.0.6'
+
+    // Add dependency
+    implementation 'com.crashlytics.sdk.android:crashlytics:2.9.8'
 }
 
 configurations {
@@ -61,4 +67,5 @@ configurations {
     }
 }
 
-apply plugin: 'com.google.gms.google-services'
\ No newline at end of file
+apply plugin: 'com.google.gms.google-services'
+apply plugin: 'io.fabric'
diff --git a/android/build.gradle b/android/build.gradle
index 83f45572..86ba0168 100644
--- a/android/build.gradle
+++ b/android/build.gradle
@@ -2,11 +2,17 @@ buildscript {
     repositories {
         google()
         jcenter()
+
+        // For Crashanalytics
+        maven {
+            url 'https://maven.fabric.io/public'
+        }
     }
 
     dependencies {
         classpath 'com.android.tools.build:gradle:3.2.1'
         classpath 'com.google.gms:google-services:4.2.0'
+        classpath 'io.fabric.tools:gradle:1.26.1'
     }
 }
 
@@ -14,6 +20,10 @@ allprojects {
     repositories {
         google()
         jcenter()
+
+        maven {
+            url 'https://maven.google.com/'
+        }
     }
 }
 
diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties
index e9c9a6b3..7ed71a08 100644
--- a/android/gradle/wrapper/gradle-wrapper.properties
+++ b/android/gradle/wrapper/gradle-wrapper.properties
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
 distributionPath=wrapper/dists
 zipStoreBase=GRADLE_USER_HOME
 zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
diff --git a/lib/main.dart b/lib/main.dart
index b64be270..24da52ad 100644
--- a/lib/main.dart
+++ b/lib/main.dart
@@ -1,10 +1,34 @@
+import 'dart:async';
+
 import 'package:flutter/material.dart';
 import 'package:shared_preferences/shared_preferences.dart';
+import 'package:flutter_crashlytics/flutter_crashlytics.dart';
 
 import 'package:journal/app.dart';
 import 'package:journal/state_container.dart';
 
 void main() async {
+  bool isInDebugMode = true;
+
+  FlutterError.onError = (FlutterErrorDetails details) {
+    if (isInDebugMode) {
+      FlutterError.dumpErrorToConsole(details);
+    } else {
+      Zone.current.handleUncaughtError(details.exception, details.stack);
+    }
+  };
+
+  await FlutterCrashlytics().initialize();
+
+  runZoned<Future<Null>>(() async {
+    await runJournalApp();
+  }, onError: (error, stackTrace) async {
+    await FlutterCrashlytics()
+        .reportCrash(error, stackTrace, forceCrash: false);
+  });
+}
+
+Future runJournalApp() async {
   var pref = await SharedPreferences.getInstance();
   var onBoardingCompleted = pref.getBool("onBoardingCompleted") ?? false;
 
diff --git a/pubspec.lock b/pubspec.lock
index 4d2f4bc2..e95f615b 100644
--- a/pubspec.lock
+++ b/pubspec.lock
@@ -92,6 +92,13 @@ packages:
     description: flutter
     source: sdk
     version: "0.0.0"
+  flutter_crashlytics:
+    dependency: "direct main"
+    description:
+      name: flutter_crashlytics
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "0.2.0"
   flutter_launcher_icons:
     dependency: "direct dev"
     description:
diff --git a/pubspec.yaml b/pubspec.yaml
index 19024abd..efb4eb71 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -9,6 +9,7 @@ dependencies:
   uuid: "^1.0.0"
   yaml: "^2.1.13"
   firebase_analytics: ^1.1.0
+  flutter_crashlytics: ^0.2.0
   shared_preferences: ^0.4.3
 
 dev_dependencies: