Skip to content

Commit 9438dfb

Browse files
authored
Merge pull request #2 from merlos/features/use-diskcache
Features/use diskcache
2 parents 8387255 + 7805b27 commit 9438dfb

12 files changed

Lines changed: 269 additions & 74 deletions

File tree

Example/MapCache.xcodeproj/project.pbxproj

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,16 +225,16 @@
225225
isa = PBXProject;
226226
attributes = {
227227
LastSwiftUpdateCheck = 0830;
228-
LastUpgradeCheck = 0830;
228+
LastUpgradeCheck = 1020;
229229
ORGANIZATIONNAME = CocoaPods;
230230
TargetAttributes = {
231231
607FACCF1AFB9204008FA782 = {
232232
CreatedOnToolsVersion = 6.3.1;
233-
LastSwiftMigration = 0900;
233+
LastSwiftMigration = 1020;
234234
};
235235
607FACE41AFB9204008FA782 = {
236236
CreatedOnToolsVersion = 6.3.1;
237-
LastSwiftMigration = 0900;
237+
LastSwiftMigration = 1020;
238238
TestTargetID = 607FACCF1AFB9204008FA782;
239239
};
240240
};
@@ -411,6 +411,7 @@
411411
isa = XCBuildConfiguration;
412412
buildSettings = {
413413
ALWAYS_SEARCH_USER_PATHS = NO;
414+
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
414415
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
415416
CLANG_CXX_LIBRARY = "libc++";
416417
CLANG_ENABLE_MODULES = YES;
@@ -419,12 +420,14 @@
419420
CLANG_WARN_BOOL_CONVERSION = YES;
420421
CLANG_WARN_COMMA = YES;
421422
CLANG_WARN_CONSTANT_CONVERSION = YES;
423+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
422424
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
423425
CLANG_WARN_EMPTY_BODY = YES;
424426
CLANG_WARN_ENUM_CONVERSION = YES;
425427
CLANG_WARN_INFINITE_RECURSION = YES;
426428
CLANG_WARN_INT_CONVERSION = YES;
427429
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
430+
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
428431
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
429432
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
430433
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
@@ -464,6 +467,7 @@
464467
isa = XCBuildConfiguration;
465468
buildSettings = {
466469
ALWAYS_SEARCH_USER_PATHS = NO;
470+
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
467471
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
468472
CLANG_CXX_LIBRARY = "libc++";
469473
CLANG_ENABLE_MODULES = YES;
@@ -472,12 +476,14 @@
472476
CLANG_WARN_BOOL_CONVERSION = YES;
473477
CLANG_WARN_COMMA = YES;
474478
CLANG_WARN_CONSTANT_CONVERSION = YES;
479+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
475480
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
476481
CLANG_WARN_EMPTY_BODY = YES;
477482
CLANG_WARN_ENUM_CONVERSION = YES;
478483
CLANG_WARN_INFINITE_RECURSION = YES;
479484
CLANG_WARN_INT_CONVERSION = YES;
480485
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
486+
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
481487
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
482488
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
483489
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
@@ -516,8 +522,7 @@
516522
MODULE_NAME = ExampleApp;
517523
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
518524
PRODUCT_NAME = "$(TARGET_NAME)";
519-
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
520-
SWIFT_VERSION = 4.0;
525+
SWIFT_VERSION = 5.0;
521526
};
522527
name = Debug;
523528
};
@@ -531,8 +536,7 @@
531536
MODULE_NAME = ExampleApp;
532537
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
533538
PRODUCT_NAME = "$(TARGET_NAME)";
534-
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
535-
SWIFT_VERSION = 4.0;
539+
SWIFT_VERSION = 5.0;
536540
};
537541
name = Release;
538542
};
@@ -552,8 +556,7 @@
552556
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
553557
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
554558
PRODUCT_NAME = "$(TARGET_NAME)";
555-
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
556-
SWIFT_VERSION = 4.0;
559+
SWIFT_VERSION = 5.0;
557560
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/MapCache_Example.app/MapCache_Example";
558561
};
559562
name = Debug;
@@ -570,8 +573,7 @@
570573
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
571574
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
572575
PRODUCT_NAME = "$(TARGET_NAME)";
573-
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
574-
SWIFT_VERSION = 4.0;
576+
SWIFT_VERSION = 5.0;
575577
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/MapCache_Example.app/MapCache_Example";
576578
};
577579
name = Release;

Example/MapCache.xcodeproj/xcshareddata/xcschemes/MapCache-Example.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0900"
3+
LastUpgradeVersion = "1020"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

Example/MapCache/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1414
var window: UIWindow?
1515

1616

17-
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
17+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
1818
// Override point for customization after application launch.
1919
return true
2020
}

Example/MapCache/Base.lproj/Main.storyboard

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,62 @@
2222
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
2323
<subviews>
2424
<mapView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" fixedFrame="YES" mapType="standard" showsUserLocation="YES" translatesAutoresizingMaskIntoConstraints="NO" id="dwC-V5-0co" userLabel="Map">
25-
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
25+
<rect key="frame" x="0.0" y="-8" width="375" height="667"/>
2626
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
2727
</mapView>
28+
<view clipsSubviews="YES" contentMode="scaleAspectFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="vE0-SZ-UXH">
29+
<rect key="frame" x="0.0" y="0.0" width="500" height="99"/>
30+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
31+
<subviews>
32+
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="w0Z-in-k23" userLabel="Update Size Button">
33+
<rect key="frame" x="142" y="48" width="116" height="30"/>
34+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
35+
<accessibility key="accessibilityConfiguration" hint="updates value of the cache size" label="Update Size">
36+
<accessibilityTraits key="traits" button="YES"/>
37+
</accessibility>
38+
<fontDescription key="fontDescription" name=".AppleSystemUIFont" family=".AppleSystemUIFont" pointSize="15"/>
39+
<state key="normal" title="Get size"/>
40+
<connections>
41+
<action selector="updateSize:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="3FC-cx-Tx6"/>
42+
</connections>
43+
</button>
44+
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="7EQ-Hp-CVK">
45+
<rect key="frame" x="284" y="48" width="71" height="30"/>
46+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
47+
<color key="backgroundColor" cocoaTouchSystemColor="lightTextColor"/>
48+
<fontDescription key="fontDescription" type="system" pointSize="15"/>
49+
<state key="normal" title="Clear"/>
50+
<connections>
51+
<action selector="clearCache:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="m3S-Qp-RDR"/>
52+
</connections>
53+
</button>
54+
</subviews>
55+
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
56+
<rect key="contentStretch" x="1" y="1" width="1" height="1"/>
57+
</view>
58+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="0.00" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Lsd-4Y-LN2">
59+
<rect key="frame" x="9" y="47" width="110" height="31"/>
60+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
61+
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
62+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
63+
<nil key="textColor"/>
64+
<nil key="highlightedColor"/>
65+
</label>
66+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Cache Size (bytes)" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="21V-Um-U9Y">
67+
<rect key="frame" x="26" y="39" width="91" height="12"/>
68+
<autoresizingMask key="autoresizingMask" flexibleMaxY="YES"/>
69+
<fontDescription key="fontDescription" type="system" pointSize="10"/>
70+
<color key="textColor" white="0.33333333329999998" alpha="1" colorSpace="calibratedWhite"/>
71+
<nil key="highlightedColor"/>
72+
</label>
2873
</subviews>
2974
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
3075
</view>
3176
<connections>
77+
<outlet property="cacheSizeLabel" destination="Lsd-4Y-LN2" id="tnk-Vq-9uT"/>
78+
<outlet property="clearCacheButton" destination="7EQ-Hp-CVK" id="tc0-k6-3vF"/>
3279
<outlet property="map" destination="dwC-V5-0co" id="Ir8-jR-9rJ"/>
80+
<outlet property="updateSizeButton" destination="w0Z-in-k23" id="BJp-Jt-0XR"/>
3381
</connections>
3482
</viewController>
3583
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>

Example/MapCache/ViewController.swift

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,69 @@ import MapCache
1414
class ViewController: UIViewController {
1515

1616
@IBOutlet weak var map: MKMapView!
17+
@IBOutlet weak var updateSizeButton: UIButton!
18+
@IBOutlet weak var clearCacheButton: UIButton!
19+
@IBOutlet weak var cacheSizeLabel: UILabel!
1720

21+
/// Map Cache config contains all the config options.
22+
/// Initialize it before seting up the cache
23+
var config: MapCacheConfig = MapCacheConfig(withUrlTemplate: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")
24+
25+
/// Da Map Cache
26+
var mapCache: MapCache?
27+
28+
/// We can initialize our cache here.
1829
override func viewDidLoad() {
1930
super.viewDidLoad()
2031

21-
//Set delegate
32+
// See below the extension of the delegate
33+
// You need to tell MKMapView to render the overlay
34+
// func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer
2235
map.delegate = self
2336

24-
//Request permissions
25-
//Config
26-
let config = MapCacheConfig(withTileUrlTemplate: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")
37+
// initialize the cache with our config
38+
mapCache = MapCache(withConfig: config)
39+
// See documentation to know more about config options
2740

2841

29-
let tileServerOverlay = CachedTileOverlay(mapCacheConfig: config)
30-
tileServerOverlay.canReplaceMapContent = true
31-
map.insert(tileServerOverlay, at: 0, level: .aboveLabels)
42+
// We tell the MKMapView to use our cache
43+
// useCache(:) is part of MapCache extension.
44+
map.useCache(mapCache!)
3245
}
33-
46+
47+
@IBAction func updateSize(_ sender: Any) {
48+
print("update cache size")
49+
cacheSizeLabel.text = String(mapCache!.calculateSize())
50+
}
51+
52+
@IBAction func clearCache(_ sender: Any) {
53+
print("clear cache")
54+
mapCache!.clear(completition: {
55+
self.cacheSizeLabel.text = String(self.mapCache!.calculateSize())
56+
})
57+
}
58+
3459
override func didReceiveMemoryWarning() {
3560
super.didReceiveMemoryWarning()
3661
// Dispose of any resources that can be recreated.
3762
}
3863
}
3964

65+
66+
//
67+
// It is important to override this method of the MKMapViewDelegate
68+
//
4069
extension ViewController : MKMapViewDelegate {
4170
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
42-
if overlay.isKind(of: MKTileOverlay.self) {
43-
return MKTileOverlayRenderer(overlay: overlay)
44-
}
45-
return MKOverlayRenderer()
71+
return mapView.mapCacheRenderer(forOverlay: overlay)
4672
}
4773
}
4874

75+
4976
extension ViewController : CLLocationManagerDelegate {
5077
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
5178
//Hello
5279
}
53-
5480
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
5581
// Hello
5682
}

Example/Pods/Pods.xcodeproj/project.pbxproj

Lines changed: 13 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MKMapView+MapCache.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// MkMapView+MapCache.swift
3+
// MapCache
4+
//
5+
// Created by merlos on 04/06/2019.
6+
//
7+
8+
import Foundation
9+
import MapKit
10+
11+
extension MKMapView {
12+
13+
public func useCache(_ cache: MapCache) {
14+
15+
let tileServerOverlay = CachedTileOverlay(withCache: cache)
16+
tileServerOverlay.canReplaceMapContent = true
17+
self.insertOverlay(tileServerOverlay, at: 0, level: .aboveLabels)
18+
}
19+
20+
public func mapCacheRenderer(forOverlay overlay: MKOverlay) -> MKOverlayRenderer {
21+
if overlay.isKind(of: MKTileOverlay.self) {
22+
return MKTileOverlayRenderer(overlay: overlay)
23+
}
24+
return MKOverlayRenderer()
25+
}
26+
}

0 commit comments

Comments
 (0)