From d7c70c45311170c7a44e18d5e060e5440227e4f9 Mon Sep 17 00:00:00 2001 From: wutschel Date: Sun, 26 Feb 2023 20:05:00 +0100 Subject: [PATCH 1/3] Introduce helper method resetKodiServerParameters Avoid code duplication --- XBMC Remote/HostManagementViewController.m | 27 +++------------------- XBMC Remote/Utilities.h | 1 + XBMC Remote/Utilities.m | 11 +++++++++ 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/XBMC Remote/HostManagementViewController.m b/XBMC Remote/HostManagementViewController.m index 371cb496d..8457e60fb 100644 --- a/XBMC Remote/HostManagementViewController.m +++ b/XBMC Remote/HostManagementViewController.m @@ -57,14 +57,7 @@ - (void)modifyHost:(NSIndexPath*)item { [serverListTableView deselectRowAtIndexPath:item animated:YES]; cell.accessoryType = UITableViewCellAccessoryNone; storeServerSelection = nil; - AppDelegate.instance.obj.serverDescription = @""; - AppDelegate.instance.obj.serverUser = @""; - AppDelegate.instance.obj.serverPass = @""; - AppDelegate.instance.obj.serverRawIP = @""; - AppDelegate.instance.obj.serverIP = @""; - AppDelegate.instance.obj.serverPort = @""; - AppDelegate.instance.obj.serverHWAddr = @""; - AppDelegate.instance.obj.tcpPort = 0; + [Utilities resetKodiServerParameters]; [[NSNotificationCenter defaultCenter] postNotificationName:@"XBMCServerHasChanged" object:nil]; NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setObject:@(-1) forKey:@"lastServer"]; @@ -169,15 +162,8 @@ - (void)deselectServerAtIndexPath:(NSIndexPath*)indexPath { [serverListTableView deselectRowAtIndexPath:indexPath animated:YES]; cell.accessoryType = UITableViewCellAccessoryNone; storeServerSelection = nil; - AppDelegate.instance.obj.serverDescription = @""; - AppDelegate.instance.obj.serverUser = @""; - AppDelegate.instance.obj.serverPass = @""; - AppDelegate.instance.obj.serverRawIP = @""; - AppDelegate.instance.obj.serverIP = @""; - AppDelegate.instance.obj.serverPort = @""; - AppDelegate.instance.obj.serverHWAddr = @""; + [Utilities resetKodiServerParameters]; AppDelegate.instance.serverOnLine = NO; - AppDelegate.instance.obj.tcpPort = 0; NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setObject:@(-1) forKey:@"lastServer"]; ((UIImageView*)[cell viewWithTag:XIB_HOST_MGMT_CELL_ICON]).image = [UIImage imageNamed:@"connection_off"]; @@ -235,14 +221,7 @@ - (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEdi } else if (storeServerSelection.row == indexPath.row) { storeServerSelection = nil; - AppDelegate.instance.obj.serverDescription = @""; - AppDelegate.instance.obj.serverUser = @""; - AppDelegate.instance.obj.serverPass = @""; - AppDelegate.instance.obj.serverRawIP = @""; - AppDelegate.instance.obj.serverIP = @""; - AppDelegate.instance.obj.serverPort = @""; - AppDelegate.instance.obj.serverHWAddr = @""; - AppDelegate.instance.obj.tcpPort = 0; + [Utilities resetKodiServerParameters]; [[NSNotificationCenter defaultCenter] postNotificationName:@"XBMCServerHasChanged" object:nil]; [userDefaults setObject:@(-1) forKey:@"lastServer"]; } diff --git a/XBMC Remote/Utilities.h b/XBMC Remote/Utilities.h index 7a492ba6a..fd9b473a0 100644 --- a/XBMC Remote/Utilities.h +++ b/XBMC Remote/Utilities.h @@ -129,5 +129,6 @@ typedef NS_ENUM(NSInteger, LogoBackgroundType) { + (UIViewController*)topMostController; + (UIViewController*)topMostControllerIgnoringClass:(Class)ignoredClass; + (uint64_t)memoryFootprint; ++ (void)resetKodiServerParameters; @end diff --git a/XBMC Remote/Utilities.m b/XBMC Remote/Utilities.m index 9a4af4574..a625ac4cc 100644 --- a/XBMC Remote/Utilities.m +++ b/XBMC Remote/Utilities.m @@ -1417,4 +1417,15 @@ + (uint64_t)memoryFootprint { return vmInfo.phys_footprint; } ++ (void)resetKodiServerParameters { + AppDelegate.instance.obj.serverDescription = @""; + AppDelegate.instance.obj.serverUser = @""; + AppDelegate.instance.obj.serverPass = @""; + AppDelegate.instance.obj.serverRawIP = @""; + AppDelegate.instance.obj.serverIP = @""; + AppDelegate.instance.obj.serverPort = @""; + AppDelegate.instance.obj.serverHWAddr = @""; + AppDelegate.instance.obj.tcpPort = 0; +} + @end From e98f802b5a8e7474d6c16d0a57bb4bcac414c3c1 Mon Sep 17 00:00:00 2001 From: wutschel Date: Mon, 2 Jun 2025 19:30:12 +0200 Subject: [PATCH 2/3] Stop reconnecting Calling disconnectFromActiveServer resets the server settings which is equal to not having server selected. In case the server list is shown, the active server must be deselected. --- XBMC Remote/HostManagementViewController.m | 13 +++++++++++++ XBMC Remote/Utilities.m | 11 +++++++++++ 2 files changed, 24 insertions(+) diff --git a/XBMC Remote/HostManagementViewController.m b/XBMC Remote/HostManagementViewController.m index 8457e60fb..652337811 100644 --- a/XBMC Remote/HostManagementViewController.m +++ b/XBMC Remote/HostManagementViewController.m @@ -267,6 +267,14 @@ - (IBAction)editTable:(id)sender forceClose:(BOOL)forceClose { } } +#pragma mark - Helper + +- (void)handleDisconnectActiveServer { + // Deselect any current active server + NSIndexPath *selection = [serverListTableView indexPathForSelectedRow]; + [self deselectServerAtIndexPath:selection]; +} + #pragma mark - Long Press & Action sheet - (void)handleLongPress { @@ -636,6 +644,11 @@ - (void)viewDidLoad { selector:@selector(enablePopGestureRecognizer:) name:@"ECSlidingViewTopDidReset" object:nil]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(handleDisconnectActiveServer) + name:@"DisconnectActiveServer" + object:nil]; } - (void)powerControl { diff --git a/XBMC Remote/Utilities.m b/XBMC Remote/Utilities.m index a625ac4cc..245f69ebf 100644 --- a/XBMC Remote/Utilities.m +++ b/XBMC Remote/Utilities.m @@ -539,21 +539,25 @@ + (UIAlertController*)createPowerControl { else { UIAlertAction *action_pwr_off_system = [UIAlertAction actionWithTitle:LOCALIZED_STR(@"Power off System") style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { [self powerAction:@"System.Shutdown"]; + [Utilities disconnectFromActiveServer]; }]; [alertCtrl addAction:action_pwr_off_system]; UIAlertAction *action_quit_kodi = [UIAlertAction actionWithTitle:LOCALIZED_STR(@"Quit XBMC application") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self powerAction:@"Application.Quit"]; + [Utilities disconnectFromActiveServer]; }]; [alertCtrl addAction:action_quit_kodi]; UIAlertAction *action_hibernate = [UIAlertAction actionWithTitle:LOCALIZED_STR(@"Hibernate") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self powerAction:@"System.Hibernate"]; + [Utilities disconnectFromActiveServer]; }]; [alertCtrl addAction:action_hibernate]; UIAlertAction *action_suspend = [UIAlertAction actionWithTitle:LOCALIZED_STR(@"Suspend") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self powerAction:@"System.Suspend"]; + [Utilities disconnectFromActiveServer]; }]; [alertCtrl addAction:action_suspend]; @@ -1428,4 +1432,11 @@ + (void)resetKodiServerParameters { AppDelegate.instance.obj.tcpPort = 0; } ++ (void)disconnectFromActiveServer { + [Utilities resetKodiServerParameters]; + NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; + [userDefaults setObject:@(-1) forKey:@"lastServer"]; + [[NSNotificationCenter defaultCenter] postNotificationName:@"DisconnectActiveServer" object:nil]; +} + @end From 0ceb471e0be2df8329546d2aea48adb372a43c3e Mon Sep 17 00:00:00 2001 From: wutschel Date: Tue, 3 Jun 2025 21:20:51 +0200 Subject: [PATCH 3/3] Only disconnect on success of power command --- XBMC Remote/Utilities.m | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/XBMC Remote/Utilities.m b/XBMC Remote/Utilities.m index 245f69ebf..2671d77f5 100644 --- a/XBMC Remote/Utilities.m +++ b/XBMC Remote/Utilities.m @@ -507,11 +507,14 @@ + (UIAlertController*)createAlertCopyClipboard:(NSString*)title message:(NSStrin return alertCtrl; } -+ (void)powerAction:(NSString*)command { ++ (void)powerAction:(NSString*)command onSuccess:(void (^)(void))onSuccess { [[Utilities getJsonRPC] callMethod:command withParameters:@{} onCompletion:^(NSString *methodName, NSInteger callId, id methodResult, DSJSONRPCError *methodError, NSError *error) { // User already confirmed, so we only show a short-lived message. if (methodError == nil && error == nil) { [Utilities showMessage:LOCALIZED_STR(@"Command executed") color:SUCCESS_MESSAGE_COLOR]; + if (onSuccess) { + onSuccess(); + } } else { [Utilities showMessage:LOCALIZED_STR(@"Cannot do that") color:ERROR_MESSAGE_COLOR]; @@ -538,51 +541,55 @@ + (UIAlertController*)createPowerControl { } else { UIAlertAction *action_pwr_off_system = [UIAlertAction actionWithTitle:LOCALIZED_STR(@"Power off System") style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { - [self powerAction:@"System.Shutdown"]; - [Utilities disconnectFromActiveServer]; + [self powerAction:@"System.Shutdown" onSuccess:^{ + [Utilities disconnectFromActiveServer]; + }]; }]; [alertCtrl addAction:action_pwr_off_system]; UIAlertAction *action_quit_kodi = [UIAlertAction actionWithTitle:LOCALIZED_STR(@"Quit XBMC application") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - [self powerAction:@"Application.Quit"]; - [Utilities disconnectFromActiveServer]; + [self powerAction:@"Application.Quit" onSuccess:^{ + [Utilities disconnectFromActiveServer]; + }]; }]; [alertCtrl addAction:action_quit_kodi]; UIAlertAction *action_hibernate = [UIAlertAction actionWithTitle:LOCALIZED_STR(@"Hibernate") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - [self powerAction:@"System.Hibernate"]; - [Utilities disconnectFromActiveServer]; + [self powerAction:@"System.Hibernate" onSuccess:^{ + [Utilities disconnectFromActiveServer]; + }]; }]; [alertCtrl addAction:action_hibernate]; UIAlertAction *action_suspend = [UIAlertAction actionWithTitle:LOCALIZED_STR(@"Suspend") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - [self powerAction:@"System.Suspend"]; - [Utilities disconnectFromActiveServer]; + [self powerAction:@"System.Suspend" onSuccess:^{ + [Utilities disconnectFromActiveServer]; + }]; }]; [alertCtrl addAction:action_suspend]; UIAlertAction *action_reboot = [UIAlertAction actionWithTitle:LOCALIZED_STR(@"Reboot") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - [self powerAction:@"System.Reboot"]; + [self powerAction:@"System.Reboot" onSuccess:nil]; }]; [alertCtrl addAction:action_reboot]; UIAlertAction *action_scan_audio_lib = [UIAlertAction actionWithTitle:LOCALIZED_STR(@"Update Audio Library") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - [self powerAction:@"AudioLibrary.Scan"]; + [self powerAction:@"AudioLibrary.Scan" onSuccess:nil]; }]; [alertCtrl addAction:action_scan_audio_lib]; UIAlertAction *action_clean_audio_lib = [UIAlertAction actionWithTitle:LOCALIZED_STR(@"Clean Audio Library") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - [self powerAction:@"AudioLibrary.Clean"]; + [self powerAction:@"AudioLibrary.Clean" onSuccess:nil]; }]; [alertCtrl addAction:action_clean_audio_lib]; UIAlertAction *action_scan_video_lib = [UIAlertAction actionWithTitle:LOCALIZED_STR(@"Update Video Library") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - [self powerAction:@"VideoLibrary.Scan"]; + [self powerAction:@"VideoLibrary.Scan" onSuccess:nil]; }]; [alertCtrl addAction:action_scan_video_lib]; UIAlertAction *action_clean_video_lib = [UIAlertAction actionWithTitle:LOCALIZED_STR(@"Clean Video Library") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - [self powerAction:@"VideoLibrary.Clean"]; + [self powerAction:@"VideoLibrary.Clean" onSuccess:nil]; }]; [alertCtrl addAction:action_clean_video_lib]; }