Skip to content

Instantly share code, notes, and snippets.

@hansemannn
Created September 7, 2016 19:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hansemannn/e59ed6709c3c51cd0124010de1ef6c14 to your computer and use it in GitHub Desktop.
Save hansemannn/e59ed6709c3c51cd0124010de1ef6c14 to your computer and use it in GitHub Desktop.
// Create a new window
var win = Ti.UI.createWindow({
backgroundColor: "#fff",
barColor: "#fff",
translucent: false,
title: "Spotlight Search Query"
});
// Create a new list to display the results
var list = Ti.UI.createListView({
allowsSelection: false,
defaultItemTemplate: Ti.UI.LIST_ITEM_TEMPLATE_SUBTITLE,
style: Ti.UI.iOS.ListViewStyle.GROUPED,
rowHeight: 60
});
// Create a new attribute set with all necessary information
var itemAttr = Ti.App.iOS.createSearchableItemAttributeSet({
itemContentType: Ti.App.iOS.UTTYPE_PLAIN_TEXT,
title: "Titanium Core Spotlight Tutorial"
});
itemAttr.contentDescription = "Tech Example \nOn: " + String.formatDate(new Date(), "short");
itemAttr.keywords = ["Mobile", "Appcelerator", "Titanium"];
itemAttr.displayName = "Hello world";
var item = Ti.App.iOS.createSearchableItem({
uniqueIdentifier: "titanium-tutorial",
domainIdentifier: "com.appcelerator",
attributeSet: itemAttr
});
// Add the item to the global Spotlight index
var indexer = Ti.App.iOS.createSearchableIndex();
indexer.addToDefaultSearchableIndex([item], function(e) {
if (e.success) {
Ti.API.info("Press the home button and now search for your keywords");
} else {
alert("Errored: " + JSON.stringify(e.error));
}
});
// Create a button to trigger a new formatted search
var btn = Ti.UI.createButton({
title: "Search"
});
btn.addEventListener("click", function() {
// An array of found Ti.App.iOS.SearchableItem's
var allItems = [];
// The search-query
var searchQuery = Ti.App.iOS.createSearchQuery({
queryString: 'title == "Titanium*"',
attributes: ["title", "displayName", "keywords", "contentType"]
});
// The event to be called when a new batch of items is found
searchQuery.addEventListener("founditems", function(e) {
for (var i = 0; i < e.items.length; i++) {
allItems.push(e.items[i]);
}
});
// The event to be called when the search-query completes
searchQuery.addEventListener("completed", function(e) {
if (!e.success) {
alert(e.error);
}
var cells = [];
// Loop through the results and access any properties you want
for (var i = 0; i < allItems.length; i++) {
var attributeSet = allItems[i].attributeSet
var foundTitle = attributeSet.title
var foundDisplayName = attributeSet.displayName
cells.push({
properties: {
title: foundTitle,
subtitle: attributeSet.keywords.join(", ")
}
});
}
list.setSections([Ti.UI.createListSection({
items: cells,
footerTitle: String(cells.length + " " + ((cells.length == 1) ? "result" : "results") + " found")
})]);
allItems = [];
});
// Start the search-query (or use searchQuery.cancel()) to abort it
searchQuery.start();
});
// Add the previously created UI-elements
win.setRightNavButton(btn);
win.add(list);
// Open the window
var nav = Ti.UI.iOS.createNavigationWindow({
window: win
});
nav.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment