Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: setup swiftlint for iOS #2799

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/on-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ jobs:
- name: Install
run: yarn install --network-timeout 1000000

- name: SwiftLint
run: swiftlint lint --config .swiftlint.yml ios/**/*.swift

- name: Lint
run: yarn lint

Expand Down
14 changes: 14 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
opt_in_rules:
- vertical_whitespace_between_cases
- vertical_whitespace_closing_braces
- vertical_whitespace_opening_braces
- vertical_parameter_alignment_on_call
- operator_usage_whitespace
- redundant_type_annotation

excluded:
# Exclude `RCTMGLStyle.swift` because it is automatically generated from "RCTMGLStyle.swift.ejs"
- ios/RCTMGL-v10/RCTMGLStyle.swift

indentation: 2
vertical_whitespace_opening_braces: true
8 changes: 4 additions & 4 deletions ios/RCTMGL-v10/CustomHttpHeaders.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import MapboxMaps

class CustomHttpHeaders : HttpServiceInterceptorInterface {
static var shared : CustomHttpHeaders = {
class CustomHttpHeaders: HttpServiceInterceptorInterface {
static var shared: CustomHttpHeaders = {
let headers = CustomHttpHeaders()
headers.install()
return headers
}()

var customHeaders : [String:String] = [:]
var customHeaders: [String: String] = [:]

func install() {
HttpServiceFactory.getInstance().setInterceptorForInterceptor(self)
Expand All @@ -27,7 +27,7 @@ class CustomHttpHeaders : HttpServiceInterceptorInterface {
}

func onDownload(forDownload download: DownloadOptions) -> DownloadOptions {
customHeaders.forEach {(key,value) in
customHeaders.forEach {(key, value) in
download.request.headers[key] = value
}
return download
Expand Down
41 changes: 20 additions & 21 deletions ios/RCTMGL-v10/MGLModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,39 @@ import Foundation
import MapboxMaps
import MapboxMobileEvents


let DEFAULT_SOURCE_ID = "composite";
let DEFAULT_SOURCE_ID = "composite"

@objc(MGLModule)
class MGLModule : NSObject {
static var accessToken : String?
class MGLModule: NSObject {
static var accessToken: String?

@objc
func constantsToExport() -> [AnyHashable: Any]! {
return [
"MapboxV10":true,
"MapboxV10": true,
"StyleURL":
[
"Street": StyleURI.streets.rawValue,
"Outdoors": StyleURI.outdoors.rawValue,
"Light": StyleURI.light.rawValue,
"Dark": StyleURI.dark.rawValue,
"Satellite": StyleURI.satellite.rawValue,
"SatelliteStreet": StyleURI.satelliteStreets.rawValue,
"SatelliteStreet": StyleURI.satelliteStreets.rawValue
],
"OfflinePackDownloadState":
[
"Inactive": RCTMGLOfflineModule.State.inactive.rawValue,
"Active": RCTMGLOfflineModule.State.active.rawValue,
"Complete": RCTMGLOfflineModule.State.complete.rawValue,
"Unknown": RCTMGLOfflineModule.State.unknown.rawValue,
"Unknown": RCTMGLOfflineModule.State.unknown.rawValue
],
"StyleSource":
["DefaultSourceID": DEFAULT_SOURCE_ID],
"LineJoin":
[
"Bevel": LineJoin.bevel.rawValue,
"Round": LineJoin.round.rawValue,
"Miter": LineJoin.miter.rawValue,
"Miter": LineJoin.miter.rawValue
],
"LocationCallbackName":
["Update": RCT_MAPBOX_USER_LOCATION_UPDATE],
Expand All @@ -48,16 +47,16 @@ class MGLModule : NSObject {
],
"EventTypes":
[
"RegionIsChanging" : RCTMGLEvent.EventType.regionIsChanging.rawValue,
"RegionDidChange" : RCTMGLEvent.EventType.regionDidChange.rawValue,
"CameraChanged" : RCTMGLEvent.EventType.cameraChanged.rawValue,
"MapIdle" : RCTMGLEvent.EventType.mapIdle.rawValue,
"RegionIsChanging": RCTMGLEvent.EventType.regionIsChanging.rawValue,
"RegionDidChange": RCTMGLEvent.EventType.regionDidChange.rawValue,
"CameraChanged": RCTMGLEvent.EventType.cameraChanged.rawValue,
"MapIdle": RCTMGLEvent.EventType.mapIdle.rawValue,
"WillStartLoadingMap": RCTMGLEvent.EventType.willStartLoadingMap.rawValue,
"DidFinishLoadingStyle": RCTMGLEvent.EventType.didFinishLoadingStyle.rawValue,
"DidFinishLoadingMap": RCTMGLEvent.EventType.didFinishLoadingMap.rawValue,
"MapLoadingError": RCTMGLEvent.EventType.mapLoadingError.rawValue,
"DidFinishRenderingFrameFully": RCTMGLEvent.EventType.didFinishRenderingFully.rawValue,
"DidFinishRenderingFrame": RCTMGLEvent.EventType.didFinishRendering.rawValue,
"DidFinishRenderingFrameFully": RCTMGLEvent.EventType.didFinishRenderingFully.rawValue,
"DidFinishRenderingFrame": RCTMGLEvent.EventType.didFinishRendering.rawValue
],
"OfflineCallbackName":
[
Expand All @@ -66,20 +65,20 @@ class MGLModule : NSObject {
],
"TileServers":
["Mapbox": "mapbox"]
];
]
}

@objc
static func requiresMainQueueSetup() -> Bool {
return true
return true
}

@objc func setAccessToken(
_ token: String,
_ token: String,
resolver: RCTPromiseResolveBlock,
rejecter: RCTPromiseRejectBlock) {
MGLModule.accessToken = token
resolver(token)
MGLModule.accessToken = token
resolver(token)
}

@objc func addCustomHeader(_ headerName: String, forHeaderValue headerValue: String ) {
Expand All @@ -89,7 +88,7 @@ class MGLModule : NSObject {
@objc func removeCustomHeader(_ headerName: String) {
CustomHttpHeaders.shared.customHeaders[headerName] = nil
}

@objc func setTelemetryEnabled(_ telemetryEnabled: Bool) {
UserDefaults.mme_configuration().mme_isCollectionEnabled = telemetryEnabled
}
Expand Down
44 changes: 22 additions & 22 deletions ios/RCTMGL-v10/MGLSnapshotModule.swift
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
import MapboxMaps

@objc(MGLSnapshotModule)
class MGLSnapshotModule : NSObject {
class MGLSnapshotModule: NSObject {
@objc
static func requiresMainQueueSetup() -> Bool {
return true
return true
}

@objc
func takeSnap(_ jsOptions: [String: Any],
resolver:@escaping RCTPromiseResolveBlock,
rejecter:@escaping RCTPromiseRejectBlock
resolver: @escaping RCTPromiseResolveBlock,
rejecter: @escaping RCTPromiseRejectBlock
) {
DispatchQueue.main.async {
logged("takeSnap", rejecter:rejecter) {
logged("takeSnap", rejecter: rejecter) {
let snapshotterOptions = try self._getSnapshotterOptions(jsOptions)
let snapshotter = Snapshotter(options: snapshotterOptions)
let cameraOptions = try self._getCameraOptions(jsOptions, snapshotter)

snapshotter.setCamera(to: cameraOptions)
if let styleURL = jsOptions["styleURL"] as? String {
snapshotter.style.uri = StyleURI(rawValue:styleURL)
snapshotter.style.uri = StyleURI(rawValue: styleURL)
}
var snapshotterReference : Snapshotter? = snapshotter
var snapshotterReference: Snapshotter? = snapshotter
snapshotter.start(overlayHandler: nil, completion: { result in
do {
switch (result) {
switch result {
case .success(let image):
guard let writeToDisk = jsOptions["writeToDisk"] as? NSNumber else {
throw RCTMGLError.paramError("writeToDisk: is not a number")
}

let value = writeToDisk.boolValue ? RNMBImageUtils.createTempFile(image) : RNMBImageUtils.createBase64(image)
_ = snapshotterReference
snapshotterReference = nil
resolver(value.absoluteString)

case .failure(let error):
_ = snapshotterReference
snapshotterReference = nil
Expand All @@ -49,34 +50,33 @@ class MGLSnapshotModule : NSObject {
}
}

func _getCameraOptions(_ jsOptions: [String:Any], _ snaphotter: Snapshotter) throws -> CameraOptions {
func _getCameraOptions(_ jsOptions: [String: Any], _ snaphotter: Snapshotter) throws -> CameraOptions {
guard let pitch = jsOptions["pitch"] as? NSNumber else {
throw RCTMGLError.paramError("pitch: is not a number")
}

guard let zoomLevel = jsOptions["zoomLevel"] as? NSNumber else {
throw RCTMGLError.paramError("zoomLevel: is not a number")
}

guard let heading = jsOptions["heading"] as? NSNumber else {
throw RCTMGLError.paramError("heading: is not a number")
}

if let centerCoordinateString = jsOptions["centerCoordinate"] as? String {
guard let centerCoordinateData = centerCoordinateString.data(using: .utf8),
let centerCoordinateGeometry = try JSONDecoder().decode(Feature.self, from: centerCoordinateData).geometry,
case .point(let centerCoordinatePoint) = centerCoordinateGeometry else {
throw RCTMGLError.paramError("centerCoordinate: bad format")
}

return CameraOptions(center: centerCoordinatePoint.coordinates, padding: nil, anchor: nil, zoom: zoomLevel.doubleValue, bearing: heading.doubleValue, pitch: pitch.doubleValue)
}
else if let bounds = jsOptions["bounds"] as? String {
} else if let bounds = jsOptions["bounds"] as? String {
guard let boundsData = bounds.data(using: .utf8) else {
throw RCTMGLError.paramError("bounds: bad format")
}
let boundsFeatures = try JSONDecoder().decode(FeatureCollection.self, from: boundsData).features
let coords : [CLLocationCoordinate2D] = try boundsFeatures.map {
let coords: [CLLocationCoordinate2D] = try boundsFeatures.map {
guard case .point(let centerCoordinatePoint) = $0.geometry else {
throw RCTMGLError.paramError("Invalid bounds geometry")
}
Expand All @@ -87,20 +87,20 @@ class MGLSnapshotModule : NSObject {
throw RCTMGLError.paramError("neither centerCoordinate nor bounds provided")
}
}
func _getSnapshotterOptions(_ jsOptions: [String:Any]) throws -> MapSnapshotOptions {

func _getSnapshotterOptions(_ jsOptions: [String: Any]) throws -> MapSnapshotOptions {
guard let width = jsOptions["width"] as? NSNumber,
let height = jsOptions["height"] as? NSNumber else {
throw RCTMGLError.paramError("width, height: is not a number")
}

let resourceOptions = ResourceOptions(accessToken: MGLModule.accessToken!)
let mapSnapshotOptions = MapSnapshotOptions(
size: CGSize(width: width.doubleValue, height: height.doubleValue),
pixelRatio: 1.0,
resourceOptions: resourceOptions
)

return mapSnapshotOptions
}
}
49 changes: 24 additions & 25 deletions ios/RCTMGL-v10/RCMTGLImage.swift
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import MapboxMaps

class RCTMGLImage : UIView {
class RCTMGLImage: UIView {
@objc
var name: String = ""

var image: UIImage? = nil
var sdf: Bool? = nil
var image: UIImage?

var sdf: Bool?
var stretchX: [[NSNumber]] = []
var stretchY: [[NSNumber]] = []
var content: [NSNumber]? = nil
var content: [NSNumber]?

weak var images: RCTMGLImageSetter? = nil {
weak var images: RCTMGLImageSetter? {
didSet {
DispatchQueue.main.async { self.setImage() }
}
}
weak var bridge : RCTBridge! = nil
var reactSubviews : [UIView] = []
weak var bridge: RCTBridge! = nil

var reactSubviews: [UIView] = []

// MARK: - subview management

@objc open override func insertReactSubview(_ subview: UIView!, at atIndex: Int) {
reactSubviews.insert(subview, at: atIndex)
if reactSubviews.count > 1 {
Expand All @@ -38,15 +38,15 @@ class RCTMGLImage : UIView {
open override func removeReactSubview(_ subview: UIView!) {
reactSubviews.removeAll(where: { $0 == subview })
}

// MARK: - view shnapshot

func changeImage(_ image: UIImage, name: String) {
if let images = images {
let _ = images.addImage(name: name, image: image, sdf: sdf, stretchX:stretchX, stretchY:stretchY, content:content, log: "RCTMGLImage.addImage")
_ = images.addImage(name: name, image: image, sdf: sdf, stretchX: stretchX, stretchY: stretchY, content: content, log: "RCTMGLImage.addImage")
}
}

func setImage() {
if let image = _createViewSnapshot() {
changeImage(image, name: name)
Expand All @@ -60,9 +60,9 @@ class RCTMGLImage : UIView {
let renderer = UIGraphicsImageRenderer(size: size)
let image = renderer.image { context in
UIColor.darkGray.setStroke()
context.stroke(CGRect(x: 0, y:0, width: 32, height: 32))
UIColor(red: 158/255, green: 215/255, blue: 245/255, alpha: 1).setFill()
context.fill(CGRect(x: 2, y: 2, width: 30, height: 30))
context.stroke(CGRect(x: 0, y: 0, width: 32, height: 32))
UIColor(red: 158 / 255, green: 215 / 255, blue: 245 / 255, alpha: 1).setFill()
context.fill(CGRect(x: 2, y: 2, width: 30, height: 30))
}
return image
}
Expand All @@ -71,24 +71,23 @@ class RCTMGLImage : UIView {
}
return _createViewSnapshot(view: reactSubviews[0])
}

func _createViewSnapshot(view: UIView) -> UIImage? {
guard view.bounds.size.width > 0 && view.bounds.size.height > 0 else {
return nil
}

let roundUp = 4

let adjustedSize = CGSize(
width: ((Int(view.bounds.size.width)+roundUp-1)/roundUp)*roundUp,
height: ((Int(view.bounds.size.height)+roundUp-1)/roundUp)*roundUp
width: ((Int(view.bounds.size.width) + roundUp - 1) / roundUp) * roundUp,
height: ((Int(view.bounds.size.height) + roundUp - 1) / roundUp) * roundUp
)

let renderer = UIGraphicsImageRenderer(size: adjustedSize)
let image = renderer.image { context in
view.layer.render(in: context.cgContext)
}
return image
}
}

3 changes: 1 addition & 2 deletions ios/RCTMGL-v10/RCMTGLImageManager.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

@objc(RCTMGLImageManager)
class RCTMGLImageManager : RCTViewManager {
class RCTMGLImageManager: RCTViewManager {
@objc
override static func requiresMainQueueSetup() -> Bool {
return true
Expand Down
Loading