Skip to content

Commit

Permalink
feat: Implement MmkvPlatformContext on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Mar 27, 2024
1 parent 87dedcd commit 660cda3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
5 changes: 0 additions & 5 deletions cpp/NativeMmkvModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@
#include "NativeMmkvModule.h"
#include "Logger.h"
#include "MmkvHostObject.h"

#if __has_include("MMKV.h")
#include "MMKV.h"
#else
#include <MMKVCore/MMKV.h>
#endif

namespace facebook::react {

Expand Down
2 changes: 2 additions & 0 deletions example/ios/.xcode.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export NODE_BINARY=/var/folders/q0/2f1y6j813zs38cn008hf8jcr0000gn/T/yarn--1711537443998-0.04007792277567268/node

2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions ios/MmkvPlatformContext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// RTNMmkvPlatformContext.h
// Pods
//
// Created by Marc Rousavy on 27.03.24.
//

#pragma once

#import <RNMmkvSpec/RNMmkvSpec.h>
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface MmkvPlatformContext : NSObject<NativeMmkvPlatformContextSpec>

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@
#import "MmkvPlatformContext.h"
#import <Foundation/Foundation.h>

std::string MmkvPlatformContext::getDefaultBasePath() {
@implementation MmkvPlatformContext

RCT_EXPORT_MODULE()

- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const facebook::react::ObjCTurboModule::InitParams &)params {
return std::make_shared<facebook::react::NativeMmkvPlatformContextSpecJSI>(params);
}

- (NSString *)getBaseDirectory {
#if TARGET_OS_TV
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
#else
Expand All @@ -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

0 comments on commit 660cda3

Please sign in to comment.