Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Add iOS native player skeleton for widget intents

Barnabase35e0a9bf95e
diff --git a/ios/AbsorbPlayerCore/Package.swift b/ios/AbsorbPlayerCore/Package.swift
new file mode 100644
index 00000000..90d81872
--- /dev/null
+++ b/ios/AbsorbPlayerCore/Package.swift
@@ -0,0 +1,21 @@
+// swift-tools-version: 5.9
+
+import PackageDescription
+
+let package = Package(
+ name: "AbsorbPlayerCore",
+ platforms: [
+ .iOS(.v15)
+ ],
+ products: [
+ .library(
+ name: "AbsorbPlayerCore",
+ targets: ["AbsorbPlayerCore"]
+ )
+ ],
+ targets: [
+ .target(
+ name: "AbsorbPlayerCore"
+ )
+ ]
+)
diff --git a/ios/AbsorbPlayerCore/Sources/AbsorbPlayerCore/AbsorbPlayerCoreProtocol.swift b/ios/AbsorbPlayerCore/Sources/AbsorbPlayerCore/AbsorbPlayerCoreProtocol.swift
new file mode 100644
index 00000000..63a06967
--- /dev/null
+++ b/ios/AbsorbPlayerCore/Sources/AbsorbPlayerCore/AbsorbPlayerCoreProtocol.swift
@@ -0,0 +1,23 @@
+import Foundation
+
+/// Protocol shared between the host app (Runner) and the widget extension.
+///
+/// The widget intents declare `@Dependency var core: AbsorbPlayerCoreProtocol`,
+/// and the host app's AppDelegate registers a concrete singleton via
+/// `AppDependencyManager.shared.add(dependency:)`. This is what tells iOS to
+/// run the intent in the host app's process rather than the widget extension
+/// process — without that, the widget's `perform()` can't reach the audio
+/// engine that lives in the host app.
+public protocol AbsorbPlayerCoreProtocol: Sendable {
+ func play()
+ func pause()
+ func toggle()
+ func skipForward(seconds: Int)
+ func skipBackward(seconds: Int)
+
+ /// Routes a log line through to the host app's Flutter log sink so it
+ /// surfaces in absorb's in-app log viewer. Widget intents running in the
+ /// host app process via @Dependency can use this to record progress
+ /// without needing Xcode/Console.app to see NSLog output.
+ func log(_ message: String)
+}
diff --git a/ios/NowPlayingWidget/NowPlayingWidget.swift b/ios/NowPlayingWidget/NowPlayingWidget.swift
index c0738793..1287e70f 100644
--- a/ios/NowPlayingWidget/NowPlayingWidget.swift
+++ b/ios/NowPlayingWidget/NowPlayingWidget.swift
@@ -1,3 +1,4 @@
+import AbsorbPlayerCore
import AppIntents
import WidgetKit
import SwiftUI
@@ -62,18 +63,30 @@ struct PlayPauseIntent: AudioPlaybackIntent {
static var title: LocalizedStringResource = "Play or Pause"
static var description = IntentDescription("Toggle audiobook playback.")
+ /// Declaring this dependency tells iOS to launch the host app's process
+ /// to run perform() — the AppDependencyManager registration lives in
+ /// Runner's AppDelegate. Without this the intent runs in the widget
+ /// extension's sandbox where it can't drive audio.
+ @Dependency
+ private var core: AbsorbPlayerCoreProtocol
+
func perform() async throws -> some IntentResult {
- NSLog("[WidgetDebug] PlayPauseIntent.perform fired")
+ // Routing through core.log lands the line in absorb's in-app log
+ // viewer (via the widget channel's "log" method) — that's how we
+ // verify Phase 1 without needing Xcode console access.
+ core.log("[NativeCore] PlayPauseIntent.perform fired (host process)")
// Optimistic UI: flip the stored state so the widget redraws immediately.
let defaults = UserDefaults(suiteName: appGroup)
let wasPlaying = defaults?.bool(forKey: "widget_is_playing") ?? false
defaults?.set(!wasPlaying, forKey: "widget_is_playing")
- NSLog("[WidgetDebug] wasPlaying=%@ -> %@ (defaults=%@)",
- wasPlaying ? "true" : "false",
- !wasPlaying ? "true" : "false",
- defaults == nil ? "nil" : "ok")
+ core.log("[NativeCore] wasPlaying=\(wasPlaying) -> \(!wasPlaying) defaults=\(defaults == nil ? "nil" : "ok")")
activateAudioSession()
+ // Phase 1: native core just logs so we can confirm @Dependency
+ // resolution actually puts us in the host app process. Once that's
+ // verified, the core will drive AVAudioEngine here directly and the
+ // Darwin notification fallback can go away.
+ core.toggle()
postDarwinNotification("com.barnabas.absorb.widget.playPause")
WidgetCenter.shared.reloadTimelines(ofKind: "NowPlayingWidget")
return .result()
diff --git a/ios/Runner.xcodeproj/project.pbxproj b/ios/Runner.xcodeproj/project.pbxproj
index cf4fa087..17a11886 100644
--- a/ios/Runner.xcodeproj/project.pbxproj
+++ b/ios/Runner.xcodeproj/project.pbxproj
@@ -20,6 +20,9 @@
AB10020000000000000E0001 /* NowPlayingWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB10010000000000000E0001 /* NowPlayingWidget.swift */; };
AB10020000000000000E0002 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = AB10010000000000000E0002 /* Assets.xcassets */; };
AB10020000000000000E0003 /* NowPlayingWidgetExtension.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = AB10010000000000000E0004 /* NowPlayingWidgetExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
+ BB10020000000000000E0001 /* AbsorbPlayerCore.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB10010000000000000E0001 /* AbsorbPlayerCore.swift */; };
+ BB10020000000000000E0020 /* AbsorbPlayerCore in Frameworks */ = {isa = PBXBuildFile; productRef = BB10010000000000000E0020 /* AbsorbPlayerCore */; };
+ BB10020000000000000E0021 /* AbsorbPlayerCore in Frameworks */ = {isa = PBXBuildFile; productRef = BB10010000000000000E0021 /* AbsorbPlayerCore */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -90,6 +93,7 @@
AB10010000000000000E0004 /* NowPlayingWidgetExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = NowPlayingWidgetExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; };
AB10010000000000000E0005 /* Runner.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Runner.entitlements; sourceTree = "<group>"; };
AB10010000000000000E0006 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
+ BB10010000000000000E0001 /* AbsorbPlayerCore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AbsorbPlayerCore.swift; sourceTree = "<group>"; };
B7990BE5669E13951B2BAD46 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };
DAACD72AEC68ADA3FA213E6C /* Pods_Runner.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Runner.framework; sourceTree = BUILT_PRODUCTS_DIR; };
E4A191144C46AC47851AEE49 /* Pods-RunnerTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RunnerTests.release.xcconfig"; path = "Target Support Files/Pods-RunnerTests/Pods-RunnerTests.release.xcconfig"; sourceTree = "<group>"; };
@@ -110,6 +114,7 @@
buildActionMask = 2147483647;
files = (
62440589FF82FB058D477B96 /* Pods_Runner.framework in Frameworks */,
+ BB10020000000000000E0020 /* AbsorbPlayerCore in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -117,6 +122,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
+ BB10020000000000000E0021 /* AbsorbPlayerCore in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -199,6 +205,7 @@
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */,
74858FAE1ED2DC5600515810 /* AppDelegate.swift */,
7884E8672EC3CC0400C636F2 /* SceneDelegate.swift */,
+ BB10010000000000000E0001 /* AbsorbPlayerCore.swift */,
74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */,
);
path = Runner;
@@ -258,6 +265,9 @@
AB10070000000000000E0001 /* PBXTargetDependency */,
);
name = Runner;
+ packageProductDependencies = (
+ BB10010000000000000E0020 /* AbsorbPlayerCore */,
+ );
productName = Runner;
productReference = 97C146EE1CF9000F007C117D /* Runner.app */;
productType = "com.apple.product-type.application";
@@ -275,6 +285,9 @@
dependencies = (
);
name = NowPlayingWidgetExtension;
+ packageProductDependencies = (
+ BB10010000000000000E0021 /* AbsorbPlayerCore */,
+ );
productName = NowPlayingWidgetExtension;
productReference = AB10010000000000000E0004 /* NowPlayingWidgetExtension.appex */;
productType = "com.apple.product-type.app-extension";
@@ -311,6 +324,9 @@
Base,
);
mainGroup = 97C146E51CF9000F007C117D;
+ packageReferences = (
+ BB10010000000000000E0010 /* XCLocalSwiftPackageReference "AbsorbPlayerCore" */,
+ );
productRefGroup = 97C146EF1CF9000F007C117D /* Products */;
projectDirPath = "";
projectRoot = "";
@@ -479,6 +495,7 @@
74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */,
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */,
7884E8682EC3CC0700C636F2 /* SceneDelegate.swift in Sources */,
+ BB10020000000000000E0001 /* AbsorbPlayerCore.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -941,6 +958,24 @@
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
+
+/* Begin XCLocalSwiftPackageReference section */
+ BB10010000000000000E0010 /* XCLocalSwiftPackageReference "AbsorbPlayerCore" */ = {
+ isa = XCLocalSwiftPackageReference;
+ relativePath = AbsorbPlayerCore;
+ };
+/* End XCLocalSwiftPackageReference section */
+
+/* Begin XCSwiftPackageProductDependency section */
+ BB10010000000000000E0020 /* AbsorbPlayerCore */ = {
+ isa = XCSwiftPackageProductDependency;
+ productName = AbsorbPlayerCore;
+ };
+ BB10010000000000000E0021 /* AbsorbPlayerCore */ = {
+ isa = XCSwiftPackageProductDependency;
+ productName = AbsorbPlayerCore;
+ };
+/* End XCSwiftPackageProductDependency section */
};
rootObject = 97C146E61CF9000F007C117D /* Project object */;
}
diff --git a/ios/Runner/AbsorbPlayerCore.swift b/ios/Runner/AbsorbPlayerCore.swift
new file mode 100644
index 00000000..8240a628
--- /dev/null
+++ b/ios/Runner/AbsorbPlayerCore.swift
@@ -0,0 +1,54 @@
+import AbsorbPlayerCore
+import Foundation
+
+/// Phase 1 stub. Each call gets logged twice: NSLog (visible in Xcode /
+/// Console.app on a Mac) AND through `logSink` which AppDelegate wires to
+/// the Flutter widget channel's "log" method so it lands in the in-app log
+/// viewer. That second path is what we actually rely on for verification —
+/// no Mac needed.
+///
+/// Once we see `[NativeCore] toggle() called` show up in absorb's in-app
+/// logs after a widget tap (with the host app previously force-quit), the
+/// architecture is proven and we can replace these stubs with real
+/// AVAudioEngine driving.
+final class AbsorbPlayerCore: AbsorbPlayerCoreProtocol, @unchecked Sendable {
+ static let shared = AbsorbPlayerCore()
+
+ /// AppDelegate populates this so log lines get forwarded to Flutter and
+ /// surface in the in-app log viewer. Stays nil if the host app hasn't
+ /// set up the widget channel yet (e.g. during init).
+ static var logSink: ((String) -> Void)?
+
+ private init() {
+ emit("[NativeCore] init - host app process is alive")
+ }
+
+ func play() {
+ emit("[NativeCore] play() called")
+ }
+
+ func pause() {
+ emit("[NativeCore] pause() called")
+ }
+
+ func toggle() {
+ emit("[NativeCore] toggle() called")
+ }
+
+ func skipForward(seconds: Int) {
+ emit("[NativeCore] skipForward(\(seconds)s) called")
+ }
+
+ func skipBackward(seconds: Int) {
+ emit("[NativeCore] skipBackward(\(seconds)s) called")
+ }
+
+ func log(_ message: String) {
+ emit(message)
+ }
+
+ private func emit(_ line: String) {
+ NSLog("%@", line)
+ Self.logSink?(line)
+ }
+}
diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift
index 404c0738..016426ed 100644
--- a/ios/Runner/AppDelegate.swift
+++ b/ios/Runner/AppDelegate.swift
@@ -1,3 +1,5 @@
+import AbsorbPlayerCore
+import AppIntents
import Flutter
import UIKit
import AVFoundation
@@ -38,9 +40,30 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadl
// work without opening the app.
registerWidgetNotifications()
- // Register platform channels on the shared engine
+ // Register platform channels on the shared engine. Must come before the
+ // logSink wiring below so widgetChannel exists when we hand it off.
registerPlatformChannels()
+ // Route native player core log output into the Flutter widget channel's
+ // "log" method, which surfaces lines as `[WidgetDebug] [NativeCore] ...`
+ // in absorb's in-app log viewer. No Mac/Xcode needed to verify behavior.
+ AbsorbPlayerCore.logSink = { [weak self] line in
+ DispatchQueue.main.async {
+ self?.widgetChannel?.invokeMethod("log", arguments: ["msg": line])
+ }
+ }
+
+ // Register the native player core as an AppIntent dependency. The widget
+ // intent declares `@Dependency var core: AbsorbPlayerCoreProtocol` — that
+ // signals to iOS to launch this host app process to run the intent's
+ // perform(), and the dependency manager hands back this concrete instance
+ // so the intent can drive audio in-process. Without this, the widget
+ // intent runs in the widget extension's sandbox and can't reach our audio
+ // engine.
+ let core: AbsorbPlayerCoreProtocol = AbsorbPlayerCore.shared
+ AppDependencyManager.shared.add(dependency: core)
+ AbsorbPlayerCore.logSink?("[NativeCore] Registered as AppIntent dependency")
+
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}