WIP corespotlight

This commit is contained in:
John Rommel Estropia
2015-12-17 07:44:48 +09:00
parent 103c715f5a
commit 16daddbcc2
3 changed files with 274 additions and 82 deletions

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9531" systemVersion="15B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="Ni8-QF-XHB">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9531" systemVersion="15C50" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="Ni8-QF-XHB">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
@@ -16,6 +16,7 @@
<string>HelveticaNeue-Bold</string>
<string>HelveticaNeue</string>
<string>HelveticaNeue</string>
<string>HelveticaNeue</string>
</mutableArray>
<mutableArray key="HelveticaNeueLights.ttc">
<string>HelveticaNeue-Light</string>
@@ -1125,33 +1126,44 @@
<scene sceneID="2O0-uM-CKG">
<objects>
<tableViewController id="rvO-rT-QbP" customClass="SpotlightDemoViewController" customModule="CoreStoreDemo" customModuleProvider="target" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" id="TLr-Zy-NC6">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" allowsSelection="NO" showsSelectionImmediatelyOnTouchBegin="NO" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" id="TLr-Zy-NC6">
<rect key="frame" x="0.0" y="0.0" width="600" height="556"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<prototypes>
<tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="SpotlightItemCell" textLabel="Lxg-sl-77T" detailTextLabel="gkH-Q1-hTv" style="IBUITableViewCellStyleSubtitle" id="mDK-sM-Mz6">
<tableViewCell contentMode="scaleToFill" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" reuseIdentifier="PaletteTableViewCell" id="2KP-BI-oxb" customClass="PaletteTableViewCell" customModule="CoreStoreDemo" customModuleProvider="target">
<rect key="frame" x="0.0" y="86" width="600" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="mDK-sM-Mz6" id="pXG-mS-dT4">
<rect key="frame" x="0.0" y="0.0" width="600" height="43.5"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="2KP-BI-oxb" id="kPu-7L-49q">
<rect key="frame" x="0.0" y="0.0" width="567" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Title" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="Lxg-sl-77T">
<rect key="frame" x="15" y="6" width="31.5" height="19.5"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Detail" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="gkH-Q1-hTv">
<rect key="frame" x="15" y="25.5" width="30.5" height="13.5"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="11"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="hTX-Dn-pv2">
<rect key="frame" x="8" y="8" width="27" height="27"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="width" secondItem="hTX-Dn-pv2" secondAttribute="height" multiplier="1:1" id="073-rz-szO"/>
</constraints>
</view>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="O5e-aP-R0t">
<rect key="frame" x="45" y="8" width="34.5" height="27"/>
<fontDescription key="fontDescription" name="HelveticaNeue" family="Helvetica Neue" pointSize="14"/>
<color key="textColor" red="0.17254901959999999" green="0.24313725489999999" blue="0.31372549020000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstItem="O5e-aP-R0t" firstAttribute="height" secondItem="hTX-Dn-pv2" secondAttribute="height" id="4Lt-tT-SDF"/>
<constraint firstItem="hTX-Dn-pv2" firstAttribute="leading" secondItem="kPu-7L-49q" secondAttribute="leadingMargin" id="imZ-w9-r9g"/>
<constraint firstAttribute="bottomMargin" secondItem="hTX-Dn-pv2" secondAttribute="bottom" id="k3I-MU-Z4p"/>
<constraint firstItem="O5e-aP-R0t" firstAttribute="leading" secondItem="hTX-Dn-pv2" secondAttribute="trailing" constant="10" id="sNZ-Ym-Pwm"/>
<constraint firstAttribute="centerY" secondItem="O5e-aP-R0t" secondAttribute="centerY" id="wvz-LF-25u"/>
<constraint firstItem="hTX-Dn-pv2" firstAttribute="top" secondItem="kPu-7L-49q" secondAttribute="topMargin" id="xNz-bS-ajl"/>
</constraints>
</tableViewCellContentView>
<connections>
<outlet property="colorView" destination="hTX-Dn-pv2" id="AgP-lI-5ea"/>
<outlet property="label" destination="O5e-aP-R0t" id="IYz-nE-xVN"/>
</connections>
</tableViewCell>
</prototypes>
<connections>

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="7701" systemVersion="14D136" minimumToolsVersion="Automatic" macOSVersion="Automatic" iOSVersion="Automatic">
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="9525" systemVersion="15C50" minimumToolsVersion="Automatic">
<entity name="Palette" representedClassName="CoreStoreDemo.Palette">
<attribute name="brightness" optional="YES" attributeType="Float" defaultValueString="0.0" syncable="YES"/>
<attribute name="colorName" optional="YES" transient="YES" attributeType="String" syncable="YES"/>
@@ -26,6 +26,9 @@
<configuration name="ObservingDemo">
<memberEntity name="Palette"/>
</configuration>
<configuration name="SpotlightDemo">
<memberEntity name="Palette"/>
</configuration>
<configuration name="TransactionsDemo">
<memberEntity name="Place"/>
</configuration>

View File

@@ -7,105 +7,282 @@
//
import UIKit
import CoreStore
import CoreSpotlight
import MobileCoreServices
private enum Static {
static let palettes: ListMonitor<Palette> = {
try! CoreStore.addSQLiteStoreAndWait(
fileName: "SpotlightDemo.sqlite",
configuration: "SpotlightDemo",
resetStoreOnModelMismatch: true
)
return CoreStore.monitorSectionedList(
From(Palette),
SectionBy("colorName"),
OrderBy(.Ascending("hue"))
)
}()
}
// MARK: - SpotlightDemoViewController
class SpotlightDemoViewController: UITableViewController {
private var dataSource = [(String, String)]()
class SpotlightDemoViewController: UITableViewController, ListSectionObserver {
// MARK: NSObject
deinit {
Static.palettes.removeObserver(self)
}
// MARK: UIViewController
override func viewDidLoad() {
super.viewDidLoad()
let navigationItem = self.navigationItem
navigationItem.leftBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .Trash,
target: self,
action: "resetBarButtonItemTouched:"
)
navigationItem.rightBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .Add,
target: self,
action: "addBarButtonItemTouched:"
)
Static.palettes.addObserver(self)
}
// MARK: UITableViewDataSource
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
return Static.palettes.numberOfSections()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataSource.count
return Static.palettes.numberOfObjectsInSection(section)
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("SpotlightItemCell")!
let item = self.dataSource[indexPath.row]
cell.textLabel?.text = item.0
cell.detailTextLabel?.text = item.1
let cell = tableView.dequeueReusableCellWithIdentifier("PaletteTableViewCell") as! PaletteTableViewCell
let palette = Static.palettes[indexPath]
cell.colorView?.backgroundColor = palette.color
cell.label?.text = palette.colorText
return cell
}
// MARK: UITableViewDelegate
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
guard case .Delete = editingStyle else {
switch editingStyle {
return
}
let identifier = self.dataSource[indexPath.row].0
if #available(iOS 9.0, *) {
CSSearchableIndex.defaultSearchableIndex().deleteSearchableItemsWithIdentifiers(
[identifier],
completionHandler: { error in
case .Delete:
let palette = Static.palettes[indexPath]
CoreStore.beginAsynchronous{ (transaction) -> Void in
// ...
}
)
transaction.delete(palette)
transaction.commit { (result) -> Void in }
}
default:
break
}
tableView.beginUpdates()
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
self.dataSource.removeAtIndex(indexPath.row)
tableView.endUpdates()
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return Static.palettes.sectionInfoAtIndex(section).name
}
// MARK: ListObserver
func listMonitorWillChange(monitor: ListMonitor<Palette>) {
self.tableView.beginUpdates()
}
func listMonitorDidChange(monitor: ListMonitor<Palette>) {
self.tableView.endUpdates()
}
// MARK: ListObjectObserver
func listMonitor(monitor: ListMonitor<Palette>, didInsertObject object: Palette, toIndexPath indexPath: NSIndexPath) {
self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
func listMonitor(monitor: ListMonitor<Palette>, didDeleteObject object: Palette, fromIndexPath indexPath: NSIndexPath) {
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
func listMonitor(monitor: ListMonitor<Palette>, didUpdateObject object: Palette, atIndexPath indexPath: NSIndexPath) {
if let cell = self.tableView.cellForRowAtIndexPath(indexPath) as? PaletteTableViewCell {
let palette = Static.palettes[indexPath]
cell.colorView?.backgroundColor = palette.color
cell.label?.text = palette.colorText
}
}
func listMonitor(monitor: ListMonitor<Palette>, didMoveObject object: Palette, fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
self.tableView.deleteRowsAtIndexPaths([fromIndexPath], withRowAnimation: .Automatic)
self.tableView.insertRowsAtIndexPaths([toIndexPath], withRowAnimation: .Automatic)
}
// MARK: ListSectionObserver
func listMonitor(monitor: ListMonitor<Palette>, didInsertSection sectionInfo: NSFetchedResultsSectionInfo, toSectionIndex sectionIndex: Int) {
self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Automatic)
}
func listMonitor(monitor: ListMonitor<Palette>, didDeleteSection sectionInfo: NSFetchedResultsSectionInfo, fromSectionIndex sectionIndex: Int) {
self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Automatic)
}
// MARK: Private
@IBAction private dynamic func addBarButtonTapped(sender: UIBarButtonItem) {
@IBAction private dynamic func resetBarButtonItemTouched(sender: AnyObject?) {
let items = [
("John", "iOS team"),
("Bob", "Android team"),
("Joe", "Infra team"),
("Ryan", "Director"),
("Jake", "Design team"),
("Mark", "Testing team")
]
guard let nextItem = items.filter({ !self.dataSource.map({ $0.0 }).contains($0.0) }).first else {
CoreStore.beginAsynchronous { (transaction) -> Void in
return
transaction.deleteAll(From(Palette))
transaction.commit()
}
if #available(iOS 9.0, *) {
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeJSON as String)
attributeSet.title = nextItem.0
attributeSet.contentDescription = nextItem.1
let item = CSSearchableItem(
uniqueIdentifier: nextItem.0,
domainIdentifier: "jp.eureka.sample",
attributeSet: attributeSet
)
CSSearchableIndex.defaultSearchableIndex().indexSearchableItems(
[item],
completionHandler: { (error) -> Void in
//...
}
)
}
let tableView = self.tableView
tableView.beginUpdates()
tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: self.dataSource.count, inSection: 0)], withRowAnimation: .Automatic)
self.dataSource.append(nextItem)
tableView.endUpdates()
}
@IBAction private dynamic func addBarButtonItemTouched(sender: AnyObject?) {
CoreStore.beginAsynchronous { (transaction) -> Void in
let palette = transaction.create(Into(Palette))
palette.setInitialValues()
transaction.commit()
}
}
// private var dataSource = [(String, String)]()
//
// // MARK: UITableViewDataSource
//
// override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
//
// return 1
// }
//
// override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//
// return self.dataSource.count
// }
//
// override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//
// let cell = tableView.dequeueReusableCellWithIdentifier("SpotlightItemCell")!
// let item = self.dataSource[indexPath.row]
// cell.textLabel?.text = item.0
// cell.detailTextLabel?.text = item.1
// return cell
// }
//
// // MARK: UITableViewDelegate
//
// override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
//
// guard case .Delete = editingStyle else {
//
// return
// }
//
// let identifier = self.dataSource[indexPath.row].0
// if #available(iOS 9.0, *) {
//
// CSSearchableIndex.defaultSearchableIndex().deleteSearchableItemsWithIdentifiers(
// [identifier],
// completionHandler: { error in
//
// // ...
// }
// )
// }
//
// tableView.beginUpdates()
// tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
// self.dataSource.removeAtIndex(indexPath.row)
// tableView.endUpdates()
// }
//
// // MARK: Private
//
// @IBAction private dynamic func addBarButtonTapped(sender: UIBarButtonItem) {
//
// let items = [
// ("John", "iOS team"),
// ("Bob", "Android team"),
// ("Joe", "Infra team"),
// ("Ryan", "Director"),
// ("Jake", "Design team"),
// ("Mark", "Testing team")
// ]
// guard let nextItem = items.filter({ !self.dataSource.map({ $0.0 }).contains($0.0) }).first else {
//
// return
// }
//
// if #available(iOS 9.0, *) {
//
// let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeJSON as String)
// attributeSet.title = nextItem.0
// attributeSet.contentDescription = nextItem.1
//
// let item = CSSearchableItem(
// uniqueIdentifier: nextItem.0,
// domainIdentifier: "jp.eureka.sample",
// attributeSet: attributeSet
// )
// CSSearchableIndex.defaultSearchableIndex().indexSearchableItems(
// [item],
// completionHandler: { (error) -> Void in
//
// //...
// }
// )
// }
//
// let tableView = self.tableView
// tableView.beginUpdates()
// tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: self.dataSource.count, inSection: 0)], withRowAnimation: .Automatic)
// self.dataSource.append(nextItem)
// tableView.endUpdates()
// }
}