|
8 | 8 | import Foundation |
9 | 9 |
|
10 | 10 | /// |
11 | | -/// Delegate protocol of `RegionDownloader`. |
12 | | -/// Implement this protocol whenever you use `RegionDownloader` it provides feedback while downloading a |
13 | | -/// region (f.i, downloaded %) and calls back the delegate once the download finished. |
| 11 | +/// `RegionDownloaderDelegate` provides callbacks for monitoring the progress of a tile region download. |
14 | 12 | /// |
15 | | -/// All methods have default empty implementations, making them optional. |
| 13 | +/// Conform to this protocol to receive notifications about download progress, individual tile results, |
| 14 | +/// and lifecycle events. All methods have default empty implementations provided via a protocol extension, |
| 15 | +/// making each callback optional — implement only the ones you need. |
| 16 | +/// |
| 17 | +/// # Usage Example |
| 18 | +/// |
| 19 | +/// ```swift |
| 20 | +/// class MyViewController: UIViewController, RegionDownloaderDelegate { |
| 21 | +/// |
| 22 | +/// func startDownload() { |
| 23 | +/// let downloader = RegionDownloader(forRegion: region, mapCache: cache) |
| 24 | +/// downloader.delegate = self |
| 25 | +/// downloader.start() |
| 26 | +/// } |
| 27 | +/// |
| 28 | +/// // Update a progress bar on percentage changes. |
| 29 | +/// func regionDownloader(_ downloader: RegionDownloader, didDownloadPercentage percentage: Double) { |
| 30 | +/// DispatchQueue.main.async { |
| 31 | +/// self.progressView.progress = Float(percentage / 100.0) |
| 32 | +/// } |
| 33 | +/// } |
| 34 | +/// |
| 35 | +/// // Show final counts when done. |
| 36 | +/// func regionDownloader(_ downloader: RegionDownloader, didFinishDownload tilesDownloaded: TileNumber) { |
| 37 | +/// DispatchQueue.main.async { |
| 38 | +/// self.label.text = "Done: \(tilesDownloaded) tiles" |
| 39 | +/// } |
| 40 | +/// } |
| 41 | +/// } |
| 42 | +/// ``` |
16 | 43 | /// |
17 | 44 | public protocol RegionDownloaderDelegate: AnyObject { |
18 | 45 |
|
19 | | - /// Did download the percentage. |
| 46 | + /// Called each time the overall download percentage crosses the notification threshold |
| 47 | + /// (controlled by `RegionDownloader.incrementInPercentageNotification`). |
| 48 | + /// |
| 49 | + /// This is the main progress callback. It is not called for every tile — only when the |
| 50 | + /// downloaded percentage surpasses the next multiple of `incrementInPercentageNotification`. |
| 51 | + /// |
| 52 | + /// - Parameters: |
| 53 | + /// - regionDownloader: The `RegionDownloader` instance that triggered the callback. |
| 54 | + /// - percentage: The current download percentage (0.0 – 100.0). |
20 | 55 | func regionDownloader(_ regionDownloader: RegionDownloader, didDownloadPercentage percentage: Double) |
21 | 56 |
|
22 | | - /// Did Finish Download all tiles. |
| 57 | + /// Called when all tiles in the region have been processed (successfully downloaded or failed). |
| 58 | + /// |
| 59 | + /// This is the terminal callback. After this, no further delegate calls will be made |
| 60 | + /// unless `start()` is called again. |
| 61 | + /// |
| 62 | + /// - Parameters: |
| 63 | + /// - regionDownloader: The `RegionDownloader` instance that triggered the callback. |
| 64 | + /// - tilesDownloaded: The total number of tiles processed (`successfulTileDownloads + failedTileDownloads`). |
23 | 65 | func regionDownloader(_ regionDownloader: RegionDownloader, didFinishDownload tilesDownloaded: TileNumber) |
24 | 66 |
|
25 | | - /// Called before the download starts. |
| 67 | + /// Called once before the download loop begins. |
| 68 | + /// |
| 69 | + /// Use this to prepare the UI, reset state, or log the start of a download. |
| 70 | + /// |
| 71 | + /// - Parameters: |
| 72 | + /// - regionDownloader: The `RegionDownloader` instance that triggered the callback. |
| 73 | + /// - totalTiles: The total number of tiles that will be attempted. |
| 74 | + /// - region: The `TileCoordsRegion` being downloaded. |
| 75 | + /// - mapCache: The `MapCacheProtocol` instance used to fetch and store tiles. |
| 76 | + /// |
| 77 | + /// # Example |
| 78 | + /// ```swift |
| 79 | + /// func regionDownloader(_ downloader: RegionDownloader, willStartDownloading totalTiles: TileNumber, region: TileCoordsRegion, mapCache: MapCacheProtocol) { |
| 80 | + /// print("Starting download of \(totalTiles) tiles at zoom \(region.zoomRange.min)–\(region.zoomRange.max)") |
| 81 | + /// } |
| 82 | + /// ``` |
26 | 83 | func regionDownloader(_ regionDownloader: RegionDownloader, willStartDownloading totalTiles: TileNumber, region: TileCoordsRegion, mapCache: MapCacheProtocol) |
27 | 84 |
|
28 | | - /// Called each time a tile is successfully downloaded. |
| 85 | + /// Called each time a tile is successfully downloaded and cached. |
| 86 | + /// |
| 87 | + /// This can fire hundreds or thousands of times. Avoid performing expensive work here; |
| 88 | + /// if you need to update the UI, dispatch to the main queue. |
| 89 | + /// |
| 90 | + /// - Parameters: |
| 91 | + /// - regionDownloader: The `RegionDownloader` instance that triggered the callback. |
| 92 | + /// - tileCoords: The coordinates (zoom, x, y) of the successfully downloaded tile. |
| 93 | + /// - dataSize: The size of the tile data in bytes. |
| 94 | + /// |
| 95 | + /// # Example |
| 96 | + /// ```swift |
| 97 | + /// func regionDownloader(_ downloader: RegionDownloader, didDownloadTileAt tileCoords: TileCoords, dataSize: Int) { |
| 98 | + /// print("✓ Tile z:\(tileCoords.zoom) x:\(tileCoords.tileX) y:\(tileCoords.tileY) — \(dataSize) bytes") |
| 99 | + /// } |
| 100 | + /// ``` |
29 | 101 | func regionDownloader(_ regionDownloader: RegionDownloader, didDownloadTileAt tileCoords: TileCoords, dataSize: Int) |
30 | 102 |
|
31 | 103 | /// Called each time a tile fails to download. |
| 104 | + /// |
| 105 | + /// This can fire hundreds or thousands of times. Avoid performing expensive work here; |
| 106 | + /// if you need to update the UI, dispatch to the main queue. |
| 107 | + /// |
| 108 | + /// - Parameters: |
| 109 | + /// - regionDownloader: The `RegionDownloader` instance that triggered the callback. |
| 110 | + /// - tileCoords: The coordinates (zoom, x, y) of the tile that failed. |
| 111 | + /// - error: The error returned by the cache or network layer. |
| 112 | + /// |
| 113 | + /// # Example |
| 114 | + /// ```swift |
| 115 | + /// func regionDownloader(_ downloader: RegionDownloader, didFailToDownloadTileAt tileCoords: TileCoords, error: Error) { |
| 116 | + /// print("✗ Tile z:\(tileCoords.zoom) x:\(tileCoords.tileX) y:\(tileCoords.tileY) – \(error.localizedDescription)") |
| 117 | + /// } |
| 118 | + /// ``` |
32 | 119 | func regionDownloader(_ regionDownloader: RegionDownloader, didFailToDownloadTileAt tileCoords: TileCoords, error: Error) |
33 | 120 | } |
34 | 121 |
|
|
0 commit comments