Skip to content

Commit

Permalink
Update to use new APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
MiSikora committed Oct 24, 2024
1 parent ca9e64d commit dff18da
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import androidx.fragment.app.viewModels
import au.com.shiftyjelly.pocketcasts.analytics.SourceView
import au.com.shiftyjelly.pocketcasts.models.entity.PodcastEpisode
import au.com.shiftyjelly.pocketcasts.reimagine.clip.ShareClipViewModel.SnackbarMessage
import au.com.shiftyjelly.pocketcasts.reimagine.ui.BackgroundAssetController
import au.com.shiftyjelly.pocketcasts.reimagine.ui.ShareColors
import au.com.shiftyjelly.pocketcasts.reimagine.ui.rememberBackgroundAssetControler
import au.com.shiftyjelly.pocketcasts.sharing.Clip
import au.com.shiftyjelly.pocketcasts.sharing.SharingClient
import au.com.shiftyjelly.pocketcasts.sharing.SocialPlatform
Expand Down Expand Up @@ -93,14 +93,14 @@ class ShareClipFragment : BaseDialogFragment() {
savedInstanceState: Bundle?,
) = ComposeView(requireActivity()).apply {
val platforms = SocialPlatform.getAvailablePlatforms(requireContext())
val assetController = BackgroundAssetController.create(requireContext().applicationContext, shareColors)
val listener = ShareClipListener(this@ShareClipFragment, viewModel, assetController, args.source)
val isTalkbackOn = Util.isTalkbackOn(requireContext())

setContent {
val uiState by viewModel.uiState.collectAsState()
val assetController = rememberBackgroundAssetControler(shareColors)
val listener = remember { ShareClipListener(this@ShareClipFragment, viewModel, assetController, args.source) }
val snackbarHostState = remember { SnackbarHostState() }

val uiState by viewModel.uiState.collectAsState()
ShareClipPage(
episode = uiState.episode,
podcast = uiState.podcast,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.view.ViewGroup
import androidx.annotation.ColorInt
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.ComposeView
Expand All @@ -16,8 +17,8 @@ import androidx.core.os.bundleOf
import androidx.fragment.app.viewModels
import au.com.shiftyjelly.pocketcasts.analytics.SourceView
import au.com.shiftyjelly.pocketcasts.models.entity.PodcastEpisode
import au.com.shiftyjelly.pocketcasts.reimagine.ui.BackgroundAssetController
import au.com.shiftyjelly.pocketcasts.reimagine.ui.ShareColors
import au.com.shiftyjelly.pocketcasts.reimagine.ui.rememberBackgroundAssetControler
import au.com.shiftyjelly.pocketcasts.sharing.SocialPlatform
import au.com.shiftyjelly.pocketcasts.utils.parceler.ColorParceler
import au.com.shiftyjelly.pocketcasts.views.fragments.BaseDialogFragment
Expand Down Expand Up @@ -60,9 +61,10 @@ class ShareEpisodeFragment : BaseDialogFragment() {
savedInstanceState: Bundle?,
) = ComposeView(requireActivity()).apply {
val platforms = SocialPlatform.getAvailablePlatforms(requireContext())
val assetController = BackgroundAssetController.create(requireContext(), shareColors)
val listener = shareListenerFactory.create(this@ShareEpisodeFragment, assetController, args.source)
setContent {
val assetController = rememberBackgroundAssetControler(shareColors)
val listener = remember { shareListenerFactory.create(this@ShareEpisodeFragment, assetController, args.source) }

val uiState by viewModel.uiState.collectAsState()
ShareEpisodePage(
podcast = uiState.podcast,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.ComposeView
Expand All @@ -15,8 +16,8 @@ import androidx.core.os.bundleOf
import androidx.fragment.app.viewModels
import au.com.shiftyjelly.pocketcasts.analytics.SourceView
import au.com.shiftyjelly.pocketcasts.models.entity.Podcast
import au.com.shiftyjelly.pocketcasts.reimagine.ui.BackgroundAssetController
import au.com.shiftyjelly.pocketcasts.reimagine.ui.ShareColors
import au.com.shiftyjelly.pocketcasts.reimagine.ui.rememberBackgroundAssetControler
import au.com.shiftyjelly.pocketcasts.sharing.SocialPlatform
import au.com.shiftyjelly.pocketcasts.utils.parceler.ColorParceler
import au.com.shiftyjelly.pocketcasts.views.fragments.BaseDialogFragment
Expand Down Expand Up @@ -58,9 +59,10 @@ class SharePodcastFragment : BaseDialogFragment() {
savedInstanceState: Bundle?,
) = ComposeView(requireActivity()).apply {
val platforms = SocialPlatform.getAvailablePlatforms(requireContext())
val assetController = BackgroundAssetController.create(requireContext(), shareColors)
val listener = shareListenerFactory.create(this@SharePodcastFragment, assetController, args.source)
setContent {
val assetController = rememberBackgroundAssetControler(shareColors)
val listener = remember { shareListenerFactory.create(this@SharePodcastFragment, assetController, args.source) }

val uiState by viewModel.uiState.collectAsState()
SharePodcastPage(
podcast = uiState.podcast,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.view.ViewGroup
import androidx.annotation.ColorInt
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.ComposeView
Expand All @@ -16,8 +17,8 @@ import androidx.core.os.bundleOf
import androidx.fragment.app.viewModels
import au.com.shiftyjelly.pocketcasts.analytics.SourceView
import au.com.shiftyjelly.pocketcasts.models.entity.PodcastEpisode
import au.com.shiftyjelly.pocketcasts.reimagine.ui.BackgroundAssetController
import au.com.shiftyjelly.pocketcasts.reimagine.ui.ShareColors
import au.com.shiftyjelly.pocketcasts.reimagine.ui.rememberBackgroundAssetControler
import au.com.shiftyjelly.pocketcasts.sharing.SocialPlatform
import au.com.shiftyjelly.pocketcasts.sharing.TimestampType
import au.com.shiftyjelly.pocketcasts.utils.parceler.ColorParceler
Expand Down Expand Up @@ -64,9 +65,10 @@ class ShareEpisodeTimestampFragment : BaseDialogFragment() {
savedInstanceState: Bundle?,
) = ComposeView(requireActivity()).apply {
val platforms = SocialPlatform.getAvailablePlatforms(requireContext())
val assetController = BackgroundAssetController.create(requireContext(), shareColors)
val listener = shareListenerFactory.create(this@ShareEpisodeTimestampFragment, assetController, args.timestampType, args.source)
setContent {
val assetController = rememberBackgroundAssetControler(shareColors)
val listener = remember { shareListenerFactory.create(this@ShareEpisodeTimestampFragment, assetController, args.timestampType, args.source) }

val uiState by viewModel.uiState.collectAsState()
ShareEpisodeTimestampPage(
podcast = uiState.podcast,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package au.com.shiftyjelly.pocketcasts.reimagine.ui

import android.content.Context
import android.graphics.Bitmap
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.Shader
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ExperimentalComposeApi
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.asAndroidBitmap
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.core.graphics.applyCanvas
import androidx.core.graphics.scale
import au.com.shiftyjelly.pocketcasts.sharing.CardType.Horizontal
Expand All @@ -22,27 +22,23 @@ import kotlin.math.roundToInt
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

internal interface BackgroundAssetController {
fun captureController(type: VisualCardType): CaptureController

suspend fun capture(type: VisualCardType): Result<File>

companion object {
private const val BITMAP_WIDTH = 1080
private const val BITMAP_HEIGHT = 1920

fun create(context: Context, shareColors: ShareColors) = object : BackgroundAssetController {
private val verticalCardController = CaptureController()
private val horizontalCardController = CaptureController()
private val squareCardController = CaptureController()
@Composable
internal fun rememberBackgroundAssetControler(
shareColors: ShareColors,
): BackgroundAssetController {
val context = LocalContext.current
val verticalCardController = rememberCaptureController()
val horizontalCardController = rememberCaptureController()
val squareCardController = rememberCaptureController()

return remember {
object : BackgroundAssetController {
override fun captureController(type: VisualCardType) = when (type) {
Vertical -> verticalCardController
Horizontal -> horizontalCardController
Square -> squareCardController
}

@OptIn(ExperimentalComposeApi::class)
override suspend fun capture(type: VisualCardType) = runCatching {
val controller = captureController(type)
val capturedBitmap = controller.captureAsync().await()
Expand Down Expand Up @@ -86,7 +82,18 @@ internal interface BackgroundAssetController {
}
}
}
}
}

private const val BITMAP_WIDTH = 1080
private const val BITMAP_HEIGHT = 1920

internal interface BackgroundAssetController {
fun captureController(type: VisualCardType): CaptureController

suspend fun capture(type: VisualCardType): Result<File>

companion object {
@Composable
fun preview() = object : BackgroundAssetController {
private val controller = rememberCaptureController()
Expand Down

0 comments on commit dff18da

Please sign in to comment.