Ejemplo n.º 1
0
        private static bool CreateNote(Note note, bool reflectOnRoaming = true)
        {
            if (note == null || note.IsEmpty())
            {
                return(false);
            }

            Debug.WriteLine("Create note: " + note.Title);
            //App.TelemetryClient.TrackEvent("NoteCreated");

            //associate with note
            foreach (var item in note.Checklist)
            {
                item.NoteId = note.ID;
            }
            foreach (var item in note.Images)
            {
                item.NoteId = note.ID;
            }
            note.Reminder.NoteId = note.ID;

            if (note.CreatedAt == null)
            {
                note.TouchCreatedAt();
            }
            if (note.UpdatedAt == null)
            {
                note.Touch();
            }

            note.Changed = false;
            note.Order   = Notes.Count;
            AppData.Notes.Insert(0, note);

            NotificationsManager.TryCreateNoteReminder(note, note.Reminder.Date);

            LocalDB.InsertWithChildren(note);
            if (reflectOnRoaming)
            {
                RoamingDB.InsertWithChildren(note);
            }

            var handler = NoteCreated;

            if (handler != null)
            {
                handler(null, new NoteEventArgs(note));
            }

            var handler2 = NotesSaved;

            if (handler2 != null)
            {
                handler2(null, EventArgs.Empty);
            }

            return(true);
        }
Ejemplo n.º 2
0
        public static bool ArchiveNote(Note note, bool reflectOnRoaming = true)
        {
            //get original reference
            if (notes != null)
            {
                note = Notes.FirstOrDefault(x => x.ID == note.ID);
            }
            if (note == null)
            {
                return(false);
            }

            Debug.WriteLine("Archive note: " + note.Title);
            //App.TelemetryClient.TrackEvent("NoteArchived");

            LoadArchivedNotesIfNecessary();
            note.IsArchived = true;
            note.TouchArchivedAt();

            RemoveNoteReminders(note);
            NotificationsManager.RemoveTileIfExists(note.ID);

            bool success = LocalDB.Update(note) == 1;

            if (!success)
            {
                return(false);
            }

            if (reflectOnRoaming)
            {
                RoamingDB.Update(note);
            }

            AppData.Notes.Remove(note);
            AppData.ArchivedNotes.Insert(0, note);
            Debug.WriteLine("AppData.ArchivedNotes.Insert(0, note);");

            var handler = NoteArchived;

            if (handler != null)
            {
                handler(null, new NoteEventArgs(note));
            }

            var handler2 = NotesSaved;

            if (handler2 != null)
            {
                handler2(null, EventArgs.Empty);
            }

            return(true);
        }
Ejemplo n.º 3
0
        public static void RemoveNoteReminders(Note note, bool reflectOnRoaming = true)
        {
            if (note == null || note.Reminder == null)
            {
                return;
            }

            LocalDB.Delete(note.Reminder);
            if (reflectOnRoaming)
            {
                RoamingDB.Delete(note.Reminder);
            }

            NotificationsManager.RemoveScheduledToastsIfExists(note);
            note.Reminder = new Reminder();
        }
Ejemplo n.º 4
0
        private static async void MergeRoamingWithLocalDB()
        {
            var allLocalNotes   = LocalDB.GetAllWithChildren <Note>();
            var allRoamingNotes = RoamingDB.GetAllWithChildren <Note>();

            foreach (var roamingNote in allRoamingNotes)
            {
                var localNote = LocalDB.Find <Note>(roamingNote.ID);

                //create local note
                if (localNote == null)
                {
                    CreateNote(roamingNote, false);
                    Debug.WriteLine("Roaming: Note created");
                }

                //archive local note
                else if (roamingNote.IsArchived && !localNote.IsArchived && roamingNote.ArchivedAt > localNote.UpdatedAt)
                {
                    ArchiveNote(localNote, false);
                    UpdateNote(roamingNote, false);
                    localNote.ReplaceWith(roamingNote);
                    Debug.WriteLine("Roaming: Note archived");
                }

                //delete local note
                else if (roamingNote.DeletedAt != localNote.DeletedAt && roamingNote.DeletedAt > localNote.UpdatedAt)
                {
                    await RemoveNote(localNote, false);

                    UpdateNote(roamingNote, false);
                    localNote.ReplaceWith(roamingNote);
                    Debug.WriteLine("Roaming: Note deleted");
                }

                //update local note
                else if (roamingNote.UpdatedAt > localNote.UpdatedAt)
                {
                    UpdateNote(roamingNote, false);
                    localNote.ReplaceWith(roamingNote);
                    Debug.WriteLine("Roaming: Note updated");
                }
            }
        }
Ejemplo n.º 5
0
        public static async Task <bool> RemoveNoteImage(NoteImage noteImage, bool deleteFromDB = true)
        {
            if (noteImage == null)
            {
                return(true);
            }
            bool success = true;

            try
            {
                Debug.WriteLine("Delete " + noteImage.URL);
                var file = await StorageFile.GetFileFromPathAsync(noteImage.URL);

                await file.DeleteAsync();

                if (deleteFromDB)
                {
                    LocalDB.Delete(noteImage);
                    RoamingDB.Delete(noteImage);
                }
            }
            catch (Exception)
            {
                success = false;
            }

            if (deleteFromDB)
            {
                var handler2 = NotesSaved;
                if (handler2 != null)
                {
                    handler2(null, EventArgs.Empty);
                }
            }

            return(success);
        }
Ejemplo n.º 6
0
        public static bool ChangeNoteColor(Note note, NoteColor newColor)
        {
            if (note == null)
            {
                return(false);
            }

            Debug.WriteLine("Change note color: " + newColor.Key);
            //App.TelemetryClient.TrackEvent("NoteColorChanged");

            note.Color = newColor;

            bool success = LocalDB.Update(note) == 1;

            if (!success)
            {
                return(false);
            }
            RoamingDB.Update(note);

            var handler = NoteColorChanged;

            if (handler != null)
            {
                handler(null, new NoteColorEventArgs(note, newColor));
            }

            var handler2 = NotesSaved;

            if (handler2 != null)
            {
                handler2(null, EventArgs.Empty);
            }

            return(true);
        }
Ejemplo n.º 7
0
        public static async Task <bool> RemoveNote(Note note, bool reflectOnRoaming = true)
        {
            if (note == null)
            {
                return(false);
            }

            Note noteFound = null;

            //get original reference
            if (notes != null)
            {
                try
                {
                    if (note.IsArchived)
                    {
                        LoadArchivedNotesIfNecessary();
                        noteFound = ArchivedNotes.FirstOrDefault(x => x.ID == note.ID);
                        if (ArchivedNotes.IndexOf(noteFound) < 0)
                        {
                            noteFound = null;
                        }
                    }
                    else
                    {
                        noteFound = Notes.FirstOrDefault(x => x.ID == note.ID);
                        if (Notes.IndexOf(note) < 0)
                        {
                            noteFound = null;
                        }
                    }
                }
                catch (Exception)
                {
                    Debug.WriteLine("Failed removing note.");
                    return(false);
                }
            }

            Debug.WriteLine("Remove note: " + note.Title);
            //App.TelemetryClient.TrackEvent("NoteRemoved");

            //remove note images from disk
            await RemoveNoteImages(note.Images);

            RemoveNoteReminders(note);
            NotificationsManager.RemoveTileIfExists(note.ID);

            if (noteFound == null)
            {
                return(false);
            }

            noteFound.SoftDelete();

            bool success = LocalDB.Update(noteFound) == 1;

            if (!success)
            {
                return(false);
            }

            if (reflectOnRoaming)
            {
                RoamingDB.Update(noteFound);
            }

            //LocalDB.Delete(noteFound);
            //RoamingDB.Delete(noteFound);

            AppData.Notes.Remove(noteFound);
            AppData.ArchivedNotes.Remove(noteFound);

            var handler = NoteRemoved;

            if (handler != null)
            {
                handler(null, new NoteIdEventArgs(noteFound.ID));
            }

            var handler2 = NotesSaved;

            if (handler2 != null)
            {
                handler2(null, EventArgs.Empty);
            }

            return(true);
        }
Ejemplo n.º 8
0
        private static bool UpdateNote(Note note, bool reflectOnRoaming = true)
        {
            if (note == null)
            {
                return(false);
            }

            Debug.WriteLine("Update note: " + note.Title);
            //App.TelemetryClient.TrackEvent("NoteUpdated");

            //associate with note
            foreach (var item in note.Checklist)
            {
                item.NoteId = note.ID;
            }
            foreach (var item in note.Images)
            {
                item.NoteId = note.ID;
            }
            note.Reminder.NoteId = note.ID;

            //NotificationsManager.TryCreateNoteReminder(note, note.Reminder.Date);

            //DB.UpdateWithChildren(note);
            bool success = LocalDB.Update(note) == 1;

            if (!success)
            {
                return(false);
            }

            if (reflectOnRoaming)
            {
                RoamingDB.Update(note);
            }

            //update checklist items and note images
            if (note.Checklist.Count > 0)
            {
                LocalDB.InsertOrReplaceAll(note.Checklist);
                if (reflectOnRoaming)
                {
                    RoamingDB.InsertOrReplaceAll(note.Checklist);
                }
            }

            if (note.Images.Count > 0)
            {
                LocalDB.InsertOrReplaceAll(note.Images);
                if (reflectOnRoaming)
                {
                    RoamingDB.InsertOrReplaceAll(note.Images);
                }
            }

            if (note.Reminder != null)
            {
                LocalDB.InsertOrReplace(note.Reminder);
                if (reflectOnRoaming)
                {
                    RoamingDB.InsertOrReplace(note.Reminder);
                }
            }

            note.Changed = false;

            var handler = NoteChanged;

            if (handler != null)
            {
                handler(null, new NoteEventArgs(note));
            }

            var handler2 = NotesSaved;

            if (handler2 != null)
            {
                handler2(null, EventArgs.Empty);
            }

            return(true);
        }