Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions LocalPackage/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// swift-tools-version: 5.9

import PackageDescription

let package = Package(
name: "TimeWatcherLocalPackage",
platforms: [
.iOS(.v17),
.macOS(.v14),
],
products: [
.library(name: "TimeWatcherCore", targets: ["TimeWatcherCore"]),
.library(name: "TimeWatcherFeature", targets: ["TimeWatcherFeature"]),
.library(name: "TimeWatcherTestSupport", targets: ["TimeWatcherTestSupport"]),
],
dependencies: [
.package(path: "../TimeWatcherExternalResouce"),
],
targets: [
.target(
name: "TimeWatcherCore",
path: "Sources/TimeWatcherCore"
),
.target(
name: "TimeWatcherFeature",
dependencies: [
"TimeWatcherCore",
.product(name: "TimeWatcherExternalResouce", package: "TimeWatcherExternalResouce"),
],
path: "Sources/TimeWatcherFeature"
),
.target(
name: "TimeWatcherTestSupport",
dependencies: ["TimeWatcherCore"],
path: "Sources/TimeWatcherTestSupport"
),
.testTarget(
name: "TimeWatcherCoreTests",
dependencies: ["TimeWatcherCore", "TimeWatcherTestSupport"],
path: "Tests/TimeWatcherCoreTests"
),
]
)
46 changes: 46 additions & 0 deletions LocalPackage/Sources/TimeWatcherCore/Domain/TimerActionType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/// タイマー操作のボタンの動作を定義
public enum TimerActionType {

/// 開始のアクション
case start

/// 停止のアクション
case stop

/// リセットのアクション
case reset
}

// MARK: - 外部公開用のプロパティ定義
extension TimerActionType {

public var buttonTitle: String {

switch self {

case .start:
"Start"

case .stop:
"Stop"

case .reset:
"Reset"
}
}

public var buttonIconName: String {

switch self {

case .start:
"play.fill"

case .stop:
"pause.fill"

case .reset:
"xmark"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Foundation

/// LiveActivityに渡すタイマー状態(ActivityKit非依存)
public struct TimerLiveActivityState {

/// 経過時間(秒)
public var timeLapse: TimeInterval
/// 現在日時
public var currentDate: Date
/// 表示用経過時間文字列
public var timeLapseString: String
/// タイマーの状態
public var timerStatus: TimerStatus

public init(timeLapse: TimeInterval,
currentDate: Date,
timeLapseString: String,
timerStatus: TimerStatus) {

self.timeLapse = timeLapse
self.currentDate = currentDate
self.timeLapseString = timeLapseString
self.timerStatus = timerStatus
}
}
56 changes: 56 additions & 0 deletions LocalPackage/Sources/TimeWatcherCore/Domain/TimerStatus.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/// タイマーの動作状態
public enum TimerStatus: Codable {

/// 停止中(タイマーリセット済み)
case initial
/// 停止中(タイマーリセット前)
case stop
/// 開始中
case start
}

// MARK: - 外部公開用のプロパティ
extension TimerStatus {

/// 使用可能なタイマーアクション
public var useableActions: [TimerActionType] {

return switch self {

case .initial:
[.start]

case .stop:
[.reset, .start]

case .start:
[.reset, .stop]
}
}

/// 経過時間計測中かどうか
public var isPlaying: Bool {

return switch self {

case .initial, .stop:
false

case .start:
true
}
}

/// 状態を表すアイコン名
public var icon: String {

return switch self {

case .initial, .stop:
"pause.fill"

case .start:
"play.fill"
}
}
}
38 changes: 38 additions & 0 deletions LocalPackage/Sources/TimeWatcherCore/Domain/WidgetUrlKey.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Foundation

/// WidgetURLで使用するURLのKey
public enum WidgetUrlKey: CaseIterable {

/// タイマーリセットのリンク
case timerResetLink
/// その他デフォルトのリンク
case defaultLink
}

extension WidgetUrlKey {

private static let scheme = "https://"

private static var defaultURL: URL {

return URL(string: "\(scheme)\(AppConstants.mainBundleId)")!
}

/// Keyに紐づくWidgetURL
public var url: URL {

return URL(string: "\(Self.scheme)\(AppConstants.mainBundleId)\(self.path)") ?? Self.defaultURL
}

public var path: String {

switch self {

case .timerResetLink:
return "/reset"

case .defaultLink:
return ""
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// LiveActivityを管理するプロトコル(ActivityKit非依存)
public protocol LiveActivityManaging: Sendable {

/// LiveActivityを開始する
func start(state: TimerLiveActivityState) async throws

/// LiveActivityの情報の更新
func update(state: TimerLiveActivityState) async throws

/// LiveActivityの終了
func stop() async throws
}

/// LiveActivity操作時のエラー
public enum LiveActivityRequestError: Error, Equatable {

case notFoundActivity
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// 何もしない LiveActivityManaging 実装(Preview・テストのデフォルト用)
public actor NullLiveActivityManager: LiveActivityManaging {

public init() {}

public func start(state: TimerLiveActivityState) async throws {}

public func update(state: TimerLiveActivityState) async throws {}

public func stop() async throws {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Foundation

/// Widget Intent がタイマーを操作するためのプロトコル
@MainActor
public protocol TimerControlable: Sendable {

var timeWatch: TimeWatch { get }
var liveActivityManager: LiveActivityManaging { get }
var dateDependency: DateDependency { get }
}

@MainActor
extension TimerControlable {

public func updateLiveActivity(status: TimerStatus) async throws {

let timeLapse = timeWatch.getCurrentTimeLapse()
try await liveActivityManager.update(state: TimerLiveActivityState(
timeLapse: timeLapse,
currentDate: dateDependency.generateNow(),
timeLapseString: timeLapse.timeLapseShortString,
timerStatus: status
))
}
}
Loading
Loading