private void UploadItem(string entity) { if (string.IsNullOrEmpty(Me.RowKey)) { Login(); } var httpWebRequest = (HttpWebRequest)WebRequest.Create(MainModel.UrlBase + entity + "/" + (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 = Me.RowKey, 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 = Me.RowKey; SaveItem(entity); StatusBartextBlock.Text = DateTime.Now.ToShortTimeString() + ": Uploaded [" + CurrentItem.Name + "]"; CurrentItem.Status = 2; } } }
/// <summary> /// All custom window initialization maintained by developers. /// </summary> private void CustomInitialize() { VisibilityLevels = new ObservableCollection <VisibilityLevel>() { new VisibilityLevel() { Key = 0, Name = "Only Me", Image = "https://justice.org.au/wp-content/uploads/2017/08/avatar-icon-300x300.png" }, new VisibilityLevel() { Key = 1, Name = "With Link", Image = "http://icon-park.com/imagefiles/link_icon_black.png" }, new VisibilityLevel() { Key = 2, Name = "Everyone", Image = "https://static.thenounproject.com/png/17086-200.png" } }; 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); ChannelSelector.Items.Insert(0, Me.RowKey); ChannelSelector.SelectedIndex = 0; SelectedChannel = ChannelSelector.Text; } else { Me = new UserAccount() { RowKey = Guid.NewGuid().ToString(), DisplayName = "Log In", Email = string.Empty }; ChannelSelector.Items.Clear(); SelectedChannel = null; } 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, Visibility = VisibilityLevels[0] }); } } AllItems = items; FilterNotes(string.Empty); // Dows it improve performance to wait for the list to be constructed? // Wire-up events ResourceContent.PreviewKeyUp += ResourceContent_PreviewKeyUp; ResourceContent.PreviewKeyDown += ResourceContent_PreviewKeyDown; Resource_Name.PreviewKeyUp += Resource_Name_PreviewKeyUp; filesList.PreviewKeyUp += FilesList_PreviewKeyUp; InisializeSettings(); SelectedProfileIndicator.DataContext = MainModel.Instance; if (MainModel.Instance.SelectedProfile == null) { MainModel.Instance.SelectedProfile = MainModel.Instance.SettingProfiles[0]; } // Provide me as model DataContext = this; }