simplenotesdroid/NoteActivity.cs

114 lines
4.2 KiB
C#

using System.Net;
using System.Net.Http.Json;
using System.Text.Json.Serialization;
using Android.Views;
using AndroidX.AppCompat.App;
using Google.Android.Material.Color;
using Google.Android.Material.FloatingActionButton;
using Google.Android.Material.Snackbar;
using Google.Android.Material.TextField;
namespace SimpleNotes;
[Activity(Label = "@string/note_page",Theme ="@style/Theme.SimpleNotes")]
public class NoteActivity : AppCompatActivity
{
protected override async void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);
DynamicColors.ApplyToActivityIfAvailable(this);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_note);
SetSupportActionBar(FindViewById<AndroidX.AppCompat.Widget.Toolbar>(Resource.Id.toolbar));
var (endpoint,username,password) = MainActivity.GetPrefs(this);
//check for id
var note_title = FindViewById<TextInputEditText>(Resource.Id.note_title);
var note_body = FindViewById<TextInputEditText>(Resource.Id.note_body);
var note_save = FindViewById<Button>(Resource.Id.note_save);
if(note_title == null || note_body == null || note_save == null) return;
long id = Intent?.GetLongExtra("note_id",0) ?? 0;
if(id > 0)
{
if(!string.IsNullOrWhiteSpace(endpoint) && !string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password))
{
var request = new HttpRequestMessage(HttpMethod.Get,$"{endpoint}/api/note?id={id}");
request.Headers.Add("Authorization",$"Basic {Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes($"{username}:{password}"))}");
var response=await MainActivity.Client.SendAsync(request);
var note = response.StatusCode == HttpStatusCode.OK ? (await response.Content.ReadFromJsonAsync<_NoteResponse>())?.Note : null;
if(note != null)
{
note_title.Text = note.Title;
this.Title = note.Title;
note_body.Text = note.Body;
}
}
}
note_title.TextChanged += (sender,e)=>{
this.Title = note_title.Text;
};
async Task<long> SaveNote(Note note)
{
var request = new HttpRequestMessage(HttpMethod.Post,$"{endpoint}/api/note");
request.Headers.Add("Authorization",$"Basic {Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes($"{username}:{password}"))}");
request.Content = new System.Net.Http.StringContent(System.Text.Json.JsonSerializer.Serialize(note),new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
var result=await MainActivity.Client.SendAsync(request);
if(result.StatusCode == HttpStatusCode.OK)
{
var res2=await result.Content.ReadFromJsonAsync<_NoteResponse>();
return res2?.Note?.Id ?? 0;
}
return 0;
}
note_save.Click += async(sender,e)=>{
var note = new Note();
note.Id = id;
this.RunOnUiThread(()=>{
note.Title = note_title.Text ?? "";
note.Body = note_body.Text ?? "";
note_save.Enabled=false;
});
if(!string.IsNullOrWhiteSpace(endpoint) && !string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password))
{
id=await Task.Run(async()=>await SaveNote(note));
this.RunOnUiThread(()=>{
note_save.Enabled=true;
Toast.MakeText(this,"Saved Note",ToastLength.Long)?.Show();
});
}
};
}
}
public class _NoteResponse
{
[JsonPropertyName("success")]
public bool Success {get;set;}
[JsonPropertyName("note")]
public Note Note {get;set;}=new Note();
}
public class Note
{
[JsonPropertyName("id")]
public long Id {get;set;}
[JsonPropertyName("title")]
public string Title {get;set;}="";
[JsonPropertyName("body")]
public string Body {get;set;}="";
}