Skip to content

Commit

Permalink
Update sample Core Data project to include settings screen for toggli…
Browse files Browse the repository at this point in the history
…ng both the stemmingDisabled and supportStemmedPrefixSearches properties of CLuceneSearchService dynamically at runtime.
  • Loading branch information
msqr committed Apr 22, 2015
1 parent c116120 commit af01b31
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
C9049BBB17CAD3860099F480 /* StickyNoteViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = C9049BB917CAD3860099F480 /* StickyNoteViewController.xib */; };
C9049BC217CAE7700099F480 /* stop-words.txt in Resources */ = {isa = PBXBuildFile; fileRef = C9049BC017CAE7700099F480 /* stop-words.txt */; };
C9049BC517CB10800099F480 /* Notifications.m in Sources */ = {isa = PBXBuildFile; fileRef = C9049BC417CB10800099F480 /* Notifications.m */; };
C9B506F71AE72F1B00108562 /* SettingsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C9B506F51AE72F1B00108562 /* SettingsViewController.m */; };
C9B506F81AE72F1B00108562 /* SettingsViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = C9B506F61AE72F1B00108562 /* SettingsViewController.xib */; };
EED3B88A827168F2772DB75F /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A06DBFE5733C082A1D5847E4 /* libPods.a */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -91,6 +93,9 @@
C9049BC117CAE7700099F480 /* en */ = {isa = PBXFileReference; lastKnownFileType = text; name = en; path = "stop-words.txt"; sourceTree = "<group>"; };
C9049BC317CB10800099F480 /* Notifications.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Notifications.h; sourceTree = "<group>"; };
C9049BC417CB10800099F480 /* Notifications.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Notifications.m; sourceTree = "<group>"; };
C9B506F41AE72F1B00108562 /* SettingsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SettingsViewController.h; sourceTree = "<group>"; };
C9B506F51AE72F1B00108562 /* SettingsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SettingsViewController.m; sourceTree = "<group>"; };
C9B506F61AE72F1B00108562 /* SettingsViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = SettingsViewController.xib; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -164,6 +169,9 @@
C9049BA917CAC6BA0099F480 /* CoreDataManager.m */,
C9049BC317CB10800099F480 /* Notifications.h */,
C9049BC417CB10800099F480 /* Notifications.m */,
C9B506F41AE72F1B00108562 /* SettingsViewController.h */,
C9B506F51AE72F1B00108562 /* SettingsViewController.m */,
C9B506F61AE72F1B00108562 /* SettingsViewController.xib */,
C9049BB217CACD870099F480 /* StickyNoteListViewController.h */,
C9049BB317CACD870099F480 /* StickyNoteListViewController.m */,
C9049BB417CACD870099F480 /* StickyNoteListViewController.xib */,
Expand Down Expand Up @@ -298,6 +306,7 @@
C9049B2117CAC48E0099F480 /* InfoPlist.strings in Resources */,
C9049B2917CAC48F0099F480 /* Default.png in Resources */,
C9049B2B17CAC48F0099F480 /* Default@2x.png in Resources */,
C9B506F81AE72F1B00108562 /* SettingsViewController.xib in Resources */,
C9049B2D17CAC48F0099F480 /* Default-568h@2x.png in Resources */,
C9049BB617CACD870099F480 /* StickyNoteListViewController.xib in Resources */,
C9049BBB17CAD3860099F480 /* StickyNoteViewController.xib in Resources */,
Expand Down Expand Up @@ -348,6 +357,7 @@
C9049B2317CAC48E0099F480 /* main.m in Sources */,
C9049B2717CAC48E0099F480 /* AppDelegate.m in Sources */,
C9049BAA17CAC6BA0099F480 /* CoreDataManager.m in Sources */,
C9B506F71AE72F1B00108562 /* SettingsViewController.m in Sources */,
C9049BAD17CAC9CD0099F480 /* Model.xcdatamodeld in Sources */,
C9049BB017CACA5C0099F480 /* StickyNote.m in Sources */,
C9049BB517CACD870099F480 /* StickyNoteListViewController.m in Sources */,
Expand Down
21 changes: 21 additions & 0 deletions SampleCoreDataProject/SampleCoreDataProject/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import <BRFullTextSearch/BRFullTextSearch.h>
#import <BRFullTextSearch/CLuceneSearchService.h>
#import "CoreDataManager.h"
#import "SettingsViewController.h"
#import "StickyNoteListViewController.h"

@implementation AppDelegate {
Expand All @@ -31,7 +32,27 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
self.viewController.searchService = searchService;
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window makeKeyAndVisible];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(settingsChanged:) name:NSUserDefaultsDidChangeNotification object:nil];
return YES;
}

- (void)settingsChanged:(NSNotification *)notification {
NSUserDefaults *defaults = notification.object;
const BOOL stemmingDisabled = [defaults boolForKey:kStemmingDisabledKey];
const BOOL stemmingPrefixEnabled = [defaults boolForKey:kStemmingPrefixSupportEnabledKey];
BOOL reindex = NO;
if ( stemmingDisabled != searchService.stemmingDisabled ) {
searchService.stemmingDisabled = stemmingDisabled;
reindex = YES;
}
if ( stemmingPrefixEnabled != searchService.supportStemmedPrefixSearches ) {
searchService.supportStemmedPrefixSearches = stemmingPrefixEnabled;
reindex = YES;
}
if ( reindex ) {
[coreDataManager reindex];
}
}

@end
2 changes: 2 additions & 0 deletions SampleCoreDataProject/SampleCoreDataProject/CoreDataManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@

@property (nonatomic, strong) id<BRSearchService> searchService;

- (void)reindex;

@end
14 changes: 14 additions & 0 deletions SampleCoreDataProject/SampleCoreDataProject/CoreDataManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import <BRFullTextSearch/BRFullTextSearch.h>
#import <MagicalRecord/CoreData+MagicalRecord.h>
#import "Notifications.h"
#import "StickyNote.h"

@implementation CoreDataManager

Expand Down Expand Up @@ -77,4 +78,17 @@ - (void)maintainSearchIndexFromManagedObjectDidSave:(NSNotification *)notificati
}];
}

- (void)reindex {
[self.searchService bulkUpdateIndex:^(id<BRIndexUpdateContext> updateContext) {
NSLog(@"Reindexing...");
[NSManagedObjectContext MR_resetContextForCurrentThread]; // make sure we pull in latest data
for ( StickyNote *note in [StickyNote MR_findAll] ) {
[self.searchService addObjectToIndex:note context:updateContext];
}
} queue:dispatch_get_main_queue() finished:^(int updateCount, NSError *error) {
NSLog(@"Reindexing complete.");
[[NSNotificationCenter defaultCenter] postNotificationName:SearchIndexDidChange object:nil];
}];
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// SettingsViewController.h
// SampleCoreDataProject
//
// Created by Matt on 4/22/15.
// Copyright (c) 2015 Blue Rocket, Inc. All rights reserved.
//

#import <UIKit/UIKit.h>

extern NSString * const kStemmingDisabledKey;
extern NSString * const kStemmingPrefixSupportEnabledKey;

/**
Provide a UI for changing system settings. Add settings are stored in @c NSUserDefaults.
*/
@interface SettingsViewController : UIViewController

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// SettingsViewController.m
// SampleCoreDataProject
//
// Created by Matt on 4/22/15.
// Copyright (c) 2015 Blue Rocket, Inc. All rights reserved.
//

#import "SettingsViewController.h"

NSString * const kStemmingDisabledKey = @"StemmingDisabled";
NSString * const kStemmingPrefixSupportEnabledKey = @"StemmingPrefixSupportEnabled";

@interface SettingsViewController ()
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *topMarginConstraint;
@property (strong, nonatomic) IBOutlet UISwitch *stemmingSwitch;
@property (strong, nonatomic) IBOutlet UISwitch *stemmingPrefixSwitch;
@end

@implementation SettingsViewController

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self updateUIFromModel];
}

- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
self.topMarginConstraint.constant = (self.topLayoutGuide.length + 20);
}

- (void)updateUIFromModel {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.stemmingSwitch.on = ([defaults boolForKey:kStemmingDisabledKey] != YES);
self.stemmingPrefixSwitch.enabled = self.stemmingSwitch.on;
self.stemmingPrefixSwitch.on = [defaults boolForKey:kStemmingPrefixSupportEnabledKey];
}

- (IBAction)switchDidChange:(UISwitch *)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ( sender == self.stemmingSwitch ) {
[defaults setBool:!sender.on forKey:kStemmingDisabledKey];
[self updateUIFromModel];
} else if ( sender == self.stemmingPrefixSwitch ) {
[defaults setBool:sender.on forKey:kStemmingPrefixSupportEnabledKey];
}
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="7531" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7520"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="SettingsViewController">
<connections>
<outlet property="stemmingPrefixSwitch" destination="Edd-UJ-tLj" id="Uck-R4-iBx"/>
<outlet property="stemmingSwitch" destination="qgV-k5-ZgA" id="N1T-Qs-S6V"/>
<outlet property="topMarginConstraint" destination="dGm-3Q-FNx" id="jiO-C1-iBo"/>
<outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view clearsContextBeforeDrawing="NO" contentMode="scaleToFill" id="i5M-Pr-FkT">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" horizontalCompressionResistancePriority="250" text="Stemming enabled" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumScaleFactor="0.5" translatesAutoresizingMaskIntoConstraints="NO" id="JdM-cd-HDl">
<rect key="frame" x="18" y="34" width="507" height="20"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="qgV-k5-ZgA">
<rect key="frame" x="533" y="28" width="51" height="31"/>
<connections>
<action selector="switchDidChange:" destination="-1" eventType="valueChanged" id="zRT-ko-8Pa"/>
</connections>
</switch>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" horizontalCompressionResistancePriority="250" text="Stemming prefix query support" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumScaleFactor="0.5" translatesAutoresizingMaskIntoConstraints="NO" id="SLs-6L-KfE">
<rect key="frame" x="18" y="85" width="507" height="20"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Edd-UJ-tLj">
<rect key="frame" x="533" y="79" width="51" height="31"/>
<connections>
<action selector="switchDidChange:" destination="-1" eventType="valueChanged" id="pwt-r9-faY"/>
</connections>
</switch>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="qgV-k5-ZgA" firstAttribute="leading" secondItem="JdM-cd-HDl" secondAttribute="trailing" constant="8" symbolic="YES" id="0aP-5D-FOf"/>
<constraint firstItem="Edd-UJ-tLj" firstAttribute="top" secondItem="qgV-k5-ZgA" secondAttribute="bottom" constant="20" id="3Ev-bu-OoJ"/>
<constraint firstItem="JdM-cd-HDl" firstAttribute="leading" secondItem="SLs-6L-KfE" secondAttribute="leading" id="8Uq-Gf-MIn"/>
<constraint firstItem="qgV-k5-ZgA" firstAttribute="trailing" secondItem="Edd-UJ-tLj" secondAttribute="trailing" id="JIL-Jh-K4H"/>
<constraint firstAttribute="trailingMargin" secondItem="qgV-k5-ZgA" secondAttribute="trailing" constant="10" id="MSD-45-ugT"/>
<constraint firstItem="JdM-cd-HDl" firstAttribute="leading" secondItem="i5M-Pr-FkT" secondAttribute="leadingMargin" constant="10" id="bpM-Su-WWX"/>
<constraint firstItem="qgV-k5-ZgA" firstAttribute="top" secondItem="i5M-Pr-FkT" secondAttribute="topMargin" constant="20" id="dGm-3Q-FNx"/>
<constraint firstItem="Edd-UJ-tLj" firstAttribute="leading" secondItem="SLs-6L-KfE" secondAttribute="trailing" constant="8" symbolic="YES" id="j0S-J3-h7l"/>
<constraint firstItem="SLs-6L-KfE" firstAttribute="centerY" secondItem="Edd-UJ-tLj" secondAttribute="centerY" id="r8K-ED-Qlk"/>
<constraint firstItem="JdM-cd-HDl" firstAttribute="centerY" secondItem="qgV-k5-ZgA" secondAttribute="centerY" id="xED-z6-xfI"/>
</constraints>
</view>
</objects>
</document>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import <BRFullTextSearch/NSDate+BRFullTextSearchAdditions.h>
#import <MagicalRecord/CoreData+MagicalRecord.h>
#import "Notifications.h"
#import "SettingsViewController.h"
#import "StickyNote.h"
#import "StickyNoteViewController.h"

Expand All @@ -25,7 +26,9 @@ @implementation StickyNoteListViewController {
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ( (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) ) {
self.navigationItem.title = @"Sticky Notes";
self.navigationItem.leftBarButtonItem = self.editButtonItem;

UIBarButtonItem *settings = [[UIBarButtonItem alloc] initWithTitle:@"Setup" style:UIBarButtonItemStylePlain target:self action:@selector(viewSettings:)];
self.navigationItem.leftBarButtonItems = @[self.editButtonItem, settings];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addStickyNote:)];
Expand All @@ -45,6 +48,11 @@ - (void)viewWillAppear:(BOOL)animated {
}
}

- (IBAction)viewSettings:(id)sender {
SettingsViewController *dest = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
[self.navigationController pushViewController:dest animated:YES];
}

- (IBAction)addStickyNote:(id)sender {
StickyNoteViewController *editor = [[StickyNoteViewController alloc] initWithNibName:@"StickyNoteViewController" bundle:nil];
[self.navigationController pushViewController:editor animated:YES];
Expand Down

0 comments on commit af01b31

Please sign in to comment.