Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
#699 Add ML Kit support
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed May 9, 2018
1 parent e2f89ee commit 5ec786b
Show file tree
Hide file tree
Showing 107 changed files with 2,396 additions and 326 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ src/**/*.d.ts
!src/index.d.ts
!src/references.d.ts
!src/analytics/analytics.d.ts
!src/mlkit/mlkit.d.ts
!src/platforms/web/typings/firebase-webapi.d.ts
!src/platforms/ios/typings/*.d.ts
!src/platforms/android/typings/**/*.d.ts
Expand All @@ -30,3 +31,4 @@ demo/report/report.html
demo/report/stats.json
!publish/scripts/*.js
publish/package
/npm-debug.log
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* [Analytics](docs/ANALYTICS.md)
* [Invites and Dynamic Links](docs/INVITES_DYNAMICLINKS.md)
* [AdMob](docs/ADMOB.md)
* [ML Kit](docs/ML_KIT.md)

## Prerequisites
Head on over to [https://console.firebase.google.com/](https://console.firebase.google.com/) and sign up for a free account.
Expand Down
4 changes: 3 additions & 1 deletion demo-ng/firebase.nativescript.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"using_ios": true,
"using_android": true,
"realtimedb": false,
"firestore": true,
"remote_config": true,
"messaging": true,
Expand All @@ -10,5 +11,6 @@
"facebook_auth": true,
"google_auth": true,
"admob": true,
"invites": true
"invites": true,
"ml_kit": true
}
4 changes: 4 additions & 0 deletions demo/app/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ button {
background-color: #BBBB77;
}

.button-mlkit {
background-color: orange;
}

.button-messaging {
background-color: #6d996c;
}
Expand Down
6 changes: 6 additions & 0 deletions demo/app/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import "./bundle-config";
import * as application from 'tns-core-modules/application';
import { UnhandledErrorEventData } from "tns-core-modules/application";

// added this here so we can do some wiring
require("nativescript-plugin-firebase");

// testing this.. we may be able to hook that up to Crashlytics. Either via docs, or automatically.
application.on(application.uncaughtErrorEvent, (args: UnhandledErrorEventData) => {
console.log("[app.js]: Uncaught NativeScript Error: " + args.error);
});

application.run({ moduleName: "main-page" });
Binary file added demo/app/images/EAN.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/app/images/please_walk_on_the_grass.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/app/images/qrcode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion demo/app/main-page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<TabViewItem.view>
<ScrollView>
<GridLayout columns="*, *"
rows="auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto"
rows="auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto"
horizontalAlignment="stretch"
class="tab-content">

Expand Down Expand Up @@ -194,6 +194,13 @@

<Button row="42" col="0" text="send invitation" tap="{{ sendInvitation }}" class="button button-invites"/>
<Button row="42" col="1" text="get invitation" tap="{{ getInvitation }}" class="button button-invites"/>

<Label row="43" colSpan="2" text="ML Kit" class="subtitle"/>

<Button row="44" col="0" text="recognize text" tap="{{ recognizeText }}" class="button button-mlkit"/>
<Button row="44" col="1" text="scan barcode" tap="{{ scanBarcode }}" class="button button-mlkit"/>

<Label row="45" colSpan="2" text="{{ mlOutput }}" class="message" textWrap="true"/>
</GridLayout>

</ScrollView>
Expand Down
44 changes: 42 additions & 2 deletions demo/app/main-view-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { isIOS } from "tns-core-modules/platform";
import { AddEventListenerResult, User } from "nativescript-plugin-firebase";
import * as fs from "tns-core-modules/file-system";

import * as firebase from"nativescript-plugin-firebase";
import * as firebase from "nativescript-plugin-firebase";
import { MLKitRecognizeTextResult, MLKitScanBarcodesResult } from "../../src/mlkit/mlkit";
import { ImageSource } from "tns-core-modules/image-source";

const firebaseWebApi = require("nativescript-plugin-firebase/app");

declare const Crashlytics: any;
Expand Down Expand Up @@ -348,7 +351,7 @@ export class HelloWorldModel extends Observable {
setTimeout(() => {
alert({
title: "Dynamic Link!",
message: result,
message: JSON.stringify(result),
okButtonText: "Awesome!"
});
}, 500);
Expand Down Expand Up @@ -392,6 +395,43 @@ export class HelloWorldModel extends Observable {
);
}

public recognizeText(): void {
const img = new ImageSource();
// img.fromFile("~/images/telerik-logo.png")
img.fromFile("~/images/please_walk_on_the_grass.jpg")
.then(() => {
firebase.mlkit.recognizeText({
image: img
}).then(
(result: MLKitRecognizeTextResult) => {
console.log("ML Kit result: " + JSON.stringify(result));
this.set("mlOutput", JSON.stringify(result.features));
}, errorMessage => {
console.log("ML Kit error: " + errorMessage);
}
);
})
.catch(err => console.log("Error loading image: " + img));
}

public scanBarcode(): void {
const img = new ImageSource();
img.fromFile("~/images/qrcode.png")
.then(() => {
firebase.mlkit.scanBarcodes({
image: img
}).then(
(result: MLKitScanBarcodesResult) => {
console.log("ML Kit result: " + JSON.stringify(result));
this.set("mlOutput", JSON.stringify(result.features));
}, errorMessage => {
console.log("ML Kit error: " + errorMessage);
}
);
})
.catch(err => console.log("Error loading image: " + img));
}

public doSetAnalyticsUserProperty(): void {
firebase.analytics.setUserProperty({
key: "origin", // note that this needs to be preregistered, see https://support.google.com/firebase/answer/6317519?hl=en&ref_topic=6317489#create-property
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@
android:label="@string/app_name"
android:theme="@style/AppTheme">

<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id"/>

<meta-data
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
android:value="text,barcode" />

<activity
android:name="com.tns.NativeScriptActivity"
Expand Down
File renamed without changes
File renamed without changes.
File renamed without changes.
7 changes: 4 additions & 3 deletions demo/firebase.nativescript.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"using_ios": true,
"using_android": true,
"firestore": false,
"realtimedb": true,
"firestore": false,
"remote_config": true,
"messaging": true,
"crashlytics": true,
"crash_reporting": false,
"crashlytics": true,
"storage": true,
"facebook_auth": true,
"google_auth": true,
"admob": true,
"invites": true
"invites": true,
"ml_kit": true
}
2 changes: 1 addition & 1 deletion demo/nsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"appPath": "app",
"appResourcesPath": "resources"
"appResourcesPath": "app_resources"
}
8 changes: 4 additions & 4 deletions docs/AUTHENTICATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -516,11 +516,11 @@ Then add the following lines to your code and check for setup instructions for y
```

#### iOS
If you didn't choose this feature during installation you can open the `Podfile` in the plugin's `platforms/ios` folder and uncomment the `GoogleSignIn` line.
If you didn't choose this feature during installation you can open the `Podfile` in the plugin's `platforms/ios` folder and uncomment the `GoogleSignIn` line.

Make sure the URL Scheme for `REVERSED_CLIENT_ID` is in `app/App_Resources/iOS/Info.plist`:
Make sure the URL Scheme for `REVERSED_CLIENT_ID` is in `app/App_Resources/iOS/Info.plist`:

```xml
```xml
<key>CFBundleURLTypes</key>
<array>
<dict>
Expand All @@ -534,7 +534,7 @@ Then add the following lines to your code and check for setup instructions for y
</array>
</dict>
</array>
```
```

#### Android

Expand Down
4 changes: 4 additions & 0 deletions docs/ML_KIT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Just dumping a few things I should not forget to add to the doc:

- see step 3 @ https://firebase.google.com/docs/ml-kit/android/recognize-text

60 changes: 37 additions & 23 deletions publish/scripts/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ function promptQuestions() {
name: 'google_auth',
description: 'Are you using Firebase Google Authentication (y/n)',
default: 'n'
}, {
name: 'ml_kit',
description: 'Are you using ML Kit (y/n)',
default: 'n'
}, {
name: 'admob',
description: 'Are you using AdMob (y/n)',
Expand Down Expand Up @@ -228,7 +232,9 @@ function writePodFile(result) {
}
try {
fs.writeFileSync(directories.ios + '/Podfile',
`pod 'Firebase', '~> 4.11.0'
`platform :ios, '9.0'
pod 'Firebase/Core', '~> 5.0.0'
pod 'Firebase/Auth'
# Uncomment if you want to enable Realtime DB
Expand Down Expand Up @@ -268,6 +274,17 @@ end`) + `
# Uncomment if you want to enable Invites and/or Dynamic Links
` + (isSelected(result.invites) ? `` : `#`) + `pod 'Firebase/Invites'
# Uncomment if you want to enable ML Kit
` + (isSelected(result.ml_kit) ? `` : `#`) + `pod 'Firebase/MLVision'
# TODO we'de better make these conditional as well
` + (!isSelected(result.ml_kit) ? `` : `
pod 'Firebase/MLVisionBarcodeModel'
pod 'Firebase/MLVisionFaceModel'
pod 'Firebase/MLVisionLabelModel'
pod 'Firebase/MLVisionTextModel'
pod 'Firebase/MLModelInterpreter'
`) + `
# Uncomment if you want to enable Facebook Authentication
` + (isSelected(result.facebook_auth) ? `` : `#`) + `pod 'FBSDKCoreKit'
` + (isSelected(result.facebook_auth) ? `` : `#`) + `pod 'FBSDKLoginKit'
Expand Down Expand Up @@ -461,17 +478,11 @@ repositories {
}
def supportVersion = project.hasProperty("supportVersion") ? project.supportVersion : "26.0.0"
def googlePlayServicesVersion = project.hasProperty('googlePlayServicesVersion') ? project.googlePlayServicesVersion : "12.0.1"
def googlePlayServicesVersion = project.hasProperty('googlePlayServicesVersion') ? project.googlePlayServicesVersion : "15.0.0"
` + (isSelected(result.firestore) ? `
if ( VersionNumber.parse( googlePlayServicesVersion ) < VersionNumber.parse( '11.4.2' ) ) {
throw new GradleException(" googlePlayServicesVersion set too low, as you want to use firestore please update to at least 11.4.2 ( currently set to $googlePlayServicesVersion )");
}
` : `
if ( VersionNumber.parse( googlePlayServicesVersion ) < VersionNumber.parse( '9.2' ) ) {
throw new GradleException("googlePlayServicesVersion set too low, please update to at least '9.2' ( currently set to $googlePlayServicesVersion )");
if ( VersionNumber.parse( googlePlayServicesVersion ) < VersionNumber.parse( '15.0.0' ) ) {
throw new GradleException(" googlePlayServicesVersion set too low, please update to at least 15.0.0 ( currently set to $googlePlayServicesVersion )");
}
`) + `
dependencies {
compile "com.android.support:appcompat-v7:$supportVersion"
Expand All @@ -481,35 +492,38 @@ dependencies {
compile "com.android.support:support-compat:$supportVersion"
// make sure you have these versions by updating your local Android SDK's (Android Support repo and Google repo)
compile "com.google.firebase:firebase-core:$googlePlayServicesVersion"
compile "com.google.firebase:firebase-auth:$googlePlayServicesVersion"
compile "com.google.firebase:firebase-core:15.0.2"
compile "com.google.firebase:firebase-auth:15.1.0"
// for reading google-services.json and configuration
compile "com.google.android.gms:play-services-base:$googlePlayServicesVersion"
// Uncomment if you want to use the regular Database
` + (!isPresent(result.realtimedb) || isSelected(result.realtimedb) ? `` : `//`) + ` compile "com.google.firebase:firebase-database:$googlePlayServicesVersion"
` + (!isPresent(result.realtimedb) || isSelected(result.realtimedb) ? `` : `//`) + ` compile "com.google.firebase:firebase-database:15.0.0"
// Uncomment if you want to use 'Cloud Firestore'
` + (isSelected(result.firestore) ? `` : `//`) + ` compile "com.google.firebase:firebase-firestore:$googlePlayServicesVersion"
` + (isSelected(result.firestore) ? `` : `//`) + ` compile "com.google.firebase:firebase-firestore:16.0.0"
// Uncomment if you want to use 'Remote Config'
` + (isSelected(result.remote_config) ? `` : `//`) + ` compile "com.google.firebase:firebase-config:$googlePlayServicesVersion"
` + (isSelected(result.remote_config) ? `` : `//`) + ` compile "com.google.firebase:firebase-config:15.0.2"
// Uncomment if you want to use 'Crash Reporting'
` + (isSelected(result.crash_reporting) && !isSelected(result.crashlytics) ? `` : `//`) + ` compile "com.google.firebase:firebase-crash:$googlePlayServicesVersion"
` + (isSelected(result.crash_reporting) && !isSelected(result.crashlytics) ? `` : `//`) + ` compile "com.google.firebase:firebase-crash:15.0.2"
// Uncomment if you want to use 'Crashlytics'
` + (isSelected(result.crashlytics) ? `` : `//`) + ` compile "com.crashlytics.sdk.android:crashlytics:2.9.1"
// Uncomment if you want FCM (Firebase Cloud Messaging)
` + (isSelected(result.messaging) ? `` : `//`) + ` compile "com.google.firebase:firebase-messaging:$googlePlayServicesVersion"
// Uncomment if you want to use FCM (Firebase Cloud Messaging)
` + (isSelected(result.messaging) ? `` : `//`) + ` compile "com.google.firebase:firebase-messaging:15.0.2"
// Uncomment if you want to use Google Cloud Storage
` + (isSelected(result.storage) ? `` : `//`) + ` compile "com.google.firebase:firebase-storage:15.0.2"
// Uncomment if you want Google Cloud Storage
` + (isSelected(result.storage) ? `` : `//`) + ` compile "com.google.firebase:firebase-storage:$googlePlayServicesVersion"
// Uncomment if you want to use AdMob
` + (isSelected(result.admob) ? `` : `//`) + ` compile "com.google.firebase:firebase-ads:15.0.0"
// Uncomment if you want AdMob
` + (isSelected(result.admob) ? `` : `//`) + ` compile "com.google.firebase:firebase-ads:$googlePlayServicesVersion"
// Uncomment if you want to use ML Kit
` + (isSelected(result.ml_kit) ? `` : `//`) + ` compile "com.google.firebase:firebase-ml-vision:15.0.0"
// Uncomment if you need Facebook Authentication
` + (isSelected(result.facebook_auth) ? `` : `//`) + ` compile ("com.facebook.android:facebook-android-sdk:4.+"){ exclude group: 'com.google.zxing' }
Expand Down Expand Up @@ -612,7 +626,7 @@ module.exports = function($logger, $projectData) {
let gradlePattern = /classpath ('|")com\\.android\\.tools\\.build:gradle:\\d+\\.\\d+\\.\\d+('|")/;
let googleServicesPattern = /classpath ('|")com\\.google\\.gms:google-services:\\d+\\.\\d+\\.\\d+('|")/;
let latestGoogleServicesPlugin = 'classpath "com.google.gms:google-services:3.1.2"';
let latestGoogleServicesPlugin = 'classpath "com.google.gms:google-services:3.3.1"';
if (googleServicesPattern.test(buildGradleContent)) {
buildGradleContent = buildGradleContent.replace(googleServicesPattern, latestGoogleServicesPlugin);
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/firebase-common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { prompt } from "tns-core-modules/ui/dialogs";
import { firestore } from "./firebase";
import * as analytics from "./analytics/analytics";
import * as mlkit from "./mlkit/mlkit";
import * as applicationSettings from "tns-core-modules/application-settings";

export const firebase: any = {
Expand All @@ -12,6 +13,7 @@ export const firebase: any = {
_receivedNotificationCallback: null,
_dynamicLinkCallback: null,
analytics,
mlkit,
firestore: {},
invites: {
MATCH_TYPE: {
Expand Down
6 changes: 3 additions & 3 deletions src/firebase.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ const dynamicLinksEnabled = lazy(() => typeof(com.google.android.gms.appinvite)
if (firebase._dynamicLinkCallback === null) {
firebase._cachedDynamicLink = {
url: result.getLink().toString(),
matchConfidence: 1,
// matchConfidence: 1,
minimumAppVersion: result.getMinimumAppVersion()
};
} else {
setTimeout(() => {
firebase._dynamicLinkCallback({
url: result.getLink().toString(),
matchConfidence: 1,
// matchConfidence: 1,
minimumAppVersion: result.getMinimumAppVersion()
});
});
Expand Down Expand Up @@ -1911,7 +1911,7 @@ firebase.uploadFile = arg => {
updated: new Date(metadata.getUpdatedTimeMillis()),
bucket: metadata.getBucket(),
size: metadata.getSizeBytes(),
url: metadata.getDownloadUrl().toString()
// url: metadata.getDownloadUrl().toString()
});
}
});
Expand Down
Loading

0 comments on commit 5ec786b

Please sign in to comment.