public void AddIfNotExist(NoteEntity note) { if (null == note) { throw new ArgumentNullException("note"); } using (var dc = new MyDataContext()) { //var a = new System.IO.StringWriter(); //dc.Log = a; var entity = dc.NoteTable.FirstOrDefault(n => n.NotePath == note.NotePath); if (null != entity) { return; } ensureTitleIsNotEmpty(note); dc.NoteTable.InsertOnSubmit(note); dc.SafeSubmitChanges(); } //Debug.WriteLine(a); if (NoteCreated != null) { NoteCreated(note); } if (NoteChanged != null) { NoteChanged(); } }
public void AddIfNotExist(NoteEntity note) { if (null == note) { throw new ArgumentNullException("note"); } using (var dc = new MyDataContext()) { //var a = new System.IO.StringWriter(); //dc.Log = a; var entity = dc.NoteTable.FirstOrDefault(n => n.NotePath == note.NotePath); if (null != entity) return; ensureTitleIsNotEmpty(note); dc.NoteTable.InsertOnSubmit(note); dc.SafeSubmitChanges(); } //Debug.WriteLine(a); if (NoteCreated != null) { NoteCreated(note); } if (NoteChanged != null) { NoteChanged(); } }
private static void ensureTitleIsNotEmpty(NoteEntity note) { if (string.IsNullOrEmpty(note.Title)) { note.Title = EmptyNoteTitle; } }
public NewNoteView() { InitializeComponent(); _noteEntity = new NoteEntity(); _photoChooserTask = new PhotoChooserTask(); _photoChooserTask.ShowCamera = true; _photoChooserTask.Completed += photoChooserTask_Completed; _isNewPage = true; }
public void ModifyIfExist(NoteEntity entity) { if (null == entity) { throw new ArgumentNullException("entity"); } using (var dc = new MyDataContext()) { //var a = new System.IO.StringWriter(); //dc.Log = a; var note = dc.NoteTable.FirstOrDefault(n => n.Id == entity.Id); if (null == note) { Debug.WriteLine("ModifyIfExist::note is null."); return; } ensureTitleIsNotEmpty(entity); var oldNote = note.DeepClone(); note.NotePath = entity.NotePath; note.Title = entity.Title; note.CreateTime = entity.CreateTime; note.ModifyTime = entity.ModifyTime; note.Size = entity.Size; note.Source = entity.Source; note.NotebookName = entity.NotebookName; note.NotebookPath = entity.NotebookPath; note.Content = entity.Content; note.Author = entity.Author; note.NoteStatus = entity.NoteStatus; dc.SafeSubmitChanges(); //Debug.WriteLine(a); if (NoteModified != null) { NoteModified(oldNote, note); } if (NoteChanged != null) { NoteChanged(); } } }
public void Copy(NoteEntity newNoteEntity) { if (null == newNoteEntity) { throw new ArgumentNullException("newNoteEntity"); } Id = newNoteEntity.Id; NotePath = newNoteEntity.NotePath; Title = newNoteEntity.Title; CreateTime = newNoteEntity.CreateTime; ModifyTime = newNoteEntity.ModifyTime; Size = newNoteEntity.Size; Source = newNoteEntity.Source; NotebookName = newNoteEntity.NotebookName; NotebookPath = newNoteEntity.NotebookPath; Content = newNoteEntity.Content; Author = newNoteEntity.Author; NoteStatus = newNoteEntity.NoteStatus; }
public NoteEntity DeepClone() { var entity = new NoteEntity { Id = Id, NotePath = NotePath, Title = Title, CreateTime = CreateTime, ModifyTime = ModifyTime, Size = Size, Source = Source, NotebookName = NotebookName, NotebookPath = NotebookPath, Content = Content, Author = Author, NoteStatus = NoteStatus }; return(entity); }
private void InternalSyncNote(NoteEntity noteEntity, Action<SyncCompletedType, object> syncCompleted) { try { if (noteEntity.NoteStatus == NoteStatus.Added) // 新增加的笔记 { var content = ConvertImageLocalPathToRemoteUrl(noteEntity.Content); var path = _api.CreateNote(content, noteEntity.Title, noteEntity.Author, noteEntity.NotebookPath, noteEntity.Source); var note = _api.GetNote(path); noteEntity.CreateTime = note.CreateTime; noteEntity.ModifyTime = note.ModifyTime; noteEntity.NotePath = path; noteEntity.Size = note.Size; noteEntity.NoteStatus = NoteStatus.Normal; NoteDao.Inst.ModifyIfExist(noteEntity); } else { syncNote(new NoteSync(noteEntity.NotePath, noteEntity.NotebookName, noteEntity.NotebookPath)); } if (null != syncCompleted) { syncCompleted(SyncCompletedType.Note, "笔记同步成功"); } } catch (YoudaoNoteException e) { if (null != syncCompleted) { syncCompleted(SyncCompletedType.Failed, e); } } catch (Exception e1) { if (null != syncCompleted) { syncCompleted(SyncCompletedType.Failed, e1); } } }
private void InternalDeleteNote(NoteEntity entity, Action<SyncCompletedType, object> deleteCompleted) { try { deleteNote(entity); if (null != deleteCompleted) { deleteCompleted(SyncCompletedType.Note, null); } } catch (Exception e) { if (null != deleteCompleted) { deleteCompleted(SyncCompletedType.Failed, e); } } }
public async Task DeleteNoteAsync(NoteEntity entity, Action<SyncCompletedType, object> deleteCompleted) { if (null == entity) { throw new ArgumentNullException("entity"); } await Task.Run(() => InternalDeleteNote(entity, deleteCompleted)); }
private static void noteCreated(NoteEntity newNoteEntity) { var belongNotebookPath = newNoteEntity.NotebookPath; lock (LockObj) { NoteCollection noteCollection; if (NoteCollectionDict.TryGetValue(belongNotebookPath, out noteCollection)) { var flag = false; for (var i = 0; i < noteCollection.Count; i++) { if (noteCollection[i].ModifyTime >= newNoteEntity.ModifyTime) continue; noteCollection.Insert(i, newNoteEntity); flag = true; break; } if (!flag) { noteCollection.Add(newNoteEntity); } } else { noteCollection = new NoteCollection { newNoteEntity }; NoteCollectionDict.Add(belongNotebookPath, noteCollection); } } }
private static void ensureTitleIsNotEmpty(NoteEntity note) { if (string.IsNullOrEmpty(note.Title)) { note.Title = EmptyNoteTitle; } }
private static void noteDeleted(NoteEntity deleteNoteEntity) { var belongNotebookPath = deleteNoteEntity.NotebookPath; NoteCollection noteCollection; if (NoteCollectionDict.TryGetValue(belongNotebookPath, out noteCollection)) { noteCollection.RemoveById(deleteNoteEntity.Id); } }
private static void noteModified(NoteEntity oldNoteEntity, NoteEntity newNoteEntity) { var belongNotebookPath = oldNoteEntity.NotebookPath; NoteCollection noteCollection; if (!NoteCollectionDict.TryGetValue(belongNotebookPath, out noteCollection)) return; var findNote = noteCollection.FirstOrDefault(n => n.Id == oldNoteEntity.Id); if (null == findNote) { return; } findNote.Copy(newNoteEntity); }
private async void btnSync_Click(object sender, RoutedEventArgs e) { if (!Util.IsNetworkAvailable()) { Toast.Prompt("额,网络不可用,请检查网络配置!"); return; } using (new WaitPopup("正在同步笔记", this)) { try { await SyncCore.GetInst().SyncNoteAsync(NoteEntity, (type, msg) => { Dispatcher.BeginInvoke(() => { switch (type) { case SyncCompletedType.Note: NoteEntity = NoteDao.Inst.GetNoteById(NoteEntity.Id); ContentWebBrowser.NavigateToString(NoteEntity.Content); tbTitle.Text = NoteEntity.Title; Toast.Prompt("笔记修改成功!"); break; case SyncCompletedType.Failed: LoggerFactory.GetLogger().Error("同步笔记失败", (Exception)msg); Toast.Prompt("额,同步笔记失败,请稍后重试!"); break; } }); }); } catch (Exception ex) { LoggerFactory.GetLogger().Error("同步笔记失败", ex); Toast.Prompt("额,同步笔记失败,请稍后重试!"); } } }
private void deleteNote(NoteEntity findLocalNote, Dictionary<NoteBatchOperateEnum, List<NoteEntity>> dic = null) { Debug.WriteLine("删除服务端笔记:{0}", findLocalNote.Title); _api.DeleteNote(findLocalNote.NotePath); Debug.WriteLine("删除本地笔记:{0}", findLocalNote.Title); if (null == dic) { NoteDao.Inst.DeleteIfExist(findLocalNote.Id); } else { if (null != findLocalNote) { dic[NoteBatchOperateEnum.Delete].Add(findLocalNote); } } }
static void NoteListCache_NoteDeleted(NoteEntity deleteNoteEntity) { Debug.WriteLine("NoteListCache::笔记删除事件触发"); Debug.Assert(deleteNoteEntity != null); if (!Deployment.Current.Dispatcher.CheckAccess()) { Deployment.Current.Dispatcher.BeginInvoke(() => { noteDeleted(deleteNoteEntity); }); } else { noteDeleted(deleteNoteEntity); } }
private void RestoreState() { GetAppbarBtns(); if (State.ContainsKey("NoteEntity")) { NoteEntity = State["NoteEntity"] as NoteEntity; } if (State.ContainsKey("_appbarBtnSaveEnabled")) { _appbarBtnSave.IsEnabled = (bool)State["_appbarBtnSaveEnabled"]; } if (State.ContainsKey("_appbarBtnCancelEnabled")) { _appbarBtnCancel.IsEnabled = (bool)State["_appbarBtnCancelEnabled"]; } if (State.ContainsKey("tbTitleText")) { tbTitle.Text = (string)State["tbTitleText"]; } if (State.ContainsKey("rtbContentText")) { rtbContent.SetHtml((string)State["rtbContentText"]); } if (State.ContainsKey("tbNotebookText")) { tbNotebook.Text = (string)State["tbNotebookText"]; } if (State.ContainsKey("_photoChooserTask")) { _photoChooserTask = (PhotoChooserTask)State["_photoChooserTask"]; } }
private NoteEntity syncNote(NoteSync noteSync, Dictionary<NoteBatchOperateEnum, List<NoteEntity>> dic = null) { var serverNote = _api.GetNote(noteSync.NotePath); var findLocalNote = NoteListCache.GetNoteByPath(noteSync.NotebookPath, noteSync.NotePath); if (null != findLocalNote) // 本地有这篇笔记 { if (serverNote.ModifyTime > findLocalNote.ModifyTime) // 服务端的笔记比本地上次同步过的新 { switch (findLocalNote.NoteStatus) { case NoteStatus.Normal: return modifyNote(noteSync, serverNote, findLocalNote, dic); case NoteStatus.Deleted: deleteNote(findLocalNote, dic); break; case NoteStatus.Modified: // 把本地的笔记备份一份新的,并标记为新增加的 var newNoteEntity = new NoteEntity { Title = "冲突 - " + findLocalNote.Title, Content = findLocalNote.Content, Author = findLocalNote.Author, Source = findLocalNote.Source, Size = findLocalNote.Size, NotebookName = findLocalNote.NotebookName, NotebookPath = findLocalNote.NotebookPath, CreateTime = findLocalNote.CreateTime, ModifyTime = findLocalNote.ModifyTime, NoteStatus = NoteStatus.Added }; if (null == dic) // 操作单篇笔记 { NoteDao.Inst.AddIfNotExist(newNoteEntity); } else { if (null != newNoteEntity) { dic[NoteBatchOperateEnum.Add].Add(newNoteEntity); } } // 将服务端的更新到本地 return modifyNote(noteSync, serverNote, findLocalNote, dic); } } else // 本地上次同步过的笔记比服务端的新 { switch (findLocalNote.NoteStatus) { case NoteStatus.Modified: var content = ConvertImageLocalPathToRemoteUrl(findLocalNote.Content); _api.UpdateNote(findLocalNote.NotePath, content, findLocalNote.Source, findLocalNote.Author, findLocalNote.Title); // 为了获取到笔记在服务器端的更新时间,不得不重新获取笔记。不能用本地的时间是因为不同的客户端的时间可能不一样 var newNote = _api.GetNote(findLocalNote.NotePath); findLocalNote.NoteStatus = NoteStatus.Normal; findLocalNote.ModifyTime = newNote.ModifyTime; if (null == dic) // 操作单篇笔记 { NoteDao.Inst.ModifyIfExist(findLocalNote); } else { if (null != findLocalNote) { dic[NoteBatchOperateEnum.Modify].Add(findLocalNote); } } return findLocalNote; case NoteStatus.Deleted: deleteNote(findLocalNote, dic); break; } } } else // 本地没有这篇笔记,直接同步到本地 { var entity = new NoteEntity(serverNote, noteSync.NotePath, noteSync.NotebookName, noteSync.NotebookPath); if (null == dic) // 操作单篇笔记 { NoteDao.Inst.AddIfNotExist(entity); } else { if (null != entity) { dic[NoteBatchOperateEnum.Add].Add(entity); } } return entity; } return null; }
public void Copy(NoteEntity newNoteEntity) { if (null == newNoteEntity) { throw new ArgumentNullException("newNoteEntity"); } Id = newNoteEntity.Id; NotePath = newNoteEntity.NotePath; Title = newNoteEntity.Title; CreateTime = newNoteEntity.CreateTime; ModifyTime = newNoteEntity.ModifyTime; Size = newNoteEntity.Size; Source = newNoteEntity.Source; NotebookName = newNoteEntity.NotebookName; NotebookPath = newNoteEntity.NotebookPath; Content = newNoteEntity.Content; Author = newNoteEntity.Author; NoteStatus = newNoteEntity.NoteStatus; }
private NoteEntity modifyNote(NoteSync noteSync, Note serverNote, NoteEntity findLocalNote, Dictionary<NoteBatchOperateEnum, List<NoteEntity>> dic = null) { findLocalNote.NotePath = noteSync.NotePath; findLocalNote.Title = serverNote.Title; findLocalNote.CreateTime = serverNote.CreateTime; findLocalNote.ModifyTime = serverNote.ModifyTime; findLocalNote.Size = serverNote.Size; findLocalNote.Source = serverNote.Source; findLocalNote.NotebookPath = noteSync.NotebookPath; findLocalNote.Content = DownloadImageToLocalAsync(noteSync.NotePath, serverNote.Content).Result; findLocalNote.Author = serverNote.Author; findLocalNote.NotebookName = noteSync.NotebookName; findLocalNote.NoteStatus = NoteStatus.Normal; if (null == dic) { NoteDao.Inst.ModifyIfExist(findLocalNote); } else { if (null != findLocalNote) { dic[NoteBatchOperateEnum.Modify].Add(findLocalNote); } } return findLocalNote; }
static void NoteListCache_NoteModified(NoteEntity oldNoteEntity, NoteEntity newNoteEntity) { Debug.WriteLine("NoteListCache::笔记修改事件触发"); Debug.Assert(oldNoteEntity != null); Debug.Assert(newNoteEntity != null); if (!Deployment.Current.Dispatcher.CheckAccess()) { Deployment.Current.Dispatcher.BeginInvoke(() => { noteModified(oldNoteEntity, newNoteEntity); }); } else { noteModified(oldNoteEntity, newNoteEntity); } }
public async Task SyncNoteAsync(NoteEntity noteEntity, Action<SyncCompletedType, object> syncCompleted) { await Task.Run(() => { try { InternalSyncNote(noteEntity, syncCompleted); } catch (Exception e) { if (null != syncCompleted) { syncCompleted(SyncCompletedType.Failed, e); } } }); }
public NoteSyncEventArgs(int totalNoteCount, int syncedNoteCount, NoteEntity syncedNote) { TotalNoteCount = totalNoteCount; SyncedNoteCount = syncedNoteCount; SyncedNote = syncedNote; }
private void RestoreState() { if (State.ContainsKey("NoteEntity")) { NoteEntity = (NoteEntity)State["NoteEntity"]; } }
public void ModifyIfExist(NoteEntity entity) { if (null == entity) { throw new ArgumentNullException("entity"); } using (var dc = new MyDataContext()) { //var a = new System.IO.StringWriter(); //dc.Log = a; var note = dc.NoteTable.FirstOrDefault(n => n.Id == entity.Id); if (null == note) { Debug.WriteLine("ModifyIfExist::note is null."); return; } ensureTitleIsNotEmpty(entity); var oldNote = note.DeepClone(); note.NotePath = entity.NotePath; note.Title = entity.Title; note.CreateTime = entity.CreateTime; note.ModifyTime = entity.ModifyTime; note.Size = entity.Size; note.Source = entity.Source; note.NotebookName = entity.NotebookName; note.NotebookPath = entity.NotebookPath; note.Content = entity.Content; note.Author = entity.Author; note.NoteStatus = entity.NoteStatus; dc.SafeSubmitChanges(); //Debug.WriteLine(a); if (NoteModified != null) { NoteModified(oldNote, note); } if (NoteChanged != null) { NoteChanged(); } } }
public NoteEntity DeepClone() { var entity = new NoteEntity { Id = Id, NotePath = NotePath, Title = Title, CreateTime = CreateTime, ModifyTime = ModifyTime, Size = Size, Source = Source, NotebookName = NotebookName, NotebookPath = NotebookPath, Content = Content, Author = Author, NoteStatus = NoteStatus }; return entity; }
private void RestoreState() { if (State.ContainsKey("_noteEntity")) { _noteEntity = (NoteEntity)State["_noteEntity"]; } if (State.ContainsKey("_photoChooserTask")) { _photoChooserTask = (PhotoChooserTask)State["_photoChooserTask"]; } }