From f61fdc0abaf5481a709b235fddc4d46ecbe0b7dd Mon Sep 17 00:00:00 2001 From: Tom Hicks Date: Sat, 12 Sep 2020 00:55:49 -0700 Subject: [PATCH] Makes Monster a CoreData entity --- iOS/MonsterCards/Models/Monster.h | 14 +++--- iOS/MonsterCards/Models/Monster.m | 48 +++++++++++++++---- .../MonsterCards.xcdatamodel/contents | 13 ++++- .../Views/LibraryViewController.m | 12 +++-- iOS/MonsterCards/Views/SearchViewController.m | 9 +++- 5 files changed, 71 insertions(+), 25 deletions(-) diff --git a/iOS/MonsterCards/Models/Monster.h b/iOS/MonsterCards/Models/Monster.h index 0a945b8..290f5e2 100644 --- a/iOS/MonsterCards/Models/Monster.h +++ b/iOS/MonsterCards/Models/Monster.h @@ -7,6 +7,7 @@ // #import +#import #import "Ability.h" #import "Action.h" #import "DamageType.h" @@ -18,13 +19,8 @@ NS_ASSUME_NONNULL_BEGIN @class Skill; -@interface Monster : NSObject +@interface Monster : NSManagedObject -@property NSString *name; -@property NSString *size; -@property NSString *type; -@property NSString *tag; -@property NSString *alignment; @property NSString *armorName; @property NSString *otherArmorDescription; @property NSString *hpText; @@ -64,8 +60,8 @@ NS_ASSUME_NONNULL_BEGIN +(int)abilityModifierForScore: (int)score; +(int)hitDieForSize: (NSString*)size; --(id)initWithJSONString:(NSString*)jsonString; --(id)initWithJSONData:(NSData*)jsonData; +-(id)initWithJSONString:(NSString*)jsonString andContext:(NSManagedObjectContext*)context; +-(id)initWithJSONData:(NSData*)jsonData andContext:(NSManagedObjectContext*)context; -(id)initWithMonster:(Monster*)monster; -(NSString*)meta; -(int)abilityScoreForAbilityScoreName: (NSString*)abilityScoreName; @@ -130,3 +126,5 @@ NS_ASSUME_NONNULL_BEGIN @end NS_ASSUME_NONNULL_END + +#import "Monster+CoreDataProperties.h" diff --git a/iOS/MonsterCards/Models/Monster.m b/iOS/MonsterCards/Models/Monster.m index 12db4fc..d8bcf55 100644 --- a/iOS/MonsterCards/Models/Monster.m +++ b/iOS/MonsterCards/Models/Monster.m @@ -10,6 +10,38 @@ @implementation Monster +@synthesize armorName; +@synthesize blindsightDistance; +@synthesize burrowSpeed; +@synthesize challengeRating; +@synthesize charismaScore; +@synthesize climbSpeed; +@synthesize constitutionScore; +@synthesize customChallengeRating; +@synthesize customHP; +@synthesize customProficiencyBonus; +@synthesize customSpeed; +@synthesize customSpeedDescription; +@synthesize darkvisionDistance; +@synthesize dexterityScore; +@synthesize flySpeed; +@synthesize hitDice; +@synthesize hover; +@synthesize hpText; +@synthesize intelligenceScore; +@synthesize isBlind; +@synthesize naturalArmorBonus; +@synthesize otherArmorDescription; +@synthesize shieldBonus; +@synthesize speed; +@synthesize strengthScore; +@synthesize swimSpeed; +@synthesize telepathy; +@synthesize tremorsenseDistance; +@synthesize truesightDistance; +@synthesize understandsBut; +@synthesize wisdomScore; + +(int)abilityModifierForScore: (int)score { @throw [[NSException alloc] initWithName:@"unimplemented" reason:@"Method not implemented." userInfo:nil]; } @@ -26,14 +58,14 @@ return self; } --(id)initWithJSONString: (NSString*)jsonString { +-(id)initWithJSONString: (NSString*)jsonString andContext:(NSManagedObjectContext*)context { NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; - return [self initWithJSONData:jsonData]; + return [self initWithJSONData:jsonData andContext:context]; } --(id)initWithJSONData: (NSData*)jsonData { - self = [super init]; +-(id)initWithJSONData: (NSData*)jsonData andContext:(NSManagedObjectContext*)context { + self = [self initWithContext:context]; NSDictionary *jsonRoot = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil]; @@ -42,12 +74,8 @@ return self; } --(id)initWithMonster:(Monster*)monster { - if (monster == nil) { - return [self init]; - } - - self = [super init]; +-(id)initWithMonster:(Monster* _Nonnull)monster { + self = [self initWithContext:monster.managedObjectContext]; self.name = monster.name; diff --git a/iOS/MonsterCards/MonsterCards.xcdatamodeld/MonsterCards.xcdatamodel/contents b/iOS/MonsterCards/MonsterCards.xcdatamodeld/MonsterCards.xcdatamodel/contents index 5d6f17b..f130f0b 100644 --- a/iOS/MonsterCards/MonsterCards.xcdatamodeld/MonsterCards.xcdatamodel/contents +++ b/iOS/MonsterCards/MonsterCards.xcdatamodeld/MonsterCards.xcdatamodel/contents @@ -1,4 +1,13 @@ - - + + + + + + + + + + + \ No newline at end of file diff --git a/iOS/MonsterCards/Views/LibraryViewController.m b/iOS/MonsterCards/Views/LibraryViewController.m index c937145..9406d5f 100644 --- a/iOS/MonsterCards/Views/LibraryViewController.m +++ b/iOS/MonsterCards/Views/LibraryViewController.m @@ -9,6 +9,7 @@ #import "LibraryViewController.h" #import "Monster.h" #import "MonsterViewController.h" +#import "AppDelegate.h" @interface LibraryViewController () @@ -20,9 +21,12 @@ - (void)viewDidLoad { [super viewDidLoad]; + AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate; + NSManagedObjectContext *context = appDelegate.persistentContainer.viewContext; + // Temporary setup of allMonsters until we bind to CoreData. - Monster *pixie = [[Monster alloc] initWithJSONString:@"{\"name\":\"Pixie\"}"]; - Monster *acolyte = [[Monster alloc] initWithJSONString:@"{\"name\":\"Acolyte\"}"]; + Monster *pixie = [[Monster alloc] initWithJSONString:@"{\"name\":\"Pixie\"}" andContext:context]; + Monster *acolyte = [[Monster alloc] initWithJSONString:@"{\"name\":\"Acolyte\"}" andContext:context]; self.allMonsters = [NSArray arrayWithObjects:acolyte, pixie, nil]; } @@ -32,7 +36,9 @@ } - (IBAction)addNewMonster:(id)sender { - Monster *monster = [[Monster alloc] init]; + AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate; + NSManagedObjectContext *context = appDelegate.persistentContainer.viewContext; + Monster *monster = [[Monster alloc] initWithContext:context]; monster.name = @"Unnamed Monster"; self.allMonsters = [self.allMonsters arrayByAddingObject:monster]; [self.monstersTable reloadData]; diff --git a/iOS/MonsterCards/Views/SearchViewController.m b/iOS/MonsterCards/Views/SearchViewController.m index 5bcf8eb..9df021c 100644 --- a/iOS/MonsterCards/Views/SearchViewController.m +++ b/iOS/MonsterCards/Views/SearchViewController.m @@ -9,6 +9,7 @@ #import "SearchViewController.h" #import "MonsterViewController.h" #import "Monster.h" +#import "AppDelegate.h" @interface SearchViewController () @@ -21,8 +22,12 @@ - (void)viewDidLoad { [super viewDidLoad]; - Monster *pixie = [[Monster alloc] initWithJSONString:@"{\"name\":\"Pixie\"}"]; - Monster *acolyte = [[Monster alloc] initWithJSONString:@"{\"name\":\"Acolyte\"}"]; + AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate; + NSManagedObjectContext *context = appDelegate.persistentContainer.viewContext; + Monster *pixie = [[Monster alloc] initWithContext:context]; + pixie.name = @"Pixie"; + Monster *acolyte = [[Monster alloc] initWithEntity:[NSEntityDescription entityForName:@"Monster" inManagedObjectContext:context] insertIntoManagedObjectContext:nil]; + acolyte.name = @"Acolyte"; self.allMonsters = [NSArray arrayWithObjects:acolyte, pixie, nil]; self.foundMonsters= self.allMonsters; }