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

Stories library part 9.1 - added errorNotification Delete pendingIntent loader and base error notification ID setter #361

Merged
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
3 changes: 2 additions & 1 deletion app/src/main/java/com/automattic/portkey/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ class MainActivity : AppCompatActivity(), MainFragment.OnFragmentInteractionList
// we need to have a way to cancel the related error notification when the user comes
// from tapping on MANAGE on the snackbar (otherwise they'll be able to discard the
// errored story but the error notification will remain existing in the system dashboard)
intent.action = getNotificationIdForError(event.storyIndex).toString() + ""
intent.action = getNotificationIdForError(
StoryComposerActivity.BASE_FRAME_MEDIA_ERROR_NOTIFICATION_ID, event.storyIndex).toString() + ""

startActivity(intent)
}
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/automattic/portkey/StoryComposerActivity.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.automattic.portkey

import android.app.ActivityOptions
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Bundle
Expand Down Expand Up @@ -90,6 +91,15 @@ class StoryComposerActivity : ComposeLoopFrameActivity(),
return notificationIntent
}

override fun loadPendingIntentForErrorNotificationDeletion(notificationId: Int): PendingIntent? {
// demo app doesn't need to provide an implementation
return null
}

override fun setupErrorNotificationBaseId(): Int {
return BASE_FRAME_MEDIA_ERROR_NOTIFICATION_ID
}

override fun loadMetadataForStory(index: StoryIndex): Bundle? {
// this is optional, external metadata that will be returned to you after the FrameSaveService finishes
val bundle = Bundle()
Expand All @@ -101,6 +111,7 @@ class StoryComposerActivity : ComposeLoopFrameActivity(),
companion object {
const val KEY_EXAMPLE_METADATA = "key_example_metadata"
const val KEY_STORY_INDEX = "key_story_index"
const val BASE_FRAME_MEDIA_ERROR_NOTIFICATION_ID: Int = 72300
}

override fun onStoryDiscarded() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.Manifest
import android.animation.LayoutTransition
import android.annotation.SuppressLint
import android.app.Activity
import android.app.PendingIntent
import android.content.ComponentName
import android.content.Context
import android.content.Intent
Expand Down Expand Up @@ -69,6 +70,7 @@ import com.wordpress.stories.compose.emoji.EmojiPickerFragment
import com.wordpress.stories.compose.emoji.EmojiPickerFragment.EmojiListener
import com.wordpress.stories.compose.frame.FrameIndex
import com.wordpress.stories.compose.frame.FrameSaveManager
import com.wordpress.stories.compose.frame.FrameSaveNotifier
import com.wordpress.stories.compose.frame.FrameSaveService
import com.wordpress.stories.compose.frame.StorySaveEvents
import com.wordpress.stories.compose.frame.StorySaveEvents.SaveResultReason.SaveError
Expand Down Expand Up @@ -133,6 +135,8 @@ interface MediaPickerProvider {

interface NotificationIntentLoader {
fun loadIntentForErrorNotification(): Intent
fun loadPendingIntentForErrorNotificationDeletion(notificationId: Int): PendingIntent?
fun setupErrorNotificationBaseId(): Int
}

interface AuthenticationHeadersProvider {
Expand Down Expand Up @@ -199,7 +203,23 @@ abstract class ComposeLoopFrameActivity : AppCompatActivity(), OnStoryFrameSelec
}
// Setup notification intent for notifications triggered from the FrameSaveService.FrameSaveNotifier class
notificationIntentLoader?.let {
// set the base notification Error Id. This is given on purpose so the host app can give a unique
// set of notific ations ID to base our error notifications from, and avoid collision with other
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// set of notific ations ID to base our error notifications from, and avoid collision with other
// set of notifications ID to base our error notifications from, and avoid collision with other

😅

// notifications the host app may have
// IMPORTANT: this needs to be the first call in the methods linedup for NotificationIntentLoader
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// IMPORTANT: this needs to be the first call in the methods linedup for NotificationIntentLoader
// IMPORTANT: this needs to be the first call in the methods lined up for NotificationIntentLoader

frameSaveService.setNotificationErrorBaseId(
it.setupErrorNotificationBaseId()
)

frameSaveService.setNotificationIntent(it.loadIntentForErrorNotification())
val notificationId = FrameSaveNotifier.getNotificationIdForError(
frameSaveService.getNotificationErrorBaseId(),
storyIndex
)

frameSaveService.setDeleteNotificationPendingIntent(
it.loadPendingIntentForErrorNotificationDeletion(notificationId)
)
}

metadataProvider?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class FrameSaveNotifier(private val context: Context, private val service: Frame
)

// val notificationId = getNotificationIdForMedia(site)
val notificationId = getNotificationIdForError(storySaveResult.storyIndex)
val notificationId = getNotificationIdForError(service.getNotificationErrorBaseId(), storySaveResult.storyIndex)
// Tap notification intent (open the media browser)
val notificationIntent = service.getNotificationIntent()
notificationIntent.putExtra(KEY_STORY_SAVE_RESULT, storySaveResult)
Expand Down Expand Up @@ -318,14 +318,9 @@ class FrameSaveNotifier(private val context: Context, private val service: Frame
notificationBuilder.setContentIntent(pendingIntent)
notificationBuilder.setAutoCancel(true)
notificationBuilder.setOnlyAlertOnce(true)
// TODO WPANDROID add deleteIntent later when integrating with WPAndroid
// notificationBuilder.setDeleteIntent(
// NotificationsProcessingService
// .getPendingIntentForNotificationDismiss(
// mContext, notificationId.toInt(),
// notificationType
// )
// )
service.getDeleteNotificationPendingIntent()?.let {
notificationBuilder.setDeleteIntent(service.getDeleteNotificationPendingIntent())
}

// Add MANAGE action and default action
notificationBuilder.addAction(
Expand All @@ -337,8 +332,6 @@ class FrameSaveNotifier(private val context: Context, private val service: Frame
}

companion object {
private const val BASE_MEDIA_ERROR_NOTIFICATION_ID = 72300

fun buildErrorMessageForMedia(context: Context, mediaItemsNotUploaded: Int) = if (mediaItemsNotUploaded == 1) {
context.getString(R.string.story_saving_failed_message_singular)
} else {
Expand All @@ -348,11 +341,8 @@ class FrameSaveNotifier(private val context: Context, private val service: Frame
)
}

@JvmStatic fun getNotificationIdForError(storyIndex: StoryIndex): Int {
// TODO WPANDROID we keep the base number because we'll use SiteId and PostModel id's to identify the error
// notification as well, and as such we are using a different base number to avoid collision of notification
// ids.
return BASE_MEDIA_ERROR_NOTIFICATION_ID + storyIndex
@JvmStatic fun getNotificationIdForError(baseId: Int, storyIndex: StoryIndex): Int {
return baseId + storyIndex
}

@JvmStatic fun buildSnackbarErrorMessage(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.wordpress.stories.compose.frame

import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
Expand Down Expand Up @@ -36,7 +37,9 @@ class FrameSaveService : Service() {
private lateinit var frameSaveNotifier: FrameSaveNotifier
private val storySaveProcessors = ArrayList<StorySaveProcessor>()
private lateinit var notificationIntent: Intent
private var deleteNotificationPendingIntent: PendingIntent? = null
private var optionalMetadata: Bundle? = null // keeps optional metadata about the Story
private var notificationErrorBaseId: Int = 700 // default

override fun onCreate() {
super.onCreate()
Expand Down Expand Up @@ -75,6 +78,14 @@ class FrameSaveService : Service() {
return notificationIntent
}

fun setDeleteNotificationPendingIntent(pendingIntent: PendingIntent?) {
deleteNotificationPendingIntent = pendingIntent
}

fun getDeleteNotificationPendingIntent(): PendingIntent? {
return deleteNotificationPendingIntent
}

fun setMetadata(bundle: Bundle?) {
optionalMetadata = bundle
}
Expand All @@ -83,6 +94,14 @@ class FrameSaveService : Service() {
return optionalMetadata
}

fun setNotificationErrorBaseId(baseId: Int) {
notificationErrorBaseId = baseId
}

fun getNotificationErrorBaseId(): Int {
return notificationErrorBaseId
}

fun saveStoryFrames(
storyIndex: Int,
photoEditor: PhotoEditor,
Expand Down