Skip to content

Commit

Permalink
Merge pull request #25 from hironytic/concurrency
Browse files Browse the repository at this point in the history
Refactor with swift concurrency support
  • Loading branch information
hironytic authored Sep 26, 2021
2 parents 0834398 + 677ec6e commit bf09192
Show file tree
Hide file tree
Showing 21 changed files with 424 additions and 393 deletions.
8 changes: 6 additions & 2 deletions MrHappyoI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
506F4A6E20DB1A8500D3EF77 /* R+RawData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 506F4A6D20DB1A8500D3EF77 /* R+RawData.swift */; };
506F4A7120DE26A300D3EF77 /* ControlPanel.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 506F4A7020DE26A300D3EF77 /* ControlPanel.storyboard */; };
506F4A7320DE276300D3EF77 /* ControlPanelViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 506F4A7220DE276300D3EF77 /* ControlPanelViewController.swift */; };
5091C20226FDC71C00C9FB0F /* TaskExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5091C20126FDC71C00C9FB0F /* TaskExtension.swift */; };
509DF48A20D4AC4E002E5123 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 509DF48920D4AC4E002E5123 /* AppDelegate.swift */; };
509DF48C20D4AC4E002E5123 /* DocumentBrowserViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 509DF48B20D4AC4E002E5123 /* DocumentBrowserViewController.swift */; };
509DF48E20D4AC4E002E5123 /* EditorViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 509DF48D20D4AC4E002E5123 /* EditorViewController.swift */; };
Expand Down Expand Up @@ -67,6 +68,7 @@
506F4A6D20DB1A8500D3EF77 /* R+RawData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "R+RawData.swift"; sourceTree = "<group>"; };
506F4A7020DE26A300D3EF77 /* ControlPanel.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = ControlPanel.storyboard; sourceTree = "<group>"; };
506F4A7220DE276300D3EF77 /* ControlPanelViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ControlPanelViewController.swift; sourceTree = "<group>"; };
5091C20126FDC71C00C9FB0F /* TaskExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TaskExtension.swift; sourceTree = "<group>"; };
509DF48620D4AC4E002E5123 /* MrHappyoI.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MrHappyoI.app; sourceTree = BUILT_PRODUCTS_DIR; };
509DF48920D4AC4E002E5123 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
509DF48B20D4AC4E002E5123 /* DocumentBrowserViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocumentBrowserViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -210,6 +212,7 @@
50F9CEC024473B2B00037C7B /* DefaultSceneDelegate.swift */,
50F9CEC224474CDB00037C7B /* ExternalDisplaySceneDelegate.swift */,
5066F13F20EBA8FE00D9CE2B /* Log.swift */,
5091C20126FDC71C00C9FB0F /* TaskExtension.swift */,
509DF49420D4AC50002E5123 /* Assets.xcassets */,
509DF49620D4AC50002E5123 /* LaunchScreen.storyboard */,
509DF49920D4AC50002E5123 /* Info.plist */,
Expand Down Expand Up @@ -385,6 +388,7 @@
506F4A6C20DB1A4700D3EF77 /* R.swift in Sources */,
509DF4A920D508FB002E5123 /* PlayerViewController.swift in Sources */,
50E08D2D20F625A700017AC3 /* ControlPanelGroupView.swift in Sources */,
5091C20226FDC71C00C9FB0F /* TaskExtension.swift in Sources */,
50511BD020F6349400CBF73C /* R+Image.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -474,7 +478,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 14.5;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -530,7 +534,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 14.5;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_COMPILATION_MODE = wholemodule;
Expand Down
20 changes: 10 additions & 10 deletions MrHappyoI/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@
import UIKit

@UIApplicationMain
public class AppDelegate: UIResponder, UIApplicationDelegate {
class AppDelegate: UIResponder, UIApplicationDelegate {

public var window: UIWindow?
public var scenarioPlayer: ScenarioPlayer?
var window: UIWindow?
var scenarioPlayerTask: Task<Void, Error>?

public static var shared: AppDelegate {
static var shared: AppDelegate {
return UIApplication.shared.delegate as! AppDelegate
}

public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

let center = NotificationCenter.default
Expand All @@ -46,7 +46,7 @@ public class AppDelegate: UIResponder, UIApplicationDelegate {

// MARK: UISceneSession Lifecycle

public func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
switch connectingSceneSession.role {
Expand All @@ -59,13 +59,13 @@ public class AppDelegate: UIResponder, UIApplicationDelegate {
}
}

public func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}

public func application(_ app: UIApplication, open inputURL: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
func application(_ app: UIApplication, open inputURL: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
// Ensure the URL is a file URL
guard inputURL.isFileURL else { return false }

Expand All @@ -87,10 +87,10 @@ public class AppDelegate: UIResponder, UIApplicationDelegate {
}

@objc private func handleScreenDidConnectNotification(_ notification: Notification) {
scenarioPlayer?.stop()
scenarioPlayerTask?.cancel()
}

@objc private func handleScreenDidDisconnectNotification(_ notification: Notification) {
scenarioPlayer?.stop()
scenarioPlayerTask?.cancel()
}
}
18 changes: 9 additions & 9 deletions MrHappyoI/Log.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,38 @@
import Foundation
import os

public struct Log {
struct Log {
private init() {}

public static func error(_ message: String) {
static func error(_ message: String) {
outputLog("%@", logType: .error, args: [message])
}

public static func error(_ message: StaticString, _ args: CVarArg...) {
static func error(_ message: StaticString, _ args: CVarArg...) {
outputLog(message, logType: .error, args: args)
}

public static func warn(_ message: String) {
static func warn(_ message: String) {
outputLog("%@", logType: .default, args: [message])
}

public static func warn(_ message: StaticString, _ args: CVarArg...) {
static func warn(_ message: StaticString, _ args: CVarArg...) {
outputLog(message, logType: .default, args: args)
}

public static func info(_ message: String) {
static func info(_ message: String) {
outputLog("%@", logType: .info, args: [message])
}

public static func info(_ message: StaticString, _ args: CVarArg...) {
static func info(_ message: StaticString, _ args: CVarArg...) {
outputLog(message, logType: .info, args: args)
}

public static func debug(_ message: String) {
static func debug(_ message: String) {
outputLog("%@", logType: .debug, args: [message])
}

public static func debug(_ message: StaticString, _ args: CVarArg...) {
static func debug(_ message: StaticString, _ args: CVarArg...) {
outputLog(message, logType: .debug, args: args)
}

Expand Down
32 changes: 16 additions & 16 deletions MrHappyoI/Model/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
import UIKit
import AVKit

public enum DocumentError: LocalizedError {
enum DocumentError: LocalizedError {
case invalidContent
case invalidScenario(Error)
case unzipError
case notEnoughContent
case zipError

public var errorDescription: String? {
var errorDescription: String? {
switch self {
case .invalidContent:
return R.String.documentErrorInvalidContent.localized()
Expand All @@ -49,13 +49,13 @@ public enum DocumentError: LocalizedError {
}
}

public class Document: UIDocument {
public var slidePDFData = DefaultValue.slidePDFData
public var scenario = DefaultValue.scenario
class Document: UIDocument {
var slidePDFData = DefaultValue.slidePDFData
var scenario = DefaultValue.scenario

private struct DefaultValue {
public static let slidePDFData = R.RawData.defaultSlide.data()
public static let scenario = Scenario(actions: [],
static let slidePDFData = R.RawData.defaultSlide.data()
static let scenario = Scenario(actions: [],
presets: [],
language: "ja-JP",
rate: AVSpeechUtteranceDefaultSpeechRate,
Expand All @@ -65,16 +65,16 @@ public class Document: UIDocument {
postDelay: 0.0)
}

public var errorHandler: ((/* error: */ Error, /* completionHandler: */ @escaping (/* isRecovered: */ Bool) -> Void) -> Void)? = nil
var errorHandler: ((/* error: */ Error, /* completionHandler: */ @escaping (/* isRecovered: */ Bool) -> Void) -> Void)? = nil

private var rootFileWrapper: FileWrapper?

private struct FileName {
public static let slidePDF = "slide.pdf"
public static let scenario = "scenario.json"
static let slidePDF = "slide.pdf"
static let scenario = "scenario.json"
}

public override func contents(forType typeName: String) throws -> Any {
override func contents(forType typeName: String) throws -> Any {
let tempURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
let tempData = tempURL.appendingPathComponent("contents")
defer { _ = try? FileManager.default.removeItem(at: tempData) }
Expand Down Expand Up @@ -125,7 +125,7 @@ public class Document: UIDocument {
guard wroteSize >= 0 else { throw DocumentError.zipError }
}

public override func load(fromContents contents: Any, ofType typeName: String?) throws {
override func load(fromContents contents: Any, ofType typeName: String?) throws {
guard let contents = contents as? Data else { throw DocumentError.invalidContent }

let tempURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
Expand Down Expand Up @@ -176,10 +176,10 @@ public class Document: UIDocument {
}

private struct LoadState {
public var isSlidePDFLoaded: Bool = false
public var isScenarioLoaded: Bool = false
var isSlidePDFLoaded: Bool = false
var isScenarioLoaded: Bool = false

public func isAllLoaded() -> Bool {
func isAllLoaded() -> Bool {
return isSlidePDFLoaded && isScenarioLoaded
}
}
Expand All @@ -199,7 +199,7 @@ public class Document: UIDocument {
}
}

public override func handleError(_ error: Error, userInteractionPermitted: Bool) {
override func handleError(_ error: Error, userInteractionPermitted: Bool) {
Log.warn("Document error: \(String(describing: error))")
guard userInteractionPermitted else { super.handleError(error, userInteractionPermitted: userInteractionPermitted); return }
guard let handler = errorHandler else { super.handleError(error, userInteractionPermitted: userInteractionPermitted); return }
Expand Down
64 changes: 32 additions & 32 deletions MrHappyoI/Model/Scenario.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import AVFoundation
}
*/

public enum ScenarioAction: Codable {
enum ScenarioAction: Codable {
case speak(SpeakParameters)
case changeSlidePage(ChangeSlidePageParameters)
case pause
Expand All @@ -69,7 +69,7 @@ public enum ScenarioAction: Codable {
case type
}

public enum CodingError: Error {
enum CodingError: Error {
case unknownType(String)
}

Expand All @@ -80,7 +80,7 @@ public enum ScenarioAction: Codable {
static let wait = "wait"
}

public init(from decoder: Decoder) throws {
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)

let type = try values.decode(String.self, forKey: .type)
Expand All @@ -105,7 +105,7 @@ public enum ScenarioAction: Codable {
}
}

public func encode(to encoder: Encoder) throws {
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

switch self {
Expand All @@ -127,26 +127,26 @@ public enum ScenarioAction: Codable {
}
}

public struct SpeakParameters: Codable {
public let text: String
public let language: String?
public let rate: Float?
public let pitch: Float? // 0.5 - 2
public let volume: Float? // 0 - 1
public let preDelay: Double?
public let postDelay: Double?
struct SpeakParameters: Codable {
let text: String
let language: String?
let rate: Float?
let pitch: Float? // 0.5 - 2
let volume: Float? // 0 - 1
let preDelay: Double?
let postDelay: Double?
}

public struct ChangeSlidePageParameters: Codable {
public let page: Page
struct ChangeSlidePageParameters: Codable {
let page: Page

public enum Page {
enum Page {
case previous
case next
case to(Int)
}

public enum CodingError: Error {
enum CodingError: Error {
case unknownPageValue(String)
}

Expand All @@ -159,11 +159,11 @@ public struct ChangeSlidePageParameters: Codable {
static let next = "next"
}

public init(page: Page) {
init(page: Page) {
self.page = page
}

public init(from decoder: Decoder) throws {
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)

if let pageString = try? values.decode(String.self, forKey: .page) {
Expand All @@ -181,7 +181,7 @@ public struct ChangeSlidePageParameters: Codable {
}
}

public func encode(to encoder: Encoder) throws {
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

switch page {
Expand All @@ -195,21 +195,21 @@ public struct ChangeSlidePageParameters: Codable {
}
}

public struct WaitParameters: Codable {
public let seconds: Double
struct WaitParameters: Codable {
let seconds: Double
}

public struct Scenario: Codable {
public let actions: [ScenarioAction]
public let presets: [SpeakParameters]
public let language: String
public let rate: Float
public let pitch: Float
public let volume: Float
public let preDelay: Double
public let postDelay: Double
struct Scenario: Codable {
let actions: [ScenarioAction]
let presets: [SpeakParameters]
let language: String
let rate: Float
let pitch: Float
let volume: Float
let preDelay: Double
let postDelay: Double

public init(actions: [ScenarioAction], presets: [SpeakParameters], language: String, rate: Float, pitch: Float, volume: Float, preDelay: Double, postDelay: Double) {
init(actions: [ScenarioAction], presets: [SpeakParameters], language: String, rate: Float, pitch: Float, volume: Float, preDelay: Double, postDelay: Double) {
self.actions = actions
self.presets = presets
self.language = language
Expand All @@ -220,7 +220,7 @@ public struct Scenario: Codable {
self.postDelay = postDelay
}

public init(from decoder: Decoder) throws {
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)

actions = try values.decode([ScenarioAction].self, forKey: .actions)
Expand Down
Loading

0 comments on commit bf09192

Please sign in to comment.