From f3a9be2cd5f9b8b9680cf33218ea597f9fab20cf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Felix=20Paul=20K=C3=BChne?= <felix@feepk.net>
Date: Fri, 12 May 2023 12:24:25 +0200
Subject: [PATCH] CarPlay: correctly show current playmode states in
 now-playing template

---
 .../CarPlay/VLCNowPlayingTemplateObserver.m   | 42 +++++++++++++++++--
 Sources/Playback/Control/VLCPlaybackService.h |  1 +
 Sources/Playback/Control/VLCPlaybackService.m |  6 +++
 3 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/Sources/CarPlay/VLCNowPlayingTemplateObserver.m b/Sources/CarPlay/VLCNowPlayingTemplateObserver.m
index 39f9c1274..dc4f60cb3 100644
--- a/Sources/CarPlay/VLCNowPlayingTemplateObserver.m
+++ b/Sources/CarPlay/VLCNowPlayingTemplateObserver.m
@@ -23,16 +23,23 @@
 {
     self = [super init];
     if (self) {
-        [[NSNotificationCenter defaultCenter] addObserver:self
-                                                 selector:@selector(playbackMetadataChanged:)
-                                                     name:VLCPlaybackServicePlaybackMetadataDidChange
-                                                   object:nil];
+        NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
+        [notificationCenter addObserver:self
+                               selector:@selector(playbackMetadataChanged:)
+                                   name:VLCPlaybackServicePlaybackMetadataDidChange
+                                 object:nil];
+        [notificationCenter addObserver:self
+                               selector:@selector(playModeUpdated:)
+                                   name:VLCPlaybackServicePlaybackModeUpdated
+                                 object:nil];
     }
     return self;
 }
 
 - (void)configureNowPlayingTemplate
 {
+    [self playModeUpdated:nil];
+
     CPNowPlayingRepeatButton *repeatButton = [[CPNowPlayingRepeatButton alloc] initWithHandler:^(CPNowPlayingRepeatButton *button) {
         VLCPlaybackService *vps = [VLCPlaybackService sharedInstance];
         VLCRepeatMode vlcRepeatMode = vps.repeatMode;
@@ -87,6 +94,33 @@
     [[VLCPlaybackService sharedInstance] next];
 }
 
+- (void)playModeUpdated:(NSNotification *)aNotification
+{
+    VLCPlaybackService *vps = [VLCPlaybackService sharedInstance];
+
+    VLCRepeatMode vlcRepeatMode = vps.repeatMode;
+    MPRepeatType reportedRepeatType;
+    switch (vlcRepeatMode) {
+        case VLCRepeatCurrentItem:
+            reportedRepeatType = MPRepeatTypeOne;
+            break;
+
+        case VLCRepeatAllItems:
+            reportedRepeatType = MPRepeatTypeAll;
+            break;
+
+        default:
+            reportedRepeatType = MPRepeatTypeOff;
+    }
+    [MPRemoteCommandCenter sharedCommandCenter].changeRepeatModeCommand.currentRepeatType = reportedRepeatType;
+
+    if (vps.shuffleMode) {
+        [MPRemoteCommandCenter sharedCommandCenter].changeShuffleModeCommand.currentShuffleType = MPShuffleTypeItems;
+    } else {
+        [MPRemoteCommandCenter sharedCommandCenter].changeShuffleModeCommand.currentShuffleType = MPShuffleTypeOff;
+    }
+}
+
 @end
 
 #pragma clang diagnostic pop
diff --git a/Sources/Playback/Control/VLCPlaybackService.h b/Sources/Playback/Control/VLCPlaybackService.h
index 7ddd53424..09114aa06 100644
--- a/Sources/Playback/Control/VLCPlaybackService.h
+++ b/Sources/Playback/Control/VLCPlaybackService.h
@@ -29,6 +29,7 @@ extern NSString *const VLCPlaybackServicePlaybackDidStop;
 extern NSString *const VLCPlaybackServicePlaybackDidFail;
 extern NSString *const VLCPlaybackServicePlaybackMetadataDidChange;
 extern NSString *const VLCPlaybackServicePlaybackPositionUpdated;
+extern NSString *const VLCPlaybackServicePlaybackModeUpdated;
 
 @class VLCPlaybackService;
 @class VLCMetaData;
diff --git a/Sources/Playback/Control/VLCPlaybackService.m b/Sources/Playback/Control/VLCPlaybackService.m
index 6dbef9581..4ca09d408 100644
--- a/Sources/Playback/Control/VLCPlaybackService.m
+++ b/Sources/Playback/Control/VLCPlaybackService.m
@@ -34,6 +34,7 @@ NSString *const VLCPlaybackServicePlaybackDidStop = @"VLCPlaybackServicePlayback
 NSString *const VLCPlaybackServicePlaybackMetadataDidChange = @"VLCPlaybackServicePlaybackMetadataDidChange";
 NSString *const VLCPlaybackServicePlaybackDidFail = @"VLCPlaybackServicePlaybackDidFail";
 NSString *const VLCPlaybackServicePlaybackPositionUpdated = @"VLCPlaybackServicePlaybackPositionUpdated";
+NSString *const VLCPlaybackServicePlaybackModeUpdated = @"VLCPlaybackServicePlaybackModeUpdated";
 
 #if TARGET_OS_IOS
 @interface VLCPlaybackService () <VLCMediaPlayerDelegate, VLCMediaDelegate, VLCMediaListPlayerDelegate, EqualizerViewDelegate>
@@ -513,6 +514,7 @@ NSString *const VLCPlaybackServicePlaybackPositionUpdated = @"VLCPlaybackService
     if ([self.delegate respondsToSelector:@selector(playModeUpdated)]) {
         [self.delegate playModeUpdated];
     }
+    [[NSNotificationCenter defaultCenter] postNotificationName:VLCPlaybackServicePlaybackModeUpdated object:self];
 }
 
 - (BOOL)currentMediaHasChapters
@@ -603,6 +605,7 @@ NSString *const VLCPlaybackServicePlaybackPositionUpdated = @"VLCPlaybackService
     if ([self.delegate respondsToSelector:@selector(playModeUpdated)]) {
         [self.delegate playModeUpdated];
     }
+    [[NSNotificationCenter defaultCenter] postNotificationName:VLCPlaybackServicePlaybackModeUpdated object:self];
 }
 
 - (NSInteger)indexOfCurrentAudioTrack
@@ -845,6 +848,7 @@ NSString *const VLCPlaybackServicePlaybackPositionUpdated = @"VLCPlaybackService
     if ([self.delegate respondsToSelector:@selector(playModeUpdated)]) {
         [self.delegate playModeUpdated];
     }
+    [[NSNotificationCenter defaultCenter] postNotificationName:VLCPlaybackServicePlaybackModeUpdated object:self];
 }
 
 - (void)shuffleMediaList {
@@ -880,6 +884,7 @@ NSString *const VLCPlaybackServicePlaybackPositionUpdated = @"VLCPlaybackService
         if ([self.delegate respondsToSelector:@selector(playModeUpdated)]) {
             [self.delegate playModeUpdated];
         }
+        [[NSNotificationCenter defaultCenter] postNotificationName:VLCPlaybackServicePlaybackModeUpdated object:self];
     } else if (self.repeatMode == VLCRepeatCurrentItem && !isButtonPressed) {
         return _currentIndex;
     }
@@ -951,6 +956,7 @@ NSString *const VLCPlaybackServicePlaybackPositionUpdated = @"VLCPlaybackService
                 if ([self.delegate respondsToSelector:@selector(playModeUpdated)]) {
                     [self.delegate playModeUpdated];
                 }
+                [[NSNotificationCenter defaultCenter] postNotificationName:VLCPlaybackServicePlaybackModeUpdated object:self];
             }
 
             if(_currentIndex > 0) {
-- 
GitLab