Skip to content

Commit 0cdf4e0

Browse files
committed
Add documentation
1 parent fc13d46 commit 0cdf4e0

1 file changed

Lines changed: 95 additions & 8 deletions

File tree

‎MapCache/Classes/RegionDownloaderDelegate.swift‎

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,114 @@
88
import Foundation
99

1010
///
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.
1412
///
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+
/// ```
1643
///
1744
public protocol RegionDownloaderDelegate: AnyObject {
1845

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).
2055
func regionDownloader(_ regionDownloader: RegionDownloader, didDownloadPercentage percentage: Double)
2156

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`).
2365
func regionDownloader(_ regionDownloader: RegionDownloader, didFinishDownload tilesDownloaded: TileNumber)
2466

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+
/// ```
2683
func regionDownloader(_ regionDownloader: RegionDownloader, willStartDownloading totalTiles: TileNumber, region: TileCoordsRegion, mapCache: MapCacheProtocol)
2784

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+
/// ```
29101
func regionDownloader(_ regionDownloader: RegionDownloader, didDownloadTileAt tileCoords: TileCoords, dataSize: Int)
30102

31103
/// 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+
/// ```
32119
func regionDownloader(_ regionDownloader: RegionDownloader, didFailToDownloadTileAt tileCoords: TileCoords, error: Error)
33120
}
34121

0 commit comments

Comments
 (0)