Skip to content

Commit

Permalink
Refactor bookmark show deep linking
Browse files Browse the repository at this point in the history
  • Loading branch information
MiSikora committed Jul 8, 2024
1 parent aa91c16 commit b0ee92a
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,25 @@ class DeepLinkFactoryTest {

assertNull(deepLink)
}

@Test
fun showBookmark() {
val intent = Intent()
.setAction("INTENT_OPEN_APP_VIEW_BOOKMARKS")
.putExtra("bookmark_uuid", "bookmark-id")

val deepLink = factory.create(intent)

assertEquals(ShowBookmarkDeepLink("bookmark-id"), deepLink)
}

@Test
fun showBookmarkWithoutBookmarkUuid() {
val intent = Intent()
.setAction("INTENT_OPEN_APP_VIEW_BOOKMARKS")

val deepLink = factory.create(intent)

assertNull(deepLink)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package au.com.shiftyjelly.pocketcasts.deeplink

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class ShowBookmarkDeepLinkTest {
private val context = InstrumentationRegistry.getInstrumentation().context

@Test
fun createShowBookmarkIntent() {
val intent = ShowBookmarkDeepLink("bookmark-id").toIntent(context)

assertEquals("INTENT_OPEN_APP_VIEW_BOOKMARKS", intent.action)
assertEquals("bookmark-id", intent.getStringExtra("bookmark_uuid"))
assertEquals("bookmark_uuid_bookmark-id", intent.getStringExtra("NOTIFICATION_TAG"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import au.com.shiftyjelly.pocketcasts.deeplink.AddBookmarkDeepLink
import au.com.shiftyjelly.pocketcasts.deeplink.ChangeBookmarkTitleDeepLink
import au.com.shiftyjelly.pocketcasts.deeplink.DeepLinkFactory
import au.com.shiftyjelly.pocketcasts.deeplink.DownloadsDeepLink
import au.com.shiftyjelly.pocketcasts.deeplink.ShowBookmarkDeepLink
import au.com.shiftyjelly.pocketcasts.discover.view.DiscoverFragment
import au.com.shiftyjelly.pocketcasts.endofyear.StoriesFragment
import au.com.shiftyjelly.pocketcasts.endofyear.StoriesFragment.StoriesSource
Expand Down Expand Up @@ -1248,10 +1249,9 @@ class MainActivity :
}
notificationHelper.removeNotification(intent.extras, Settings.NotificationId.BOOKMARK.value)
}
}
} else if (action == Settings.INTENT_OPEN_APP_VIEW_BOOKMARKS) {
intent.getStringExtra(BOOKMARK_UUID)?.let {
viewModel.viewBookmark(it)
is ShowBookmarkDeepLink -> {
viewModel.viewBookmark(deepLink.bookmarkUuid)
}
}
} else if (action == Settings.INTENT_OPEN_APP_DELETE_BOOKMARK) {
intent.getStringExtra(BOOKMARK_UUID)?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package au.com.shiftyjelly.pocketcasts.deeplink
import android.content.Context
import android.content.Intent
import au.com.shiftyjelly.pocketcasts.deeplink.DeepLink.Companion.ACTION_OPEN_ADD_BOOKMARK
import au.com.shiftyjelly.pocketcasts.deeplink.DeepLink.Companion.ACTION_OPEN_BOOKMARK
import au.com.shiftyjelly.pocketcasts.deeplink.DeepLink.Companion.ACTION_OPEN_CHANGE_BOOKMARK_TITLE
import au.com.shiftyjelly.pocketcasts.deeplink.DeepLink.Companion.ACTION_OPEN_DOWNLOADS
import au.com.shiftyjelly.pocketcasts.deeplink.DeepLink.Companion.EXTRA_BOOKMARK_UUID
Expand All @@ -15,6 +16,7 @@ sealed interface DeepLink {
const val ACTION_OPEN_DOWNLOADS = "INTENT_OPEN_APP_DOWNLOADING"
const val ACTION_OPEN_ADD_BOOKMARK = "INTENT_OPEN_APP_ADD_BOOKMARK"
const val ACTION_OPEN_CHANGE_BOOKMARK_TITLE = "INTENT_OPEN_APP_CHANGE_BOOKMARK_TITLE"
const val ACTION_OPEN_BOOKMARK = "INTENT_OPEN_APP_VIEW_BOOKMARKS"

const val EXTRA_BOOKMARK_UUID = "bookmark_uuid"
const val EXTRA_NOTIFICATION_TAG = "NOTIFICATION_TAG"
Expand All @@ -41,6 +43,15 @@ data class ChangeBookmarkTitleDeepLink(
.putExtra(EXTRA_NOTIFICATION_TAG, "${EXTRA_BOOKMARK_UUID}_$bookmarkUuid")
}

data class ShowBookmarkDeepLink(
val bookmarkUuid: String,
) : DeepLink {
override fun toIntent(context: Context) = context.launcherIntent
.setAction(ACTION_OPEN_BOOKMARK)
.putExtra(EXTRA_BOOKMARK_UUID, bookmarkUuid)
.putExtra(EXTRA_NOTIFICATION_TAG, "${EXTRA_BOOKMARK_UUID}_$bookmarkUuid")
}

private val Context.launcherIntent get() = requireNotNull(packageManager.getLaunchIntentForPackage(packageName)) {
"Missing launcher intent for $packageName"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package au.com.shiftyjelly.pocketcasts.deeplink

import android.content.Intent
import au.com.shiftyjelly.pocketcasts.deeplink.DeepLink.Companion.ACTION_OPEN_ADD_BOOKMARK
import au.com.shiftyjelly.pocketcasts.deeplink.DeepLink.Companion.ACTION_OPEN_BOOKMARK
import au.com.shiftyjelly.pocketcasts.deeplink.DeepLink.Companion.ACTION_OPEN_CHANGE_BOOKMARK_TITLE
import au.com.shiftyjelly.pocketcasts.deeplink.DeepLink.Companion.ACTION_OPEN_DOWNLOADS
import au.com.shiftyjelly.pocketcasts.deeplink.DeepLink.Companion.EXTRA_BOOKMARK_UUID
Expand All @@ -12,6 +13,7 @@ class DeepLinkFactory {
DownloadsAdapter(),
AddBookmarkAdapter(),
ChangeBookmarkTitleAdapter(),
ShowBookmarkAdapter(),
)

fun create(intent: Intent): DeepLink? {
Expand Down Expand Up @@ -66,3 +68,11 @@ private class ChangeBookmarkTitleAdapter : DeepLinkAdapter {
null
}
}

private class ShowBookmarkAdapter : DeepLinkAdapter {
override fun create(intent: Intent) = if (intent.action == ACTION_OPEN_BOOKMARK) {
intent.getStringExtra(EXTRA_BOOKMARK_UUID)?.let(::ShowBookmarkDeepLink)
} else {
null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ interface Settings {
const val INTENT_OPEN_APP_PODCAST_UUID = "INTENT_OPEN_APP_PODCAST_UUID"
const val INTENT_OPEN_APP_EPISODE_UUID = "INTENT_OPEN_APP_EPISODE_UUID"
const val INTENT_OPEN_APP_DELETE_BOOKMARK = "INTENT_OPEN_APP_DELETE_BOOKMARK"
const val INTENT_OPEN_APP_VIEW_BOOKMARKS = "INTENT_OPEN_APP_VIEW_BOOKMARKS"
const val INTENT_LINK_CLOUD_FILES = "pktc://cloudfiles"
const val INTENT_LINK_UPGRADE = "pktc://upgrade"
const val INTENT_LINK_PROMO_CODE = "pktc://redeem/promo/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import au.com.shiftyjelly.pocketcasts.deeplink.AddBookmarkDeepLink
import au.com.shiftyjelly.pocketcasts.deeplink.ChangeBookmarkTitleDeepLink
import au.com.shiftyjelly.pocketcasts.deeplink.ShowBookmarkDeepLink
import au.com.shiftyjelly.pocketcasts.preferences.Settings
import au.com.shiftyjelly.pocketcasts.preferences.Settings.Companion.INTENT_OPEN_APP_DELETE_BOOKMARK
import au.com.shiftyjelly.pocketcasts.preferences.Settings.Companion.INTENT_OPEN_APP_VIEW_BOOKMARKS
import au.com.shiftyjelly.pocketcasts.repositories.R
import au.com.shiftyjelly.pocketcasts.repositories.playback.PlaybackManager
import au.com.shiftyjelly.pocketcasts.repositories.sync.NotificationBroadcastReceiver.Companion.INTENT_EXTRA_NOTIFICATION_TAG
Expand Down Expand Up @@ -101,13 +101,7 @@ private fun buildAndShowNotification(
.setSmallIcon(IR.drawable.notification)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setContentIntent(
buildPendingIntent(
context,
INTENT_OPEN_APP_VIEW_BOOKMARKS,
bookmarkUuid,
),
)
.setContentIntent(buildPendingIntent(context, ShowBookmarkDeepLink(bookmarkUuid).toIntent(context)))
.addAction(changeTitleAction)
.addAction(deleteAction)
.build()
Expand Down

0 comments on commit b0ee92a

Please sign in to comment.