//
//  Database+Settings.m
//  DrivoMeter
//
//  Created by Triin Uudam on 5/3/12.
//  Copyright (c) 2012 Mobi Solutions OÜ. All rights reserved.
//

#import "Database+Settings.h"
#import "Setting.h"
#import "Constants.h"

@implementation Database (Settings)

- (Setting *)findOrCreateSettingWithTitle:(NSString *)title {
  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@", title];
  Setting *setting = [self findCoreObjectNamed:@"Setting" withPredicate:predicate];
  if (setting == nil) {
    MSLog(@"new %@ setting", title);
    setting = [Setting insertInManagedObjectContext:self.managedObjectContext];
    [setting setName:title];
    [setting setValueValue:YES];
  }
  [self saveContext];
  return setting;
}

- (void)updateSetting:(Setting *)setting withValue:(BOOL)newValue {
  [setting setValueValue:newValue];
  [self saveContext];
}
@end
