Skip to content

Commit

Permalink
Support for SwiftUI and Combine.
Browse files Browse the repository at this point in the history
  • Loading branch information
DnV1eX committed May 26, 2021
1 parent 669ba66 commit a5ba080
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
3 changes: 0 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import PackageDescription

let package = Package(
name: "EnterpriseAppUpdater",
platforms: [
.iOS(.v10),
],
products: [
.library(
name: "EnterpriseAppUpdater",
Expand Down
60 changes: 51 additions & 9 deletions Sources/EnterpriseAppUpdater/EnterpriseAppUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import Foundation


/// Manage in-house application updates.
public struct EnterpriseAppUpdater {

Expand All @@ -36,6 +35,14 @@ public struct EnterpriseAppUpdater {
}


public func message(with metadata: Manifest.Item.Metadata) -> String {
var message = String(format: Message.titleVersionCurrentVersion, metadata.title, metadata.version ?? "?", bundleVersion ?? "?")
if let subtitle = metadata.subtitle {
message += "\n\n\(subtitle.replacingOccurrences(of: "\\n", with: "\n"))"
}
return message
}

/// Load and read the application manifest file.
public func loadManifest(onCompletion: @escaping (Result<Manifest, ManifestError>) -> Void) {

Expand All @@ -62,7 +69,6 @@ public struct EnterpriseAppUpdater {
}
}


/// Check the manifest for an application update.
public func check(manifest: Manifest, strict: Bool = true) -> Result<Manifest.Item, ItemError> {

Expand Down Expand Up @@ -97,7 +103,7 @@ public struct EnterpriseAppUpdater {
#if canImport(UIKit)
import UIKit


@available(iOS 10.0, *)
public extension EnterpriseAppUpdater {
/// Start the application update.
func start(onError: ((URLError) -> Void)? = nil) {
Expand All @@ -113,15 +119,10 @@ public extension EnterpriseAppUpdater {
}
}


/// Provides an alert with update information.
func alert(for item: Manifest.Item, onStart: ((UIAlertAction) -> Void)? = nil, onPostpone: ((UIAlertAction) -> Void)? = nil) -> UIAlertController {

var message = String(format: Message.titleVersionCurrentVersion, item.metadata.title, item.metadata.version ?? "?", bundleVersion ?? "?")
if let subtitle = item.metadata.subtitle {
message += "\n\n\(subtitle.replacingOccurrences(of: "\\n", with: "\n"))"
}
let updateAlert = UIAlertController(title: Message.available, message: message, preferredStyle: .alert)
let updateAlert = UIAlertController(title: Message.available, message: message(with: item.metadata), preferredStyle: .alert)
let startAction = UIAlertAction(title: Message.start, style: .destructive, handler: onStart ?? { _ in self.start() })
updateAlert.addAction(startAction)
let postponeAction = UIAlertAction(title: Message.postpone, style: .cancel, handler: onPostpone)
Expand All @@ -132,6 +133,47 @@ public extension EnterpriseAppUpdater {
#endif


#if canImport(SwiftUI)
import SwiftUI

@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public extension EnterpriseAppUpdater {
/// Provides an alert with update information.
func alert(for item: Manifest.Item, onStart: @escaping (URL) -> Void, onPostpone: (() -> Void)? = nil) -> Alert {

Alert(title: Text(Message.available),
message: Text(message(with: item.metadata)),
primaryButton: .destructive(Text(Message.start), action: { url.map(onStart) }),
secondaryButton: .cancel(Text(Message.postpone), action: onPostpone))
}
}
#endif


#if canImport(Combine)
import Combine

@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public extension EnterpriseAppUpdater {

var publisher: AnyPublisher<Manifest.Item, Error> {
Deferred {
Future { promise in
loadManifest { result in
do {
try promise(.success(check(manifest: result.get()).get()))
} catch {
promise(.failure(error))
}
}
}
}
.eraseToAnyPublisher()
}
}
#endif


public extension EnterpriseAppUpdater {

enum ManifestError: Error, CustomStringConvertible {
Expand Down

0 comments on commit a5ba080

Please sign in to comment.