Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Add iOS widget debug logging (tagged [WidgetDebug])

Barnabas844d5dc887b7
diff --git a/ios/NowPlayingWidget/NowPlayingWidget.swift b/ios/NowPlayingWidget/NowPlayingWidget.swift
index 9d49afa5..5d9e40fe 100644
--- a/ios/NowPlayingWidget/NowPlayingWidget.swift
+++ b/ios/NowPlayingWidget/NowPlayingWidget.swift
@@ -14,6 +14,7 @@ private let playPauseURL = URL(string: "audiobookshelf://widget/play_pause?homeW
/// Posts a Darwin notification visible to the main app process.
private func postDarwinNotification(_ name: String) {
+ NSLog("[WidgetDebug] postDarwinNotification: %@", name)
let center = CFNotificationCenterGetDarwinNotifyCenter()
CFNotificationCenterPostNotification(center, CFNotificationName(name as CFString), nil, nil, true)
}
@@ -28,6 +29,7 @@ struct SkipBackIntent: AppIntent {
static var openAppWhenRun: Bool = false
func perform() async throws -> some IntentResult {
+ NSLog("[WidgetDebug] SkipBackIntent.perform fired")
postDarwinNotification("com.barnabas.absorb.widget.skipBack")
return .result()
}
@@ -39,10 +41,15 @@ struct PlayPauseIntent: AppIntent {
static var openAppWhenRun: Bool = false
func perform() async throws -> some IntentResult {
+ NSLog("[WidgetDebug] PlayPauseIntent.perform fired")
// 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")
postDarwinNotification("com.barnabas.absorb.widget.playPause")
WidgetCenter.shared.reloadTimelines(ofKind: "NowPlayingWidget")
@@ -56,6 +63,7 @@ struct SkipForwardIntent: AppIntent {
static var openAppWhenRun: Bool = false
func perform() async throws -> some IntentResult {
+ NSLog("[WidgetDebug] SkipForwardIntent.perform fired")
postDarwinNotification("com.barnabas.absorb.widget.skipForward")
return .result()
}
@@ -97,6 +105,9 @@ struct NowPlayingProvider: TimelineProvider {
private func readEntry() -> NowPlayingEntry {
let d = UserDefaults(suiteName: appGroup)
+ if d == nil {
+ NSLog("[WidgetDebug] readEntry: UserDefaults(suiteName:%@) returned nil - app group not accessible from extension", appGroup)
+ }
let hasBook = d?.bool(forKey: "widget_has_book") ?? false
let title = d?.string(forKey: "widget_title") ?? ""
let author = d?.string(forKey: "widget_author") ?? ""
@@ -104,12 +115,28 @@ struct NowPlayingProvider: TimelineProvider {
let progress = Double(d?.integer(forKey: "widget_progress") ?? 0) / 1000.0
let skipBack = d?.integer(forKey: "widget_skip_back") ?? 0
let skipForward = d?.integer(forKey: "widget_skip_forward") ?? 0
+ let coverPath = d?.string(forKey: "widget_cover_path") ?? ""
var cover: UIImage? = nil
- if let path = d?.string(forKey: "widget_cover_path"), !path.isEmpty {
- cover = UIImage(contentsOfFile: path)
+ var coverStatus = "empty"
+ if !coverPath.isEmpty {
+ let exists = FileManager.default.fileExists(atPath: coverPath)
+ if !exists {
+ coverStatus = "path_missing"
+ } else {
+ cover = UIImage(contentsOfFile: coverPath)
+ coverStatus = cover == nil ? "decode_failed" : "ok"
+ }
}
+ NSLog("[WidgetDebug] readEntry: hasBook=%@ title=\"%@\" isPlaying=%@ progress=%.3f coverPath=\"%@\" coverStatus=%@",
+ hasBook ? "true" : "false",
+ title,
+ isPlaying ? "true" : "false",
+ progress,
+ coverPath,
+ coverStatus)
+
return NowPlayingEntry(
date: .now,
hasBook: hasBook,
diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift
index d0dc4266..18582491 100644
--- a/ios/Runner/AppDelegate.swift
+++ b/ios/Runner/AppDelegate.swift
@@ -59,6 +59,7 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadl
{ (_, observer, name, _, _) in
guard let observer = observer,
let rawName = name?.rawValue as String? else { return }
+ NSLog("[WidgetDebug] AppDelegate received Darwin notification: %@", rawName)
let appDelegate = Unmanaged<AppDelegate>.fromOpaque(observer).takeUnretainedValue()
let action: String
switch rawName {
@@ -68,6 +69,7 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadl
default: return
}
DispatchQueue.main.async {
+ NSLog("[WidgetDebug] AppDelegate dispatching widget action to Flutter: %@", action)
appDelegate.widgetChannel?.invokeMethod("widgetAction", arguments: ["action": action])
}
},
@@ -76,6 +78,7 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadl
.deliverImmediately
)
}
+ NSLog("[WidgetDebug] AppDelegate registered %d Darwin notification observers", names.count)
}
private func registerPlatformChannels() {
@@ -121,8 +124,10 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadl
switch call.method {
case "getGroupContainerPath":
if let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.barnabas.absorb") {
+ NSLog("[WidgetDebug] getGroupContainerPath resolved: %@", url.path)
result(url.path)
} else {
+ NSLog("[WidgetDebug] getGroupContainerPath: containerURL returned nil - app group entitlement missing or misconfigured")
result(nil)
}
default:
diff --git a/lib/services/home_widget_service.dart b/lib/services/home_widget_service.dart
index 0c40be64..107964ba 100644
--- a/lib/services/home_widget_service.dart
+++ b/lib/services/home_widget_service.dart
@@ -42,17 +42,21 @@ class HomeWidgetService {
// Set up App Group for iOS widget data sharing.
if (Platform.isIOS) {
await HomeWidget.setAppGroupId(_appGroupId);
+ debugPrint('[WidgetDebug] setAppGroupId=$_appGroupId');
try {
_groupContainerPath =
await _widgetChannel.invokeMethod<String>('getGroupContainerPath');
+ debugPrint(
+ '[WidgetDebug] groupContainerPath=${_groupContainerPath ?? "<null>"}');
} catch (e) {
- debugPrint('[HomeWidget] Failed to get group container path: $e');
+ debugPrint('[WidgetDebug] Failed to get group container path: $e');
}
// Receive widget AppIntent actions forwarded from AppDelegate.
_widgetChannel.setMethodCallHandler((call) async {
if (call.method == 'widgetAction') {
final action = (call.arguments as Map?)?['action'] as String?;
+ debugPrint('[WidgetDebug] widgetAction received: $action');
switch (action) {
case 'playPause':
await _handlePlayPause();
@@ -120,18 +124,22 @@ class HomeWidgetService {
void _handleSkipBack() {
final player = AudioPlayerService();
+ debugPrint('[WidgetDebug] _handleSkipBack hasBook=${player.hasBook}');
if (!player.hasBook) return;
player.skipBackward();
}
void _handleSkipForward() {
final player = AudioPlayerService();
+ debugPrint('[WidgetDebug] _handleSkipForward hasBook=${player.hasBook}');
if (!player.hasBook) return;
player.skipForward();
}
Future<void> _handlePlayPause() async {
final player = AudioPlayerService();
+ debugPrint(
+ '[WidgetDebug] _handlePlayPause hasBook=${player.hasBook} isPlaying=${player.isPlaying}');
// When there's an active session the widget uses a MediaSession media button
// instead of launching the app, so this path is only hit for cold resume.
@@ -139,8 +147,10 @@ class HomeWidgetService {
// On iOS, widget links always come through here (no MediaSession shortcut).
if (Platform.isIOS) {
if (player.isPlaying) {
+ debugPrint('[WidgetDebug] -> pause()');
player.pause();
} else {
+ debugPrint('[WidgetDebug] -> play()');
player.play();
}
}
@@ -267,6 +277,9 @@ class HomeWidgetService {
final player = AudioPlayerService();
final hasBook = player.hasBook;
+ debugPrint(
+ '[WidgetDebug] _updateWidgetData hasBook=$hasBook isPlaying=${player.isPlaying} title="${player.currentTitle ?? ""}" itemId=${player.currentItemId}');
+
await HomeWidget.saveWidgetData<bool>('widget_has_book', hasBook);
// Push user's skip durations so the widget shows them on the buttons.
@@ -334,16 +347,21 @@ class HomeWidgetService {
final player = AudioPlayerService();
final coverUrl = player.currentCoverUrl;
final cacheKey = '$itemId|$coverUrl';
- if (_lastCoverItemId == cacheKey) return;
+ if (_lastCoverItemId == cacheKey) {
+ debugPrint('[WidgetDebug] cover unchanged, skipping (key=$cacheKey)');
+ return;
+ }
_lastCoverItemId = cacheKey;
String? coverPath;
+ String source = 'none';
try {
// Check for a locally downloaded cover first.
final downloadService = DownloadService();
if (downloadService.isDownloaded(itemId)) {
coverPath = await downloadService.getLocalCoverPath(itemId);
+ source = 'download';
}
// If no local cover, download from server to a temp/shared file.
@@ -358,6 +376,9 @@ class HomeWidgetService {
if (response.statusCode == 200 && response.bodyBytes.isNotEmpty) {
await coverFile.writeAsBytes(response.bodyBytes);
coverPath = coverFile.path;
+ source = 'network';
+ } else {
+ source = 'network_failed(${response.statusCode})';
}
}
} else if (Platform.isIOS && _groupContainerPath != null) {
@@ -368,16 +389,23 @@ class HomeWidgetService {
await File(coverPath).copy(sharedFile.path);
}
coverPath = sharedFile.path;
+ source = 'download_copied_to_group';
}
} catch (e) {
- debugPrint('[HomeWidget] Cover update failed: $e');
+ debugPrint('[WidgetDebug] cover update failed: $e');
}
+ final pathInGroup =
+ coverPath != null && _groupContainerPath != null && coverPath.startsWith(_groupContainerPath!);
+ final exists = coverPath != null && File(coverPath).existsSync();
+ debugPrint(
+ '[WidgetDebug] cover source=$source path=$coverPath exists=$exists inAppGroup=$pathInGroup');
+
try {
await HomeWidget.saveWidgetData<String?>('widget_cover_path', coverPath);
await _updateAllWidgets();
} catch (e) {
- debugPrint('[HomeWidget] Cover save failed: $e');
+ debugPrint('[WidgetDebug] cover save failed: $e');
}
}