public async void Save(EditText titleTV, EditText noteTV) { if (note.Title == null && note.Note == null) { Notes newNote = new Notes() { Title = titleTV.Text, Note = noteTV.Text }; await MainActivity.db.AddItemAsync(newNote); if (isOnline()) { Toast.MakeText(this, "Zapisywanie notatki na serwerze", ToastLength.Short).Show(); var simpleNotesService = new SimpleNotesService(); await simpleNotesService.PostNotesAsync(Uri, newNote); } } else { note.Title = titleTV.Text; note.Note = noteTV.Text; await MainActivity.db.UpdateItemAsync(note); PUTNote putNote = new PUTNote() { Note_ID = note.sqlNote_ID, Title = note.Title, Note = note.Note }; if (isOnline()) { Toast.MakeText(this, "Zapisywanie notatki na serwerze", ToastLength.Short).Show(); var simpleNotesService = new SimpleNotesService(); await simpleNotesService.PutNotesAsync(Uri, note.sqlNote_ID, putNote); } } NotesListViewAdapter adapter = new NotesListViewAdapter(this, await MainActivity.db.GetItemsAsync()); MainActivity.NotesListView.Adapter = adapter; MainActivity.notes = await MainActivity.db.GetItemsAsync(); if (isOnline()) { var simpleNotesService = new SimpleNotesService(); List <Notes> temp = await simpleNotesService.GetNotesAsync(Uri); for (int i = 0; i < MainActivity.notes.Count; i++) { MainActivity.notes[i].sqlNote_ID = temp[i].Note_ID; } } Finish(); }
protected override async void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Main); // this method is currently not working //Start(); notesListView = FindViewById <ListView>(Resource.Id.notesListView); notes = await db.GetItemsAsync(); NotesListViewAdapter adapter = new NotesListViewAdapter(this, notes); notesListView.Adapter = adapter; // api operations // operacje związane z api var simpleNotesService = new SimpleNotesService(); if (isOnline()) { // removing all notes from sql server database // usunięcie wszystkich notatek z serwera List <Notes> no = await simpleNotesService.GetNotesAsync(Uri); if (no != null) { await simpleNotesService.DeleteAllNotesAsync("http://smnotes.azurewebsites.net/api/", "deleteallnotes"); } // if there are notes saved offline in local database // upload them to the sql server // jeżeli jakieś notatki były zapisane lokalnie bez dostępu do internetu // to należy je wysłać na serwer if (notes != null) { foreach (Notes n in notes) { Notes note = new Notes() { Note_ID = n.Note_ID, Title = n.Title, Note = n.Note }; await simpleNotesService.PostNotesAsync(Uri, note); } GetSqlID(); } } /////////////////// notesListView.ItemClick += delegate(object sender, ItemClickEventArgs args) { Intent details_activity = new Intent(this, typeof(DetailsActivity)); details_activity.PutExtra("ID", notes[(int)args.Id].Note_ID); details_activity.PutExtra("sqlID", notes[(int)args.Id].sqlNote_ID); details_activity.PutExtra("title", notes[(int)args.Id].Title); details_activity.PutExtra("note", notes[(int)args.Id].Note); StartActivity(details_activity); }; notesListView.ItemLongClick += delegate(object sender, ItemLongClickEventArgs args) { AlertDialog.Builder dialog = new AlertDialog.Builder(this); AlertDialog alert = dialog.Create(); alert.SetMessage("Czy na pewno chcesz usunąć tę notatkę?"); alert.SetButton("OK", async(c, ev) => { Notes delNote = new Notes() { Note_ID = notes[(int)args.Id].Note_ID, sqlNote_ID = notes[(int)args.Id].sqlNote_ID, Title = notes[(int)args.Id].Title, Note = notes[(int)args.Id].Note }; await db.DeteleItemAsync(delNote); notes.RemoveAt((int)args.Id); // if there is an Internet connection, we have to delete selected note // also from sql server database // jeżeli jest połączenie z Internetem trzeba usunąć notatkę także z serwera if (isOnline()) { await simpleNotesService.DeleteNotesAsync(Uri, delNote.sqlNote_ID); } adapter = new NotesListViewAdapter(this, notes); notesListView.Adapter = adapter; }); alert.SetButton2("Anuluj", (c, ev) => { }); alert.Show(); }; }