From af16d907896eaa1c5327abdead630bb5eb4d63df Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Tue, 17 Mar 2020 12:11:03 +0100 Subject: [PATCH] add android example --- .gitignore | 3 ++- example/android/app/build.gradle | 3 +++ .../android/app/src/main/AndroidManifest.xml | 6 ++++- .../kotlin/com/example/example/Application.kt | 22 +++++++++++++++++++ example/android/build.gradle | 1 + example/lib/main.dart | 5 ++--- lib/src/channel_list_view.dart | 2 +- lib/src/stream_chat.dart | 21 +++++++++++++++--- 8 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 example/android/app/src/main/kotlin/com/example/example/Application.kt diff --git a/.gitignore b/.gitignore index 28b825a6..457042a0 100644 --- a/.gitignore +++ b/.gitignore @@ -59,4 +59,5 @@ doc/api/ *.js.deps *.js.map -fvm \ No newline at end of file +fvm +google-services.json \ No newline at end of file diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 0f6a5e54..dda667cf 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -64,4 +64,7 @@ dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' + implementation 'com.google.firebase:firebase-messaging:20.1.2' } + +apply plugin: 'com.google.gms.google-services' diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index 1b2ad157..c5f1e85b 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -7,7 +7,7 @@ FlutterApplication and put your custom class here. --> + + + + diff --git a/example/android/app/src/main/kotlin/com/example/example/Application.kt b/example/android/app/src/main/kotlin/com/example/example/Application.kt new file mode 100644 index 00000000..dabf2307 --- /dev/null +++ b/example/android/app/src/main/kotlin/com/example/example/Application.kt @@ -0,0 +1,22 @@ +package com.example.example + +import io.flutter.app.FlutterApplication +import io.flutter.plugin.common.PluginRegistry +import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback +import io.flutter.plugins.GeneratedPluginRegistrant +import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin +import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService +import io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin + +class Application : FlutterApplication(), PluginRegistrantCallback { + override fun onCreate() { + super.onCreate() + FlutterFirebaseMessagingService.setPluginRegistrant(this) + } + + override fun registerWith(registry: PluginRegistry?) { + SharedPreferencesPlugin.registerWith(registry?.registrarFor( + "io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin")); + FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin")) + } +} \ No newline at end of file diff --git a/example/android/build.gradle b/example/android/build.gradle index 3100ad2d..70b3637d 100644 --- a/example/android/build.gradle +++ b/example/android/build.gradle @@ -8,6 +8,7 @@ buildscript { dependencies { classpath 'com.android.tools.build:gradle:3.5.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + classpath 'com.google.gms:google-services:4.3.2' } } diff --git a/example/lib/main.dart b/example/lib/main.dart index cb77a82a..9d8dc1fa 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -5,12 +5,11 @@ void main() async { final client = Client( 's2dxdhpxd94g', logLevel: Level.INFO, - persistenceEnabled: false, ); await client.setUser( - User(id: 'super-band-9'), - 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoic3VwZXItYmFuZC05In0.0L6lGoeLwkz0aZRUcpZKsvaXtNEDHBcezVTZ0oPq40A', + User(id: 'super-band-007'), + 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoic3VwZXItYmFuZC0wMDcifQ.DlbyHN6nK7jBTJDp6RJOuBJWwnYoy8Zq8208RPL4uLY', ); runApp(MyApp(client)); diff --git a/lib/src/channel_list_view.dart b/lib/src/channel_list_view.dart index 14b68bf4..67fc3c5f 100644 --- a/lib/src/channel_list_view.dart +++ b/lib/src/channel_list_view.dart @@ -116,7 +116,7 @@ class _ChannelListViewState extends State { child: StreamBuilder>( stream: streamChat.channelsStream, builder: (context, snapshot) { - if (snapshot.hasError) { + if (false && snapshot.hasError) { if (snapshot.error is Error) { print((snapshot.error as Error).stackTrace); } diff --git a/lib/src/stream_chat.dart b/lib/src/stream_chat.dart index e18de6d0..554afaa1 100644 --- a/lib/src/stream_chat.dart +++ b/lib/src/stream_chat.dart @@ -59,7 +59,7 @@ class StreamChat extends StatefulWidget { } } -class StreamChatState extends State { +class StreamChatState extends State with WidgetsBindingObserver { final List _subscriptions = []; Client get client => widget.client; final GlobalKey _navigatorKey = GlobalKey(); @@ -212,8 +212,22 @@ class StreamChatState extends State { _queryChannelsLoadingController.sink.add(false); } catch (err, stackTrace) { _queryChannelsLoadingController.addError(err, stackTrace); - } finally { -// _queryChannelsLoadingController.sink.add(false); + } + } + + @override + void initState() { + super.initState(); + WidgetsBinding.instance.addObserver(this); + } + + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + print('NEW STATE $state'); + if (state == AppLifecycleState.paused) { + client.disconnect(); + } else if (state == AppLifecycleState.resumed) { + client.connect(); } } @@ -221,6 +235,7 @@ class StreamChatState extends State { void dispose() { _subscriptions.forEach((s) => s.cancel()); _queryChannelsLoadingController.close(); + WidgetsBinding.instance.removeObserver(this); super.dispose(); } }