Skip to content

Latest commit

 

History

History
103 lines (74 loc) · 3.02 KB

save_cellular_traffic.md

File metadata and controls

103 lines (74 loc) · 3.02 KB

Save cellular data

Translations: 简体中文

Important

Only available on Android platform

The cellular traffic saving function can set the depth parameter of ImageRequest to Depth.LOCAL when detecting that current cellular traffic is present, so that images will no longer be downloaded from the network.

Install component

${LAST_VERSION}: Download (Not included 'v')

implementation("io.github.panpf.sketch4:sketch-extensions-core:${LAST_VERSION}")

Configure

First register the SaveCellularTrafficRequestInterceptor request interceptor, as follows:

// Register for all ImageRequests when customizing Sketch
Sketch.Builder(context).apply {
    components {
        addRequestInterceptor(SaveCellularTrafficRequestInterceptor())
    }
}.build()

// Register for a single ImageRequest when loading an image
ImageRequest(context, "https://example.com/image.jpg") {
    components {
        addRequestInterceptor(SaveCellularTrafficRequestInterceptor())
    }
}

Then enable the cellular data saving function for a single request, as follows:

ImageRequest(context, "https://example.com/image.jpg") {
    saveCellularTraffic(true)
}

Finally, configure the error status picture dedicated to the cellular traffic saving function, as follows:

// View
ImageRequest(context, "https://example.com/image.jpg") {
    saveCellularTraffic(true)

    error(
        ConditionStateImage(defaultImage = R.drawable.ic_error) {
            saveCellularTrafficError(R.drawable.ic_signal_cellular)
        }
    )
}

// Compose
ComposableImageRequest(context, "https://example.com/image.jpg") {
    saveCellularTraffic(true)

    error(
        ComposableConditionStateImage(defaultImage = Res.drawable.ic_error) {
            saveCellularTrafficError(Res.drawable.ic_signal_cellular)
        }
    )
}

Tip

saveCellularTrafficError(Res.drawable.ic_signal_cellular) needs to import the sketch-extensions-compose-resources module

Click to force load

Important

  1. Only supports Android View
  2. This feature requires the use of SketchImageView

Enable clicking ImageView to ignore cellular data and redisplay the image

sketchImageView.setClickIgnoreSaveCellularTrafficEnabled(true)