From 660cda3bd154ab412cefd8fa9945dca5e39bec35 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Wed, 27 Mar 2024 12:08:36 +0100 Subject: [PATCH] feat: Implement MmkvPlatformContext on iOS --- cpp/NativeMmkvModule.cpp | 5 ----- example/ios/.xcode.env.local | 2 ++ example/ios/Podfile.lock | 2 +- ios/MmkvPlatformContext.h | 19 +++++++++++++++++++ ...ontext.mm => MmkvPlatformContextModule.mm} | 17 +++++++++++++---- 5 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 example/ios/.xcode.env.local create mode 100644 ios/MmkvPlatformContext.h rename ios/{AppleMmkvPlatformContext.mm => MmkvPlatformContextModule.mm} (55%) diff --git a/cpp/NativeMmkvModule.cpp b/cpp/NativeMmkvModule.cpp index 662c7115..f10a680b 100644 --- a/cpp/NativeMmkvModule.cpp +++ b/cpp/NativeMmkvModule.cpp @@ -8,12 +8,7 @@ #include "NativeMmkvModule.h" #include "Logger.h" #include "MmkvHostObject.h" - -#if __has_include("MMKV.h") #include "MMKV.h" -#else -#include -#endif namespace facebook::react { diff --git a/example/ios/.xcode.env.local b/example/ios/.xcode.env.local new file mode 100644 index 00000000..792172c7 --- /dev/null +++ b/example/ios/.xcode.env.local @@ -0,0 +1,2 @@ +export NODE_BINARY=/var/folders/q0/2f1y6j813zs38cn008hf8jcr0000gn/T/yarn--1711537443998-0.04007792277567268/node + diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 2f7224f7..b401522b 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -1394,7 +1394,7 @@ SPEC CHECKSUMS: React-jsitracing: 29548fd6ba558ab2441f89c87a5b2820da6ef276 React-logger: 4092c2c3bc5feb8ccce0fbabda8f77d03dfba47a React-Mapbuffer: 14b88e15d38a2c448ab94ad4ec3d00583b7722cd - react-native-mmkv: b58c81977459eb559e0e4a5a8440ac850439b64f + react-native-mmkv: d259daef87054a7bea4cd5c852d822989434f92f React-nativeconfig: 83f91b53a8d6a6ecf16a8acd845c99570db7a548 React-NativeModulesApple: 59ed2248373b3d36badb153f34bb48a6b9ee4865 React-perflogger: 12c33674b7ad945eee0a2bdb982383a1bb81c75e diff --git a/ios/MmkvPlatformContext.h b/ios/MmkvPlatformContext.h new file mode 100644 index 00000000..26912130 --- /dev/null +++ b/ios/MmkvPlatformContext.h @@ -0,0 +1,19 @@ +// +// RTNMmkvPlatformContext.h +// Pods +// +// Created by Marc Rousavy on 27.03.24. +// + +#pragma once + +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface MmkvPlatformContext : NSObject + +@end + +NS_ASSUME_NONNULL_END diff --git a/ios/AppleMmkvPlatformContext.mm b/ios/MmkvPlatformContextModule.mm similarity index 55% rename from ios/AppleMmkvPlatformContext.mm rename to ios/MmkvPlatformContextModule.mm index 4736efff..787393d6 100644 --- a/ios/AppleMmkvPlatformContext.mm +++ b/ios/MmkvPlatformContextModule.mm @@ -8,7 +8,15 @@ #import "MmkvPlatformContext.h" #import -std::string MmkvPlatformContext::getDefaultBasePath() { +@implementation MmkvPlatformContext + +RCT_EXPORT_MODULE() + +- (std::shared_ptr)getTurboModule:(const facebook::react::ObjCTurboModule::InitParams &)params { + return std::make_shared(params); +} + +- (NSString *)getBaseDirectory { #if TARGET_OS_TV NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); #else @@ -17,9 +25,10 @@ NSString* documentPath = (NSString*)[paths firstObject]; if ([documentPath length] > 0) { NSString* basePath = [documentPath stringByAppendingPathComponent:@"mmkv"]; - std::string string = [basePath UTF8String]; - return string; + return basePath; } else { - return ""; + @throw [NSException exceptionWithName:@"BasePathNotFound" reason:@"Cannot find base-path to store MMKV files!" userInfo:nil]; } } + +@end