Skip to content

Commit 9f5d3f6

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 3dbaf9a commit 9f5d3f6

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
@@ -100,6 +100,7 @@
100100
C76594B52C5E883A00B93A2A /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 1A9786C21683D87800B4F3F8 /* Localizable.strings */; };
101101
C76594B62C5E885B00B93A2A /* Roboto-Regular.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 0FE6087E164B3094009CA3A9 /* Roboto-Regular.ttf */; };
102102
C76594B72C5E8A5B00B93A2A /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = C70F41D02BA4D98E00847C75 /* PrivacyInfo.xcprivacy */; };
103+
C771C7292EC7879300581D44 /* fmt_convert.mm in Sources */ = {isa = PBXBuildFile; fileRef = C771C7282EC7879300581D44 /* fmt_convert.mm */; };
103104
C78C30F528F877870055CD95 /* VersionCheck.m in Sources */ = {isa = PBXBuildFile; fileRef = C78C30F428F877870055CD95 /* VersionCheck.m */; };
104105
C78C30FA28F8AADA0055CD95 /* SharingActivityItemSource.m in Sources */ = {isa = PBXBuildFile; fileRef = C78C30F928F89A3E0055CD95 /* SharingActivityItemSource.m */; };
105106
C795123F2DC95A7E00A8CEE5 /* LinkPresentation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C795123E2DC95A7E00A8CEE5 /* LinkPresentation.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
@@ -326,6 +327,8 @@
326327
C76451CB29C51221000AE949 /* UIButton+WebCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIButton+WebCache.m"; path = "SDWebImage/UIButton+WebCache.m"; sourceTree = "<group>"; };
327328
C76451CC29C51221000AE949 /* SDWebImageManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDWebImageManager.h; path = SDWebImage/SDWebImageManager.h; sourceTree = "<group>"; };
328329
C76451CD29C51221000AE949 /* UIImage+MultiFormat.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIImage+MultiFormat.m"; path = "SDWebImage/UIImage+MultiFormat.m"; sourceTree = "<group>"; };
330+
C771C7282EC7879300581D44 /* fmt_convert.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = fmt_convert.mm; sourceTree = "<group>"; };
331+
C771C72A2EC787B500581D44 /* fmt_convert.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = fmt_convert.h; sourceTree = "<group>"; };
329332
C772AD2A2E675D8300435181 /* bs-BA */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "bs-BA"; path = "bs-BA.lproj/InfoPlist.strings"; sourceTree = "<group>"; };
330333
C772AD2B2E675D8300435181 /* bs-BA */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "bs-BA"; path = "bs-BA.lproj/Localizable.strings"; sourceTree = "<group>"; };
331334
C78204462E3930FC00B3F1E9 /* uk-UA */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "uk-UA"; path = "uk-UA.lproj/InfoPlist.strings"; sourceTree = "<group>"; };
@@ -588,6 +591,8 @@
588591
0F4019B218F415A50064E4DC /* MessagesView.m */,
589592
0F4AB8181903DDCE005DEC5C /* RemoteControllerGestureZoneView.h */,
590593
0F4AB8191903DDCE005DEC5C /* RemoteControllerGestureZoneView.m */,
594+
C771C72A2EC787B500581D44 /* fmt_convert.h */,
595+
C771C7282EC7879300581D44 /* fmt_convert.mm */,
591596
0F747736151DD98600EF78AD /* classes */,
592597
0F554901151D1187007E633F /* Supporting Files */,
593598
0F554C21151D197F007E633F /* nib */,
@@ -936,6 +941,7 @@
936941
C76451CF29C51221000AE949 /* UIImage+WebP.m in Sources */,
937942
0F7F3DA5164A6D730080A14A /* ECSlidingViewController.m in Sources */,
938943
C7B7D03A2F76D36E00689B5A /* UILabel+Extensions.m in Sources */,
944+
C771C7292EC7879300581D44 /* fmt_convert.mm in Sources */,
939945
C79B0EA52DF0CF6900046334 /* BaseMasterViewController.m in Sources */,
940946
0F7F3DA6164A6D730080A14A /* UIImage+ImageWithUIView.m in Sources */,
941947
0F7F3DAB164AAD0F0080A14A /* InitialSlidingViewController.m in Sources */,

XBMC Remote/SettingsValuesViewController.m

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#import "StackScrollViewController.h"
1616
#import "Utilities.h"
1717
#import "UIBarButtonItem+Extensions.h"
18+
#import "fmt_convert.h"
1819

1920
#define SETTINGS_CELL_LABEL 1
2021
#define SETTINGS_CELL_DESCRIPTION 2
@@ -371,43 +372,37 @@ - (void)setSettingValue:(id)value sender:(id)sender {
371372

372373
#pragma mark - Helper
373374

374-
- (NSString*)getFormatString:(NSString*)format {
375-
// Default format does not provide any units
376-
NSString *defaultFormat = settingValueType == SettingValueTypeNumber ? @"%.2f" : @"%i";
377-
378-
// Identify fmt-like format string
375+
- (BOOL)fmtLikeFormat:(NSString*)format {
379376
NSInteger openBraceLoc = [format rangeOfString:@"{"].location;
380377
NSInteger closeBraceLoc = [format rangeOfString:@"}" options:NSBackwardsSearch].location;
381378
if (format.length && openBraceLoc != NSNotFound && closeBraceLoc != NSNotFound && openBraceLoc < closeBraceLoc) {
382-
// Gather range for unit which is added after last "}"
383-
NSRange range;
384-
range.location = closeBraceLoc + 1;
385-
range.length = format.length - range.location;
386-
387-
// Extract unit and make percent character is formatted correctly
388-
NSString *unit = [format substringWithRange:range];
389-
unit = [unit stringByReplacingOccurrencesOfString:@"%" withString:@"%%"];
390-
391-
// Build std format string, appending the fmt format string's unit
392-
format = [defaultFormat stringByAppendingString:unit];
393-
}
394-
// 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)
395-
else if (!format.length || AppDelegate.instance.serverVersion >= 18) {
396-
format = defaultFormat;
379+
return YES;
397380
}
398-
return format;
381+
return NO;
399382
}
400383

401384
- (NSString*)getStringForSliderItem:(id)item value:(float)value {
402-
NSString *format = [self getFormatString:item[@"formatlabel"]];
385+
NSString *defaultFormat = settingValueType == SettingValueTypeNumber ? @"%.2f" : @"%i";
386+
NSString *format = item[@"formatlabel"] ?: defaultFormat;
387+
BOOL fmtLike = [self fmtLikeFormat:format];
403388
NSString *stringResult;
404389
switch (settingValueType) {
405390
case SettingValueTypeNumber:
406-
stringResult = [NSString stringWithFormat:format, value];
391+
if (fmtLike) {
392+
stringResult = [NSString fmtFormatted:format defaultFormat:defaultFormat floatValue:value];
393+
}
394+
else {
395+
stringResult = [NSString stringWithFormat:format, value];
396+
}
407397
break;
408398
case SettingValueTypeInteger:
409399
default:
410-
stringResult = [NSString stringWithFormat:format, (int)value];
400+
if (fmtLike) {
401+
stringResult = [NSString fmtFormatted:format defaultFormat:defaultFormat intValue:(int)value];
402+
}
403+
else {
404+
stringResult = [NSString stringWithFormat:format, (int)value];
405+
}
411406
break;
412407
}
413408
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)