-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Merge pull request #2652 from mapbox/maxim/566-add-offline-su…
- Loading branch information
1 parent
89f6cf5
commit aea714c
Showing
30 changed files
with
335 additions
and
1,096 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import UIKit | ||
|
||
class ResizableView: UIControl { | ||
let lineLayer = CAShapeLayer() | ||
// Associated background layer will be masked by the frame of the resizable view | ||
weak var backgroundLayer: CAShapeLayer? | ||
let maskLayer = CAShapeLayer() | ||
var imageView: UIImageView! | ||
var panRecognizer: UIPanGestureRecognizer! | ||
var resizePanRecognizer: UIPanGestureRecognizer! | ||
|
||
convenience init(frame: CGRect, backgroundLayer: CAShapeLayer) { | ||
self.init(frame: frame) | ||
self.backgroundLayer = backgroundLayer | ||
} | ||
|
||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
|
||
clipsToBounds = false | ||
layer.masksToBounds = false | ||
isUserInteractionEnabled = true | ||
backgroundColor = .clear | ||
isOpaque = false | ||
layer.backgroundColor = UIColor.clear.cgColor | ||
|
||
panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(pan(_:))) | ||
resizePanRecognizer = UIPanGestureRecognizer(target: self, action: #selector(resizePan(_:))) | ||
|
||
let image = UIImage(named: "ic_resize")!.withPadding(x: 12, y: 12)! | ||
imageView = UIImageView(image: image.withRenderingMode(.alwaysTemplate)) | ||
imageView.layer.cornerRadius = image.size.width.mid | ||
imageView.backgroundColor = .white | ||
imageView.tintColor = #colorLiteral(red: 0, green: 0.5490196078, blue: 1, alpha: 1) | ||
imageView.isUserInteractionEnabled = true | ||
imageView.layer.shadowColor = #colorLiteral(red: 0.1029271765, green: 0.08949588804, blue: 0.1094761982, alpha: 0.8005611796) | ||
imageView.layer.shadowOffset = CGSize(width: 0, height: 1) | ||
imageView.layer.shadowOpacity = 1 | ||
imageView.layer.shadowRadius = 1.0 | ||
|
||
panRecognizer.require(toFail: resizePanRecognizer) | ||
|
||
addGestureRecognizer(panRecognizer) | ||
imageView.addGestureRecognizer(resizePanRecognizer) | ||
addSubview(imageView) | ||
} | ||
|
||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
@objc func pan(_ sender: UIPanGestureRecognizer) { | ||
if sender.state == .began || sender.state == .changed { | ||
center = sender.location(in: superview) | ||
|
||
layoutSubviews() | ||
} | ||
} | ||
|
||
@objc func resizePan(_ sender: UIPanGestureRecognizer) { | ||
let location = sender.location(in: superview) | ||
|
||
if sender.state == .began || sender.state == .changed { | ||
let origin = CGPoint(x: frame.minX, y: frame.minY) | ||
frame = CGRect(origin: origin, | ||
size: CGSize(width: location.x - origin.x, | ||
height: location.y - origin.y)) | ||
layoutSubviews() | ||
} | ||
} | ||
|
||
override func layoutSubviews() { | ||
super.layoutSubviews() | ||
|
||
if lineLayer.superlayer == nil { | ||
lineLayer.strokeColor = #colorLiteral(red: 0, green: 0.5490196078, blue: 1, alpha: 1).cgColor | ||
lineLayer.fillColor = UIColor.clear.cgColor | ||
lineLayer.lineWidth = 1 | ||
lineLayer.lineDashPattern = [5, 5] | ||
layer.addSublayer(lineLayer) | ||
} | ||
|
||
lineLayer.path = UIBezierPath(rect: bounds).cgPath | ||
|
||
let clippedPath = UIBezierPath(rect: superview!.bounds) | ||
clippedPath.append(UIBezierPath(rect: lineLayer.frame)) | ||
|
||
let superFrame = self.convert(superview!.bounds, to: self) | ||
|
||
if let backgroundLayer = backgroundLayer { | ||
backgroundLayer.path = UIBezierPath(rect: superFrame).cgPath | ||
backgroundLayer.frame = superFrame | ||
|
||
let path = UIBezierPath(rect: frame) | ||
path.append(UIBezierPath(rect: backgroundLayer.bounds)) | ||
|
||
maskLayer.fillRule = .evenOdd | ||
maskLayer.path = path.cgPath | ||
backgroundLayer.mask = maskLayer | ||
} | ||
|
||
imageView.center = CGPoint(x: bounds.maxX-5, y: bounds.maxY-5) | ||
|
||
bringSubviewToFront(imageView) | ||
} | ||
} | ||
|
||
fileprivate extension CGFloat { | ||
var mid: CGFloat { | ||
return self / 2 | ||
} | ||
} | ||
|
||
extension UIImage { | ||
func withPadding(x: CGFloat, y: CGFloat) -> UIImage? { | ||
let width: CGFloat = size.width + x | ||
let height: CGFloat = size.height + y | ||
UIGraphicsBeginImageContextWithOptions(CGSize(width: width, height: height), false, 0) | ||
|
||
defer { | ||
UIGraphicsEndImageContext() | ||
} | ||
|
||
let origin: CGPoint = CGPoint(x: (width - size.width) / 2, y: (height - size.height) / 2) | ||
draw(at: origin) | ||
|
||
return UIGraphicsGetImageFromCurrentImageContext() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import UIKit | ||
import MapboxDirections | ||
import MapboxCoreNavigation | ||
|
||
typealias Payload = () -> () | ||
|
||
protocol ItemProtocol { | ||
var title: String { get } | ||
var subtitle: String? { get } | ||
// View controller to present on SettingsViewController.tableView(_:didSelectRowAt:) | ||
var viewControllerType: UIViewController.Type? { get } | ||
// Closure to call on SettingsViewController.tableView(_:didSelectRowAt:) | ||
var payload: Payload? { get } | ||
// SettingsViewController.tableView(_:canEditRowAt:) | ||
var canEditRow: Bool { get } | ||
} | ||
|
||
struct Item: ItemProtocol { | ||
let title: String | ||
let subtitle: String? | ||
let viewControllerType: UIViewController.Type? | ||
let payload: Payload? | ||
var canEditRow: Bool | ||
|
||
init(title: String, subtitle: String? = nil, viewControllerType: UIViewController.Type? = nil, payload: Payload? = nil, canEditRow: Bool = false) { | ||
self.title = title | ||
self.subtitle = subtitle | ||
self.viewControllerType = viewControllerType | ||
self.payload = payload | ||
self.canEditRow = canEditRow | ||
} | ||
} | ||
|
||
struct Section { | ||
let title: String | ||
let items: [ItemProtocol] | ||
} | ||
|
||
extension SettingsViewController { | ||
// The property is used to decide whether to show the settings button or not | ||
static let numberOfSections = 0 | ||
|
||
func sections() -> [Section] { | ||
return [] | ||
} | ||
} | ||
|
||
extension URL { | ||
var directorySize: Int? { | ||
guard (try? resourceValues(forKeys: [.isDirectoryKey]).isDirectory) as Bool?? != nil else { return nil } | ||
var directorySize = 0 | ||
|
||
(FileManager.default.enumerator(at: self, includingPropertiesForKeys: nil)?.allObjects as? [URL])?.lazy.forEach { | ||
directorySize += (try? $0.resourceValues(forKeys: [.totalFileAllocatedSizeKey]))?.totalFileAllocatedSize ?? 0 | ||
} | ||
|
||
return directorySize | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import UIKit | ||
import MapboxDirections | ||
import MapboxCoreNavigation | ||
|
||
class SettingsViewController: UITableViewController { | ||
let cellIdentifier = "cellId" | ||
var dataSource: [Section]! | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
dataSource = sections() | ||
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Close", style: .done, target: self, action: #selector(close)) | ||
} | ||
|
||
override func viewWillAppear(_ animated: Bool) { | ||
super.viewWillAppear(animated) | ||
|
||
dataSource = sections() | ||
tableView.reloadData() | ||
} | ||
|
||
@IBAction func close() { | ||
dismiss(animated: true, completion: nil) | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | ||
var cell: UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) | ||
if cell == nil { | ||
cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellIdentifier) | ||
} | ||
|
||
let item = dataSource[indexPath.section].items[indexPath.row] | ||
|
||
cell.textLabel?.text = item.title | ||
cell.detailTextLabel?.text = item.subtitle | ||
|
||
return cell | ||
} | ||
|
||
override func numberOfSections(in tableView: UITableView) -> Int { | ||
return dataSource.count | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | ||
return dataSource[section].items.count | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { | ||
return dataSource[section].title | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { | ||
return dataSource[indexPath.section].items[indexPath.row].canEditRow | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle { | ||
return .delete | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { | ||
let item = dataSource[indexPath.section].items[indexPath.row] | ||
|
||
guard let url = Bundle.mapboxCoreNavigation.suggestedTileURL(version: item.title) else { return } | ||
try? FileManager.default.removeItem(atPath: url.path) | ||
|
||
dataSource = sections() | ||
tableView.reloadData() | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | ||
tableView.deselectRow(at: indexPath, animated: true) | ||
|
||
let item = dataSource[indexPath.section].items[indexPath.row] | ||
|
||
if let viewController = item.viewControllerType?.init() { | ||
navigationController?.pushViewController(viewController, animated: true) | ||
} | ||
|
||
if let payload = item.payload { | ||
payload() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,11 @@ | ||
import UIKit | ||
|
||
extension UIViewController { | ||
|
||
func presentAlert(_ title: String? = nil, message: String? = nil, handler: ((UIAlertAction) -> Void)? = nil) { | ||
DispatchQueue.main.async { | ||
let controller = UIAlertController(title: title, message: message, preferredStyle: .alert) | ||
|
||
let defaultHandler: ((UIAlertAction) -> Void) = { (action) in | ||
controller.dismiss(animated: true, completion: nil) | ||
} | ||
|
||
controller.addAction(UIAlertAction(title: NSLocalizedString("ALERT_OK", value: "OK", comment: "Alert action"), style: .default, handler: handler ?? defaultHandler)) | ||
self.present(controller, animated: true, completion: nil) | ||
} | ||
func presentAlert(_ title: String? = nil, message: String? = nil) { | ||
let controller = UIAlertController(title: title, message: message, preferredStyle: .alert) | ||
controller.addAction(UIAlertAction(title: NSLocalizedString("ALERT_OK", value: "OK", comment: "Alert action"), style: .default, handler: { (action) in | ||
controller.dismiss(animated: true, completion: nil) | ||
})) | ||
present(controller, animated: true, completion: nil) | ||
} | ||
} |
Oops, something went wrong.