Skip to content

Commit

Permalink
categories in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisDemyanko committed May 8, 2019
1 parent 41e4cf8 commit 2818ad2
Show file tree
Hide file tree
Showing 76 changed files with 3,177 additions and 668 deletions.
290 changes: 250 additions & 40 deletions BeamWallet.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

34 changes: 12 additions & 22 deletions BeamWallet/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,18 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

private func tryLinkingBot(url:URL) {
if let params = url.queryParameters {
if let id = params["user_id"], let name = params["username"] {
TGBotManager.sharedManager.user.userId = id
TGBotManager.sharedManager.user.userName = name

if AppModel.sharedManager().isLoggedin {
TGBotManager.sharedManager.startLinking { (_ ) in

}
if TGBotManager.sharedManager.isValidUserFromUrl(url: url) {
if AppModel.sharedManager().isLoggedin {
TGBotManager.sharedManager.startLinking { (_ ) in

}
else{
if let vc = UIApplication.getTopMostViewController() {
vc.alert(title: "Telegram bot", message: "Please open wallet to link telegram bot") { (_ ) in
if let passVC = UIApplication.getTopMostViewController() as? EnterWalletPasswordViewController {
passVC.biometricAuthorization()
}
}
else{
if let vc = UIApplication.getTopMostViewController() {
vc.alert(title: "Telegram bot", message: "Please open wallet to link telegram bot") { (_ ) in

if let passVC = UIApplication.getTopMostViewController() as? EnterWalletPasswordViewController {
passVC.biometricAuthorization()
}
}
}
Expand All @@ -159,12 +154,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

if let url = userActivity.webpageURL {
if ((UIApplication.getTopMostViewController() as? EnterWalletPasswordViewController) != nil) {
if let params = url.queryParameters {
if let id = params["user_id"], let name = params["username"] {
TGBotManager.sharedManager.user.userId = id
TGBotManager.sharedManager.user.userName = name
}
}
_ = TGBotManager.sharedManager.isValidUserFromUrl(url: url)
}
else{
tryLinkingBot(url: url)
Expand Down
14 changes: 13 additions & 1 deletion BeamWallet/BeamSDK/AppModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#import "BMContact.h"
#import "BMPaymentProof.h"
#import "Settings.h"
#import "BMCategory.h"
#import "BMDuration.h"

@protocol WalletModelDelegate <NSObject>
@optional
Expand All @@ -42,6 +44,7 @@
-(void)onContactsChange:(NSArray<BMContact*>*_Nonnull)contacts;
-(void)onReceivePaymentProof:(BMPaymentProof*_Nonnull)proof;
-(void)onLocalNodeStarted;
-(void)onCategoriesChange;
@end

@interface AppModel : NSObject
Expand All @@ -63,6 +66,7 @@
@property (nonatomic,strong) NSMutableArray<BMUTXO*>*_Nullable utxos;
@property (nonatomic,strong) NSMutableArray<BMAddress*>*_Nullable walletAddresses;
@property (nonatomic,strong) NSMutableArray<BMContact*>*_Nonnull contacts;
@property (nonatomic,strong) NSMutableArray<BMCategory*>*_Nonnull categories;

+(AppModel*_Nonnull)sharedManager;

Expand Down Expand Up @@ -103,7 +107,6 @@
-(void)deleteAddress:(NSString*_Nullable)address;
-(BOOL)isValidAddress:(NSString*_Nullable)address;
-(BOOL)isExpiredAddress:(NSString*_Nullable)address;
-(BOOL)isAddressDeleted:(NSString*_Nullable)address;
-(BOOL)isMyAddress:(NSString*_Nullable)address;
-(void)clearAllAddresses;

Expand Down Expand Up @@ -137,4 +140,13 @@
-(BMContact*_Nullable)getContactFromId:(NSString*_Nonnull)idValue;
-(void)clearAllContacts;

//categories
-(void)deleteCategory:(BMCategory*_Nonnull)category;
-(void)editCategory:(BMCategory*_Nonnull)category;
-(void)addCategory:(BMCategory*_Nonnull)category;
-(BOOL)isNameAlreadyExist:(NSString*_Nonnull)name id:(int)ID;
-(BMCategory*_Nullable)findCategoryById:(NSString*_Nonnull)ID;
-(BMCategory*_Nullable)findCategoryByAddress:(NSString*_Nonnull)ID;
-(NSMutableArray<BMAddress*>*_Nonnull)getAddressFromCategory:(BMCategory*_Nonnull)category;

@end
167 changes: 152 additions & 15 deletions BeamWallet/BeamSDK/AppModel.mm
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
using namespace beam::io;

static int proofSize = 330;
static NSString *deletedAddressesKEY = @"deletedAddresses";
static NSString *categoriesKey = @"categoriesKey";

@implementation AppModel {
BOOL isStarted;
Expand Down Expand Up @@ -96,6 +96,8 @@ -(id)init{

_contacts = [[NSMutableArray alloc] init];

_categories = [[NSMutableArray alloc] initWithArray:[self allCategories]];

[self checkInternetConnection];

[[NSNotificationCenter defaultCenter] addObserver:self
Expand Down Expand Up @@ -652,17 +654,6 @@ -(void)deleteAddress:(NSString*_Nullable)address {
}
}

-(BOOL)isAddressDeleted:(NSString*_Nullable)address {
NSMutableArray *deletedAddresses = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults]objectForKey:deletedAddressesKEY]];

for (NSString *a in deletedAddresses) {
if ([a isEqualToString:address]){
return YES;
}
}

return NO;
}

-(void)generateNewWalletAddress {
wallet->getAsync()->generateNewAddress();
Expand Down Expand Up @@ -704,10 +695,27 @@ -(void)generateNewWalletAddress {
}

-(void)editAddress:(BMAddress*_Nonnull)address {

WalletID walletID(Zero);
if (walletID.FromHex(address.walletId.string))
{
std::vector<WalletAddress> addresses = walletDb->getAddresses(true);

for (int i=0; i<addresses.size(); i++)
{
NSString *wAddress = [NSString stringWithUTF8String:to_string(addresses[i].m_walletID).c_str()];

NSString *wCategory = [NSString stringWithUTF8String:addresses[i].m_category.c_str()];

if ([wAddress isEqualToString:address.walletId] && ![wCategory isEqualToString:address.category])
{
addresses[i].m_category = address.category.string;

wallet->getAsync()->saveAddress(addresses[i], true);

break;
}
}

if(address.isNowExpired) {
wallet->getAsync()->saveAddressChanges(walletID, address.label.string, false, false, true);
}
Expand All @@ -723,8 +731,8 @@ -(void)editAddress:(BMAddress*_Nonnull)address {
if (address.isExpired) {
wallet->getAsync()->saveAddressChanges(walletID, address.label.string, false, false, true);
}
else{
wallet->getAsync()->saveAddressChanges(walletID, address.label.string, (address.duration == 0 ? true : false), true, false);
else {
wallet->getAsync()->saveAddressChanges(walletID, address.label.string, (address.duration == 0 ? true : false), address.isChangedDate ? true : false, false);
}
}
}
Expand Down Expand Up @@ -1133,9 +1141,138 @@ -(BMContact*_Nullable)getContactFromId:(NSString*_Nonnull)idValue {
}

-(void)clearAllContacts{

for (BMContact *contact in _contacts) {
[self deleteAddress:contact.address.walletId];
}
}

#pragma mark - Categories

-(BMCategory*_Nullable)findCategoryById:(NSString*)ID {
if (ID.isEmpty) {
return nil;
}

for (int i=0; i<_categories.count; i++) {
if (_categories[i].ID == ID.intValue) {
return _categories[i];
}
}

return nil;
}

-(BMCategory*_Nullable)findCategoryByAddress:(NSString*_Nonnull)ID {
if (ID.isEmpty) {
return nil;
}

for (BMAddress *address in _walletAddresses) {
if ([address.walletId isEqualToString:ID]) {
return [self findCategoryById:address.category];
}
}

return nil;
}

-(NSUInteger)findCategoryIndex:(int)ID {
for (int i=0; i<_categories.count; i++) {
if (_categories[i].ID == ID) {
return i;
}
}

return 0;
}

-(NSMutableArray<BMCategory*>*_Nonnull)allCategories{
if ([[NSUserDefaults standardUserDefaults] objectForKey:categoriesKey]) {
NSData *data = [[NSUserDefaults standardUserDefaults] dataForKey:categoriesKey];

NSMutableArray *array = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:data]];

return array;
}

return @[].mutableCopy;
}

-(void)deleteCategory:(BMCategory*_Nonnull)category {
[_categories removeObjectAtIndex:[self findCategoryIndex:category.ID]];

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_categories];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:categoriesKey];
[[NSUserDefaults standardUserDefaults] synchronize];

for(id<WalletModelDelegate> delegate in [AppModel sharedManager].delegates)
{
if ([delegate respondsToSelector:@selector(onCategoriesChange)]) {
[delegate onCategoriesChange];
}
}
}

-(void)editCategory:(BMCategory*_Nonnull)category {
NSUInteger index = [self findCategoryIndex:category.ID];

[_categories replaceObjectAtIndex:index withObject:category];

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_categories];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:categoriesKey];
[[NSUserDefaults standardUserDefaults] synchronize];

for(id<WalletModelDelegate> delegate in [AppModel sharedManager].delegates)
{
if ([delegate respondsToSelector:@selector(onCategoriesChange)]) {
[delegate onCategoriesChange];
}
}
}

-(void)addCategory:(BMCategory*_Nonnull)category {
[_categories addObject:category];

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_categories];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:categoriesKey];
[[NSUserDefaults standardUserDefaults] synchronize];

for(id<WalletModelDelegate> delegate in [AppModel sharedManager].delegates)
{
if ([delegate respondsToSelector:@selector(onCategoriesChange)]) {
[delegate onCategoriesChange];
}
}
}

-(BOOL)isNameAlreadyExist:(NSString*_Nonnull)name id:(int)ID{
for(BMCategory *category in _categories) {
if (ID == 0) {
if ([category.name isEqualToString:name]) {
return YES;
}
}
else{
if ([category.name isEqualToString:name] && category.ID != ID) {
return YES;
}
}
}

return NO;
}

-(NSMutableArray<BMAddress*>*_Nonnull)getAddressFromCategory:(BMCategory*_Nonnull)category {
NSMutableArray *addresses = [NSMutableArray array];

for (BMAddress *address in _walletAddresses) {
if (address.category.intValue == category.ID) {
[addresses addObject:address];
}
}

return addresses;
}

@end
1 change: 1 addition & 0 deletions BeamWallet/BeamSDK/Objects/BMAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
@property (nonatomic,assign) BOOL isNowExpired;
@property (nonatomic,assign) BOOL isNowActive;
@property (nonatomic,assign) UInt64 isNowActiveDuration;
@property (nonatomic,assign) BOOL isChangedDate;

-(BOOL)isExpired;
-(UInt64)getExpirationTime;
Expand Down
29 changes: 29 additions & 0 deletions BeamWallet/BeamSDK/Objects/BMCategory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// BMCategory.h
// BeamWallet
//
// Copyright 2018 Beam Development
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import <Foundation/Foundation.h>


@interface BMCategory : NSObject

@property (nonatomic,strong) NSString * _Nonnull name;
@property (nonatomic,strong) NSString * _Nonnull color;
@property (nonatomic,assign) int ID;

@end
43 changes: 43 additions & 0 deletions BeamWallet/BeamSDK/Objects/BMCategory.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// BMCategory.m
// BeamWallet
//
// Copyright 2018 Beam Development
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import "BMCategory.h"

@implementation BMCategory

- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:[NSNumber numberWithInt:self.ID] forKey: @"ID"];
[encoder encodeObject:self.name forKey: @"name"];
[encoder encodeObject:self.color forKey: @"color"];
}

-(id)initWithCoder:(NSCoder *)decoder
{
self = [super init];
if(self)
{
self.ID = [[decoder decodeObjectForKey: @"ID"] intValue];
self.name = [decoder decodeObjectForKey: @"name"];
self.color = [decoder decodeObjectForKey:@"color"];
}
return self;
}

@end
Loading

0 comments on commit 2818ad2

Please sign in to comment.