Created
July 28, 2015 11:04
-
-
Save RadwaEbrahim/6c6b7802b6a2aae7373c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Sticker.m | |
// edume | |
// | |
// Created by Radwa Muhammad on 1/8/15. | |
// Copyright (c) 2015 Radwa Muhammad. All rights reserved. | |
// | |
#import "Sticker.h" | |
@implementation Sticker | |
- (Sticker*)init | |
{ | |
self = [super init]; | |
if (self) { | |
self.isEnabled=NO; | |
self.pickedCount= 0; | |
} | |
return self; | |
} | |
- (Sticker*)initWithDictionary:(NSDictionary*)dictionary | |
{ | |
self = [super init]; | |
if (self) { | |
self.stickerID= [[dictionary objectForKey:@"StickerID"] integerValue]; | |
self.stickerImage =[dictionary objectForKey:@"StickerImage"]; | |
self.stickerImageNameInGame = [dictionary objectForKey:@"imageNameInGame"]; | |
self.stickerDescription= [dictionary objectForKey:@"StickerDescription"]; | |
self.pickedCount= 0; | |
} | |
return self; | |
} | |
- (id)initWithCoder:(NSCoder *)decoder { | |
self = [super init]; | |
if (!self) { | |
return nil; | |
} | |
self.stickerID = [decoder decodeIntegerForKey:@"StickerID"]; | |
self.pickedCount = [decoder decodeIntegerForKey:@"PickedCount"]; | |
return self; | |
} | |
- (void)encodeWithCoder:(NSCoder *)encoder { | |
[encoder encodeInteger:self.stickerID forKey:@"StickerID"]; | |
[encoder encodeInteger:self.pickedCount forKey:@"PickedCount"]; | |
} | |
-(void)fillSticker:(Sticker*)yoursSticker WithSticker:(Sticker*)templateSticker{ | |
yoursSticker.stickerImage = templateSticker.stickerImage; | |
yoursSticker.stickerDescription = templateSticker.stickerDescription; | |
} | |
-(BOOL)isEqual:(Sticker *)object{ | |
if ([object isKindOfClass:[Sticker class]]){ | |
if (self.stickerID == [(Sticker*)object stickerID]){ | |
return YES; | |
} | |
else return NO; | |
} | |
else return NO; | |
} | |
-(NSUInteger)hash{ | |
return self.stickerID; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// StickerStore.m | |
// Edume | |
// | |
// Created by Radwa Muhammad on 1/8/15. | |
// Copyright (c) 2015 Radwa Muhammad. All rights reserved. | |
// | |
#import "StickerStore.h" | |
#import "DataLoader.h" | |
#import "UserManager.h" | |
#define kStickersFileName @"Stickers.plist" | |
@implementation StickerStore | |
#pragma mark Singleton Methods | |
+ (StickerStore*)sharedManager { | |
static StickerStore *sharedMyManager = nil; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
sharedMyManager = [[self alloc] init]; | |
}); | |
return sharedMyManager; | |
} | |
- (id)init { | |
if (self = [super init]) { | |
self.allStickersArray=[[NSMutableArray alloc] init]; | |
[self.allStickersArray addObjectsFromArray:[DataLoader loadArrayFromPlist:kStickersFileName]] ; | |
[self initStickersArray]; | |
} | |
return self; | |
} | |
-(void)initStickersArray{ | |
NSUInteger count = self.allStickersArray.count; | |
for (int index=0; index < count; index++) { | |
NSDictionary* dic=self.allStickersArray[index]; | |
Sticker* sticker=[[ Sticker alloc] initWithDictionary:dic]; | |
[self.allStickersArray replaceObjectAtIndex:index withObject:sticker]; | |
} | |
} | |
#pragma mark - Stickers | |
- (NSMutableSet*)getRandomStickersWithCount:(int)count{ | |
return [self getRandomStickersWithCount:count forUser:[UserManager sharedManager].currentUser]; | |
} | |
- (NSMutableSet*)getRandomStickersWithCount:(int)count forUser:(User*)user{ | |
// NSMutableArray* newArray; | |
NSMutableSet *selectedSet = [[NSMutableSet alloc]init]; | |
if (count && user) { | |
NSMutableArray* remainingStickersArray= [NSMutableArray arrayWithArray: self.allStickersArray]; | |
if (self.allStickersArray.count> user.unlockedStickersArray.count){ | |
[remainingStickersArray removeObjectsInArray:user.unlockedStickersArray]; //get the unpicked ones | |
} | |
if (remainingStickersArray.count && count) { | |
NSInteger maxCount= remainingStickersArray.count > count ? count: remainingStickersArray.count ; | |
while (selectedSet.count < maxCount) { | |
int index = arc4random()%(remainingStickersArray.count); | |
Sticker* sticker= [remainingStickersArray objectAtIndex:index]; | |
[selectedSet addObject:sticker]; | |
} | |
} | |
} | |
return selectedSet; | |
} | |
-(NSMutableIndexSet*) indexSetFromArray:(NSArray*) array{ | |
NSMutableIndexSet* set=[NSMutableIndexSet indexSet]; | |
for (NSNumber* index in array) { //Hint : this works only on my case that this array holds nsnumber,will need modification for more genral usage | |
[set addIndex:index.integerValue]; | |
} | |
return set; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UserManager.m | |
// Edume | |
// | |
// Created by Radwa Muhammad on 1/11/15. | |
// Copyright (c) 2015 Radwa Muhammad. All rights reserved. | |
// | |
#import "UserManager.h" | |
#define kProfilesFileName @"Profiles" | |
#define kfilesDirectory @"EduFiles" | |
#define kPlistExtention @"plist" | |
#define kMaxUSerCount 3 | |
@implementation UserManager | |
@synthesize currentUser=_currentUser, registeredUsers=_registeredUsers; | |
#pragma mark Singleton Methods | |
+ (UserManager*)sharedManager { | |
static UserManager *sharedMyManager = nil; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
sharedMyManager = [[self alloc] init]; | |
}); | |
return sharedMyManager; | |
} | |
- (id)init { | |
if (self = [super init]) { | |
_registeredUsers=[[NSMutableArray alloc] init]; | |
} | |
return self; | |
} | |
#pragma mark - | |
-(void)setCurrentUser:(User *)user{ | |
//set it in user default for presistancy | |
[[NSUserDefaults standardUserDefaults] setValue:@(user.userID +1) forKey:kCurrentUserKey]; | |
//hold it in properity for easier use in the current open session | |
_currentUser=user; | |
} | |
-(User *)currentUser{ | |
if (!_currentUser) { | |
NSInteger currentUserID=[[[NSUserDefaults standardUserDefaults] valueForKey:kCurrentUserKey] integerValue]; | |
if (currentUserID && self.registeredUsers) { | |
User* tempUser=[User new]; | |
tempUser.userID=currentUserID-1; | |
NSInteger index=[self.registeredUsers indexOfObject:tempUser]; | |
_currentUser=[self.registeredUsers objectAtIndex:index]; | |
} | |
} | |
return _currentUser; | |
} | |
-(int)currentUserIndex{ | |
int currentUserID=0; | |
if (self.currentUser) { | |
currentUserID=[[[NSUserDefaults standardUserDefaults] valueForKey:kCurrentUserKey] intValue]-1; | |
} | |
return currentUserID; | |
} | |
-(BOOL) didReachMaxUsersCount{ | |
return self.registeredUsers.count < kMaxUSerCount ? NO: YES; | |
} | |
-(NSMutableArray*)registeredUsers{ | |
if (!_registeredUsers.count) { | |
NSString* filePath=[self usersFilePath]; | |
if (filePath) { //else then there is an error in the file itself | |
NSArray* array; | |
@try { | |
// array=[NSArray arrayWithContentsOfFile:filePath]; | |
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { | |
array=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];//[NSArray arrayWithContentsOfFile:filePath]; | |
} | |
} | |
@catch (NSException *exception) { | |
array=[NSArray array]; | |
} | |
@finally{ | |
if (array.count) { | |
[_registeredUsers setArray:array]; | |
} | |
} | |
} | |
} | |
return _registeredUsers; | |
} | |
-(BOOL)addUser:(User*)user{ | |
BOOL success=NO; | |
if (self.registeredUsers.count<kMaxUSerCount){ | |
user.userID = self.registeredUsers.count; | |
[_registeredUsers addObject:user]; | |
if (user.userID == 0) { | |
[self setCurrentUser:user]; | |
} | |
success= [self synchronize]; | |
} | |
return success; | |
} | |
-(BOOL)updateUser:(User*)user{ | |
BOOL success=NO; | |
NSLog(@"%@", user.userImage); | |
if (user && self.registeredUsers) { | |
NSInteger index=[_registeredUsers indexOfObject:user]; | |
if (index != NSNotFound){ | |
[_registeredUsers replaceObjectAtIndex:index withObject:user]; | |
}else{ | |
return [self addUser:user]; | |
} | |
success= [self synchronize]; | |
} | |
return success; | |
} | |
-(void)user:(User*)user unlockedSticker:(Sticker*)sticker{ | |
NSInteger index =[user.unlockedStickersArray indexOfObject:sticker]; | |
if ( index== NSNotFound) { | |
sticker.pickedCount++; | |
[user.unlockedStickersArray addObject:sticker]; | |
} | |
else { | |
Sticker* oldSticker =[user.unlockedStickersArray objectAtIndex:index]; | |
oldSticker.pickedCount++; | |
} | |
[self synchronize]; | |
} | |
-(void)userUnlockedSticker:(Sticker*)sticker{ | |
[self user:_currentUser unlockedSticker:sticker]; | |
} | |
- (BOOL) synchronize{ | |
NSString* filePath=[self usersFilePath]; | |
NSData * archivedProfiles = [NSKeyedArchiver archivedDataWithRootObject:_registeredUsers]; | |
NSError * error; | |
[archivedProfiles writeToFile:filePath options:NSDataWritingFileProtectionNone error:&error]; | |
if (error) { | |
// Error saving file, handle accordingly | |
NSLog(@"Error saving file %@",error ); | |
return NO; | |
} | |
else | |
return YES; | |
} | |
-(NSString*) usersFilePath{ | |
NSString* documentDirectory=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; | |
NSString *filesPath = [documentDirectory stringByAppendingPathComponent:kProfilesFileName]; | |
NSString *plistPath = [filesPath stringByAppendingPathExtension:kPlistExtention]; | |
return plistPath; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment