Skip to content

Commit

Permalink
Merge pull request #37 from apptentive/PBI-5217-removeobserver
Browse files Browse the repository at this point in the history
PBI-5217 Support for removeObserver for survey and message callbacks
  • Loading branch information
PoornimaApptentive committed Jul 12, 2023
2 parents 9f34879 + 393d744 commit f33eb13
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 34 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ android {
}

dependencies {
implementation 'com.apptentive:apptentive-kit-android:6.0.4'
implementation 'com.apptentive:apptentive-kit-android:6.0.5'
testImplementation 'junit:junit:4.13.2'
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package com.apptentive.apptentive_flutter
import android.app.Activity
import android.app.Application
import apptentive.com.android.feedback.*
import apptentive.com.android.feedback.model.EventNotification
import apptentive.com.android.feedback.model.MessageCenterNotification
import apptentive.com.android.util.*
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
Expand Down Expand Up @@ -104,6 +106,7 @@ class ApptentiveFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAware
"setPushNotificationIntegration" -> setPushNotificationIntegration(call, result)
"getUnreadMessageCount" -> getUnreadMessageCount(result)
"registerListeners" -> registerListeners(result)
"unregisterListeners" -> unregisterListeners(result)
"sendAttachmentText" -> sendAttachmentText(call, result)
"handleRequestPushPermissions" -> { /* Only iOS. */ }
else -> result.notImplemented()
Expand Down Expand Up @@ -319,50 +322,56 @@ class ApptentiveFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAware
// Register listeners for native callbacks:
private fun registerListeners(result: Result) {
try {
registerForSurveyListener()
registerForUnreadMessageListener()
Apptentive.eventNotificationObservable.observe(::surveyFinishedObserver)
Apptentive.messageCenterNotificationObservable.observe(::messageObserver)
result.success(true)
} catch(e: Exception) {
result.error(ERROR_CODE, "Failed to register Apptentive listeners.", e.toString())
}
}

private fun registerForSurveyListener() {
Apptentive.eventNotificationObservable.observe { notification ->
val name = notification?.name
val interaction = notification?.interaction
val vendor = notification?.vendor
val interactionId = notification?.interactionId
val notificationText = "Name: \"$name\". Vendor: \"$vendor\". " +
"Interaction: \"$interaction\". Interaction ID: $interactionId"
Log.d(LogTags.EVENT_NOTIFICATION, notificationText)
when {
interaction == "Survey" && name == "submit" ->
activity?.runOnUiThread {
channel.invokeMethod("onSurveyFinished", mapOf("completed" to true))
}

interaction == "Survey" && name == "cancel" || name == "cancel_partial" ->
activity?.runOnUiThread {
channel.invokeMethod("onSurveyFinished", mapOf("completed" to false))
}
}
private fun unregisterListeners(result: Result) {
try {
Apptentive.eventNotificationObservable.removeObserver(::surveyFinishedObserver)
Apptentive.messageCenterNotificationObservable.removeObserver(::messageObserver)
result.success(true)
} catch(e: Exception) {
result.error(ERROR_CODE, "Failed to unregister Apptentive listeners.", e.toString())
}
}

private fun registerForUnreadMessageListener() {
Apptentive.messageCenterNotificationObservable.observe { notification ->
val notificationText =
"Can Show Message Center: ${notification?.canShowMessageCenter}. " +
"Unread Message Count: ${notification?.unreadMessageCount}. " +
"Person Name: ${notification?.personName}. " +
"Person Email: ${notification?.personEmail}"
private fun surveyFinishedObserver(notification: EventNotification?) {
val name = notification?.name
val interaction = notification?.interaction
val vendor = notification?.vendor
val interactionId = notification?.interactionId
val notificationText = "Name: \"$name\". Vendor: \"$vendor\". " +
"Interaction: \"$interaction\". Interaction ID: $interactionId"
Log.d(LogTags.EVENT_NOTIFICATION, notificationText)
when {
interaction == "Survey" && name == "submit" ->
activity?.runOnUiThread {
channel.invokeMethod("onSurveyFinished", mapOf("completed" to true))
}

Log.d(LogTags.MESSAGE_CENTER_NOTIFICATION, notificationText)
if (notification?.unreadMessageCount != 0) {
interaction == "Survey" && name == "cancel" || name == "cancel_partial" ->
activity?.runOnUiThread {
channel.invokeMethod("onUnreadMessageCountChanged", mapOf("count" to notification?.unreadMessageCount))
channel.invokeMethod("onSurveyFinished", mapOf("completed" to false))
}
}
}

private fun messageObserver(notification: MessageCenterNotification?) {
val notificationText =
"Can Show Message Center: ${notification?.canShowMessageCenter}. " +
"Unread Message Count: ${notification?.unreadMessageCount}. " +
"Person Name: ${notification?.personName}. " +
"Person Email: ${notification?.personEmail}"

Log.d(LogTags.MESSAGE_CENTER_NOTIFICATION, notificationText)
if (notification?.unreadMessageCount != 0) {
activity?.runOnUiThread {
channel.invokeMethod("onUnreadMessageCountChanged", mapOf("count" to notification?.unreadMessageCount))
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,12 @@ class _MyAppState extends State<MyApp> {
},
child: Text('Register Listeners'),
),

OutlinedButton(
onPressed: () {
ApptentiveFlutter.unregisterListeners();
},
child: Text('Unregister Listeners'),
),
OutlinedButton(
onPressed: () {
if (integration_token != null) {
Expand Down
6 changes: 6 additions & 0 deletions lib/apptentive_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ class ApptentiveFlutter {
return successful;
}

// Unregister callback listeners
static Future<bool> unregisterListeners() async {
final bool successful = await _channel.invokeMethod('unregisterListeners', {});
return successful;
}

static Future<bool> sendAttachmentText({required String message}) async {
final bool successful = await _channel.invokeMethod('sendAttachmentText', {
"message": message
Expand Down

0 comments on commit f33eb13

Please sign in to comment.