public async void LoadDataFromSmartStore(bool includeEdit)
        {
            if (!_store.HasSoup(NotesSoup))
            {
                NotifyNotesSynced();
                return;
            }
            QuerySpec querySpec = QuerySpec.BuildAllQuerySpec(NotesSoup, NoteObject.TitleField,
                                                              QuerySpec.SqlOrder.ASC,
                                                              Limit);
            CoreDispatcher core    = CoreApplication.MainView.CoreWindow.Dispatcher;
            JArray         results = _store.Query(querySpec, 0);

            if (results == null)
            {
                NotifyNotesSynced();
                return;
            }
            NoteObject[] notes;

            if (includeEdit)
            {
                notes = (from note in results
                         let model = new NoteObject(note.Value <JObject>())
                                     select model).ToArray();
            }
            else
            {
                notes = (from note in results
                         let model = new NoteObject(note.Value <JObject>())
                                     where !model.ObjectId.Contains("local")
                                     orderby model.Title
                                     select model).ToArray();
            }
            string[] references =
                (from note in notes
                 let title = note.Title[0].ToString().ToLower()
                             group note by title
                             into g
                             orderby g.Key
                             select g.Key).ToArray();
            await core.RunAsync(CoreDispatcherPriority.Normal, () => IndexReference.Clear());

            await core.RunAsync(CoreDispatcherPriority.Normal, () => IndexReference.Add("all"));

            for (int i = 0, max = references.Length; i < max; i++)
            {
                int closure = i;
                await core.RunAsync(CoreDispatcherPriority.Normal, () => IndexReference.Add(references[closure]));
            }
            for (int i = 0, max = notes.Length; i < max; i++)
            {
                NoteObject t = notes[i];
                await UpdateNote(t);
            }
            NotifyNotesSynced();
            await core.RunAsync(CoreDispatcherPriority.Normal, () => MainPage.MainPageReference.UpdateTable());
        }
        private async Task <bool> UpdateNote(NoteObject obj)
        {
            if (obj == null || String.IsNullOrWhiteSpace(obj.ObjectId))
            {
                return(false);
            }
            CoreDispatcher core = CoreApplication.MainView.CoreWindow.Dispatcher;
            await core.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                Notes.Add(obj);
                OnPropertyChanged("Notes");
            });

            return(true);
        }
 public async void DeleteObject(NoteObject note)
 {
     if (note == null)
     {
         return;
     }
     try
     {
         var item = _store.Retrieve(NotesSoup,
                                    _store.LookupSoupEntryId(NotesSoup, Constants.Id, note.ObjectId))[0].ToObject <JObject>();
         item[SyncManager.Local]          = true;
         item[SyncManager.LocallyDeleted] = true;
         _store.Upsert(NotesSoup, item);
         note.Deleted = true;
         await UpdateNote(note);
     }
     catch (Exception)
     {
         Debug.WriteLine("Exception occurred while trying to delete");
     }
 }
        public async void SaveNote(NoteObject note, bool isCreated)
        {
            if (note == null)
            {
                return;
            }
            try
            {
                QuerySpec querySpec = QuerySpec.BuildExactQuerySpec(NotesSoup, Constants.Id, note.ObjectId, 1);
                JArray    returned  = _store.Query(querySpec, 0);
                JObject   item      = returned.Count > 0 ? returned[0].ToObject <JObject>() : new JObject();

                item[NoteObject.TitleField]      = note.Title;
                item[NoteObject.ContentField]    = Convert.ToBase64String(Encoding.UTF8.GetBytes(note.Content));
                item[SyncManager.Local]          = true;
                item[SyncManager.LocallyUpdated] = !isCreated;
                item[SyncManager.LocallyCreated] = isCreated;
                item[SyncManager.LocallyDeleted] = false;
                if (isCreated && item[Constants.Attributes.ToLower()] == null)
                {
                    item[Constants.Id] = "local_" + SmartStore.CurrentTimeMillis;
                    note.ObjectId      = item[Constants.Id].Value <string>();
                    var attributes = new JObject();
                    attributes[Constants.Type.ToLower()] = Constants.Contact;
                    item[Constants.Attributes.ToLower()] = attributes;
                    _store.Create(NotesSoup, item);
                }
                else
                {
                    _store.Upsert(NotesSoup, item);
                }
                note.UpdatedOrCreated = true;
                await UpdateNote(note);
            }
            catch (Exception)
            {
                Debug.WriteLine("Exception occurred while trying to save");
            }
        }