From cf2e07860147c93337b658b79251246e5d898d23 Mon Sep 17 00:00:00 2001 From: Felix Horvat Date: Wed, 17 Feb 2021 17:48:34 +0100 Subject: [PATCH 1/4] added support for setOfflineTileCount --- .../java/com/mapbox/mapboxgl/GlobalMethodHandler.java | 4 ++++ .../java/com/mapbox/mapboxgl/OfflineManagerUtils.java | 11 ++++++++++- ios/Classes/OfflineManagerUtils.swift | 7 ++++++- ios/Classes/SwiftMapboxGlFlutterPlugin.swift | 5 +++++ lib/src/global.dart | 8 ++++++++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/mapbox/mapboxgl/GlobalMethodHandler.java b/android/src/main/java/com/mapbox/mapboxgl/GlobalMethodHandler.java index 2f03245ac..7b2ab95f1 100644 --- a/android/src/main/java/com/mapbox/mapboxgl/GlobalMethodHandler.java +++ b/android/src/main/java/com/mapbox/mapboxgl/GlobalMethodHandler.java @@ -69,6 +69,10 @@ public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) { OfflineManagerUtils.mergeRegions(result, context, methodCall.argument("path")); break; + case "setOfflineTileCountLimit": + OfflineManagerUtils.setOfflineTileCountLimit(result, context, methodCall.argument("count")); + break; + case "downloadOfflineRegion": // Get download region arguments from caller OfflineRegionData regionData = new Gson().fromJson(methodCall.argument("region").toString(), OfflineRegionData.class); diff --git a/android/src/main/java/com/mapbox/mapboxgl/OfflineManagerUtils.java b/android/src/main/java/com/mapbox/mapboxgl/OfflineManagerUtils.java index 4a4e42956..9d2d7acb1 100644 --- a/android/src/main/java/com/mapbox/mapboxgl/OfflineManagerUtils.java +++ b/android/src/main/java/com/mapbox/mapboxgl/OfflineManagerUtils.java @@ -42,6 +42,11 @@ public void onError(String error) { }); } + static void setOfflineTileCountLimit(MethodChannel.Result result, Context context, long limit){ + OfflineManager.getInstance(context).setOfflineMapboxTileCountLimit(limit); + result.success(null); + } + static void downloadRegion( MethodChannel.Result result, Context context, @@ -56,7 +61,11 @@ static void downloadRegion( //Tracker of result AtomicBoolean isComplete = new AtomicBoolean(false); //Download region - OfflineManager.getInstance(context).createOfflineRegion(definition, metadata, new OfflineManager.CreateOfflineRegionCallback() { + OfflineManager manager = OfflineManager.getInstance(context); + + manager.setOfflineMapboxTileCountLimit(60000); + + manager.createOfflineRegion(definition, metadata, new OfflineManager.CreateOfflineRegionCallback() { private OfflineRegion _offlineRegion; @Override diff --git a/ios/Classes/OfflineManagerUtils.swift b/ios/Classes/OfflineManagerUtils.swift index 2cfaac1ee..000614c83 100644 --- a/ios/Classes/OfflineManagerUtils.swift +++ b/ios/Classes/OfflineManagerUtils.swift @@ -65,7 +65,12 @@ class OfflineManagerUtils { } result(regionsArgsJsonString) } - + static func setOfflineTileCountLimit(result: @escaping FlutterResult, maximumCount: UInt64) { + let offlineStorage = MGLOfflineStorage.shared + offlineStorage.setMaximumAllowedMapboxTiles(maximumCount) + result(nil) + } + static func deleteRegion(result: @escaping FlutterResult, id: Int) { let offlineStorage = MGLOfflineStorage.shared guard let pacs = offlineStorage.packs else { return } diff --git a/ios/Classes/SwiftMapboxGlFlutterPlugin.swift b/ios/Classes/SwiftMapboxGlFlutterPlugin.swift index 63cd5c797..4c1ae06c3 100644 --- a/ios/Classes/SwiftMapboxGlFlutterPlugin.swift +++ b/ios/Classes/SwiftMapboxGlFlutterPlugin.swift @@ -29,6 +29,11 @@ public class SwiftMapboxGlFlutterPlugin: NSObject, FlutterPlugin { result: result, registrar: registrar ) + case "setOfflineTileCountLimit": + guard let arguments = methodCall.arguments as? [String: Int] else { return } + + OfflineManagerUtils.setOfflineTileCountLimit(result: result, maximumCount: arguments["limit"]) + case "getListOfRegions": // Note: this does not download anything from internet, it only fetches data drom database OfflineManagerUtils.regionsList(result: result) diff --git a/lib/src/global.dart b/lib/src/global.dart index 69e8a8d1b..0509e1b9f 100644 --- a/lib/src/global.dart +++ b/lib/src/global.dart @@ -59,6 +59,14 @@ Future updateOfflineRegionMetadata( return OfflineRegion.fromJson(json.decode(regionJson)); } +Future setOfflineTileCountLimit(int limit) => + _globalChannel.invokeMethod( + 'setOfflineTileCountLimit', + { + 'limt': limit, + }, + ); + Future deleteOfflineRegion(int id, {String accessToken}) => _globalChannel.invokeMethod( 'deleteOfflineRegion', From a26a594b6b5b7d93d7f254e32f42b4bca12c7a80 Mon Sep 17 00:00:00 2001 From: Felix Horvat Date: Wed, 17 Feb 2021 22:14:18 +0100 Subject: [PATCH 2/4] fixed issues with setOfflineTileCountLimit --- .../com/mapbox/mapboxgl/GlobalMethodHandler.java | 2 +- ios/Classes/SwiftMapboxGlFlutterPlugin.swift | 13 ++++++++++--- lib/src/global.dart | 5 +++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/android/src/main/java/com/mapbox/mapboxgl/GlobalMethodHandler.java b/android/src/main/java/com/mapbox/mapboxgl/GlobalMethodHandler.java index 7b2ab95f1..f0c78f27c 100644 --- a/android/src/main/java/com/mapbox/mapboxgl/GlobalMethodHandler.java +++ b/android/src/main/java/com/mapbox/mapboxgl/GlobalMethodHandler.java @@ -70,7 +70,7 @@ public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) { break; case "setOfflineTileCountLimit": - OfflineManagerUtils.setOfflineTileCountLimit(result, context, methodCall.argument("count")); + OfflineManagerUtils.setOfflineTileCountLimit(result, context, methodCall.argument("limit").longValue()); break; case "downloadOfflineRegion": diff --git a/ios/Classes/SwiftMapboxGlFlutterPlugin.swift b/ios/Classes/SwiftMapboxGlFlutterPlugin.swift index 4c1ae06c3..2f3cc527e 100644 --- a/ios/Classes/SwiftMapboxGlFlutterPlugin.swift +++ b/ios/Classes/SwiftMapboxGlFlutterPlugin.swift @@ -30,9 +30,16 @@ public class SwiftMapboxGlFlutterPlugin: NSObject, FlutterPlugin { registrar: registrar ) case "setOfflineTileCountLimit": - guard let arguments = methodCall.arguments as? [String: Int] else { return } - - OfflineManagerUtils.setOfflineTileCountLimit(result: result, maximumCount: arguments["limit"]) + guard let arguments = methodCall.arguments as? [String: Any], + let limit = arguments["limit"] as? Int else { + result(FlutterError( + code: "SetOfflineTileCountLimitError", + message: "could not decode arguments", + details: nil + )) + return + } + OfOfflineManagerUtils.setOfflineTileCountLimit(result: result, maximumCount: limit) case "getListOfRegions": // Note: this does not download anything from internet, it only fetches data drom database diff --git a/lib/src/global.dart b/lib/src/global.dart index 0509e1b9f..211cbe1e0 100644 --- a/lib/src/global.dart +++ b/lib/src/global.dart @@ -59,11 +59,12 @@ Future updateOfflineRegionMetadata( return OfflineRegion.fromJson(json.decode(regionJson)); } -Future setOfflineTileCountLimit(int limit) => +Future setOfflineTileCountLimit(int limit, {String accessToken}) => _globalChannel.invokeMethod( 'setOfflineTileCountLimit', { - 'limt': limit, + 'limit': limit, + 'accessToken': accessToken, }, ); From cc797a1b8402d82695c6fa0e9ba08c5ab294a299 Mon Sep 17 00:00:00 2001 From: Felix Horvat Date: Fri, 19 Feb 2021 14:34:49 +0100 Subject: [PATCH 3/4] fixed ios issues --- ios/Classes/OfflineManagerUtils.swift | 2 +- ios/Classes/SwiftMapboxGlFlutterPlugin.swift | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ios/Classes/OfflineManagerUtils.swift b/ios/Classes/OfflineManagerUtils.swift index 000614c83..6f247a4e3 100644 --- a/ios/Classes/OfflineManagerUtils.swift +++ b/ios/Classes/OfflineManagerUtils.swift @@ -68,7 +68,7 @@ class OfflineManagerUtils { static func setOfflineTileCountLimit(result: @escaping FlutterResult, maximumCount: UInt64) { let offlineStorage = MGLOfflineStorage.shared offlineStorage.setMaximumAllowedMapboxTiles(maximumCount) - result(nil) + result() } static func deleteRegion(result: @escaping FlutterResult, id: Int) { diff --git a/ios/Classes/SwiftMapboxGlFlutterPlugin.swift b/ios/Classes/SwiftMapboxGlFlutterPlugin.swift index 2f3cc527e..c2dd55da7 100644 --- a/ios/Classes/SwiftMapboxGlFlutterPlugin.swift +++ b/ios/Classes/SwiftMapboxGlFlutterPlugin.swift @@ -1,5 +1,6 @@ import Flutter import UIKit +import os public class SwiftMapboxGlFlutterPlugin: NSObject, FlutterPlugin { public static func register(with registrar: FlutterPluginRegistrar) { @@ -31,7 +32,7 @@ public class SwiftMapboxGlFlutterPlugin: NSObject, FlutterPlugin { ) case "setOfflineTileCountLimit": guard let arguments = methodCall.arguments as? [String: Any], - let limit = arguments["limit"] as? Int else { + let limit = arguments["limit"] as? UInt64 else { result(FlutterError( code: "SetOfflineTileCountLimitError", message: "could not decode arguments", @@ -39,7 +40,7 @@ public class SwiftMapboxGlFlutterPlugin: NSObject, FlutterPlugin { )) return } - OfOfflineManagerUtils.setOfflineTileCountLimit(result: result, maximumCount: limit) + OfflineManagerUtils.setOfflineTileCountLimit(result: result, maximumCount: limit) case "getListOfRegions": // Note: this does not download anything from internet, it only fetches data drom database From 21f5fbde79d429ebf1e64aaa4e4afc8c1cb96cf3 Mon Sep 17 00:00:00 2001 From: Felix Horvat Date: Fri, 19 Feb 2021 14:50:04 +0100 Subject: [PATCH 4/4] removed test code --- .../main/java/com/mapbox/mapboxgl/OfflineManagerUtils.java | 6 +----- ios/Classes/OfflineManagerUtils.swift | 3 ++- ios/Classes/SwiftMapboxGlFlutterPlugin.swift | 2 -- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/android/src/main/java/com/mapbox/mapboxgl/OfflineManagerUtils.java b/android/src/main/java/com/mapbox/mapboxgl/OfflineManagerUtils.java index 9d2d7acb1..de050a600 100644 --- a/android/src/main/java/com/mapbox/mapboxgl/OfflineManagerUtils.java +++ b/android/src/main/java/com/mapbox/mapboxgl/OfflineManagerUtils.java @@ -61,11 +61,7 @@ static void downloadRegion( //Tracker of result AtomicBoolean isComplete = new AtomicBoolean(false); //Download region - OfflineManager manager = OfflineManager.getInstance(context); - - manager.setOfflineMapboxTileCountLimit(60000); - - manager.createOfflineRegion(definition, metadata, new OfflineManager.CreateOfflineRegionCallback() { + OfflineManager.getInstance(context).createOfflineRegion(definition, metadata, new OfflineManager.CreateOfflineRegionCallback() { private OfflineRegion _offlineRegion; @Override diff --git a/ios/Classes/OfflineManagerUtils.swift b/ios/Classes/OfflineManagerUtils.swift index 6f247a4e3..cca425daf 100644 --- a/ios/Classes/OfflineManagerUtils.swift +++ b/ios/Classes/OfflineManagerUtils.swift @@ -65,10 +65,11 @@ class OfflineManagerUtils { } result(regionsArgsJsonString) } + static func setOfflineTileCountLimit(result: @escaping FlutterResult, maximumCount: UInt64) { let offlineStorage = MGLOfflineStorage.shared offlineStorage.setMaximumAllowedMapboxTiles(maximumCount) - result() + result(nil) } static func deleteRegion(result: @escaping FlutterResult, id: Int) { diff --git a/ios/Classes/SwiftMapboxGlFlutterPlugin.swift b/ios/Classes/SwiftMapboxGlFlutterPlugin.swift index c2dd55da7..feef90895 100644 --- a/ios/Classes/SwiftMapboxGlFlutterPlugin.swift +++ b/ios/Classes/SwiftMapboxGlFlutterPlugin.swift @@ -1,6 +1,5 @@ import Flutter import UIKit -import os public class SwiftMapboxGlFlutterPlugin: NSObject, FlutterPlugin { public static func register(with registrar: FlutterPluginRegistrar) { @@ -41,7 +40,6 @@ public class SwiftMapboxGlFlutterPlugin: NSObject, FlutterPlugin { return } OfflineManagerUtils.setOfflineTileCountLimit(result: result, maximumCount: limit) - case "getListOfRegions": // Note: this does not download anything from internet, it only fetches data drom database OfflineManagerUtils.regionsList(result: result)