Skip to content

Commit bd99a3e

Browse files
committed
Proof-of-concept for fmt conversion
- Check check for fmt like format - Introduce fmt conversion with fallback option to default format
1 parent b6f96f2 commit bd99a3e

File tree

4 files changed

+80
-24
lines changed

4 files changed

+80
-24
lines changed

Kodi Remote.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
C76594B52C5E883A00B93A2A /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 1A9786C21683D87800B4F3F8 /* Localizable.strings */; };
102102
C76594B62C5E885B00B93A2A /* Roboto-Regular.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 0FE6087E164B3094009CA3A9 /* Roboto-Regular.ttf */; };
103103
C76594B72C5E8A5B00B93A2A /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = C70F41D02BA4D98E00847C75 /* PrivacyInfo.xcprivacy */; };
104+
C771C7292EC7879300581D44 /* fmt_convert.mm in Sources */ = {isa = PBXBuildFile; fileRef = C771C7282EC7879300581D44 /* fmt_convert.mm */; };
104105
C78C30F528F877870055CD95 /* VersionCheck.m in Sources */ = {isa = PBXBuildFile; fileRef = C78C30F428F877870055CD95 /* VersionCheck.m */; };
105106
C78C30FA28F8AADA0055CD95 /* SharingActivityItemSource.m in Sources */ = {isa = PBXBuildFile; fileRef = C78C30F928F89A3E0055CD95 /* SharingActivityItemSource.m */; };
106107
C795123F2DC95A7E00A8CEE5 /* LinkPresentation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C795123E2DC95A7E00A8CEE5 /* LinkPresentation.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
@@ -322,6 +323,8 @@
322323
C76451CB29C51221000AE949 /* UIButton+WebCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIButton+WebCache.m"; path = "SDWebImage/UIButton+WebCache.m"; sourceTree = "<group>"; };
323324
C76451CC29C51221000AE949 /* SDWebImageManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDWebImageManager.h; path = SDWebImage/SDWebImageManager.h; sourceTree = "<group>"; };
324325
C76451CD29C51221000AE949 /* UIImage+MultiFormat.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIImage+MultiFormat.m"; path = "SDWebImage/UIImage+MultiFormat.m"; sourceTree = "<group>"; };
326+
C771C7282EC7879300581D44 /* fmt_convert.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = fmt_convert.mm; sourceTree = "<group>"; };
327+
C771C72A2EC787B500581D44 /* fmt_convert.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = fmt_convert.h; sourceTree = "<group>"; };
325328
C772AD2A2E675D8300435181 /* bs-BA */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "bs-BA"; path = "bs-BA.lproj/InfoPlist.strings"; sourceTree = "<group>"; };
326329
C772AD2B2E675D8300435181 /* bs-BA */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "bs-BA"; path = "bs-BA.lproj/Localizable.strings"; sourceTree = "<group>"; };
327330
C78204462E3930FC00B3F1E9 /* uk-UA */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "uk-UA"; path = "uk-UA.lproj/InfoPlist.strings"; sourceTree = "<group>"; };
@@ -580,6 +583,8 @@
580583
0F4019B218F415A50064E4DC /* MessagesView.m */,
581584
0F4AB8181903DDCE005DEC5C /* RemoteControllerGestureZoneView.h */,
582585
0F4AB8191903DDCE005DEC5C /* RemoteControllerGestureZoneView.m */,
586+
C771C72A2EC787B500581D44 /* fmt_convert.h */,
587+
C771C7282EC7879300581D44 /* fmt_convert.mm */,
583588
0F747736151DD98600EF78AD /* classes */,
584589
0F554901151D1187007E633F /* Supporting Files */,
585590
0F554C21151D197F007E633F /* nib */,
@@ -909,6 +914,7 @@
909914
0F4156451646D810009E6DD4 /* JBKenBurnsView.m in Sources */,
910915
C76451CF29C51221000AE949 /* UIImage+WebP.m in Sources */,
911916
0F7F3DA5164A6D730080A14A /* ECSlidingViewController.m in Sources */,
917+
C771C7292EC7879300581D44 /* fmt_convert.mm in Sources */,
912918
C79B0EA52DF0CF6900046334 /* BaseMasterViewController.m in Sources */,
913919
0F7F3DA6164A6D730080A14A /* UIImage+ImageWithUIView.m in Sources */,
914920
0F7F3DAB164AAD0F0080A14A /* InitialSlidingViewController.m in Sources */,

XBMC Remote/SettingsValuesViewController.m

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#import "ViewControllerIPad.h"
1515
#import "StackScrollViewController.h"
1616
#import "Utilities.h"
17+
#import "fmt_convert.h"
1718

1819
#define SETTINGS_CELL_LABEL 1
1920
#define SETTINGS_CELL_DESCRIPTION 2
@@ -372,43 +373,37 @@ - (void)setSettingValue:(id)value sender:(id)sender {
372373

373374
#pragma mark - Helper
374375

375-
- (NSString*)getFormatString:(NSString*)format {
376-
// Default format does not provide any units
377-
NSString *defaultFormat = settingValueType == SettingValueTypeNumber ? @"%.2f" : @"%i";
378-
379-
// Identify fmt-like format string
376+
- (BOOL)fmtLikeFormat:(NSString*)format {
380377
NSInteger openBraceLoc = [format rangeOfString:@"{"].location;
381378
NSInteger closeBraceLoc = [format rangeOfString:@"}" options:NSBackwardsSearch].location;
382379
if (format.length && openBraceLoc != NSNotFound && closeBraceLoc != NSNotFound && openBraceLoc < closeBraceLoc) {
383-
// Gather range for unit which is added after last "}"
384-
NSRange range;
385-
range.location = closeBraceLoc + 1;
386-
range.length = format.length - range.location;
387-
388-
// Extract unit and make percent character is formatted correctly
389-
NSString *unit = [format substringWithRange:range];
390-
unit = [unit stringByReplacingOccurrencesOfString:@"%" withString:@"%%"];
391-
392-
// Build std format string, appending the fmt format string's unit
393-
format = [defaultFormat stringByAppendingString:unit];
394-
}
395-
// Fallback to default in case no format is defined or we missed to identify fmt-style format (which is used for Kodi 18 and later)
396-
else if (!format.length || AppDelegate.instance.serverVersion >= 18) {
397-
format = defaultFormat;
380+
return YES;
398381
}
399-
return format;
382+
return NO;
400383
}
401384

402385
- (NSString*)getStringForSliderItem:(id)item value:(float)value {
403-
NSString *format = [self getFormatString:item[@"formatlabel"]];
386+
NSString *defaultFormat = settingValueType == SettingValueTypeNumber ? @"%.2f" : @"%i";
387+
NSString *format = item[@"formatlabel"] ?: defaultFormat;
388+
BOOL fmtLike = [self fmtLikeFormat:format];
404389
NSString *stringResult;
405390
switch (settingValueType) {
406391
case SettingValueTypeNumber:
407-
stringResult = [NSString stringWithFormat:format, value];
392+
if (fmtLike) {
393+
stringResult = [NSString fmtFormatted:format defaultFormat:defaultFormat floatValue:value];
394+
}
395+
else {
396+
stringResult = [NSString stringWithFormat:format, value];
397+
}
408398
break;
409399
case SettingValueTypeInteger:
410400
default:
411-
stringResult = [NSString stringWithFormat:format, (int)value];
401+
if (fmtLike) {
402+
stringResult = [NSString fmtFormatted:format defaultFormat:defaultFormat intValue:(int)value];
403+
}
404+
else {
405+
stringResult = [NSString stringWithFormat:format, (int)value];
406+
}
412407
break;
413408
}
414409
return stringResult;

XBMC Remote/fmt_convert.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// fmt_convert.h
3+
// Kodi Remote
4+
//
5+
// Created by Buschmann on 14.11.25.
6+
// Copyright © 2025 Team Kodi. All rights reserved.
7+
//
8+
9+
@interface NSString (fmt)
10+
11+
+ (NSString*)fmtFormatted:(NSString*)format defaultFormat:(NSString*)defaultFormat intValue:(int)value;
12+
+ (NSString*)fmtFormatted:(NSString*)format defaultFormat:(NSString*)defaultFormat floatValue:(float)value;
13+
14+
@end
15+

XBMC Remote/fmt_convert.mm

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// fmt_convert.m
3+
// Kodi Remote
4+
//
5+
// Created by Buschmann on 14.11.25.
6+
// Copyright © 2025 Team Kodi. All rights reserved.
7+
//
8+
9+
#import "fmt_convert.h"
10+
11+
#include <fmt/core.h>
12+
#include <string>
13+
14+
@implementation NSString (fmt)
15+
16+
+ (NSString*)fmtFormatted:(NSString*)format defaultFormat:(NSString*)defaultFormat intValue:(int)value {
17+
try {
18+
const std::string formatted = fmt::format(format.UTF8String, value);
19+
// not sure if `length` param should be +1 to account for the terminating 0-character
20+
return [[NSString alloc] initWithBytes:formatted.data() length:formatted.size() encoding:NSUTF8StringEncoding];
21+
}
22+
catch (const std::exception &exc) {
23+
NSLog(@"generic format error: %s", exc.what());
24+
}
25+
return [NSString stringWithFormat:defaultFormat, value];
26+
}
27+
28+
+ (NSString*)fmtFormatted:(NSString*)format defaultFormat:(NSString*)defaultFormat floatValue:(float)value {
29+
try {
30+
const std::string formatted = fmt::format(format.UTF8String, value);
31+
// not sure if `length` param should be +1 to account for the terminating 0-character
32+
return [[NSString alloc] initWithBytes:formatted.data() length:formatted.size() encoding:NSUTF8StringEncoding];
33+
}
34+
catch (const std::exception &exc) {
35+
NSLog(@"generic format error: %s", exc.what());
36+
}
37+
return [NSString stringWithFormat:defaultFormat, value];
38+
}
39+
40+
@end

0 commit comments

Comments
 (0)