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

Integrated Dynamic Link and its handling for product share #73

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
14 changes: 12 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if (flutterRoot == null) {

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
flutterVersionCode = '3'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
Expand All @@ -26,8 +26,15 @@ apply plugin: 'com.google.gms.google-services'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

rootProject.ext {
set('FlutterFire', [
FirebaseSDKVersion: '25.12.0'
])
}

android {
compileSdkVersion 28
compileSdkVersion 30
buildToolsVersion '29.0.0'

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand All @@ -44,6 +51,7 @@ android {
targetSdkVersion 29
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
multiDexEnabled true
}

buildTypes {
Expand All @@ -61,6 +69,8 @@ flutter {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.google.firebase:firebase-dynamic-links'
implementation platform('com.google.firebase:firebase-bom:26.6.0')
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.android.support:multidex:1.0.3'
}
14 changes: 12 additions & 2 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
In most cases you can leave this as-is, but you if you want to provide
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name="io.flutter.app.FlutterApplication"
android:label="retro_shopping"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:name="io.flutter.embedding.android.FlutterActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
Expand All @@ -37,6 +37,16 @@
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data
android:host="relicbazaar.page.link"
android:scheme="https" />
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {

dependencies {
classpath 'com.google.gms:google-services:4.3.5'
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand Down
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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-5.6.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
22 changes: 11 additions & 11 deletions android/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

include ':app'

def localPropertiesFile = new File(rootProject.projectDir, "local.properties")
def properties = new Properties()
def flutterProjectRoot = rootProject.projectDir.parentFile.toPath()

assert localPropertiesFile.exists()
localPropertiesFile.withReader("UTF-8") { reader -> properties.load(reader) }
def plugins = new Properties()
def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins')
if (pluginsFile.exists()) {
pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) }
}

def flutterSdkPath = properties.getProperty("flutter.sdk")
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
apply from: "$flutterSdkPath/packages/flutter_tools/gradle/app_plugin_loader.gradle"
plugins.each { name, path ->
def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile()
include ":$name"
project(":$name").projectDir = pluginDirectory
}
1 change: 1 addition & 0 deletions android/settings_aar.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ':app'
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MyApp extends StatelessWidget {
routes: <String, WidgetBuilder>{
'/dashboard': (BuildContext context) => Dashboard(),
},
home: Splash(),
home: Splash(initLink: true),
);
}
}
36 changes: 36 additions & 0 deletions lib/services/dynamicLink.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:firebase_dynamic_links/firebase_dynamic_links.dart';
import 'package:flutter/material.dart';

class DynamicLinkService {
static Future<Uri> createDynamicLink(
{int height,
String text,
String owner,
String image,
String seller,
String amount,
bool isProduct = false}) async {
final DynamicLinkParameters parameters = DynamicLinkParameters(
uriPrefix: 'https://relicbazaar.page.link',
// text: text,
// owner: owner,
// image: image,
// prodHeight: height,
// seller: seller,
// amount: amount,
link: isProduct
? Uri.parse(
'https://relicbazaar.page.link.com/?isProduct=$isProduct&text=$text&height=$height&image=$image&seller=$seller&amount=$amount&owner=$owner')
: Uri.parse(
'https://relicbazaar.page.link.com/?isProduct=$isProduct'),
androidParameters: AndroidParameters(
packageName: 'com.example.retro_shopping',
),
);
Uri url;
final ShortDynamicLink shortLink = await parameters.buildShortLink();
url = shortLink.shortUrl;
print(url);
return url;
}
}
65 changes: 59 additions & 6 deletions lib/widgets/product/product_page.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import 'dart:io';

import 'package:esys_flutter_share/esys_flutter_share.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:retro_shopping/helpers/constants.dart';

import '../../helpers/app_icons.dart';
import 'package:retro_shopping/services/dynamicLink.dart';
import '../retro_button.dart';

// ignore: must_be_immutable
Expand All @@ -25,6 +31,36 @@ class ProductPage extends StatefulWidget {
}

class _ProductPageState extends State<ProductPage> {
Uri uri;
@override
void initState() {
super.initState();
_getUrl();
}

Future<void> _getUrl() async {
uri = await DynamicLinkService.createDynamicLink(
isProduct: true,
text: widget.text,
image: widget.image,
owner: widget.owner,
seller: widget.seller,
height: widget.prodHeight,
amount: widget.amount);
}

Future<void> _shareWithImage() async {
try {
var bytes = await rootBundle.load(widget.image);
await Share.file('${widget.text}', '${widget.text}.png',
bytes.buffer.asUint8List(), 'image/png',
text:
'Checkout this amazing product ${widget.text} on ${uri.toString()}');
} catch (err) {
print(err);
}
}

@override
Widget build(BuildContext context) {
final double height = MediaQuery.of(context).size.height;
Expand Down Expand Up @@ -329,7 +365,7 @@ class _ProductPageState extends State<ProductPage> {
),
),
Transform.translate(
offset: const Offset(145, 375),
offset: Offset(80, 375),
child: Row(
children: <Widget>[
RetroButton(
Expand All @@ -338,13 +374,30 @@ class _ProductPageState extends State<ProductPage> {
width: width * 0.12,
height: height * 0.05,
borderColor: Colors.white,
child: const Center(
child: Icon(
Icons.favorite,
color: RelicColors.primaryColor,
),
],
),
),
Transform.translate(
offset: Offset(145, 375),
child: Row(
children: [
InkWell(
onTap: _shareWithImage,
child: RetroButton(
child: Center(
child: Icon(
Icons.share,
color: RelicColors.primaryColor,
),
),
upperColor: Colors.white,
lowerColor: Colors.black,
width: width * 0.12,
height: height * 0.05,
borderColor: Colors.white,
),
)
),
],
),
),
Expand Down
68 changes: 62 additions & 6 deletions lib/widgets/splash.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:firebase_dynamic_links/firebase_dynamic_links.dart';
import 'package:retro_shopping/helpers/slide_route.dart';
import 'package:retro_shopping/widgets/product/product_page.dart';
import 'package:firebase_core/firebase_core.dart';

class Splash extends StatefulWidget {
final bool initLink;
Splash({this.initLink});
@override
_SplashState createState() => _SplashState();
}
Expand All @@ -10,16 +16,66 @@ class _SplashState extends State<Splash> {
@override
void initState() {
super.initState();
startTime();
_initLink();
}

Future<Timer> startTime() async {
const Duration _duration = Duration(seconds: 3);
return Timer(_duration, navigationPage);
@override
void dispose() {
super.dispose();
}

Future initDynamicLinks() async {
final PendingDynamicLinkData data =
await FirebaseDynamicLinks.instance.getInitialLink();
if (data == null) {
Navigator.of(context).pushReplacementNamed('/dashboard');
}

await _handleDeepLink(data);

// Register a link callback to fire if the app is opened up from the background
// using a dynamic link.
FirebaseDynamicLinks.instance.onLink(
onSuccess: (PendingDynamicLinkData dynamicLink) async {
// handle link that has been retrieved
await _handleDeepLink(dynamicLink);
}, onError: (OnLinkErrorException e) async {
print('Link Failed: ${e.message}');
});
}

Future<void> _handleDeepLink(PendingDynamicLinkData data) async {
final Uri deepLink = data?.link;
if (deepLink != null) {
if (deepLink.queryParameters['isProduct'] == 'true') {
Navigator.of(context).pushReplacementNamed('/dashboard');
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => ProductPage(
text: deepLink.queryParameters['text'],
owner: deepLink.queryParameters['owner'],
image: deepLink.queryParameters['image'],
prodHeight:
int.tryParse(deepLink.queryParameters['height']),
seller: deepLink.queryParameters['seller'],
amount: deepLink.queryParameters['amount'],
)));
}
}
}

void navigationPage() {
Navigator.of(context).pushReplacementNamed('/dashboard');
_initLink() async {
Future.delayed(
Duration(milliseconds: 300),
() async {
if (widget.initLink)
await initDynamicLinks();
else {
Navigator.of(context).pushReplacementNamed('/dashboard');
}
},
);
}

@override
Expand Down
8 changes: 6 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ dependencies:
adobe_xd: ^1.1.0+1
cupertino_icons: ^0.1.3
fluttertoast: ^7.1.8
firebase_core: ^1.0.0
firebase_dynamic_links: ^0.6.3
esys_flutter_share: ^1.0.2


firebase_core: ^0.5.3
google_fonts: ^1.1.0
razorpay_flutter: 1.1.0


dependency_overrides:
flutter_svg: ^0.20.0-nullsafety.3
flutter_svg: 0.19.3

dev_dependencies:
flutter_test:
Expand Down
30 changes: 0 additions & 30 deletions test/widget_test.dart

This file was deleted.