private void UploadItem(string entity) { var httpWebRequest = (HttpWebRequest)WebRequest.Create(DataAccess.UrlBase + "/" + (CurrentItem.Synced == DateTime.MinValue ? "insert" : "update/" + CurrentItem.Key)); httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "POST"; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { TextRange range = new TextRange(ResourceContent.Document.ContentStart, ResourceContent.Document.ContentEnd); NoteEntity item = new NoteEntity() { Author = SelectedChannel, RowKey = CurrentItem.Key, Content = range.Text, Title = CurrentItem.Name }; streamWriter.Write(JsonConvert.SerializeObject(item)); streamWriter.Flush(); streamWriter.Close(); var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var result = streamReader.ReadToEnd(); CurrentItem.Synced = DateTime.Now; CurrentItem.Author = SelectedChannel; SaveItem(entity); StatusBartextBlock.Text = DateTime.Now.ToShortTimeString() + ": Uploaded [" + CurrentItem.Name + "]"; } } }
/// <summary> /// All custom window initialization maintained by developers. /// </summary> private void CustomInitialize() { if (Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/OTB/MemoDrops/Account")) { string myIdentity = File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/OTB/MemoDrops/Account/me.json"); Me = JsonConvert.DeserializeObject <UserAccount>(myIdentity); } else { Me = new UserAccount() { RowKey = Guid.NewGuid().ToString(), DisplayName = "Log In", Email = string.Empty }; } ChannelSelector.SelectedIndex = 0; FileItems = new ObservableCollection <MyItem>(); // Don't be invasive, ask user for permission to ctreate stuffs on his disk if is not in your app folder. if (!Directory.Exists(BasePath)) { var s = MessageBox.Show("Folder is expected. Create [" + BasePath + "] ?", "Startup", MessageBoxButton.YesNo); if (s == MessageBoxResult.Yes) { Directory.CreateDirectory(BasePath); } else { return; } } // Colect notes from disk var files = Directory.GetFiles(BasePath).ToList(); ObservableCollection <MyItem> items = new ObservableCollection <MyItem>(); foreach (string filePath in files) { if (filePath.EndsWith(".meta")) { NoteEntity noteMeta = JsonConvert.DeserializeObject <NoteEntity>(File.ReadAllText(filePath)); items.Add(new MyItem() { Key = noteMeta.RowKey, Name = noteMeta.Title, Synced = noteMeta.Synced, Label = string.Empty }); } } AllItems = items; FilterNotes(string.Empty); // Dows it improve performance to wait for the list to be constructed? // Wire-up events ResourceContent.PreviewKeyUp += ResourceContent_PreviewKeyUp; Resource_Name.PreviewKeyUp += Resource_Name_PreviewKeyUp; filesList.PreviewKeyUp += FilesList_PreviewKeyUp; // Provide me as model DataContext = this; }