On this page

    Track the progress and state of an offline pack download - Kotlin SDK

    The MapTiler SDK for Kotlin provides a powerful Offline Pack API that allows you to track the progress and state of an offline pack download.

    You can track the download state and progress reactively using Kotlin Flows.

    Before you begin, ensure you have followed the Android getting started with offline pack API to set up your project and obtain your API key.

    Tracking download progress

    
    import com.maptiler.maptilersdk.offline.MTOfflineManager
    import com.maptiler.maptilersdk.offline.MTOfflinePackState
    import kotlinx.coroutines.MainScope
    import kotlinx.coroutines.launch
    
    MainScope().launch {
        try {
            // Create the pack (assuming `definition` is already created)
            val pack = MTOfflineManager.createPack(context, definition)
    
            // Observe the state flow
            launch {
                pack.stateFlow.collect { state ->
                    println("Pack state changed to: ${state.name}")
                    if (state == MTOfflinePackState.COMPLETED) {
                        println("Download finished successfully!")
                    }
                }
            }
    
            // Observe the progress flow
            launch {
                pack.progressFlow.collect { progress ->
                    val percent = "%.1f".format(progress.percentage * 100)
                    println("Download progress: $percent% (${progress.downloadedResources}/${progress.totalResources})")
                }
            }
            
            // Start the download
            pack.download()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
    

    Tip

    Alternatively, you can implement the MTOfflineDownloadDelegate and use pack.setDelegate(observer) alongside pack.isProgressReportingEnabled = true if you prefer a callback-based approach.

    Learn more

    For more information browse our API Reference

    Check out our SDK Kotlin Examples.

    Was this helpful?
    Mobile SDK
    Android
    Track the progress and state of an offline pack download - Kotlin SDK
    Tracking download progress