Skip to content

Commit

Permalink
change: Target.getCrossfadeTransition(CrossfadeTransition.Factory) ch…
Browse files Browse the repository at this point in the history
…ange to convertTransition(Transition.Factory)
  • Loading branch information
panpf committed Aug 30, 2024
1 parent e109dc6 commit d9b07ce
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ class AsyncImageState internal constructor(
}
}

// TODO Go out independently for easy testing
private inner class AsyncImageTarget : GenericComposeTarget() {

override var painter: Painter?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,16 @@ interface ComposeTarget : Target {
return ComposeResizeOnDrawHelper
}

override fun getCrossfadeTransition(factory: CrossfadeTransition.Factory): Transition.Factory? {
return ComposeCrossfadeTransition.Factory(
durationMillis = factory.durationMillis,
fadeStart = factory.fadeStart,
preferExactIntrinsicSize = factory.preferExactIntrinsicSize,
alwaysUse = factory.alwaysUse,
)
override fun convertTransition(factory: Transition.Factory): Transition.Factory? {
if (factory is CrossfadeTransition.Factory) {
return ComposeCrossfadeTransition.Factory(
durationMillis = factory.durationMillis,
fadeStart = factory.fadeStart,
preferExactIntrinsicSize = factory.preferExactIntrinsicSize,
alwaysUse = factory.alwaysUse,
)
}
return null
}

override fun getComponents(): ComponentRegistry? = ComponentRegistry.Builder().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,90 @@ import com.github.panpf.sketch.transition.Transition
import kotlinx.coroutines.Job

/**
* A listener that accepts the result of an image request.
* [Target] is mainly used to display [Image].
*
* [Target] can also provide [RequestManager], [RequestDelegate], [Listener], [ProgressListener],
* [LifecycleResolver], [SizeResolver], [ScaleDecider], [ResizeOnDrawHelper], [CrossfadeTransition]
* related to [Target] when building the request, [ImageOptions], [ComponentRegistry] and other configurations
*
* @see com.github.panpf.sketch.compose.core.common.test.target.GenericComposeTargetTest
* @see com.github.panpf.sketch.view.core.test.target.GenericViewTargetTest
* @see com.github.panpf.sketch.view.core.test.target.RemoteViewsTargetTest
*/
interface Target {

/**
* The current image displayed by the target.
*
* @see com.github.panpf.sketch.state.CurrentStateImage
*/
val currentImage: Image?
get() = null


/**
* Get the [RequestManager] associated with the target.
*/
fun getRequestManager(): RequestManager? = null

/**
* Create a new [RequestDelegate] for the request.
*/
fun newRequestDelegate(
sketch: Sketch,
initialRequest: ImageRequest,
job: Job
): RequestDelegate? = null


/**
* Get the [Listener] associated with the target.
*/
fun getListener(): Listener? = null

/**
* Get the [ProgressListener] associated with the target.
*/
fun getProgressListener(): ProgressListener? = null

/**
* Get the [LifecycleResolver] associated with the target.
*/
fun getLifecycleResolver(): LifecycleResolver? = null


/**
* Get the [SizeResolver] associated with the target.
*/
fun getSizeResolver(): SizeResolver? = null

/**
* Get the [ScaleDecider] associated with the target.
*/
fun getScaleDecider(): ScaleDecider? = null

/**
* Get the [ResizeOnDrawHelper] associated with the target.
*/
fun getResizeOnDrawHelper(): ResizeOnDrawHelper? = null

fun getCrossfadeTransition(factory: CrossfadeTransition.Factory): Transition.Factory? = null

/**
* Get the [ImageOptions] associated with the target.
*/
fun getImageOptions(): ImageOptions? = null

/**
* Get the [ComponentRegistry] associated with the target.
*/
fun getComponents(): ComponentRegistry? = null


/**
* Convert a generic [Transition.Factory] to a [Transition.Factory] appropriate for the current Target
*/
fun convertTransition(factory: Transition.Factory): Transition.Factory? = null


/**
* Called when the request starts.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ class CrossfadeTransition(transition: Transition) : Transition by transition {
): Transition? {
val fromMemoryCache = result.asOrNull<ImageResult.Success>()?.dataFrom == MEMORY_CACHE
if (!alwaysUse && fromMemoryCache) return null
val targetCrossfadeTransitionFactory =
target.getCrossfadeTransition(this)
val targetCrossfadeTransitionFactory = target.convertTransition(this) ?: return null
val targetCrossfadeTransition = targetCrossfadeTransitionFactory
?.create(requestContext, target, result)
?: return null
.create(requestContext, target, result) ?: return null
return CrossfadeTransition(targetCrossfadeTransition)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,16 @@ interface ViewTarget<T : View> : Target {

override fun getResizeOnDrawHelper(): ResizeOnDrawHelper? = ViewResizeOnDrawHelper

override fun getCrossfadeTransition(factory: CrossfadeTransition.Factory): Transition.Factory? {
return ViewCrossfadeTransition.Factory(
durationMillis = factory.durationMillis,
fadeStart = factory.fadeStart,
preferExactIntrinsicSize = factory.preferExactIntrinsicSize,
alwaysUse = factory.alwaysUse,
)
override fun convertTransition(factory: Transition.Factory): Transition.Factory? {
if (factory is CrossfadeTransition.Factory) {
return ViewCrossfadeTransition.Factory(
durationMillis = factory.durationMillis,
fadeStart = factory.fadeStart,
preferExactIntrinsicSize = factory.preferExactIntrinsicSize,
alwaysUse = factory.alwaysUse,
)
}
return null
}

override fun getImageOptions(): ImageOptions? =
Expand Down

0 comments on commit d9b07ce

Please sign in to comment.