Created
December 28, 2013 09:38
-
-
Save iantearle/8157754 to your computer and use it in GitHub Desktop.
Alloy Tableview not popluating
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('tiparse'); | |
if(OS_ANDROID) { | |
var countly = require("ly.count"); | |
countly.countInit(); | |
} else if(OS_IOS) { | |
var RichText = require('com.obscure.RichText'), | |
uTils = require('uTils'), | |
countly = require('count.ly'); | |
countly.start(); | |
var richTextView = RichText.createRichTextView({ html: '<p>Test</p>' }); | |
$.test.add(richTextView); | |
} | |
var properties = Alloy.Collections.properties, | |
alreadyCalled = false, | |
isFavs = false, | |
limit = Alloy.Globals.settings.itemLimit, | |
distance = Alloy.Globals.settings.maxDistance, | |
searchIsOpen = false, | |
taffy = require('taffydb4ti').taffy, | |
ready = 0, | |
locationMessage, | |
theOrientation, | |
preMessage, | |
lastQuery; | |
if(Alloy.isTablet) { | |
var pin = Alloy.createWidget("be.k0suke.ssgizmo", "widget", { | |
icon: "location", | |
color: '#BF3A31', | |
font: {fontSize: 30}, | |
height: Ti.UI.SIZE, | |
width: Ti.UI.SIZE, | |
textAlign: 'center' | |
}).toImage(null, true); | |
var leftButtonImage = Alloy.createWidget("be.k0suke.ssgizmo", "widget", { | |
icon: "next", | |
color: '#BF3A31', | |
font: {fontSize: 30}, | |
height: Ti.UI.SIZE, | |
width: Ti.UI.SIZE, | |
textAlign: 'center' | |
}).toImage(null, true); | |
var leftButton = Ti.UI.createView({ | |
backgroundImage: leftButtonImage, | |
width: 30, | |
height: 38, | |
bottom: 0 | |
}); | |
// leftButton.add(leftButtonImage); | |
var annotationsArray = []; | |
} | |
function getPosition() { | |
Ti.Geolocation.purpose = 'get position'; | |
Ti.Geolocation.getCurrentPosition(function(e) { | |
if(e.success) { | |
var test = JSON.stringify(e); | |
Ti.API.info("Ti Results stringified" + test); | |
Ti.API.info(JSON.stringify(e)); | |
Alloy.Globals.position = { | |
latitude: e.coords.latitude, | |
longitude: e.coords.longitude | |
} | |
var posString = Alloy.Globals.position.latitude + ', '+ Alloy.Globals.position.longitude; | |
preMessage = String.format(L('location_msg_position'), posString); | |
propertiesFetchLocation(Alloy.Globals.position.latitude, Alloy.Globals.position.longitude); | |
ready += 1; | |
} else { | |
var a = Titanium.UI.createAlertDialog({title:'Heritage'}); | |
a.setMessage(L('location_error')); | |
} | |
}); | |
} | |
function propertiesFetchLocation(latitude, longitude) { | |
Ti.API.info('propertiesFetchLocation()'); | |
lastQuery = 'SELECT * FROM properties WHERE distance(latitude, longitude, '+ latitude +', '+ longitude +') < '+ distance +' ORDER BY distance(latitude, longitude, '+ latitude +', '+ longitude +') ASC LIMIT '+ limit; | |
properties.fetch({ | |
query: lastQuery, | |
add: false, | |
silent: true | |
}); | |
if(Alloy.isTablet) { | |
annotationsArray = []; | |
$.contentMapView.removeAllAnnotations(); | |
addPins(); | |
} | |
Ti.API.info('Length '+properties.models.length); | |
return properties.models.length; | |
} | |
function propertiesFetchTitle(title) { | |
lastQuery = 'SELECT * FROM properties WHERE REPLACE(UPPER(title), "/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g", "") LIKE "%' + title.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g, "") + '%" ORDER BY distance(latitude, longitude, '+ Alloy.Globals.position.latitude +', '+ Alloy.Globals.position.longitude +') ASC'; | |
properties.fetch({ | |
query: lastQuery, | |
add: false, | |
silent: true | |
}); | |
if(Alloy.isTablet) { | |
annotationsArray = []; | |
$.contentMapView.removeAllAnnotations(); | |
addPins(); | |
} | |
Ti.API.info('Length '+properties.models.length); | |
} | |
function propertiesFetchArray(array) { | |
lastQuery = 'SELECT * FROM properties WHERE id IN ('+array+') ORDER BY distance(latitude, longitude, '+ Alloy.Globals.position.latitude +', '+ Alloy.Globals.position.longitude +') ASC'; | |
properties.fetch({ | |
query: lastQuery, | |
add: false, | |
silent: true | |
}); | |
} | |
function myLoader(e) { | |
var thelength = properties.models.length; | |
if(thelength<limit) { | |
return; | |
} | |
properties.fetch({ | |
query: lastQuery + ' OFFSET '+ thelength, | |
add: true, | |
silent: true, | |
success: function (col) { | |
(col.models.length === thelength) ? e.done() : e.success(); | |
$.is.setOptions({ | |
msgDone: String.format(L('isDone'), col.models.length) | |
}); | |
}, | |
error: e.error | |
}); | |
if(Alloy.isTablet) { | |
addPins(); | |
} | |
} | |
function getCurrentPlaceResultsCallback(e){ | |
if(e.success){ | |
var test = JSON.stringify(e); | |
Ti.API.info("getCurrentPlaceResultsCallback() Results stringified" + test); | |
if(e.places[0].countryCode != 'GB') { | |
Ti.API.info("It worked"); | |
} | |
preMessage = String.format(L('location_msg_place'), e.places[0].addressDictionary.FormattedAddressLines.toString().replace(/,/g, ', ')); | |
ready += 1; | |
} else { | |
ready += 1; | |
} | |
} | |
function forwardGeoCallback(e) { | |
Alloy.Globals.loading.show('Finding...', true); | |
if(e.success) { | |
if(e.places[0].countryCode != 'GB') { | |
Ti.API.info("It worked"); | |
} | |
propertiesFetchLocation(e.places[0].latitude, e.places[0].longitude); | |
if(Alloy.isTablet) { | |
$.contentMapView.region = { | |
latitude: e.places[0].latitude, | |
longitude: e.places[0].longitude, | |
latitudeDelta: 0.04, | |
longitudeDelta: 0.04 | |
}; | |
} | |
Alloy.Globals.loading.hide(); | |
} | |
} | |
if(Alloy.isTablet) { function addPins() { | |
properties.forEach(function(model) { | |
if(!uTils.contains(annotationsArray, model.get('id'))) { | |
var pinid = model.get('id'); | |
$.contentMapView.addAnnotation( | |
Ti.Map.createAnnotation({ | |
latitude:model.get('latitude'), | |
longitude:model.get('longitude'), | |
title:model.get('title'), | |
subtitle:model.get('address'), | |
image: pin, | |
rightButton: Titanium.UI.iPhone.SystemButton.DISCLOSURE, | |
animate:false, | |
id: pinid | |
}) | |
); | |
annotationsArray.push(pinid); | |
} | |
}); | |
} } | |
function getFavs() { | |
if(!isFavs) { | |
if(Alloy.isTablet) { | |
$.favoriteIconNav.setProperties({ | |
icon: 'rows' | |
}); | |
} | |
var local_favourites = new taffy('favourites',{autocommit:true}); | |
local_favourites.save(); | |
propertiesFetchArray(local_favourites().select("pid")); | |
isFavs = true; | |
} else { | |
if(Alloy.isTablet) { | |
$.favoriteIconNav.setProperties({ | |
icon: 'favorite' | |
}); | |
} | |
getPosition(); | |
Alloy.Globals.geoCurrent.getCurrentPlace(getCurrentPlaceResultsCallback); | |
isFavs = false; | |
} | |
} | |
var animation1 = Titanium.UI.createAnimation({}); | |
var animation2 = Titanium.UI.createAnimation({}); | |
function doAnimateOpen() { | |
if(searchIsOpen === true) { | |
return; | |
} | |
$.searchField.visible = true; | |
$.searchField.focus(); | |
animation1 = Titanium.UI.createAnimation({ | |
left: 5, | |
duration:500, | |
curve: Ti.UI.ANIMATION_CURVE_EASE_IN_OUT, | |
}); | |
animation2 = Titanium.UI.createAnimation({ | |
left: 260, | |
duration:500, | |
curve: Ti.UI.ANIMATION_CURVE_EASE_IN_OUT, | |
}); | |
$.searchIcon.animate(animation1); | |
$.searchCancel.animate(animation2); | |
searchIsOpen = true; | |
} | |
function doAnimateClose() { | |
if(searchIsOpen === false) { | |
return; | |
} | |
$.searchField.visible = false; | |
animation1 = Titanium.UI.createAnimation({ | |
left: 'auto', | |
duration:500, | |
curve: Ti.UI.ANIMATION_CURVE_EASE_IN_OUT, | |
}); | |
animation2 = Titanium.UI.createAnimation({ | |
left: 320, | |
duration:500, | |
curve: Ti.UI.ANIMATION_CURVE_EASE_IN_OUT, | |
}); | |
$.searchIcon.animate(animation1); | |
$.searchCancel.animate(animation2); | |
searchIsOpen = false; | |
} | |
$.searchBar.addEventListener('singletap', doAnimateOpen); | |
$.searchField.addEventListener('blur', doAnimateClose); | |
$.searchCancel.addEventListener('singletap', function() { | |
$.searchField.blur(); | |
}); | |
$.searchBar.addEventListener('swipe', function() { | |
getPosition(); | |
$.searchField.blur(); | |
$.content.scrollToTop(0, { | |
animated: true | |
}); | |
}); | |
$.searchField.addEventListener('return', function(e) { | |
if(!e.value || e.value === '') { | |
return; | |
} | |
var postcode = new RegExp('^([A-PR-UWYZa-pr-uwyz]([0-9]{1,2}|([A-HK-Ya-hk-y][0-9]|[A-HK-Ya-hk-y][0-9]([0-9]|[ABEHMNPRV-Yabehmnprv-y]))|[0-9][A-HJKS-UWa-hjks-uw])\ {0,1}[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}|([Gg][Ii][Rr]\ 0[Aa][Aa])|([Ss][Aa][Nn]\ {0,1}[Tt][Aa]1)|([Bb][Ff][Pp][Oo]\ {0,1}([Cc]\/[Oo]\ )?[0-9]{1,4})|(([Aa][Ss][Cc][Nn]|[Bb][Bb][Nn][Dd]|[BFSbfs][Ii][Qq][Qq]|[Pp][Cc][Rr][Nn]|[Ss][Tt][Hh][Ll]|[Tt][Dd][Cc][Uu]|[Tt][Kk][Cc][Aa])\ {0,1}1[Zz][Zz]))$'); | |
var partial = new RegExp("^(GIR 0AA)|(((A[BL]|B[ABDHLNRSTX]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]?|F[KY]|G[LUY]?|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTY]?|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)[1-9]?[0-9]|((E|N|NW|SE|SW|W)1|EC[1-4]|WC[12])[A-HJKMNPR-Y]|(SW|W)([2-9]|[1-9][0-9])|EC[1-9][0-9])( [0-9][ABD-HJLNP-UW-Z]{2})?)$"); | |
var searchString = e.value.toUpperCase(); | |
var ownersArray = [ | |
{"name":L('category_eh').toUpperCase().replace(/^\s+/,''), "id":3}, | |
{"name":L('category_ind').toUpperCase().replace(/^\s+/,''), "id":4}, | |
{"name":L('category_cadw').toUpperCase().replace(/^\s+/,''), "id":5}, | |
{"name":L('category_hha').toUpperCase().replace(/^\s+/,''), "id":6}, | |
{"name":L('category_lt').toUpperCase().replace(/^\s+/,''), "id":7}, | |
{"name":L('category_nts').toUpperCase().replace(/^\s+/,''), "id":8}, | |
{"name":L('category_hs').toUpperCase().replace(/^\s+/,''), "id":9}, | |
{"name":L('category_cct').toUpperCase().replace(/^\s+/,''), "id":10}, | |
{"name":L('category_nt').toUpperCase().replace(/^\s+/,''), "id":11}, | |
{"name":L('category_twam').toUpperCase().replace(/^\s+/,''), "id":12}, | |
{"name":L('category_cg').toUpperCase().replace(/^\s+/,''), "id":13} | |
]; | |
var ownerResult = ownersArray.filter(function (person) { | |
return person.name == searchString; | |
}); | |
if(postcode.test(searchString)) { | |
Ti.API.info("Is it a postcode? "+postcode.test(searchString)); | |
Alloy.Globals.geoCoder.forwardGeocoder(searchString + ' , United Kingdom', forwardGeoCallback); | |
} else if(partial.test(searchString)) { | |
Ti.API.info("Is it a partial postcode? "+partial.test(searchString)); | |
Alloy.Globals.geoCoder.forwardGeocoder(searchString + ' , United Kingdom', forwardGeoCallback); | |
} else if(ownerResult.length > 0) { | |
annotationsArray = []; | |
$.contentMapView.removeAllAnnotations(); | |
lastQuery = 'SELECT * FROM properties WHERE cid = ' + ownerResult[0].id + ' LIMIT 20'; | |
properties.fetch({ | |
query: lastQuery, | |
add: false, | |
silent: true | |
}); | |
addPins(); | |
} else if(!postcode.test(searchString) || partial.test(searchString)) { | |
if(searchString.match(/^\s/)) { | |
var test = properties.fetch({ | |
query: 'SELECT * FROM properties WHERE REPLACE(UPPER(title), "/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g", "") LIKE "%' + searchString.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g, "") + '%"' | |
}); | |
if(test == '') { | |
Alloy.Globals.geoCoder.forwardGeocoder(e.value + ' , United Kingdom', forwardGeoCallback); | |
} else { | |
propertiesFetchTitle(searchString); | |
} | |
} else { | |
Alloy.Globals.geoCoder.forwardGeocoder(e.value + ' , United Kingdom', forwardGeoCallback); | |
} | |
} | |
doAnimateClose(); | |
}); | |
$.imageView.addEventListener("singletap", function() { | |
var settings = Alloy.createController('settings'); | |
settings.getView().open(); | |
}); | |
Ti.App.addEventListener('loadFavs', getFavs); | |
if(Alloy.isTablet) { | |
$.favoriteIconNav.addEventListener('click', function(e) { | |
getFavs(); | |
}); | |
$.contentMapView.addEventListener('click', function(e) { | |
// map event properties | |
var annotation = e.annotation.id; | |
var title = e.title; | |
var clickSource = e.clicksource; | |
// use custom event attribute to determine if atlanta annotation was clicked | |
if(e.clicksource == 'rightButton') { | |
var model = properties.get(annotation); | |
model = model.toJSON(); | |
var details = Alloy.createController('details'); | |
details.setDetails(model); | |
details.getView().open(); | |
} | |
}); | |
} | |
getPosition(); | |
Alloy.Globals.geoCurrent.getCurrentPlace(getCurrentPlaceResultsCallback); | |
var readyState = setInterval(function() { | |
if(ready === 2) { | |
var args = { | |
message: preMessage, | |
color: "#2c3e50" | |
}; | |
var locationMessage = Alloy.createController('message.row', args).getView(); | |
/* | |
$.content.insertRowBefore(0, locationMessage, { | |
animated:true | |
}); | |
*/ | |
setTimeout(function() { | |
$.content.deleteRow(locationMessage); | |
}, 8000); | |
clearInterval(readyState); | |
} | |
}, 500); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment