Skip to content

Latest commit

 

History

History
131 lines (101 loc) · 3.01 KB

listener.md

File metadata and controls

131 lines (101 loc) · 3.01 KB

Listener

Translations: 简体中文

Compose

In Compose you have to pass loadState and progress of AsyncImageState Properties to monitor the status and progress of the request. For specific reasons, please refer to 《Compose》, as follows:

val state = rememberAsyncImageState()
val loadState: LoadState? = state.loadState
when (loadState) {
    is Started -> {

    }
    is Success -> {

    }
    is Error -> {

    }
    is Canceled -> {

    }
    else -> {
        // null
    }
}
val progress: Progress? = state.progress
AsyncImage(
    uri = imageUri,
    contentDescription = "photo",
    state = state
)

Android View

ImageRequest You can monitor start, completion, error, cancellation, and progress through Listener and ProgressListener, as follows:

ImageRequest(context, "https://example.com/image.jpg") {
    addListener(object : Listener {
        override fun onStart(request: ImageRequest) {
            // ...
        }

        override fun onSuccess(request: ImageRequest, result: ImageResult.Success) {
            // ...
        }

        override fun onError(request: ImageRequest, error: ImageResult.Error) {
            // ...
        }

        override fun onCancel(request: ImageRequest) {
            // ...
        }
    })
    //
    addListener(
        onStart = { request: ImageRequest ->
            // ...
        },
        onSuccess = { request: ImageRequest, result: ImageResult.Success ->
            // ...
        },
        onError = { request: ImageRequest, error: ImageResult.Error ->
            // ...
        },
        onCancel = { request: ImageRequest ->
            // ...
        },
    )

    addProgressListener { request: ImageRequest, progress: Progress ->
        // ...
    }
}

Tip

All callbacks will be executed on the main thread

SketchImageView

SketchImageView provides Flow method to monitor the status and progress of requests, as follows:

val sketchImageView = SketchImageView(context)
scope.launch {
    sketchImageView.requestState.loadState.collect { loadState ->
        when (loadState) {
            is Started -> {

            }
            is Success -> {

            }
            is Error -> {

            }
            is Canceled -> {

            }
            else -> {
                // null
            }
        }
    }
}

scope.launch {
    sketchImageView.requestState.progressState.collect { progress ->

    }
}