Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moves WalletProperties to mojo #2712

Merged
merged 2 commits into from Jun 16, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Prev

Fixup iOS target after moving WalletProperties and Grant to mojo

  • Loading branch information
kylehickinson authored and NejcZdovc committed Jun 15, 2019
commit f5a351a54afd88ab271fed3ee5c36a56bad110d8
@@ -209,9 +209,8 @@ - (void)onWalletInitialized:(ledger::Result)result
- (void)fetchWalletDetails:(void (^)(BATWalletProperties *))completion
{
ledger->FetchWalletProperties(^(ledger::Result result, ledger::WalletPropertiesPtr info) {
const auto walletInfo = *info.get();
[self onWalletProperties:result arg1:std::move(info)];
dispatch_async(dispatch_get_main_queue(), ^{
[self onWalletProperties:result arg1:std::make_unique<ledger::WalletProperties>(walletInfo)];
if (completion) {
completion(self.walletInfo);
}
@@ -222,9 +221,8 @@ - (void)fetchWalletDetails:(void (^)(BATWalletProperties *))completion
- (void)onWalletProperties:(ledger::Result)result arg1:(ledger::WalletPropertiesPtr)arg1
{
if (result == ledger::LEDGER_OK) {
const auto* walletInfo = arg1.get();
if (walletInfo != nullptr) {
self.walletInfo = [[BATWalletProperties alloc] initWithWalletInfo:*walletInfo];
if (arg1.get() != nullptr) {
self.walletInfo = [[BATWalletProperties alloc] initWithWalletPropertiesPtr:std::move(arg1)];
} else {
self.walletInfo = nil;
}
@@ -482,10 +480,10 @@ - (BOOL)isGrantUGP:(const ledger::Grant &)grant
return grant.type == "ugp";
}

- (NSString *)notificationIDForGrant:(const ledger::Grant &)grant
- (NSString *)notificationIDForGrant:(const ledger::GrantPtr)grant
{
const auto prefix = [self isGrantUGP:grant] ? @"rewards_grant_" : @"rewards_grant_ads_";
const auto promotionId = [NSString stringWithUTF8String:grant.promotion_id.c_str()];
const auto prefix = [self isGrantUGP:*grant] ? @"rewards_grant_" : @"rewards_grant_ads_";
const auto promotionId = [NSString stringWithUTF8String:grant->promotion_id.c_str()];
return [NSString stringWithFormat:@"%@%@", prefix, promotionId];
}

@@ -503,14 +501,14 @@ - (void)fetchAvailableGrantsForLanguage:(NSString *)language paymentId:(NSString
- (void)onGrant:(ledger::Result)result grant:(ledger::GrantPtr)grant
{
if (result == ledger::LEDGER_OK) {
[self.mPendingGrants addObject:[[BATGrant alloc] initWithGrant:grant]];
[self.mPendingGrants addObject:[[BATGrant alloc] initWithGrant:*grant]];

bool isUGP = [self isGrantUGP:grant];
bool isUGP = [self isGrantUGP:*grant];
auto notificationKind = isUGP ? BATRewardsNotificationKindGrant : BATRewardsNotificationKindGrantAds;

[self addNotificationOfKind:notificationKind
userInfo:nil
notificationID:[self notificationIDForGrant:grant]
notificationID:[self notificationIDForGrant:std::move(grant)]
onlyOnce:YES];
}
}
@@ -560,7 +558,7 @@ - (void)onGrantFinish:(ledger::Result)result grant:(ledger::GrantPtr)grant
grant->probi);
}

[self clearNotificationWithID:[self notificationIDForGrant:grant]];
[self clearNotificationWithID:[self notificationIDForGrant:std::move(grant)]];

// TODO:
// brave-core notifies that the balance report has been updated here.
@@ -64,5 +64,5 @@
@end

@interface BATWalletProperties (Private)
- (instancetype)initWithWalletInfo:(const ledger::WalletProperties&)obj;
- (instancetype)initWithWalletPropertiesPtr:(ledger::WalletPropertiesPtr)obj;
@end
@@ -101,7 +101,7 @@ NS_SWIFT_NAME(TwitchEventInfo)
@property (nonatomic) NSString * status;
@end

NS_SWIFT_NAME(WalletInfo)
NS_SWIFT_NAME(WalletProperties)
@interface BATWalletProperties : NSObject
@property (nonatomic) NSString * altcurrency;
@property (nonatomic) NSString * probi;
@@ -143,17 +143,19 @@ - (instancetype)initWithTwitchEventInfo:(const ledger::TwitchEventInfo&)obj {
@end

@implementation BATWalletProperties
- (instancetype)initWithWalletInfo:(const ledger::WalletProperties&)obj {
- (instancetype)initWithWalletPropertiesPtr:(ledger::WalletPropertiesPtr)obj {
if ((self = [super init])) {
self.altcurrency = [NSString stringWithUTF8String:obj.alt_currency.c_str()];
self.probi = [NSString stringWithUTF8String:obj.probi.c_str()];
self.balance = obj.balance;
self.feeAmount = obj.fee_amount;
self.rates = NSDictionaryFromMap(obj.rates);
self.parametersChoices = NSArrayFromVector(obj.parameters_choices);
self.parametersRange = NSArrayFromVector(obj.parameters_range);
self.parametersDays = obj.parameters_days;
self.grants = NSArrayFromVector(obj.grants, ^BATGrant *(const ledger::Grant& o){ return [[BATGrant alloc] initWithGrant: o]; });
self.altcurrency = [NSString stringWithUTF8String:obj->alt_currency.c_str()];
self.probi = [NSString stringWithUTF8String:obj->probi.c_str()];
self.balance = obj->balance;
self.feeAmount = obj->fee_amount;
self.rates = NSDictionaryFromMap(obj->rates);
self.parametersChoices = NSArrayFromVector(obj->parameters_choices);
self.parametersRange = NSArrayFromVector(obj->parameters_range);
self.parametersDays = obj->parameters_days;
self.grants = NSArrayFromVector(std::move(obj->grants), ^BATGrant *(const ledger::GrantPtr& o){
return [[BATGrant alloc] initWithGrant:*o];
});
}
return self;
}
@@ -7,6 +7,7 @@
#import <vector>
#import <string>
#import <objc/runtime.h>
#import "base/containers/flat_map.h"

static std::map<const char *, SEL> numberInitMap = {
{ @encode(bool), @selector(numberWithBool:) },
@@ -165,6 +166,20 @@ NS_INLINE NSDictionary<NSString *, NSNumber *> *NSDictionaryFromMap(std::map<std
return d;
}

/// Convert a String's to primitives mapping to an NSDictionary<NSString*, NSNumber *>
template <typename T>
NS_INLINE NSDictionary<NSString *, NSNumber *> *NSDictionaryFromMap(base::flat_map<std::string, T> m) {
const auto d = [NSMutableDictionary new];
if (m.empty()) {
return @{};
}
for (auto item : m) {
d[[NSString stringWithCString:item.first.c_str() encoding:NSUTF8StringEncoding]] =
NumberFromPrimitive(item.second);
}
return d;
}

/// Convert a String to String mapping to an NSDictionary
NS_INLINE NSDictionary<NSString *, NSString *> *NSDictionaryFromMap(std::map<std::string, std::string> m) {
const auto d = [NSMutableDictionary new];
@@ -178,6 +193,19 @@ NS_INLINE NSDictionary<NSString *, NSString *> *NSDictionaryFromMap(std::map<std
return d;
}

/// Convert a String to String mapping to an NSDictionary
NS_INLINE NSDictionary<NSString *, NSString *> *NSDictionaryFromMap(base::flat_map<std::string, std::string> m) {
const auto d = [NSMutableDictionary new];
if (m.empty()) {
return @{};
}
for (auto item : m) {
d[[NSString stringWithCString:item.first.c_str() encoding:NSUTF8StringEncoding]] =
[NSString stringWithCString:item.second.c_str() encoding:NSUTF8StringEncoding];
}
return d;
}

/// Convert a String to C++ object mapping to an NSDictionary of String to Obj-C objects
template <typename V, typename ObjCObj>
NS_INLINE NSDictionary<NSString *, ObjCObj> *NSDictionaryFromMap(std::map<std::string, V> m, ObjCObj(^transform)(V)) {
@@ -191,6 +219,19 @@ NS_INLINE NSDictionary<NSString *, ObjCObj> *NSDictionaryFromMap(std::map<std::s
return d;
}

/// Convert a String to C++ object mapping to an NSDictionary of String to Obj-C objects
template <typename V, typename ObjCObj>
NS_INLINE NSDictionary<NSString *, ObjCObj> *NSDictionaryFromMap(base::flat_map<std::string, V> m, ObjCObj(^transform)(V)) {
const auto d = [NSMutableDictionary new];
if (m.empty()) {
return @{};
}
for (auto item : m) {
d[[NSString stringWithCString:item.first.c_str() encoding:NSUTF8StringEncoding]] = transform(item.second);
}
return d;
}

/// Convert any mapping to an NSDictionary of Obj-C objects by transforming both the key and the value types to Obj-C
/// types
template <typename K, typename KObjC, typename V, typename VObjC>
@@ -204,3 +245,17 @@ NS_INLINE NSDictionary<KObjC, VObjC> *NSDictionaryFromMap(std::map<K, V> m, KObj
}
return d;
}

/// Convert any mapping to an NSDictionary of Obj-C objects by transforming both the key and the value types to Obj-C
/// types
template <typename K, typename KObjC, typename V, typename VObjC>
NS_INLINE NSDictionary<KObjC, VObjC> *NSDictionaryFromMap(base::flat_map<K, V> m, KObjC(^transformKey)(K), VObjC(^transformValue)(V)) {
const auto d = [NSMutableDictionary new];
if (m.empty()) {
return @{};
}
for (auto item : m) {
d[transformKey(item.first)] = transformValue(item.second);
}
return d;
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.