Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/stop sound widget #10

Merged
merged 6 commits into from
Mar 1, 2025
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
25 changes: 20 additions & 5 deletions App/IntentRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,37 @@ import UIKit
import MediaPlayer

enum IntentRunner {
private static var activePlayer: [UUID: AudioPlayer] = [:]

static func perform(intent: SoundIntent) async throws -> some IntentResult {
print("Start playing \(intent.sound.title)")
let player = try! AudioPlayer(url: intent.sound.file.fileURL!)
let soundID = intent.sound.id

if let player = activePlayer[soundID], player.isPlaying {
print("Stopping sound \(intent.sound.title)")
player.stop()
activePlayer[soundID] = nil
return .result()
}

guard let fileURL = intent.sound.file.fileURL, let newPlayer = try? AudioPlayer(url: fileURL) else {
AudioErrorManager.errorManager.reportError("There was a error in the Widget")
return .result()
}

activePlayer[soundID] = newPlayer
print("Created sound for \(intent.sound.title)")

if intent.isFullBlast && player.isOnSpeaker {
if intent.isFullBlast && newPlayer.isOnSpeaker {
await MPVolumeView.setVolume(1)
}

do {
print("Playing sound \(intent.sound.title)")
try await player.playOnQueue()
defer { activePlayer[soundID] = nil }
try await newPlayer.playOnQueue()
print("Sound \(intent.sound.title) stopped")
} catch {
print("Playing Sound fauled error:", error.localizedDescription)
AudioErrorManager.errorManager.reportError("There was a error playing sound in the Widget")
}
return .result()
}
Expand Down
20 changes: 19 additions & 1 deletion App/Views/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ struct MainView: View {

@ScaledMetric(relativeTo: .headline) var minimumWidth: CGFloat = 130

@StateObject private var errorManager = AudioErrorManager.errorManager

var body: some View {
NavigationSplitView(preferredCompactColumn: $preferredCompactColumn, sidebar: {
ScrollView(.vertical) {
Expand Down Expand Up @@ -144,7 +146,11 @@ struct MainView: View {
GalleryBoard.animals.save()
}
}

.alert("Error", isPresented: $errorManager.showWidgetError) {
Button("OK") { }
} message: {
Text(errorManager.errorMessage ?? "Unknown error")
}
}
}

Expand All @@ -171,6 +177,18 @@ extension Array {
}
}

class AudioErrorManager: ObservableObject {
static let errorManager = AudioErrorManager()
@Published var errorMessage: String?
@Published var showWidgetError: Bool = false

func reportError(_ message: String) {
DispatchQueue.main.async {
self.errorMessage = message
self.showWidgetError = true
}
}
}

#Preview {
MainView()
Expand Down