2020-01-11 19:30:16 +01:00
|
|
|
//
|
|
|
|
// UndoAvailableAlertController.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Phil Viso on 9/29/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import UIKit
|
|
|
|
|
2020-05-13 06:33:51 +02:00
|
|
|
protocol MarkAsReadAlertControllerSourceType {}
|
|
|
|
extension CGRect: MarkAsReadAlertControllerSourceType {}
|
|
|
|
extension UIView: MarkAsReadAlertControllerSourceType {}
|
|
|
|
extension UIBarButtonItem: MarkAsReadAlertControllerSourceType {}
|
|
|
|
|
|
|
|
|
2020-01-11 19:30:16 +01:00
|
|
|
struct MarkAsReadAlertController {
|
|
|
|
|
2020-05-13 06:33:51 +02:00
|
|
|
static func confirm<T>(_ controller: UIViewController?,
|
|
|
|
coordinator: SceneCoordinator?,
|
|
|
|
confirmTitle: String,
|
|
|
|
sourceType: T,
|
|
|
|
cancelCompletion: (() -> Void)? = nil,
|
|
|
|
completion: @escaping () -> Void) where T: MarkAsReadAlertControllerSourceType {
|
2020-01-11 19:30:16 +01:00
|
|
|
|
|
|
|
guard let controller = controller, let coordinator = coordinator else {
|
|
|
|
completion()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-02 04:47:45 +02:00
|
|
|
if AppDefaults.shared.confirmMarkAllAsRead {
|
2020-05-13 06:33:51 +02:00
|
|
|
let alertController = MarkAsReadAlertController.alert(coordinator: coordinator, confirmTitle: confirmTitle, cancelCompletion: cancelCompletion, sourceType: sourceType) { _ in
|
2020-01-11 19:30:16 +01:00
|
|
|
completion()
|
|
|
|
}
|
|
|
|
controller.present(alertController, animated: true)
|
|
|
|
} else {
|
|
|
|
completion()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 06:33:51 +02:00
|
|
|
private static func alert<T>(coordinator: SceneCoordinator,
|
2020-01-11 19:30:16 +01:00
|
|
|
confirmTitle: String,
|
|
|
|
cancelCompletion: (() -> Void)?,
|
2020-05-13 06:33:51 +02:00
|
|
|
sourceType: T,
|
|
|
|
completion: @escaping (UIAlertAction) -> Void) -> UIAlertController where T: MarkAsReadAlertControllerSourceType {
|
|
|
|
|
2020-01-11 19:30:16 +01:00
|
|
|
|
|
|
|
let title = NSLocalizedString("Mark As Read", comment: "Mark As Read")
|
2021-02-01 01:12:20 +01:00
|
|
|
let message = NSLocalizedString("You can turn this confirmation off in Settings.",
|
|
|
|
comment: "You can turn this confirmation off in Settings.")
|
2020-01-11 19:30:16 +01:00
|
|
|
let cancelTitle = NSLocalizedString("Cancel", comment: "Cancel")
|
|
|
|
let settingsTitle = NSLocalizedString("Open Settings", comment: "Open Settings")
|
|
|
|
|
2020-05-13 06:33:51 +02:00
|
|
|
let alertController = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
|
2020-01-11 19:30:16 +01:00
|
|
|
let cancelAction = UIAlertAction(title: cancelTitle, style: .cancel) { _ in
|
|
|
|
cancelCompletion?()
|
|
|
|
}
|
|
|
|
let settingsAction = UIAlertAction(title: settingsTitle, style: .default) { _ in
|
|
|
|
coordinator.showSettings(scrollToArticlesSection: true)
|
|
|
|
}
|
|
|
|
let markAction = UIAlertAction(title: confirmTitle, style: .default, handler: completion)
|
|
|
|
|
|
|
|
alertController.addAction(markAction)
|
|
|
|
alertController.addAction(settingsAction)
|
|
|
|
alertController.addAction(cancelAction)
|
2020-05-13 06:33:51 +02:00
|
|
|
|
|
|
|
if let barButtonItem = sourceType as? UIBarButtonItem {
|
|
|
|
alertController.popoverPresentationController?.barButtonItem = barButtonItem
|
|
|
|
}
|
|
|
|
|
|
|
|
if let rect = sourceType as? CGRect {
|
|
|
|
alertController.popoverPresentationController?.sourceRect = rect
|
|
|
|
}
|
|
|
|
|
|
|
|
if let view = sourceType as? UIView {
|
|
|
|
alertController.popoverPresentationController?.sourceView = view
|
|
|
|
}
|
2020-01-11 19:30:16 +01:00
|
|
|
|
|
|
|
return alertController
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|