|
| 1 | +import Foundation |
| 2 | +import UIKit |
| 3 | +@_implementationOnly import MapboxCommon_Private |
| 4 | + |
| 5 | +/// API for attribution menu configuration |
| 6 | +/// Restricted API. Please contact Mapbox to discuss your use case if you intend to use this property. |
| 7 | +@_spi(Restricted) |
| 8 | +public class AttributionMenu { |
| 9 | + private let urlOpener: AttributionURLOpener |
| 10 | + private let feedbackURLRef: Ref<URL?> |
| 11 | + |
| 12 | + /// Filters attribution menu items based on the provided closure. |
| 13 | + public var filter: ((AttributionMenuItem) -> Bool)? |
| 14 | + |
| 15 | + init( |
| 16 | + urlOpener: AttributionURLOpener, |
| 17 | + feedbackURLRef: Ref<URL?>, |
| 18 | + filter: ((AttributionMenuItem) -> Bool)? = nil |
| 19 | + ) { |
| 20 | + self.urlOpener = urlOpener |
| 21 | + self.filter = filter |
| 22 | + self.feedbackURLRef = feedbackURLRef |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +extension AttributionMenu { |
| 27 | + var isMetricsEnabled: Bool { |
| 28 | + get { UserDefaults.standard.MGLMapboxMetricsEnabled } |
| 29 | + set { UserDefaults.standard.MGLMapboxMetricsEnabled = newValue } |
| 30 | + } |
| 31 | + |
| 32 | + internal func menu(from attributions: [Attribution]) -> AttributionMenuSection { |
| 33 | + var elements = [AttributionMenuElement]() |
| 34 | + let items = attributions.compactMap { attribution in |
| 35 | + switch attribution.kind { |
| 36 | + case .actionable(let url): |
| 37 | + return AttributionMenuItem(title: attribution.localizedTitle, id: .copyright, category: .main) { [weak self] in |
| 38 | + self?.urlOpener.openAttributionURL(url) |
| 39 | + } |
| 40 | + case .nonActionable: |
| 41 | + return AttributionMenuItem(title: attribution.localizedTitle, id: .copyright, category: .main) |
| 42 | + case .feedback: |
| 43 | + guard let feedbackURL = feedbackURLRef.value else { return nil } |
| 44 | + return AttributionMenuItem(title: attribution.localizedTitle, id: .contribute, category: .main) { [weak self] in |
| 45 | + self?.urlOpener.openAttributionURL(feedbackURL) |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + let menuSubtitle: String? |
| 50 | + if items.count == 1, let item = items.first, item.action == nil { |
| 51 | + menuSubtitle = item.title |
| 52 | + } else { |
| 53 | + menuSubtitle = nil |
| 54 | + elements.append(contentsOf: items.map(AttributionMenuElement.item)) |
| 55 | + } |
| 56 | + |
| 57 | + elements.append(.section(telemetryMenu)) |
| 58 | + |
| 59 | + elements.append(.item(privacyPolicyItem)) |
| 60 | + elements.append(.item(cancelItem)) |
| 61 | + |
| 62 | + let mainTitle = Bundle.mapboxMaps.localizedString( |
| 63 | + forKey: "SDK_NAME", |
| 64 | + value: "Powered by Mapbox", |
| 65 | + table: Ornaments.localizableTableName |
| 66 | + ) |
| 67 | + |
| 68 | + return AttributionMenuSection(title: mainTitle, subtitle: menuSubtitle, category: .main, elements: elements) |
| 69 | + } |
| 70 | + |
| 71 | + private var cancelItem: AttributionMenuItem { |
| 72 | + let cancelTitle = NSLocalizedString("ATTRIBUTION_CANCEL", |
| 73 | + tableName: Ornaments.localizableTableName, |
| 74 | + bundle: .mapboxMaps, |
| 75 | + value: "Cancel", |
| 76 | + comment: "Title of button for dismissing attribution action sheet") |
| 77 | + |
| 78 | + return AttributionMenuItem(title: cancelTitle, style: .cancel, id: .cancel, category: .main) { } |
| 79 | + } |
| 80 | + |
| 81 | + private var privacyPolicyItem: AttributionMenuItem { |
| 82 | + let privacyPolicyTitle = NSLocalizedString("ATTRIBUTION_PRIVACY_POLICY", |
| 83 | + tableName: Ornaments.localizableTableName, |
| 84 | + bundle: .mapboxMaps, |
| 85 | + value: "Mapbox Privacy Policy", |
| 86 | + comment: "Privacy policy action in attribution sheet") |
| 87 | + |
| 88 | + return AttributionMenuItem(title: privacyPolicyTitle, id: .privacyPolicy, category: .main) { [weak self] in |
| 89 | + self?.urlOpener.openAttributionURL(Attribution.privacyPolicyURL) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private var telemetryMenu: AttributionMenuSection { |
| 94 | + let telemetryTitle = TelemetryStrings.telemetryTitle |
| 95 | + let telemetryURL = URL(string: Ornaments.telemetryURL)! |
| 96 | + let message: String |
| 97 | + let participateTitle: String |
| 98 | + let declineTitle: String |
| 99 | + |
| 100 | + if isMetricsEnabled { |
| 101 | + message = TelemetryStrings.telemetryEnabledMessage |
| 102 | + participateTitle = TelemetryStrings.telemetryEnabledOnMessage |
| 103 | + declineTitle = TelemetryStrings.telemetryEnabledOffMessage |
| 104 | + } else { |
| 105 | + message = TelemetryStrings.telemetryDisabledMessage |
| 106 | + participateTitle = TelemetryStrings.telemetryDisabledOnMessage |
| 107 | + declineTitle = TelemetryStrings.telemetryDisabledOffMessage |
| 108 | + } |
| 109 | + |
| 110 | + return AttributionMenuSection(title: telemetryTitle, actionTitle: TelemetryStrings.telemetryName, subtitle: message, category: .telemetry, elements: [ |
| 111 | + AttributionMenuItem(title: TelemetryStrings.telemetryMore, id: .telemetryInfo, category: .telemetry) { [weak self] in |
| 112 | + self?.urlOpener.openAttributionURL(telemetryURL) |
| 113 | + }, |
| 114 | + AttributionMenuItem(title: declineTitle, id: .disable, category: .telemetry) { [weak self] in |
| 115 | + self?.isMetricsEnabled = false |
| 116 | + }, |
| 117 | + AttributionMenuItem(title: participateTitle, style: .cancel, id: .enable, category: .telemetry) { [weak self] in |
| 118 | + self?.isMetricsEnabled = true |
| 119 | + } |
| 120 | + ].map(AttributionMenuElement.item)) |
| 121 | + } |
| 122 | +} |
0 commit comments