forked from readium/swift-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCPRenewDelegate.swift
77 lines (58 loc) · 2.79 KB
/
LCPRenewDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// Copyright 2021 Readium Foundation. All rights reserved.
// Use of this source code is governed by the BSD-style license
// available in the top-level LICENSE file of the project.
//
import Foundation
import SafariServices
import UIKit
import R2Shared
/// UX delegate for the loan renew LSD interaction.
public protocol LCPRenewDelegate {
/// Called when the renew interaction allows to customize the end date programmatically.
///
/// You can prompt the user for the number of days to renew, for example.
/// The returned date should not exceed `maximumDate`.
func preferredEndDate(maximum: Date?, completion: @escaping (CancellableResult<Date?, Error>) -> Void)
/// Called when the renew interaction uses an HTML web page.
///
/// You should present the URL in a `SFSafariViewController` and call the `completion` callback when the browser
/// is dismissed by the user.
func presentWebPage(url: URL, completion: @escaping (CancellableResult<Void, Error>) -> Void)
}
/// Default `LCPRenewDelegate` implementation using standard views.
///
/// No date picker is presented for selecting a preferred end date. If you want to support one, you can subclass or
/// decorate `LCPRenewDelegate`.
public class LCPDefaultRenewDelegate: NSObject, LCPRenewDelegate {
private let presentingViewController: UIViewController
private let modalPresentationStyle: UIModalPresentationStyle
public init(presentingViewController: UIViewController, modalPresentationStyle: UIModalPresentationStyle = .formSheet) {
self.presentingViewController = presentingViewController
self.modalPresentationStyle = modalPresentationStyle
}
public func preferredEndDate(maximum: Date?, completion: @escaping (CancellableResult<Date?, Error>) -> ()) {
completion(.success(nil))
}
public func presentWebPage(url: URL, completion: @escaping (CancellableResult<(), Error>) -> ()) {
let safariVC = SFSafariViewController(url: url)
safariVC.modalPresentationStyle = self.modalPresentationStyle
safariVC.presentationController?.delegate = self
safariVC.delegate = self
webPageCallback = completion
presentingViewController.present(safariVC, animated: true)
}
private var webPageCallback: ((CancellableResult<(), Error>) -> Void)? = nil
}
extension LCPDefaultRenewDelegate: UIAdaptivePresentationControllerDelegate {
public func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
webPageCallback?(.success(()))
webPageCallback = nil
}
}
extension LCPDefaultRenewDelegate: SFSafariViewControllerDelegate {
public func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
webPageCallback?(.success(()))
webPageCallback = nil
}
}